Random Variable Arithmetic

Jackson Kwok

2017-12-30

Remarkably, this package is only ~150 lines long, and it can do arithmetic of random variables, build regression and hierarchical models, and perform statistical inference.

library(rvArithmetic)
library(magrittr)

All of the following can be run in a REPL with repl(). (repl() also accepts R commands, but not multi-line ones; type exit() to leave the REPL session.)

Simple arithmetic

run_script("let x be normal(0,1); let y be poisson(5); find x + y")


run_script(
  "let x be normal(0,1);
  let y be poisson(5);
  let z be normal(-10, 1);
  find sin(x - y) * z"
)

Hierarchical models

run_script(
  "let theta be gamma(3, 2);
  let y be poisson(3 + 2 * theta);
  find y"
)


run_script(
  "let p be uniform(0.6, 0.8);
  let y be binomial(1, p);
  find y"
)

Inference

run_script(
  "let data0 be binomial(1, 0.7);
  let coin be model(p) {
    binomial(1, p);
  };
  infer coin from data0;"
)
#> $param
#> [1] 0.7101552
#> 
#> $distance_from_data
#> [1] 7.531564e-07
#> 
#> $iter
#> [1] 130
run_script(
  "let data1 be gamma(5, 2);
  let m0 be model(alpha, beta) {
    gamma(alpha, beta);
  };
  infer m0 from data1;"
)
#> $param
#> [1] 4.857849 1.913026
#> 
#> $distance_from_data
#> [1] 0.0001129845
#> 
#> $iter
#> [1] 1000