Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem in transition matrix #189

Open
khoilx opened this issue Oct 10, 2024 · 11 comments
Open

Problem in transition matrix #189

khoilx opened this issue Oct 10, 2024 · 11 comments

Comments

@khoilx
Copy link

khoilx commented Oct 10, 2024

Hello Prof. Maih,
I'm facing the problem of retcode "3" (Problem in transition matrix) because I declared an alien list to calculate the transitional probability (tp_1_2) from an outside function (that calculate the mvncdf of a vector) (e.g. ! s1_tp_1_2 =tp_func(...)). Could you please help me to deal with this problem in RISE?

@jmaih
Copy link
Owner

jmaih commented Oct 11, 2024 via email

@khoilx
Copy link
Author

khoilx commented Oct 11, 2024

Hello Prof. Maih,
Yes, I pass the alien function correctly (I declared as:
m=rise('cmr_mod','steady_state_file','model_ss',...
'rise_flags',model_settings,...
'alien_list','tp_func2');).
I'm sending you the 'tp_func2.m' function file. Could you please check it for me?

function y=tp_func2(z1,z2,z3,a1,a2,a3,xmin,xmax,ymin,ymax,zmin,zmax,wmin,wmax,varargin)

if ~isreal(z1) && ~isreal(z2) && ~isreal(z3)

iz1=imag(z1);

iz2 = imag(z2);

iz3 = imag(z3);

z1=real(z1);

z2=real(z2);

z3=real(z3);

if abs(iz1)>1e-12 && abs(iz2)>1e-12 && abs(iz3)>1e-12 
    
    y=nan;
    
    return
    
end

end

Pev_sq = [a1 a2; a2 a3];

Pvv = [1, z3; z3, 1];

A = [z1 0; 0 z2];

try
% Try solving Sigw = ASigwA'+Pvv for Sigw.
Sigw = dlyap(A,Pvv);
catch
% If A is not stable, throw error and skip all calculations.
warning('A is not stable. Assigning 0 to likelihood');
Sigw = eye(2)*.1e10; %replace s.Sigw by garbage
%lik = 0; %replace s.lik by 0
end

%compute the covariance Pvve
Pvve = Pvv - Pev_sq;

%compute covariance matrix for 4d multivariate normal in the calculation of time-varying transition
Sigma_mvn = [Sigw, SigwA'; ASigw, Pvve + ASigwA'];

if isempty(varargin)

y=main();

else

if length(varargin)==2
    
    if ~strcmp(varargin{1},'diff')
        
        error('Wrong call to function')
        
    end
    
    switch varargin{2}
        
        case 1
            
            y=linhbin_z();
            
        case {2,3,4,5}
            
            error('Higher-order derivatives not implemented')
            
    end
    
else
    
    error('Wrong call to function')
    
end

end

function y=main()
    
    y=mvncdf([xmin,ymin,zmin,wmin],[xmax,ymax,zmax,wmax],[0 0 0 0],Sigma_mvn)/mvncdf([zmin,wmin],[zmax,wmax],[0 0],Sigw);
    
end

function fz=linhbin_z()
    
    fz=(mvnpdf([xmax,ymax,zmax,wmax],[0 0 0 0],Sigma_mvn)-mvnpdf([xmin,ymin,zmin,wmin],[0 0 0 0],Sigma_mvn))/...
        (mvnpdf([zmax,wmax],[0 0],Sigw)-mvnpdf([zmin,wmin],[0 0],Sigw));
           
end

end

And in .rs file, I define the transition probabilities as, e.g.
! s1_tp_1_2 =1-tp_func2(alpha1,alpha2,rho_v,pev11_0,pev12_1,pev22_0,-inf,b1_0,-inf,b2_0,-inf,tau1,-inf,tau2)

Thank you for your help,
Khoi

@jmaih
Copy link
Owner

jmaih commented Oct 11, 2024 via email

@khoilx
Copy link
Author

khoilx commented Oct 12, 2024

Hello Prof. Maih,
I realize that I can only solve the model if I use the .m function with only scalar (the normpdf of a value) to calculate the transitional probabilities, it doesn't work (as retcode shows "3" (Problem in Transition Matrix)) when I declare command of multiple variables cdf (mvdcdf). Do you have any suggestion?

@jmaih
Copy link
Owner

jmaih commented Oct 12, 2024 via email

@khoilx
Copy link
Author

khoilx commented Oct 12, 2024

So what is the way to use the "mvncdf" to calculate the cdf of a vector in the alien function? The output of "mvncdf" is scalar, but its input needs mean vector and covariance matrix.

@jmaih
Copy link
Owner

jmaih commented Oct 12, 2024 via email

@jmaih
Copy link
Owner

jmaih commented Oct 12, 2024 via email

@khoilx
Copy link
Author

khoilx commented Oct 12, 2024

Dear Prof. Maih,
Yes, I used scalar inputs and arrange it into matrix in the alien function to calculate the multivariate normal distribution value.
Could you please check if my alien function is declared correctly?

`function y=tp_func2(z1,z2,z3,a1,a2,a3,xmin,xmax,ymin,ymax,zmin,zmax,wmin,wmax,varargin)

if ~isreal(z1) && ~isreal(z2) && ~isreal(z3)

iz1=imag(z1);

iz2 = imag(z2);

iz3 = imag(z3);

z1=real(z1);

z2=real(z2);

z3=real(z3);

if abs(iz1)>1e-12 && abs(iz2)>1e-12 && abs(iz3)>1e-12

y=nan;

return

end
end

Pev_sq = [a1 a2; a2 a3];

Pvv = [1, z3; z3, 1];

A = [z1 0; 0 z2];

try
% Try solving Sigw = ASigwA'+Pvv for Sigw.
Sigw = dlyap(A,Pvv);
catch
% If A is not stable, throw error and skip all calculations.
warning('A is not stable. Assigning 0 to likelihood');
Sigw = eye(2)*.1e10; %replace s.Sigw by garbage
%lik = 0; %replace s.lik by 0
end

%compute the covariance Pvve
Pvve = Pvv - Pev_sq;

%compute covariance matrix for 4d multivariate normal in the calculation of time-varying transition
Sigma_mvn = [Sigw, SigwA'; ASigw, Pvve + ASigwA'];

if isempty(varargin)

y=main();
else

if length(varargin)==2

if ~strcmp(varargin{1},'diff')
    
    error('Wrong call to function')
    
end

switch varargin{2}
    
    case 1
        
        y=linhbin_z();
        
    case {2,3,4,5}
        
        error('Higher-order derivatives not implemented')
        
end

else

error('Wrong call to function')

end
end

function y=main()

y=mvncdf([xmin,ymin,zmin,wmin],[xmax,ymax,zmax,wmax],[0 0 0 0],Sigma_mvn)/mvncdf([zmin,wmin],[zmax,wmax],[0 0],Sigw);

end

function fz=linhbin_z()

fz=(mvnpdf([xmax,ymax,zmax,wmax],[0 0 0 0],Sigma_mvn)-mvnpdf([xmin,ymin,zmin,wmin],[0 0 0 0],Sigma_mvn))/...
    (mvnpdf([zmax,wmax],[0 0],Sigw)-mvnpdf([zmin,wmin],[0 0],Sigw));

end`
And the transitional probability is defined in .rs file as following:
! s1_tp_1_2 =1-tp_func2(alpha1,alpha2,rho_v,pev11_0,pev12_1,pev22_0,-inf,b1_0,-inf,b2_0,-inf,tau1,-inf,tau2)

@khoilx
Copy link
Author

khoilx commented Oct 12, 2024

The function uses the scalar input (e.g. 1-tp_func2(0.5, 0.5, 0.5, 0.5, 0.5, 0.5, -inf, 0, -inf, 0, -inf, 0, -inf, 0)), and give a scalar result (e.g. 0.25).

@jmaih
Copy link
Owner

jmaih commented Oct 12, 2024 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants