home *** CD-ROM | disk | FTP | other *** search
/ Really Useful CD 1 / ReallyUsefulCD1.iso / extras / languages / smalltalk / _smalltalk / tests / prob < prev    next >
Encoding:
Text File  |  1987-12-30  |  1.4 KB  |  69 lines

  1. Class DiscreteProbability
  2.      | randnum |
  3. [
  4.      initialize
  5.           randnum <- Random new
  6.  
  7. |    next
  8.           ^ self inverseDistribution: randnum next
  9.  
  10. |    computeSample: m outOf: n     
  11.           m > n ifTrue: [^ 0.0]
  12.           ^ n factorial / (n - m) factorial
  13. ]
  14.  
  15. Class Geometric     :DiscreteProbability
  16.      | prob |  
  17.  
  18. [
  19.      mean: m
  20.           prob <- m
  21.  
  22. |    mean
  23.           ^ 1.0 / prob
  24.  
  25. |    variance
  26.           ^ (1.0 - prob) / prob * prob
  27.  
  28. |    density: x
  29.           x > 0 ifTrue: [^prob * ((1.0-prob) raisedTo: x-1)]
  30.                 ifFalse: [^1.0]
  31.  
  32. |    inverseDistribution: x
  33.           ^ (x ln / (1.0 - prob) ln) ceiling
  34. ]
  35.  
  36. Class Binomial :DiscreteProbability
  37.      | number prob |
  38. [
  39.      events: num mean: p
  40.           (p between: 0.0 and: 1.0)
  41.              ifFalse: [self error: 'mean must be > 0'].
  42.           number <- num.
  43.           prob <- p
  44.  
  45. |    mean
  46.           ^ prob
  47.  
  48. |    variance
  49.           ^ prob * (1 - prob)
  50.  
  51. |    density: x
  52.           (x between: 0.0 and number)
  53.              ifTrue: [^((self computeSample: x outOf: number)
  54.                / (self computeSample: x outOf: x))
  55.                * (prob raisedTo: x) * ((1 - prob) raisedTo: number - x)]
  56.              ifFalse: [^0.0]
  57.  
  58. |    inverseDistribution: x
  59.           x <= prob
  60.                ifTrue: [^ 1]
  61.                ifFalse: [^ 0]
  62.  
  63. |    next
  64.      | t |
  65.           t <- 0.
  66.           number timesRepeat: [t <- t + super next].
  67.           ^ t
  68. ]
  69.