Question about a function

2 Ansichten (letzte 30 Tage)
Tommy
Tommy am 14 Jun. 2012
I plan to solve a function matrix x(j), which want each x(j) could lead W(j) to 0. I have written a function about it, but I do not know what code I need to write to solve this function. I will be really appreciate if anyone could take some time to give me some help. Thank you very much
%%function for omega of every time period for OI scheme
function f=function_1_5_1(x)
clear
r0=.02;%interest rate
sigma=.15;%vatality rate of risky asset
mu0=.06;%drift rate of risky asset
gamma=5;%risk aversion rate
M=10000;%number of trajectories
N=55;%time period
T=55;%total time period
R=40;%time of retirement
R_l=T-R;%length of retiment
dt=T/N;%each time period
t=1:dt:T;
omega=x;
Rf=exp(r0);
for j=1:N
Rs(:,j+1)=exp(mu0+sigma*randn(M,1));
a(:,j)=.5*rand(M,1);
end
for j=1:N-1
for i=1:M
if j=1
w(:,N-j+1)=omega(N-j+1)*ones(M,1)
w(i,N-j)=(Rf+omega(N-j)*a(i,N-j+1)*(Rs(i,N-j+1)-Rf))^(-gamma)*(Rs(i,N-j+1)-Rf);
else
if j<R_l
w(i,N-j)=(a(i,N-j)*(Rf+omega(N-j)*(Rs(i,N-j+1)-Rf))-a(i,N-j+1))^(-gamma)*(Rs(i,N-j+1)-Rf);
else
w(i,N-j)=(a(i,N-j)*(Rf+omgea(N-j)*(Rs(i,N-j+1)-Rf))+1-a(i,N-j+1))^(-gamma)*(Rs(i,N-j+1)-Rf);
end
end
end
W(j)=sum(w(:,j));
end
f=W;

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 14 Jun. 2012
It would be more robust if you defined t as linspace(1,T,N) -- the exact length of 1:dt:T can vary by 1 element because of accumulated round-off error. If, that is, you had not happened to define N and T as being identical and thus implicitly defining dT as being exactly 1.
"if j=1" is invalid syntax. You need "if j==1"
Your "else" of j<R_1 references a variable named "omgea" instead of "omega"
Your f output is copied from W and W(j) is the only place W is assigned, so the size of W is the same as the maximum j, and is thus N-1 (i.e., 54)
Your input vector, x, is copied to omega. The last element of omega that you access is omega(N-j+1) for the case that j=1, so that is omega(N) (i.e., omega(55)). The first element of omega that you access is omega(N-j) when j = N-1 [or would once the omgea spelling mistake is corrected], so omega(1); that's okay, but needed to be cross-checked through the logic.
So your input vector, x, must be length 55, but your output vector is length 54. Please check that that is what you want, as it disagrees with your opening statement that each x(j) should lead W(j) to be 0, as that would imply that x(55) should lead W(55) to 0 but there is no W(55) in your calculation.
  2 Kommentare
Tommy
Tommy am 14 Jun. 2012
Thx for your reply.May I ask if the code following could work, and if it could, how I should write another program to solve this function in order to find x in each time step which cause W=0
%% function for omega of every time period for OI scheme
function f=function_1_5_1(x)
clear
r0=.02;%interest rate
sigma=.15;%vatality rate of risky asset
mu0=.06;%drift rate of risky asset
gamma=5;%risk aversion rate
M=10000;%number of trajectories
N=55;%time period
T=55;%total time period
R=40;%time of retirement
R_l=T-R;%length of retiment
dt=T/N;%each time period
t=1:dt:T;
omega=x;
Rf=exp(r0);
for j=1:N
Rs(:,j)=exp(mu0+sigma*randn(M,1));
a(:,j)=.5*rand(M,1);
end
for j=1:N-1
for i=1:M
if j=1
w(:,1)=omega(1)*ones(M,1)
w(i,2)=(Rf+omega(N-j)*a(i,2)*(Rs(i,N-j+1)-Rf))^(-gamma)*(Rs(i,N-j+1)-Rf);%function(3)
else
if j<R_l
w(i,j+1)=(a(i,N-j)*(Rf+omega(N-j)*(Rs(i,N-j+1)-Rf))-a(i,j))^(-gamma)*(Rs(i,N-j+1)-Rf);%function(4)
else
w(i,j+1)=(a(i,N-j)*(Rf+omega(N-j)*(Rs(i,N-j+1)-Rf))+1-a(i,j))^(-gamma)*(Rs(i,N-j+1)-Rf);%function(5)
end
end
end
W(j)=sum(w(:,j));
end
f=W;
Walter Roberson
Walter Roberson am 14 Jun. 2012
You still have the odd condition that your input is of length 55 but your output is of length 54.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Andrei Bobrov
Andrei Bobrov am 14 Jun. 2012
tst = find(abs(W) < eps(100));
out = x(tst);

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by