rubythinking 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,29 @@
1
+ class Rubythinking::Distributions::Binomial
2
+ class << self
3
+ def samples(n, size:, success:)
4
+ dist = Croupier::Distributions::Binomial.new(size: size, success: success)
5
+ samples = dist.to_enum.take(n)
6
+ end
7
+
8
+ def density(value:, size:, success:)
9
+ # Use variable names often used in math
10
+ # textbooks and wikipedia to have code
11
+ # close to the formula
12
+ k = value
13
+ n = size
14
+ p = success
15
+
16
+ (factorial(n).to_f / (factorial(k) * factorial(n - k))).to_f * (p**k) * ((1-p)**(n-k))
17
+ end
18
+
19
+ def factorial(n)
20
+ return 1 if n < 1
21
+ n.to_i.downto(1).inject(:*)
22
+ end
23
+
24
+
25
+ def likelihood(w, l, p)
26
+ density(value: w, size: (w+l), success: p)
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,4 @@
1
+ module Rubythinking
2
+ module Distributions
3
+ end
4
+ end
@@ -0,0 +1,3 @@
1
+ module Rubythinking
2
+ VERSION = "0.2.0"
3
+ end
@@ -0,0 +1,17 @@
1
+ require "rubythinking/version"
2
+ require "rubythinking/distributions"
3
+ require "rubythinking/distributions/binomial"
4
+ require "vega"
5
+ require "cmd_stan_rb"
6
+ require "croupier"
7
+
8
+ module Rubythinking
9
+ # Mimicks R API
10
+ def dbinom(value, size:, prob:)
11
+ Rubythinking::Distributions::Binomial.density(value: value, success: prob, size: size)
12
+ end
13
+
14
+ def rbinom(n, size:, prob:)
15
+ Rubythinking::Distributions::Binomial.samples(n, success: prob, size: size)
16
+ end
17
+ end
@@ -0,0 +1 @@
1
+ gem build rubythinking.gemspec && gem install rubythinking-0.1.0.gem && iruby register --force