# ranvar

## ranvar, contextual keyword

The word ranvar is a contextual keyword of the Envision language. It refers to the primitive data type ranvar that represents a probability distribution over $\mathbb{Z}$.

The primary usage of the keyword ranvar is within read statements, but it can also be used within an expect statement.

Example:

r = poisson(2)
expect r : ranvar
show table "" export: "/sample/oneranvar.ion" with r


followed by

read "/sample/oneranvar.ion" as T with
r : ranvar

show scalar "" a1b2 with same(T.r)


## ranvar(T.a : number) 🡒 ranvar, aggregator

Returns the empiric ranvar from the given observations. Let’s $y_i$ be the $N$ observations, the empiric probability distribution is defined by:

$$P[X = x] = \frac{1}{N} | \{ y_i | y_i = x \} |$$

Example:

table T = with
[| as A, as B |]
[| 1,   "a"   |]
[| 2,   "a"   |]
[| 1,   "b"   |]
[| 2,   "b"   |]
[| 3,   "c"   |]

table G[gdim] = by T.B

where T.B != "c"
show table "" a1b4 with
gdim
ranvar(T.A)
group by gdim


Input numbers are rounded to the nearest integer.

The aggregator returns dirac(0) on empty groups.

## ranvar(T.a : number, T.w : number) 🡒 ranvar, aggregator

Returns the empiric ranvar from the given observations. Let’s $y_i$ be the $N$ observations of respective weights $w_i$, the empiric probability distribution is defined by:

$$P[X = x] = \frac{1}{\sum_{i=1}^N w_i} \sum_{ i | y_i = x} w_i$$

Example:

table T = with
[| as A, as B, as W |]
[| 1,   "a", 1.0 |]
[| 2,   "a", 1.0 |]
[| 1,   "b", 0.5 |]
[| 2,   "b", 2.5 |]
[| 3,   "c", 1.0 |]

table G[gdim] = by T.B

where T.B != "c"
show table "" a1b4 with
gdim
ranvar(T.A, T.W)
group by gdim


The aggregator returns dirac(0) on empty groups.