Operations on Random Variables
Apart from just creating wrapper code around sampling procedures which represent random variables, it is also important to do transformations on random variables to yield new more interesting random variables and distributions. In statistics one often formulates certain random variables as algebraic operations on other simpler random variables.
It is possible to do common algebraic operations on instances of continuous random variables.
1 2 3 4 5 6 7 8 9 10 11
import spire.implicits._ val b = RandomVariable(new Beta(7.5, 7.5)) val g = RandomVariable(new Gamma(1.5, 1.2)) val n = GaussianRV(0.0, 1.0) val addR = b + n - g val multR = b * (n + g) histogram((1 to 1000).map(_ => multR.sample()))
In many cases random variables can be expressed as functions of one another, for example chi square random variables are obtained by squaring normally distributed samples.
1 2 3 4
val chsq = MeasurableFunction(n, DataPipe((x: Double) => x*x)) //Generate a chi square distribution with one degree of freedom histogram((1 to 1000).map(_ => chsq.sample()))