So am trying to fit a linear least squares model on MATLAB for a custom function. My data, called logprice_hour_seas
, looks like a complicated nonlinear function, which I want to fit using my custom function called seasonMatrix
but to understand how MATLAB's MLE works I do this dumb fit saying seasonMatrix is simply a linear function. Help me understand this code, which I copied from MATLAB's site, and the logic (read below)
Times = [0:1/8760:8712/8760];
% Calibrate parameters for the seasonality model
seasonMatrix = @(t) [t];
C = seasonMatrix(Times);
seasonParam = C\logprice_hour_seas;
Now I should have some error in my model (a lot of it!). But I do logprice_hour_seas-C*seasonParam
, and this is all zeros! Well, MLE was solved using logprice_hour_seas=C*seasonParam
so this is not surprising. What do I not understand??
As mentioned in the comment, you are messing up matrix sizes.
The way you create your values seasonParam
becomes 5x26
matrix. and C
a 1x26
matrix. You are simulating a under-determined system of equations, that can have multiple solutions.
In this case, fortunately for the algorithm, some of the values of C
are 1 ! That means that of all 26 5x1
vectors in the solution (seasonParam
) only the ones multiplied by 1 need to be set up as the result value (logprice_hour_seas
) to have a perfect fit! Thus, your solution seasonParam
is
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
5.2560 5.2151 5.2324 5.2224 5.2292
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
The whole model makes no sense, so I am assuming you are just messing up the dimensions. Try the following:
logprice_hour_seas=[5.2560 5.2151 5.2324 5.2224 5.2292]';
PriceTimes = [0:1/8760:4/8760]';
seasonMatrix = @(t) [sin(2.*pi.*t) cos(2.*pi.*t) sin(4.*pi.*t) cos(4.*pi.*t) t ones(size(t, 1), 1)];
C = seasonMatrix(PriceTimes);
seasonParam = C\logprice_hour_seas;