Hodrick-Prescott filter with smoothing factor as parameter

5 vues (au cours des 30 derniers jours)
Giacomo
Giacomo le 10 Sep 2024
Commenté : Giacomo le 10 Sep 2024
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

Réponse acceptée

Milan Bansal
Milan Bansal le 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!
  1 commentaire
Giacomo
Giacomo le 10 Sep 2024
Thank you Milan. You are really helpfull.
Hope you the best and be safe.

Connectez-vous pour commenter.

Plus de réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by