James Rodriguez
James Rodriguez

Reputation: 119

Matlab: binomial simulation

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

Answers (1)

Phil Goddard
Phil Goddard

Reputation: 10762

Exactly what you are trying to calculate is not clear. However some things that are obviously wrong with the code are:

  1. widget(n) presumable isn't a function of acme(n-1) but rather 'widget(n-1)`
  2. Every entry of 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.)
  3. The probability of a negative return for acme is 5/6, not 0.5.
  4. To find the locations of the negative returns you want acmedrop = (marketchangeacme < 0); not < 0.5 (nor any other probability). Similarly for widgetdrop.
  5. You are not preallocating vacme5 nor vwidget5 (but you do preallocate v5 twice, and then never use it.
  6. You don't create a variable called mean (and you never should) so plot(mean) will not work.

Upvotes: 1

Related Questions