probaberry 0.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: e4594b8946190094cd9746d9c9d881ce7a188c21
4
+ data.tar.gz: 3e369243b46a846e6ce6c66d99d32ef9baf8b5ae
5
+ SHA512:
6
+ metadata.gz: 3e13ffb2ffeb2c1e25887622ee73922a47923425e7f42810147d65bb9d125089e595b8f2590f5886751b92e6324fa29823a8ccac632b398079b0144c03488c18
7
+ data.tar.gz: 38a557425a87ea96124cadef636de696fe027b5ffc7ab3050e584b9b34ded4b511297f65c71bf31e2fde577e43fd6a15c0f2bf5aef8559b591303db479d4c432
@@ -0,0 +1,30 @@
1
+ class Integer
2
+ def !
3
+ if self > 0
4
+ (1..self).reduce :*
5
+ elsif self.zero?
6
+ 1
7
+ else
8
+ raise ArgumentError, "No factorial for #{self}"
9
+ end
10
+ end
11
+
12
+ alias :"!@" :!
13
+ alias :fact :!
14
+
15
+ def choose r
16
+ raise ArgumentError, "r = #{r} is outside 0..#{self}" if r < 0 or r > self
17
+ (self.!).fdiv((self - r).!).fdiv(r.!).to_i
18
+ end
19
+
20
+ end
21
+
22
+ def ncr n, r
23
+ raise ArgumentError, "r = #{r} is outside 0..#{n}" if r < 0 or r > n
24
+ n.choose r
25
+ end
26
+
27
+ def npr n, r
28
+ raise ArgumentError, "r = #{r} is outside 0..#{n}" if r < 0 or r > n
29
+ n.!.fdiv((n - r).!).to_i
30
+ end
@@ -0,0 +1,92 @@
1
+ module Cumulative
2
+ def < x
3
+ m = (0...x).map{|i| self == i}.reduce(0, :+)
4
+ end
5
+ def <= x
6
+ (self == x ) + (self < x)
7
+ end
8
+ def > x
9
+ 1 - (self <= x)
10
+ end
11
+ def >= x
12
+ 1 - (self < x)
13
+ end
14
+ def inbetween? x, y
15
+ (self <= y) - (self < x)
16
+ end
17
+ def exbetween?
18
+ (self < y) - (self < x - 1)
19
+ end
20
+ end
21
+
22
+ class Binomial
23
+ include Cumulative
24
+ attr_accessor :n, :p
25
+ def initialize n, p
26
+ @n = n
27
+ @p = p
28
+ end
29
+ def == x
30
+ n.choose(x) * p ** x * (1 - p) ** (n - x)
31
+ end
32
+ end
33
+
34
+ class Poisson
35
+ include Cumulative
36
+ attr_accessor :la
37
+ def initialize la
38
+ @la = la
39
+ end
40
+ def == x
41
+ Math::exp(-la) * la ** x / x.!
42
+ end
43
+ end
44
+
45
+ class NegativeBinomial
46
+ include Cumulative
47
+ attr_accessor :n, :p
48
+ def initialize n, p
49
+ @n = n
50
+ @p = p
51
+ end
52
+ def == x
53
+ ncr.choose(x - 1, n - 1) * p ** n * (1 - p) ** (x - n)
54
+ end
55
+ end
56
+
57
+ class Geometric
58
+ attr_accessor :p
59
+ def initialize p
60
+ @p = p
61
+ end
62
+ def == x
63
+ p * (1 - p) ** (x - 1)
64
+ end
65
+ def > x
66
+ # tail lemma!
67
+ (1 - p) ** x
68
+ end
69
+ def >= x
70
+ (self == x) + (self > x)
71
+ end
72
+ def < x
73
+ 1 - (self >= x)
74
+ end
75
+ def <= x
76
+ 1 - (self > x)
77
+ end
78
+ end
79
+
80
+ class Hypergeometric
81
+ include Cumulative
82
+ attr_accessor :n, :s, :p
83
+ def initialize
84
+ @n = n
85
+ @s = s
86
+ @p = p
87
+ end
88
+ def == x
89
+ (ncr(n * p, x) * ncr(n * (1 - p), (s - x))).fdiv ncr(n, s)
90
+ end
91
+
92
+ end
@@ -0,0 +1,7 @@
1
+ def nice n, p=3
2
+ return n unless n.kind_of? Numeric
3
+ i = n.to_i
4
+ f = ((n - i).round(p).to_s)[2..-1]
5
+ i = i.to_s.reverse.gsub(/...(?=.)/, '\& ').reverse
6
+ "#{i}.#{f}"
7
+ end
data/lib/probaberry.rb ADDED
@@ -0,0 +1,3 @@
1
+ require 'probaberry/counting'
2
+ require 'probaberry/discrete'
3
+ require 'probaberry/nice'
metadata ADDED
@@ -0,0 +1,47 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: probaberry
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Johnny Lee Othon
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-11-03 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Do probabily homework with the console!
14
+ email: jleeothon@outlook.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/probaberry.rb
20
+ - lib/probaberry/counting.rb
21
+ - lib/probaberry/discrete.rb
22
+ - lib/probaberry/nice.rb
23
+ homepage: https://github.com/jleeothon/probaberry
24
+ licenses:
25
+ - MIT
26
+ metadata: {}
27
+ post_install_message:
28
+ rdoc_options: []
29
+ require_paths:
30
+ - lib
31
+ required_ruby_version: !ruby/object:Gem::Requirement
32
+ requirements:
33
+ - - ">="
34
+ - !ruby/object:Gem::Version
35
+ version: '0'
36
+ required_rubygems_version: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ requirements: []
42
+ rubyforge_project:
43
+ rubygems_version: 2.2.2
44
+ signing_key:
45
+ specification_version: 4
46
+ summary: Probability homework!
47
+ test_files: []