Effacer les filtres
Effacer les filtres

Loop Computation with i=1:N

22 vues (au cours des 30 derniers jours)
Lorenzo Pinna
Lorenzo Pinna le 2 Fév 2021
Commenté : Lorenzo Pinna le 2 Fév 2021
N=20000; %
sigma=6; %CES
w=1;
T=1;
rho=1/sigma;
f=0.75;
delta=0.02;
M=100000;
V=1;
%%
tic;
for i=1:N;
%Productivity draw
sd=rng;
phi(i)=normrnd(1,0.15); %G-distribution Draw
rng=sd;
Prod.av=sum(phi,2)./N; %Average Productivity
p_i(i)=((w)./(rho.*phi(i))); %Pricing rule
phi_tilda=(sum(phi(i)./Prod.av)).^sigma./((sum((phi(i)./Prod.av).^sigma))./(phi(i)));%Aggregate productivity = weighted harmonic mean
P=(N^(1/(1-sigma))).*(w/(rho*phi_tilda)); %D-S Price index
Q=(M.*V)./P;
q_i(i)=Q.*((p_i(i)./P).^(-sigma));
R=P.*Q;
r(i)=R.*((P.*rho.*phi(i)).^(sigma-1));
PI=N.*((R./sigma).'.*(P.'.*rho.*phi_tilda).^(sigma-1))-f;
pi(i)=r(i)./sigma-f;
end
Hello there!
I am inside of this loop. All seems working excepts from r(i) computation onwards. Albeit q_i(i) seems to be computed properly, r(i) presents a similar computation but, at the end of the day, the vector is computed as if i is equal to the 20000th observation ignoring all the others 19999. I do not know why matlab does not consider that the index i is composed by 20000 obs..
Indeed, if I compute r(1:N)=R.*((P.*rho.*phi(1:N)).^(sigma-1)); seems to work and it is what I want. How can I solve this? I.e: Obtain the same result of r(1:N) for r(i) as I did in q_i(i) and p_i(i).
Thanks, here below the script:
  2 commentaires
Daniel Pollard
Daniel Pollard le 2 Fév 2021
Please format your code correctly -
using Alt+enter -
with the appropriate indenting (ctrl+i is useful for that) to make it easier to read, and therefor easier to help you.
Lorenzo Pinna
Lorenzo Pinna le 2 Fév 2021
Modifié(e) : Lorenzo Pinna le 2 Fév 2021
Done! Thanks for the hint.

Connectez-vous pour commenter.

Réponses (1)

Steven Lord
Steven Lord le 2 Fév 2021
What do you expect this code segment to do?
sd=rng;
phi(i)=normrnd(1,0.15); %G-distribution Draw
rng=sd;
If you expect this to call the rng function to store the random number state, generate some numbers, then reset the random number state it will not. The third line tells MATLAB that rng is a variable and so MATLAB cannot call the rng function in your code.
To do what you expect instead call rng on the first line rather than assigning to rng.
sd=rng;
phi(i)=normrnd(1,0.15); %G-distribution Draw
rng(sd);
But note if you do this, all the elements of phi will be the same.
In general, if you're resetting the random number generator inside a for loop you're probably doing something you shouldn't. It would be like a casino shuffling a deck of cards back to the same state each time a game starts -- the gamblers would probably pick up on that pretty quick. Initializing the generator before the loop and resetting it after the loop may be more appropriate.
  1 commentaire
Lorenzo Pinna
Lorenzo Pinna le 2 Fév 2021
Ok, thanks!
I changed this setting. However, the vector r(i) does not change. Everything seems to remain unchanged. Or better, have you find a solution to give r(i) exact as r(1:N)?
Thank you for the availability Steven

Connectez-vous pour commenter.

Catégories

En savoir plus sur Operating on Diagonal Matrices dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by