Dear community,
I want to calculate the loss of the following system:
%Hybrid NKM under commitment
%% Step 1 define the parameters
%same Parameters as from the script
%Parameters
gam_f = 0.5
gam_b =0.5
beta= 1
gam_x = 0.2
lambda = 0.5
phi = 0.5 % IS Parameter for interest rate
sig = 1 % IS Parameter for interest rate
AR_par = 0.8
%% Step 2
% System is: (w;v)(+1) = A*[w;v] + [ 1;0;0;0;0]*eps
A11= [AR_par 0 0;0 0 0; 0 0 0];
A12= [ 0 0; 1 0; 0 1];
A21 =[ 0 -gam_f/(beta^2*gam_b) 0; -1/(beta*gam_f) 0 -gam_b/gam_f]; %Klammern nicht vergessen
A22 =[ 1/(beta^2*gam_b) gam_x/(beta^2*lambda*gam_b); -gam_x/(beta*gam_f) 1/(beta*gam_f)];
A = [ [A11 A12] ; [A21 A22] ]
%% Step 3
% using the Schur Decomposition to solve the state equations
% solve the system
disp('Schur decomposition')
[Z, T] = schur(A, 'complex')
disp('reorder eigenvalues in increasing order along the principal diagonal')
[Z T] = ordschur(Z,T, 1:5)
if abs(sum(sum(Z*T*Z'-A))) > 0.0001 && sum(sum(Z'*Z-eye(lenght(Z)))) > 0.0001
disp('Error in Schur decomposition')
end
disp('check Blanchard-Kahn')
abs_diag_T = abs(diag(T))'
%% Calculating the solution time path for nu, x and pi using the following law of motion:
% z(+1) = E[z(+1)] + Z_11^-1 * [1;0;0] * eps
T_11 = T(1:3,1:3)
Z_11 = Z(1:3,1:3)
Z_21 = Z(4:5,1:3)
T=1000;
z_solution= zeros(3,T); % zeros because we have variables in i with t-1
w_solution= zeros(3,T); % zeros because we have variables in i with t-1
w_solution(:,1)=[ 1; 0; 0]; %initial jump
z_solution(:,1)=inv(Z_11)* w_solution(: ,1);%initial jump
v_solution= zeros(2,T);
i_solution= zeros(1,T); % nominal interest rate: IS umgestellt nach der Variable i.Hier liegt anscheinend das Problem.
for t= 2:T
z_solution(:,t)= T_11* z_solution(: ,t-1 );
w_solution(:,t)= Z_11 * z_solution(:,t);
v_solution(:,t)= Z_21 *inv(Z_11)* w_solution(:,t);
end
for t= 1:T-1
i_solution(:,t) =((1- phi)*v_solution(1,t+1)+phi*w_solution(2,t)-w_solution(2,t+1))*sig+ v_solution(2,t+1); % Jump in 1 anstatt 2. umgestellte IS-Kurve.
end;
For calculating the loss I need the v_solution that is a vector containing two variables x and pi. I need the mean of these two for my loss function.
function [ LossVal, vol_pi, vol_x ] = Loss_Fun( data, w_pi, w_x )
vol_pi = (mean(data.v_solution(2,1:T).^2));
vol_x = (mean(data.v_solution(1,1:T).^2));
LossVal = w_pi * vol_pi ...
+ w_x * vol_x;
w_pi = 0.5 ; %1;
w_x = 0.25; %0.5;
end
but something seems to be wrong here. But I dont knwo what

3 comentarios

Geoff Hayes
Geoff Hayes el 28 de Mayo de 2019
Farah - which line of code is throwing the error? Please copy and paste the full error message to this question. Also, you have included the Loss_Fun function but I don't see how it is used with the remainder of the code. Please clarify.
Farah Shahpoor
Farah Shahpoor el 26 de Jun. de 2019
Hello Geoff,
this is the error message:
Not enough input arguments.
Error in lossc (line 3)
vol_pi = (mean(data.v_solution(2,T).^2));
I want to calculate the loss the model, which I programmed with the first code. I want to take the values of the model to compute the loss in the second step.
Farah Shahpoor
Farah Shahpoor el 26 de Jun. de 2019
I run the second code exactly as this:
function [ LossVal, vol_pi, vol_x ] = Loss_Fun( data, w_pi, w_x )
vol_pi = (mean(data.v_solution(2,1:T).^2));
vol_x = (mean(data.v_solution(1,1:T).^2));
LossVal = w_pi * vol_pi ...
+ w_x * vol_x;
w_pi = 0.5 ; %1;
w_x = 0.25; %0.5;
end
I got a hint that I migh not run the code properly. And that I need to plug in the input arguments. But what does it mean? I was thinking these are the input arguments:
vol_pi = (mean(data.v_solution(2,1:T).^2));
vol_x = (mean(data.v_solution(1,1:T).^2));

Iniciar sesión para comentar.

 Respuesta aceptada

Guillaume
Guillaume el 26 de Jun. de 2019

0 votos

You haven't shown us how you are calling your function Loss_fun.
If you run it with the green run button, or call it without any input argument then yes, you'll get the error you see.
>>Loss_Fun
Not enough input arguments.
Error in Loss_fun (line 3)
vol_pi = (mean(data.v_solution(2,1:T).^2));
Since you're not passing the required inputs data, w_pi and w_px.
The proper way to call the function would something like:
result = Loss_fun(something, somethingelse, anothersomething); %3 inputs required
Even if you passed the required inputs, you'd still get an error: Undefined function of variable T since it's neither an input to the function nor defined inside the function. It is unclear where you thought that T would come from.

8 comentarios

Farah Shahpoor
Farah Shahpoor el 26 de Jun. de 2019
Hello Guillaume,
You haven't shown us how you are calling your function Loss_fun.
If you run it with the green run button, or call it without any input argument then yes, you'll get the error you see.
Yes, I did both.
I run it with the green run botton or just called the function by typing Loss_fun in the command field.
result = Loss_fun(something, somethingelse, anothersomething); %3 inputs required
Can you explain me what are you doing here exactly, please?
I get this point. So I need to define what T is. T is the lenght of the simulation and in my case 1000.
%% Calculating the solution time path for nu, x and pi using the following law of motion:
% z(+1) = E[z(+1)] + Z_11^-1 * [1;0;0] * eps
T_11 = T(1:3,1:3)
Z_11 = Z(1:3,1:3)
Z_21 = Z(4:5,1:3)
T=1000;
Guillaume
Guillaume el 26 de Jun. de 2019
The same way that you need to pass your input to the sin function:
>> sin %no input passed to sin
Error using sin
Not enough input arguments.
>> sin(1:5) %required input passed to sin
ans =
0.84147 0.9093 0.14112 -0.7568 -0.95892
you need to pass the required inputs to your own function. You've defined a function that requires 3 inputs, so you need to pass these 3 inputs to your function. I have no idea what these are supposed to be, you haven't explained and it's not clear from what you have posted so far. It's clear that the first input is supposed to be a structure with at least one field named v_solution.
As for T, it needs to be an additional input to the function or needs to be hard-coded inside the function, before it is used.
Note that:
T_11 = T(1:3,1:3)
%....
T=1000;
is poor coding. You start with a T variable that is a matrix, then reuse that variable name to store a scalar. You've got an infinity of possible variable names, you don't need to reuse an existing one. Using meaningful words instead of single letter would make your code easier to understand as well.
Guillaume
Guillaume el 26 de Jun. de 2019
Farah wrote:
Yes, okay. But when I define
vol_pi = (mean(data.v_solution(2,1:T).^2));
vol_x = (mean(data.v_solution(1,1:T).^2));
isnt it the input argument? I was thinking I already included it.
No, you define the function as
function [ LossVal, vol_pi, vol_x ] = Loss_Fun(data, w_pi, w_x )
%... rest of the code doesn't matter
end
So that function takes 3 inputs which it calls data, w_pi and w_x. (It also returns 3 outputs).
Since the function takes 3 inputs, when you want to use it, you have to give it these 3 inputs. Again, you have not explained what these 3 inputs should be. It could be something like:
mydata = struct('v_solution', 1:10);
[loss, vpi, vx] = Loss_Fun(mydata, 3.14, 5); %call function with 3 inputs. 1st one is mydata, 2nd one is 3.14, 3rd one is 5
A function doesn't define its own inputs, the inputs need to be given by the caller
Note that the problem is not (yet) with the line
vol_pi = mean(data.v_solution(2,1:T).^2); %unnecessary brackets removed
but with the fact that you call the function with just
Loss_fun;
instead of
Loss_fun(xxxx, yyy, zzz); %don't know what xxx, yy, zzz are but you need to give it its 3 inputs.
Furthermore, if you don't capture the return values of the function, there's very little point in calling it, so:
[aaa, bbb, ccc] = Loss_fun(xxxx, yyy, zzz); %don't know which variables you want to put the result in.
is probably required.
Farah Shahpoor
Farah Shahpoor el 28 de Jun. de 2019
Thanks alot for your explanation. This is now my actual code.
Iam calling the Lossfunction in the main script as follow:
data = struct('v_solution', 1:1000);
w_pi = 0.5 ;
w_x = 0.25;
[ LossVal, vol_pi, vol_x ] = Loss_Fun( data, w_pi, w_x )
and my Function looks like
function [ LossVal, vol_pi, vol_x ] = Loss_Fun( data, w_pi, w_x )
vol_pi = (mean(data.v_solution(2,:).^2));
vol_x = (mean(data.v_solution(1,:).^2));
LossVal = w_pi * vol_pi ...
+ w_x * vol_x;
end
I saved my main script and the Loss_fun.m in the same folder. It is possibe to delete T from my function?
Now I have the followiing error:
Cannot find an exact (case-sensitive) match for 'Loss_Fun'
The closest match is: Loss_fun in C:\Users\Farah\Documents\MATLAB\Commitment\Loss_fun.m
Error in COMMITMENT (line 96)
[ LossVal, vol_pi, vol_x ] = Loss_Fun( data, w_pi, w_x )
Guillaume
Guillaume el 28 de Jun. de 2019
You saved the function as Loss_fun (small f) but use it as Loss_Fun (capital F), you either need to call it with Loss_fun:
[ LossVal, vol_pi, vol_x ] = Loss_fun( data, w_pi, w_x ); %with a small f
Or rename the file so it has a capital F. If on windows, you'll first have to rename it to something completely different before renaming it to Loss_Fun (with a capital F).
Farah Shahpoor
Farah Shahpoor el 28 de Jun. de 2019
Editada: Guillaume el 30 de Jun. de 2019
Thanks!
Stupid mistake...
the problem is solved now with a small f.
As you mentioned above now I got the error with the T. Undefined function or variable 'T'.
you said it is unclear where the T should come from.
%% Calculating the solution time path for nu, x and pi using the following law of motion:
% z(+1) = E[z(+1)] + Z_11^-1 * [1;0;0] * eps
T_11 = T(1:3,1:3)
Z_11 = Z(1:3,1:3)
Z_21 = Z(4:5,1:3)
T=1000
I was thinking matlab would take this T as given in the main script.
Guillaume
Guillaume el 30 de Jun. de 2019
If T from the main script is needed by the function, then you need to pass it as an input argument. Hence your function should have 4 inputs:
function [LossVal, vol_pi, vol_x ] = Loss_Fun(data, w_pi, w_x, T) %function has now 4 inputs
Farah Shahpoor
Farah Shahpoor el 16 de Jul. de 2019
Thank you.
Changed it as you said. I get the following solution for my loss:
LossVal =
8.3459e+04
LossVal =
8.3459e+04
vol_pi =
0.9976
vol_x = 3.3383e+05
The loss should be 13.085 but here it is 8.3459. And why do I have two LossVal?

Iniciar sesión para comentar.

Más respuestas (1)

KALYAN ACHARJYA
KALYAN ACHARJYA el 26 de Jun. de 2019
Editada: KALYAN ACHARJYA el 26 de Jun. de 2019

0 votos

# Check Commnet, this function file have no role in main script.
Save the function in different file as "Loss_Fun.m" (Same directory)
function [LossVal,vol_pi,vol_x]=Loss_Fun(data,w_pi,w_x)
vol_pi=(mean(data.v_solution(2,1:T).^2));
vol_x=(mean(data.v_solution(1,1:T).^2));
LossVal=w_pi*vol_pi+w_x*vol_x;
%w_pi = 0.5 ; %1;
%w_x = 0.25; %0.5;
end
Main Script (Run the main script)
%Hybrid NKM under commitment
%% Step 1 define the parameters
%same Parameters as from the script
%Parameters
gam_f = 0.5
gam_b =0.5
beta= 1
gam_x = 0.2
lambda = 0.5
phi = 0.5 % IS Parameter for interest rate
sig = 1 % IS Parameter for interest rate
AR_par = 0.8
%% Step 2
% System is: (w;v)(+1) = A*[w;v] + [ 1;0;0;0;0]*eps
A11= [AR_par 0 0;0 0 0; 0 0 0];
A12= [ 0 0; 1 0; 0 1];
A21 =[ 0 -gam_f/(beta^2*gam_b) 0; -1/(beta*gam_f) 0 -gam_b/gam_f]; %Klammern nicht vergessen
A22 =[ 1/(beta^2*gam_b) gam_x/(beta^2*lambda*gam_b); -gam_x/(beta*gam_f) 1/(beta*gam_f)];
A = [ [A11 A12] ; [A21 A22] ]
%% Step 3
% using the Schur Decomposition to solve the state equations
% solve the system
disp('Schur decomposition')
[Z, T] = schur(A, 'complex')
disp('reorder eigenvalues in increasing order along the principal diagonal')
[Z T] = ordschur(Z,T, 1:5)
if abs(sum(sum(Z*T*Z'-A))) > 0.0001 && sum(sum(Z'*Z-eye(lenght(Z)))) > 0.0001
disp('Error in Schur decomposition')
end
disp('check Blanchard-Kahn')
abs_diag_T = abs(diag(T))'
%% Calculating the solution time path for nu, x and pi using the following law of motion:
% z(+1) = E[z(+1)] + Z_11^-1 * [1;0;0] * eps
T_11 = T(1:3,1:3)
Z_11 = Z(1:3,1:3)
Z_21 = Z(4:5,1:3)
T=1000;
z_solution= zeros(3,T); % zeros because we have variables in i with t-1
w_solution= zeros(3,T); % zeros because we have variables in i with t-1
w_solution(:,1)=[ 1; 0; 0]; %initial jump
z_solution(:,1)=inv(Z_11)* w_solution(: ,1);%initial jump
v_solution= zeros(2,T);
i_solution= zeros(1,T); % nominal interest rate: IS umgestellt nach der Variable i.Hier liegt anscheinend das Problem.
for t= 2:T
z_solution(:,t)= T_11* z_solution(: ,t-1 );
w_solution(:,t)= Z_11 * z_solution(:,t);
v_solution(:,t)= Z_21 *inv(Z_11)* w_solution(:,t);
end
for t= 1:T-1
i_solution(:,t) =((1- phi)*v_solution(1,t+1)+phi*w_solution(2,t)-w_solution(2,t+1))*sig+ v_solution(2,t+1); % Jump in 1 anstatt 2. umgestellte IS-Kurve.
end;
Output
gam_f =
0.5000
gam_b =
0.5000
beta =
1
gam_x =
0.2000
lambda =
0.5000
phi =
0.5000
sig =
1
AR_par =
0.8000
A =
0.8000 0 0 0 0
0 0 0 1.0000 0
0 0 0 0 1.0000
0 -1.0000 0 2.0000 0.8000
-2.0000 0 -1.0000 -0.4000 2.0000
Schur decomposition
Z =
0.0000 - 0.0000i 0.0000 - 0.0000i -0.2949 + 0.0000i -0.3320 + 0.0694i 0.0258 - 0.8929i
-0.1586 - 0.3781i 0.2772 + 0.2163i 0.7860 + 0.0000i -0.0795 + 0.1836i 0.0683 - 0.2138i
0.2674 - 0.1121i 0.0822 - 0.3647i -0.1149 + 0.0000i 0.0359 + 0.8130i 0.3023 + 0.0965i
0.0856 - 0.7009i 0.5138 - 0.1167i -0.3731 + 0.0000i 0.0328 - 0.2581i -0.0960 + 0.0882i
0.4956 + 0.0605i -0.0443 - 0.6760i 0.3779 + 0.0000i -0.0505 - 0.3353i -0.1247 - 0.1357i
T =
1.4956 + 0.8535i 0.5385 + 0.0000i 0.8527 - 1.0580i 0.9668 - 0.8355i -0.4607 + 0.3907i
0.0000 + 0.0000i 1.4956 - 0.8535i -0.7757 + 1.1632i 1.2424 - 0.1948i -1.0957 - 0.3520i
0.0000 + 0.0000i 0.0000 + 0.0000i 0.8000 + 0.0000i 0.2651 - 0.3937i -0.1464 + 0.7130i
0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.5044 + 0.2878i -0.6670 + 0.0000i
0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.5044 - 0.2878i
reorder eigenvalues in increasing order along the principal diagonal
Z =
0.0000 + 0.0000i -0.0000 - 0.0000i 0.4092 + 0.2263i 0.2994 - 0.2642i 0.1468 - 0.7748i
0.1237 - 0.6952i 0.4512 - 0.2721i 0.2129 + 0.1177i 0.0988 + 0.3509i -0.1754 + 0.0321i
0.4916 + 0.0874i -0.2543 - 0.6279i -0.3849 - 0.2129i 0.1566 + 0.1010i -0.0454 - 0.2480i
-0.1377 - 0.3862i 0.3311 + 0.1184i -0.5507 - 0.3046i -0.0533 - 0.4638i 0.2349 - 0.1977i
0.2731 - 0.0974i -0.0364 - 0.3721i 0.3187 + 0.1763i -0.3534 - 0.5745i 0.2796 + 0.3322i
T =
0.5044 - 0.2878i -0.1691 + 0.0663i 0.7809 - 0.5847i 0.4891 - 0.5818i 0.0876 + 1.3505i
0.0000 + 0.0000i 0.5044 + 0.2878i -0.8802 - 0.0511i 0.4373 - 1.3339i -0.8048 - 0.1024i
0.0000 + 0.0000i 0.0000 + 0.0000i 0.8000 + 0.0000i 0.4870 + 0.8044i -0.1559 + 0.7071i
0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 1.4956 - 0.8535i -1.2778 - 0.5945i
0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 1.4956 + 0.8535i
check Blanchard-Kahn
abs_diag_T =
0.5807 0.5807 0.8000 1.7220 1.7220
T_11 =
0.5044 - 0.2878i -0.1691 + 0.0663i 0.7809 - 0.5847i
0.0000 + 0.0000i 0.5044 + 0.2878i -0.8802 - 0.0511i
0.0000 + 0.0000i 0.0000 + 0.0000i 0.8000 + 0.0000i
Z_11 =
0.0000 + 0.0000i -0.0000 - 0.0000i 0.4092 + 0.2263i
0.1237 - 0.6952i 0.4512 - 0.2721i 0.2129 + 0.1177i
0.4916 + 0.0874i -0.2543 - 0.6279i -0.3849 - 0.2129i
Z_21 =
-0.1377 - 0.3862i 0.3311 + 0.1184i -0.5507 - 0.3046i
0.2731 - 0.0974i -0.0364 - 0.3721i 0.3187 + 0.1763i
>>

7 comentarios

Farah Shahpoor
Farah Shahpoor el 26 de Jun. de 2019
Thx I did it the way you said but still I have the error message
Not enough input arguments.
Error in Loss_fun (line 3)
vol_pi = (mean(data.v_solution(2,1:T).^2));
Does it work if you run both codes?
Farah Shahpoor
Farah Shahpoor el 26 de Jun. de 2019
Loss_fun.PNG
KALYAN ACHARJYA
KALYAN ACHARJYA el 26 de Jun. de 2019
Editada: KALYAN ACHARJYA el 26 de Jun. de 2019
Initially, I preassumed that Loss_Fun has been called in main script.
But there is no role of Loss_Fun in the main script
Check carefully. Just run the main script, it is executing
KALYAN ACHARJYA
KALYAN ACHARJYA el 26 de Jun. de 2019
Editada: KALYAN ACHARJYA el 26 de Jun. de 2019
Does it work if you run both codes?
No, for function call you need to run the main code only (Main Script), not the function file
Requested you read about custom function call here or here
Farah Shahpoor
Farah Shahpoor el 26 de Jun. de 2019
I got ur point. I dont need to run my Loss_fun.m code seperately as I did before. How can I include Loss_fun in in the main script.
I tried to add:
%Loss_fun
Loss_fun.m
to the main script.
But I get the error message:
Undefined variable "Loss_fun" or class "Loss_fun.m".
Error in COMMITMENT (line 94)
Loss_fun.m
And I saved the main script and the Loss_fun file in the same path.
Guillaume
Guillaume el 26 de Jun. de 2019
I'm not sure how many times I can explain that like any function, a function that you write must be called with its required inputs. A function Loss_fun with 3 input arguments cannot be called with just Loss_fun the same way that the sin function cannot be called with just sin.
Farah Shahpoor
Farah Shahpoor el 26 de Jun. de 2019
Yes, okay. But when I define
vol_pi = (mean(data.v_solution(2,1:T).^2));
vol_x = (mean(data.v_solution(1,1:T).^2));
isnt it the input argument? I was thinking I already included it.
the v_solution is a vector that contains the two variables x and pi. And my loss function just need the two weighted meams of it.
In your example it was sin(1:5)
and I thought
function [ LossVal, vol_pi, vol_x ] is the same

Iniciar sesión para comentar.

Categorías

Más información sobre Linear Algebra en Centro de ayuda y File Exchange.

Preguntada:

el 28 de Mayo de 2019

Comentada:

el 16 de Jul. de 2019

Community Treasure Hunt

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

Start Hunting!

Translated by