开发者

Simulating a binomial distribution

I want to simulate a binomial distribution where the price of something can only go up or down with the probability of p that is 50% this time.

My variables:

S=100 (the basic value)

u=1,1 (how much the value goes up in each experiment if it goes up)

d=1/u (how much the value goes down in each experiment if it goes down)

p=0.5 (probability)

n=400 (number of experiments)

I did not declare these variables, because I want to read these values from specific cells.

My code (for the first step):

Sub BINOM()

    S = Range("L4").Value
    u = Range("M4").Value
    d = Ra开发者_Go百科nge("N4").Value
    p = Range("O4").Value
    n = Range("P4").Value
    v = Rnd()

    If v > p Then
        Range("B2").Value = S * u
    Else
        Range("B2").Value = S * d
    End If

End Sub

The result of the second experiment (that should be written in the B3 cell) has to be calculated from the result of the first experiment and so on but not with using the same random number.


I'll try my best but I just removed Excel in favor of Calc which doesn't support the same type of language as far as I can tell.

Sub BINOM()
  Dim intCounter, v

  S = Range("L4").Value
  u = Range("M4").Value
  d = Range("N4").Value
  p = Range("O4").Value
  n = Range("P4").Value

  Range("B1").Value = s    

  For intCounter = 2 to n

    '//If this creates an error then just remove it.
    '//It should keep the same random number from appearing over and over.
    Randomize

    '//Create new Random number in v
    v = Rnd()

    If v > p Then
      Range("B" & intCounter).Value = Range("B" & (intCounter - 1)).Value * u
    Else
      Range("B" & intCounter).Value = Range("B" & (intCounter - 1)).Value * d
    End If

  Next intCounter
End Sub

Let me know if that works out for you or if any errors appear. Updated to base each cell from data given of previous cell.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜