来自 MATLAB 的简单 MLE 解决方案
Simple MLE solution from MATLAB
所以我正在尝试在 MATLAB 上为自定义函数拟合线性最小二乘模型。我的数据,称为 logprice_hour_seas
,看起来像一个复杂的非线性函数,我想使用我的自定义函数 seasonMatrix
来拟合它,但是为了理解 MATLAB 的 MLE 是如何工作的,我做了这个愚蠢的拟合,说 seasonMatrix 只是一个线性函数功能。帮助我理解我从 MATLAB 站点复制的这段代码及其逻辑(阅读下文)
Times = [0:1/8760:8712/8760];
% Calibrate parameters for the seasonality model
seasonMatrix = @(t) [t];
C = seasonMatrix(Times);
seasonParam = C\logprice_hour_seas;
现在我的模型中应该有一些错误(很多错误!)。但我做 logprice_hour_seas-C*seasonParam
,这全是零!好吧,MLE 是使用 logprice_hour_seas=C*seasonParam
求解的,所以这并不奇怪。我有什么不明白的??
如评论中所述,您弄乱了矩阵大小。
您创建价值观的方式 seasonParam
变成了 5x26
矩阵。和 C
一个 1x26
矩阵。您正在模拟一个欠定的方程组,它可以有多个解。
在这种情况下,对于算法来说幸运的是,C
的某些值是 1 !这意味着在解决方案 (seasonParam
) 中的所有 26 5x1
个向量中,只有乘以 1 的向量需要设置为结果值 (logprice_hour_seas
) 才能完美拟合!因此,您的解决方案 seasonParam
是
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
整个模型毫无意义,所以我假设您只是弄乱了尺寸。
请尝试以下操作:
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;
所以我正在尝试在 MATLAB 上为自定义函数拟合线性最小二乘模型。我的数据,称为 logprice_hour_seas
,看起来像一个复杂的非线性函数,我想使用我的自定义函数 seasonMatrix
来拟合它,但是为了理解 MATLAB 的 MLE 是如何工作的,我做了这个愚蠢的拟合,说 seasonMatrix 只是一个线性函数功能。帮助我理解我从 MATLAB 站点复制的这段代码及其逻辑(阅读下文)
Times = [0:1/8760:8712/8760];
% Calibrate parameters for the seasonality model
seasonMatrix = @(t) [t];
C = seasonMatrix(Times);
seasonParam = C\logprice_hour_seas;
现在我的模型中应该有一些错误(很多错误!)。但我做 logprice_hour_seas-C*seasonParam
,这全是零!好吧,MLE 是使用 logprice_hour_seas=C*seasonParam
求解的,所以这并不奇怪。我有什么不明白的??
如评论中所述,您弄乱了矩阵大小。
您创建价值观的方式 seasonParam
变成了 5x26
矩阵。和 C
一个 1x26
矩阵。您正在模拟一个欠定的方程组,它可以有多个解。
在这种情况下,对于算法来说幸运的是,C
的某些值是 1 !这意味着在解决方案 (seasonParam
) 中的所有 26 5x1
个向量中,只有乘以 1 的向量需要设置为结果值 (logprice_hour_seas
) 才能完美拟合!因此,您的解决方案 seasonParam
是
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
整个模型毫无意义,所以我假设您只是弄乱了尺寸。 请尝试以下操作:
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;