Hodrick-Prescott filter with smoothing factor as parameter

11 ビュー (過去 30 日間)
Giacomo
Giacomo 2024 年 9 月 10 日
コメント済み: Giacomo 2024 年 9 月 10 日
Hi i'm trying to parametrize Hodrick-Prescott filter (HP) as a function of smoothing parameter. Simple xy plot with the smoothing factor as parameter. I post my code below
excel=readmatrix("COMEX_SIZ2024, 1D_1f4e1.csv");
price=excel(:,6);
lambda=500:500:5500;
>> for pp=1:length(lambda)
[Trend(length(price),pp),Cyclical(length(price),pp)]=hpfilter(price,Smoothing=lambda(pp));
end
ERROR: Unable to perform assignment because the indices on the left side are not compatible with the size of the right side.
I'm using a for loop to impile the lambda on columns and calculation (x=price) on rows. However it doesnt seem to work.
Where is my error? Thanks

採用された回答

Milan Bansal
Milan Bansal 2024 年 9 月 10 日
Hi Giacomo
I understand that you are facing an error while trying to parametrize the Hodrick-Prescott filter with different smoothing parameters.
The error occurs because the hpfilter function returns a vector, not a single value, and you're trying to assign this vector to a single element of Trend and Cyclical.
Here's how you can fix the issue:
  • Preallocate Trend and Cyclical matrices with the correct dimensions to store the entire output vectors for each smoothing parameter.
  • Ensure the loop assigns the entire vector output to the appropriate columns.
Here's a revised version of your code:
excel = readmatrix("COMEX_SIZ2024, 1D_1f4e1.csv");
price = excel(:,6);
lambda = 500:500:5500;
Trend = zeros(length(price), length(lambda));
Cyclical = zeros(length(price), length(lambda));
for pp = 1:length(lambda)
[Trend(:, pp), Cyclical(:, pp)] = hpfilter(price, 'Smoothing', lambda(pp));
end
Please refer to the following documentation link to learn more about Hodrick-Prescott filter in MATLAB.
Hope this helps!
  1 件のコメント
Giacomo
Giacomo 2024 年 9 月 10 日
Thank you Milan. You are really helpfull.
Hope you the best and be safe.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMultirate Signal Processing についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by