MatLab - System Identification Toolbox vs Code - discrepancies and small fit percentages

48 Views Asked by At

I need help understanding what is wrong with what I am doing or why this is happening. I have two tables and one variable:

u=21000x1 - the original signal

y=21000x1 - signal corrupted by noise

dt=1^-4

I am attempting to create a polynomial model based on ARX (though I plan to try ARMAX, BJ, and OE as well). Using the System Identification toolbox, I've imported and divided my data into Training and Validation subsets with a 75/25 ratio. I then clicked on estimate >>> polynomial models >>> Order Selection >>> Estimate. After a bar-chart appeared, I zoomed in on the bar marked in red (indicating the best fit according to MDL and AIC). I selected this model along with a few others from the chart.

However, the calculated fit in the Model Output graph was only 2.5% for the best fit and even lower for other models. I attempted random value entry, but all models had very low fits, either below 0 or slightly above it.

To verify if there were truly no best fits, I created a program using a for loop to generate models and check their fit with the compare function. Unfortunately, the coded models had several issues, including very low fits, fit-percentage calculations differing from the System Identification Toolbox, and fit percentages for my coded models plummeting to negative values.

I imported and divided my data, selected models based on the red-marked bar in the Order Selection, and attempted random value entry. However, all models exhibited very low fits, and the fit percentages for both the System Identification Toolbox and the coded models were unsatisfactory. To investigate further, I created a program to generate models in a for loop and checked their fit with the compare function. Unfortunately, the coded models had issues, including: 1)low fits, 2) differing fit-percentage calculations from the System Identification Toolbox, 3) ver low (in thousands) negative fit percentages for my coded models(this did not happen for Toolbox ARX models).

I need assistance in understanding why no model can fit my data and would appreciate help in fixing my code to yield results identical to the Polynomial Model in the System Identification Toolbox. Additionally, an explanation of why the coded models do not match the Toolbox results would be valuable.

My code: ` min_wsp = 1; % min value of the coefficient max_wsp = 10; % max value of the coefficient

wsp_range = min_wsp:max_wsp;

num_wsp = 3;

combinations = combvec(wsp_range, wsp_range, wsp_range);

train_ratio = 0.75;
validation_ratio = 0.25;


num_samples = length(u);

num_train = round(train_ratio * num_samples);
num_validation = num_samples - num_train;

u_train = u(1:num_train);
y_train = y(1:num_train);

u_validation = u(num_train+1:end);
y_validation = y(num_train+1:end);

results = cell(size(combinations, 2), 5); % 5 kolumn: a1, a2, a3, % wariancji, % wariancji modelu


for i = 1:size(combinations, 2)
    a3 = combinations(1, i); % it represents nk from System Identification toolbox
    a2 = combinations(2, i); % it represents nb from System Identification toolbox
    a1 = combinations(3, i); % it represents na from System Identification toolbox
    
    data_train = [u_train, y_train];
    data_valid = [u_validation, y_validation];
    model = arx(data,[a1 a2 a3], 'Ts', dt)
    
    y_simulated_train = sim(model, data_train);
    
    y_simulated_validation = sim(model, data_valid);
    
    [output, percentage_fit] = compare(data_valid, model);
    
    fit_percentage_validation = percentage_fit
    
    disp(['Model ARX for a1=', num2str(a1), ', a2=', num2str(a2), ', a3=', num2str(a3)]);
    disp(['% Fit percentage (validation): ', num2str(percentage_fit), '%']);
    
    results{i, 1} = a1;
    results{i, 2} = a2;
    results{i, 3} = a3;
    results{i, 4} = fit_percentage_validation;
    
    disp('---------------------------------');
end

headers = {'a1', 'a2', 'a3', 'Fit percentage (validation)'};
xlswrite('wyniki.xlsx', headers, 'Sheet1', 'A1');
xlswrite('wyniki.xlsx', results, 'Sheet1', 'A2');
disp('Wyniki zapisane do pliku Excela (wyniki.xlsx).');`
0

There are 0 best solutions below