ranvar
ranvar, contextual keyword
The ranvar type represents a probability distribution over integers.
r = poisson(2)
show summary "Ranvar" with
mean(r) as "Mean"
dispersion(r) as "Dispersion"
This outputs the following summary:
| Mean | Dispersion |
|---|---|
| 1.999924 | 0.9996943 |
ranvar, function
def nosort process ranvar(a: number): ranvar
def nosort process ranvar(a: number, w: number): ranvar
Builds an empirical ranvar from observations a. The weighted overload uses
non-negative weights w, normalized within each group.
a: the observations (rounded to integers).w: the weights (non-negative; if their sum is zero, a warning is emitted).
Examples
table T = with
[| as A, as G |]
[| 1, "a" |]
[| 2, "a" |]
[| 1, "b" |]
[| 3, "b" |]
table G[gdim] = by T.G
show table "Empirical means" with
gdim
mean(ranvar(T.A)) as "Mean"
group by gdim
This outputs the following table:
| gdim | Mean |
|---|---|
| a | 1.5 |
| b | 2 |
table T = with
[| as A, as W, as G |]
[| 1, 1.0, "a" |]
[| 2, 2.0, "a" |]
[| 1, 0.5, "b" |]
[| 3, 1.5, "b" |]
table G[gdim] = by T.G
show table "Weighted means" with
gdim
mean(ranvar(T.A, T.W)) as "Mean"
group by gdim
This outputs the following table:
| gdim | Mean |
|---|---|
| a | 1.666667 |
| b | 2.5 |
Remarks
The aggregator returns dirac(0) on empty groups and when weights sum to zero.
ranvar, montecarlo accumulator
The accumulator returns the empirical distribution of the sampled values. The target may be a scalar or a vector. In the vector case, one empirical ranvar is accumulated per line.
r = poisson(3)
montecarlo 1000 with
dev = random.ranvar(r)
sample r2 = ranvar(dev)
show summary "Empirical ranvar" with
mean(r2) as "Mean"
dispersion(r2) as "Dispersion"
This outputs the following summary:
| Mean | Dispersion |
|---|---|
| 2.928 | 0.9975465 |
table T = extend.range(3)
montecarlo 100 with
sample T.R = ranvar(random.normal(T.N, 0.1))
show table "Vector empirical ranvars" with
T.N
mean(T.R) as "Mean"
dispersion(T.R) as "Dispersion"