jmaih / RISE_toolbox

Solution and estimation of Markov Switching Rational Expectations / DSGE Models
BSD 3-Clause "New" or "Revised" License
103 stars 76 forks source link

problem with filter #187

Closed Zhanwenqing closed 1 month ago

Zhanwenqing commented 1 month ago

Hi professor, sorry to bother you. I'm working with a endogenous regime switching model. When I run [~,loglik,~,retcode,~] = filter(m), it returns the "loglik=-100000000'', "retcode=5''. When I run decipher(retcode), it reports"definitions are nan or inf or imaginary".Could you please let me know where the problems come from , thanks professor! Best wishes!

jmaih commented 1 month ago

Do you have lines of code starting with a # sign? These are definitions

The error message says that the evaluation of those expression gives nan. This could be due to the fact that

J.

On 19 Jul 2024, at 14:53, Zhanwenqing @.***> wrote:

Hi professor, sorry to bother you. I'm working with a endogenous regime switching model. When I run [,loglik,,retcode,~] = filter(m), it returns the "loglik=-100000000'', "retcode=5''. When I run decipher(retcode), it reports"definitions are nan or inf or imaginary".Could you please let me know where the problems come from , thanks professor! Best wishes!

— Reply to this email directly, view it on GitHub https://github.com/jmaih/RISE_toolbox/issues/187, or unsubscribe https://github.com/notifications/unsubscribe-auth/AATKBT3TAOPD4VXUP2GWYHTZNED3TAVCNFSM6AAAAABLERIR22VHI2DSMVQWIX3LMV43ASLTON2WKOZSGQYTQOBRG4ZTSNQ. You are receiving this because you are subscribed to this thread.

Zhanwenqing commented 1 month ago

Hi Professor Maih, Sorry to bothor you again. I found after changing the initial value of the parameter, the "retcode=5'' problem solved. But there is another problem, when I use csminwel.m to estimate the posterior mode, it reports

Improvement on iteration 1 = 0.000000000 improvement < crit termination smallest step still improving too slow, reversed gradient

Because csminwel is a local optimizer, so I guess the initial value of parameters are not properate? Could you please tell me where the problems come from and how to fix it? If I should use some global optimizer like cmaes? If so, how to call the cmaes function in RISE? Tnank you professor!

Best wishes! Zhan

jmaih commented 1 month ago

If you want want to use CMAES, you will need to write a wrapping function that calls it. The wrapping function should take the same inputs and outputs as wcsminwel, which is rise’s wrapper for csminwel.

I have no control about what specific optimisers do.

Best,

J.

On 19 Jul 2024, at 16:00, Zhanwenqing @.***> wrote:

Hi Professor Maih, Sorry to bothor you again. I found after changing the initial value of the parameter, the "retcode=5'' problem solved. But there is another problem, when I use csminwel.m to estimate the posterior mode, it reports

Improvement on iteration 1 = 0.000000000 improvement < crit termination smallest step still improving too slow, reversed gradient

Because  csminwel.m is a local optimizer, so I guess the initial value of parameters are not properate? Could you please tell me where the problems come from and how to fix it? If I should use some global optimizer like cmaes.m? If so, how to call the cmaes function in RISE? Tnank you professor!

Best wishes! Zhan

— Reply to this email directly, view it on GitHub https://github.com/jmaih/RISE_toolbox/issues/187#issuecomment-2239248683, or unsubscribe https://github.com/notifications/unsubscribe-auth/AATKBT6KRXSK73AHYEFVIFTZNELWZAVCNFSM6AAAAABLERIR22VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDEMZZGI2DQNRYGM. You are receiving this because you commented.

Zhanwenqing commented 1 month ago

Dear professor: Thanks for your reply. I saw you said in issue #26 :”You reached a minimum, not necessarily the "global one", which you cannot prove for the type of models we estimate unless you know it in advance.“ I'm thinking, since we can not find the posterior mode exactly using some Optimization Algorithm, can we use an arbitrary initial value for the parameters to implement MH algrithm, after finishing the sampling, we can know the posterior mode. Hope for your reply, thanks in advance.

Best wishes! Zhan

jmaih commented 1 month ago
  1. To be clear about the terminology, there may be multiple modes. All of them are modes. The question is which one is the highest and can we find it?
  2. No theory says that posterior maximization is a pre-condition for posterior sampling. You can perfectly do posterior sampling without posterior maximization. All you need is a starting point and a positive definite covariance matrix.
  3. Part of the reason why we typically perform posterior maximization first is because we do not trust the posterior sampler.
  4. There is no way to know whether the highest peak of posterior sampling is the global mode
  5. No guarantee starting posterior sampling at an arbitrary point will result in a sampling whose highest peak is at least as high as what you would obtain by doing posterior maximization.
  6. Unless the only problem you have is to find the global mode, in which case you should probably use a global optimizer which itself will still not guarantee that you found the global peak, you should not lose sight of the goal which is to tell an economic story. The global peak by itself does not guarantee that you get a good economic story either.

On Sat, Jul 20, 2024 at 9:23 AM Zhanwenqing @.***> wrote:

Dear professor: Thanks for your reply. I saw you said in issue #26 https://github.com/jmaih/RISE_toolbox/issues/26 :”You reached a minimum, not necessarily the "global one", which you cannot prove for the type of models we estimate unless you know it in advance.“ I'm thinking, since we can not find the posterior mode exactly using some Optimization Algorithm, can we use an arbitrary initial value for the parameters to implement MH algrithm, after finishing the sampling, we can know the posterior mode. Hope for your reply, thanks in advance.

Best wishes! Zhan

— Reply to this email directly, view it on GitHub https://github.com/jmaih/RISE_toolbox/issues/187#issuecomment-2240984400, or unsubscribe https://github.com/notifications/unsubscribe-auth/AATKBTYO3WJ2E4ABQPAZ6Q3ZNIGAHAVCNFSM6AAAAABLERIR22VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDENBQHE4DINBQGA . You are receiving this because you commented.Message ID: @.***>

Zhanwenqing commented 1 month ago

Thanks for your reply professor. It helps me a lot. Thanks again. There is still a problem confusing me. If I use csminwel.m to compute the posterior mode, it reports

Improvement on iteration 1 = 0.000000000 improvement < crit termination smallest step still improving too slow, reversed gradient

I have tries many initial values of parameters, this problem always rise. I wanna ask professor how to fix this problem in practice? Sorry to bothor you again, thanks in advance.

Best wishes! Zhan

jmaih commented 1 month ago

I do not know the details of csminwel.

Did you estimate the model before?

Did you try a different optimizer?

Does your dataset have actual values and not just nans?

On Sat, Jul 20, 2024, 17:05 Zhanwenqing @.***> wrote:

Thanks for your reply professor. It helps me a lot. Thanks again. There is still a problem confusing me. If I use csminwel.m to compute the posterior mode, it reports

Improvement on iteration 1 = 0.000000000 improvement < crit termination smallest step still improving too slow, reversed gradient

I have tries many initial values of parameters, this problem always rise. I wanna ask professor how to fix this problem in practice? Sorry to bothor you again, thanks in advance.

Best wishes! Zhan

— Reply to this email directly, view it on GitHub https://github.com/jmaih/RISE_toolbox/issues/187#issuecomment-2241179074, or unsubscribe https://github.com/notifications/unsubscribe-auth/AATKBT7AXUR5JMXRE4UFP23ZNJ4EBAVCNFSM6AAAAABLERIR22VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDENBRGE3TSMBXGQ . You are receiving this because you commented.Message ID: @.***>

Zhanwenqing commented 1 month ago

Thank you for your reply professor. I did not estimate the model before, and I will check my data again.But I have tried cmaes.m, however the sulution is weired. Below is the code which I use to compute posterior mode,

H0 =0.2*ones(size(20,1),1); cmaesOptions = cmaes; cmaesOptions.LBounds = P.para(:,2); cmaesOptions.UBounds = P.para(:,3); [x_opt_hat, fhat, COUNTEVAL, STOPFLAG, OUT, BESTEVER] = cmaes('PostKer',xini',H0,cmaesOptions,block,m,P);

Where block is 1x24 double of 1,2,...,24(I have 24 parameters in total), m is the rise object, P is the parameter struct, which has parameter name/ parameter type/parameter value (initial value, lower bound, upper bound, mean and std). After running this code, it finishes super quicklly and reports

n=24: (6,13)-CMA-ES(w=[38 25 17 11 7 3]%, mu_eff=4.0) on function PostKer Iterat, #Fevals: Function Value (median,worst) |Axis Ratio|idx:Min SD idx:Max SD 1 , 14 : 1.0000000000000e+08 +(0e+00,0e+00) | 1.04e+00 | 13:1.9e-01 5:1.9e-01

Fevals: f(returned x) | bestever.f | stopflag (saved to variablescmaes.mat)

 15: 1.00000000000e+08 | 1.00000000000e+08 | equalfunvals

mean solution: +4.3e+00 +3.3e-01 +4.0e-01 +5.4e-01 +3.5e+00 +3.8e-01 +9.0e-01 +7.5e-01 +5.8e-01 +8.0e-01 +1.3e-01 +1.5e-01 +6.1e-01 +2.1e+00 +2.5e-02 +9.0e-01 +9.6e-01 +1.9e+00 +1.0e+00 -3.5e-01 +2.3e-01 +4.4e-01 -5.7e-01 -4.2e-01 std deviation: 1.9e-01 1.9e-01 1.9e-01 1.9e-01 1.9e-01 1.9e-01 1.9e-01 1.9e-01 1.9e-01 1.9e-01 1.9e-01 1.9e-01 1.9e-01 1.9e-01 1.9e-01 1.9e-01 1.9e-01 1.9e-01 1.9e-01 1.9e-01 1.9e-01 1.9e-01 1.9e-01 1.9e-01 use plotcmaesdat.m for plotting the output at any time (option LogModulo must not be zero)

Then the output x_opt_hat is the same as xini. Below is the code which I use to compute posterior mode using csminwel, I mimic this code to call function cmaes

[realbig,xopt,~,Hopt,~,~,~] = csminwel('PostKer',xini,Hini,[],1e-5,500,0,block,m,P);

This code returns

Improvement on iteration 1 = 0.000000000 improvement < crit termination smallest step still improving too slow, reversed gradient

I think I call the csminwel function correctly, there's other problems for not finding posterior mode. I wanna ask professor do I call the cmaes.m function correctly? What does the solution means after running cmaes.m?Sorry to bothor you again, thanks in advance.

Best wishes! Zhan

jmaih commented 1 month ago

So you've written a function PostKer that, I guess, is going to use the model object, but it is unclear how.

You are actually calling csminwel literally. I thought you were calling wcsminwel.

The details of the function PostKer may matter for the results you are obtaining.

Please start by using fmincon or fminsearch but without the function you wrote.

I suspect the implementation of PostKer is problematic but I could be wrong.

On Sat, Jul 20, 2024, 18:23 Zhanwenqing @.***> wrote:

Thank you for your reply professor. I did not estimate the model before, and I will check my data again.But I have tried cmaes.m, however the sulution is weired. Below is the code which I use to compute posterior mode,

H0 =0.2*ones(size(20,1),1); cmaesOptions = cmaes; cmaesOptions.LBounds = P.para(:,2); cmaesOptions.UBounds = P.para(:,3); [x_opt_hat, fhat, COUNTEVAL, STOPFLAG, OUT, BESTEVER] = cmaes('PostKer',xini',H0,cmaesOptions,block,m,P);

Where block is 1x24 double of 1,2,...,24(I have 24 parameters in total), m is the rise object, P is the parameter struct, which has parameter name/ parameter type/parameter value (initial value, lower bound, upper bound, mean and std). After running this code, it finishes super quicklly and reports

n=24: (6,13)-CMA-ES(w=[38 25 17 11 7 3]%, mu_eff=4.0) on function PostKer Iterat, #Fevals: Function Value (median,worst) |Axis Ratio|idx:Min SD idx:Max SD 1 , 14 : 1.0000000000000e+08 +(0e+00,0e+00) | 1.04e+00 | 13:1.9e-01 5:1.9e-01

Fevals: f(returned x) | bestever.f | stopflag (saved to

variablescmaes.mat) 15: 1.00000000000e+08 | 1.00000000000e+08 | equalfunvals mean solution: +4.3e+00 +3.3e-01 +4.0e-01 +5.4e-01 +3.5e+00 +3.8e-01 +9.0e-01 +7.5e-01 +5.8e-01 +8.0e-01 +1.3e-01 +1.5e-01 +6.1e-01 +2.1e+00 +2.5e-02 +9.0e-01 +9.6e-01 +1.9e+00 +1.0e+00 -3.5e-01 +2.3e-01 +4.4e-01 -5.7e-01 -4.2e-01 std deviation: 1.9e-01 1.9e-01 1.9e-01 1.9e-01 1.9e-01 1.9e-01 1.9e-01 1.9e-01 1.9e-01 1.9e-01 1.9e-01 1.9e-01 1.9e-01 1.9e-01 1.9e-01 1.9e-01 1.9e-01 1.9e-01 1.9e-01 1.9e-01 1.9e-01 1.9e-01 1.9e-01 1.9e-01 use plotcmaesdat.m for plotting the output at any time (option LogModulo must not be zero)

Then the output x_opt_hat is the same as xini. Below is the code which I use to compute posterior mode using csminwel, I mimic this code to call function cmaes

[realbig,xopt,,Hopt,,,] = csminwel('PostKer',xini,Hini,[],1e-5,500,0,block,m,P);

This code returns

Improvement on iteration 1 = 0.000000000 improvement < crit termination smallest step still improving too slow, reversed gradient

I think I call the csminwel function correctly, there's other problems for not finding posterior mode. I wanna ask professor do I call the cmaes.m function correctly? What does the solution means after running cmaes.m?Sorry to bothor you again, thanks in advance.

Best wishes! Zhan

— Reply to this email directly, view it on GitHub https://github.com/jmaih/RISE_toolbox/issues/187#issuecomment-2241197986, or unsubscribe https://github.com/notifications/unsubscribe-auth/AATKBT6BFG2QKFK7JUE2CTDZNKFIBAVCNFSM6AAAAABLERIR22VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDENBRGE4TOOJYGY . You are receiving this because you commented.Message ID: @.***>

Zhanwenqing commented 1 month ago

Thanks for your reply prefessor. After running estimate, it reports

Looking for good enough start values. Please wait... 1 :: definitions are nan or inf or imaginary 2 :: definitions are nan or inf or imaginary 3 :: definitions are nan or inf or imaginary ........ 495 :: definitions are nan or inf or imaginary 496 :: definitions are nan or inf or imaginary 497 :: definitions are nan or inf or imaginary 498 :: definitions are nan or inf or imaginary 499 :: definitions are nan or inf or imaginary 错误使用 generic/find_posterior_mode find_posterior_mode:: No admissible starting value after 500 trials

错误 generic/estimate (第 191 行) [x1,f1,H,x0,f0,viol,funevals,issue,obj]=find_posterior_mode(obj,x0,lb_short,ub_short); %#ok

I wanna ask professor howto fix this problem. Sorry to bothor you again, thanks in advance.

Best wishes! Zhan

jmaih commented 1 month ago

This confirms my suspicions: you were not doing any estimation with the function you wrote.Now RISE is telling you that estimation cannot take place. And it tells you that it is related to your problem with definitions. The same problem from the very beginning.Before estimating the model, make sure you can solve it and print its solution.Sendt fra min iPhone21. juli 2024 kl. 07:41 skrev Zhanwenqing @.***>: Thanks for your reply prefessor. After running estimate, it reports Looking for good enough start values. Please wait... 1 :: definitions are nan or inf or imaginary 2 :: definitions are nan or inf or imaginary 3 :: definitions are nan or inf or imaginary ........ 495 :: definitions are nan or inf or imaginary 496 :: definitions are nan or inf or imaginary 497 :: definitions are nan or inf or imaginary 498 :: definitions are nan or inf or imaginary 499 :: definitions are nan or inf or imaginary 错误使用 generic/find_posterior_mode find_posterior_mode:: No admissible starting value after 500 trials 错误 generic/estimate (第 191 行) [x1,f1,H,x0,f0,viol,funevals,issue,obj]=find_posterior_mode(obj,x0,lb_short,ub_short); %#ok I wanna ask professor howto fix this problem. Sorry to bothor you again, thanks in advance. Best wishes! Zhan

—Reply to this email directly, view it on GitHub, or unsubscribe.You are receiving this because you commented.Message ID: @.***>