Reputation: 119
How do i simulate a binomial distribution with values for investment with two stocks acme and widget? Number of trials is 1000 invest in each stock for 5 years
This is my code. What am I doing wrong?
nyears = 5;
ntrials = 1000;
startamount = 100;
yrdeposit = 50;
acme = zeros(nyears, 1);
widget = zeros(nyears,1);
v5 = zeros(ntrials*5, 1);
v5 = zeros(ntrials*5, 1);
%market change between -5 to 1%
marketchangeacme = (-5+(1+5)*rand(nyears,1));
marketchangewidget = (-3+(3+3)*rand(nyears,1));
acme(1) = startamount;
widget(1) = startamount;
for m=1:numTrials
for n=1:nyears
acme(n) = acme(n-1) + (yrdeposit * (marketchangeacme(n)));
widget(n) = acme(n-1) + (yrdeposit * (marketchangewidget(n)));
vacme5(i) = acme(j);
vwidget5(i) = widget(j);
end
theMean(m) = mean(1:n*nyears);
p = 0.5 % prob neg return
acmedrop = (marketchangeacme < p)
widgetdrop = (marketchangewidget <p)
end
plot(mean)
Upvotes: 0
Views: 151
Reputation: 10762
Exactly what you are trying to calculate is not clear. However some things that are obviously wrong with the code are:
widget(n)
presumable isn't a function of acme(n-1)
but rather 'widget(n-1)`theMean
will be mean(1:nyears*nyears)
, which for nyears=5
will be 13
. (This is because n=nyears
always at that point in code.)acmedrop = (marketchangeacme < 0);
not < 0.5
(nor any other probability). Similarly for widgetdrop.vacme5
nor vwidget5
(but you do preallocate v5
twice, and then never use it.mean
(and you never should) so plot(mean)
will not work.Upvotes: 1