Hodrick-Prescott filter with smoothing factor as parameter

8 views (last 30 days)
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

Accepted Answer

Milan Bansal
Milan Bansal on 10 Sep 2024
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!

More Answers (0)

Community Treasure Hunt

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

Start Hunting!