User Tools

Site Tools


distributions:start

Distributions

Reverse parameter calculation

In the reference manuals, the mean (μ) and variance (σ^2) of a distribution are given in terms of parameters of the distribution. Nornmally, you want it the other way around. Given a wanted mean and variance, you need values for the parameters. In the table below, distribution parameter values are specified in terms of mean (m) and variance (v).

If the distribution that you need is not listed, you'll have to compute it yourself. However, please add the result in the table, so others can use your efforts.

Distribution
gamma(m*m/v, v/m)

Level 2 Headline

Another frequently occurring problem is verification of a distribution. The Chi (0.8) code below should help here:

from random import gamma
from std import hd,tl, sort, len
 
func sfunc(a,b:real) -> bool = |[ ret a < b ]|
 
// Compute mean value from a list of reals
func calc_mean(xs:real*) -> real =
|[ t: real, ys:real*
 | ys := sort(xs, sfunc)  // sorting first reduces numerical errors
 ; *[ len(ys)>0 -> t := t + hd(ys); ys := tl(ys) ]
 ; ret t / len(xs)
]|
 
// Compute variance from a list of reals and a mean value
func calc_variance(xs:real*, mean:real) -> real =
|[ ys:real*, d: real, count:nat
 | ys := []; count := len(xs)
 ; *[len(xs) > 0 -> d := hd(xs) - mean; ys := ys ++ [d*d]; xs := tl(xs) ]
 ; ys := sort(ys, sfunc)  // sorting reduces numerical errors
 ; d := 0.0
 ; *[ len(ys)>0 -> d := d + hd(ys); ys := tl(ys) ]
 ; ret d / (count - 1)
]|
 
proc P(mean, variance:real) =
|[ n:nat, a,b, m,v: real, xs:real*, d: -> real
 | a := mean * mean / variance; b := variance / mean    // compute distribution parameters
 ; d := gamma(a,b)                                      // construct distribution
 ; xs := []
 ; n :=0 ; *[ n < 1000 -> xs := xs ++ [sample d] ; n := n + 1 ]
 ; m := calc_mean(xs)
 ; ! "Mean=", mean, ", mymean=", m, "\n"
 ; v := calc_variance(xs, m)
 ; ! "Variance=", variance, ", myvariance=", v, "\n"
]|
 
xper = |[ P(96.0, 10.0) ]|
distributions/start.txt · Last modified: Thursday, 13 September 2007 : 09:31:31 (external edit)