fortune 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/README.md +152 -2
  2. data/lib/fortune/version.rb +1 -1
  3. metadata +25 -11
  4. checksums.yaml +0 -7
data/README.md CHANGED
@@ -1,6 +1,16 @@
1
1
  # Fortune
2
2
 
3
- TODO: Write a gem description
3
+ This gem allows you calculate the various statistical parameters and values.
4
+
5
+ Main features of the library are:
6
+ * `P` - Probabilities
7
+ * `Odds` - Calculation of odds
8
+ * `A`, `Pn`, `C` - Combinatorics (accommodation, permutation, combinations)
9
+ * `Event` - Random selection of events from the list
10
+
11
+ ## Requirements
12
+
13
+ ruby 1.9 or higher
4
14
 
5
15
  ## Installation
6
16
 
@@ -18,7 +28,143 @@ Or install it yourself as:
18
28
 
19
29
  ## Usage
20
30
 
21
- TODO: Write usage instructions here
31
+ #### For Hash
32
+
33
+ Recalculation of the key values to probability, assuming that P = 1
34
+ ```ruby
35
+ {:a => 1, :b => 2, :c => 3}.to_p
36
+ # => {:a=>0.16666666666666666, :b=>0.3333333333333333, :c=>0.5}
37
+ ```
38
+ Select random key by value weights
39
+ ```ruby
40
+ {:a => 1, :b => 2, :c => 3}.choose
41
+ # => return :c with 50% probability, :a ~ 16%, :b ~ 33%
42
+ ```
43
+ Choose from set by value weights
44
+ ```ruby
45
+ {[:a, :b, :c] => 1, [:d] => 3}.choose_set
46
+ # => return :d with 75% probability
47
+ ```
48
+
49
+ #### Probability
50
+
51
+ Some methods for P
52
+ ```ruby
53
+ p = Fortune::P.new(:m => 1, :n => 10)
54
+ p = Fortune::P.new(1, 10)
55
+ p = Fortune::P.new(0.1)
56
+ # => #<Fortune::P:0x00000002316df0 @p=0.1>
57
+
58
+ p + p # => #<Fortune::P:0x0000000225e890 @p=0.2>
59
+ p * p # => #<Fortune::P:0x000000022602a8 @p=0.010000000000000002>
60
+
61
+ p.to_percent # => 10.0
62
+
63
+ p.odds # => #<Fortune::Odds:0x0000000230ff50 @p=0.1, @s=1, @k=9.0, @type=:on_win>
64
+ p.odds.to_s # => "1:9.0 on_win (p: 10.00%)"
65
+ ```
66
+
67
+ Check event is occured
68
+ ```ruby
69
+ Fortune::P.is(1,10) # => false (90%)
70
+ ```
71
+ Select random element
72
+ ```ruby
73
+ Fortune::P.n(3) # => 1 or 2 or 3
74
+ ```
75
+ Select random key from array or range by weights ({P.n(key) => weight, ...}, P(sum(weights)) = 1)
76
+ ```ruby
77
+ Fortune::P.n_select(10 => 1, 5 => 1000) # => 4
78
+ Fortune::P.n_select((1..10) => 1, (100..200) => 1000) # => 157
79
+ Fortune::P.n_select([:a,:b,:c] => 1, [:d,:e] => 1000) # => :d
80
+ ```
81
+
82
+ #### Odds
83
+ Class for odds calculations
84
+ ```ruby
85
+ odds = Fortune::Odds.new(:win => 5, :lose => 6)
86
+ # => #<Fortune::Odds:0x000000026ccf08 @p=0.45454545454545453, @s=5, @k=6, @type=:on_win>
87
+ odds_other = Fortune::Odds.new(:s => 5, :k => 7)
88
+ # => #<Fortune::Odds:0x00000002706168 @p=0.4166666666666667, @s=5, @k=7, @type=:on_win>
89
+ odds > odds_other # => true
90
+ odds.variants # => 11
91
+ odds.p # => 0.45454545454545453
92
+ ```
93
+ Large values can be simple converted to human view
94
+ ```ruby
95
+ Fortune::Odds.new(:win => 1345, :lose => 3623).to_s # => "1345:3623 on_win (p: 27.07%)"
96
+ Fortune::Odds.new(:win => 1345, :lose => 3623).to_human.to_s # => "2:5 on_win (p: 28.57%)"
97
+ Fortune::Odds.new(:win => 1345, :lose => 3623).to_human(:k => 5, :fractions => true).to_s # => "1.5:4 on_win (p: 27.27%)"
98
+ ```
99
+ Reverse odds to lose
100
+ ```ruby
101
+ odds = Fortune::Odds.new(:win => 5, :lose => 6)
102
+ # => #<Fortune::Odds:0x00000002822e98 @p=0.45454545454545453, @s=5, @k=6, @type=:on_win>
103
+ odds.revert
104
+ # => #<Fortune::Odds:0x00000002822e98 @p=0.5454545454545454, @s=6, @k=5, @type=:on_win>
105
+ odds.to_lose
106
+ # => #<Fortune::Odds:0x00000002822e98 @p=0.5454545454545454, @s=5, @k=6, @type=:on_lose>
107
+ ```
108
+
109
+ ### Combinatorics
110
+
111
+ #### Accomodation (A)
112
+
113
+ Permutation without repetition with select k elements.
114
+
115
+ Example: 20 different elements and you need select 5 of them (how many ways of selection exists?). Elements is ordered ([a,b,c] != [b,c,a])
116
+ ```ruby
117
+ Fortune::A.calc(:elements => 10, :select => 5) # => 30240
118
+ ```
119
+
120
+ #### Permutation
121
+
122
+ ##### Permutation without repetition (Pn)
123
+
124
+ Example: the amount of distributions of the four teams in four places
125
+
126
+ ```ruby
127
+ Fortune::Pn.calc(4) # => 24
128
+ ```
129
+ other examples
130
+ ```ruby
131
+ Fortune::Pn.calc(:elements => 5) # => 120
132
+ Fortune::Pn.calc(:elements => 5, :select => 2) # => 20
133
+ ```
134
+
135
+ ##### Permutation with repetition (Pnr)
136
+
137
+ Example: how many different options you can dress up if there are three sweaters two skirts and two hats (3*2*2, if all thing is equal 3, then: Pn_repetition(:n => 3, :k => 3), k - element groups, n - elements count in group)
138
+
139
+ ```ruby
140
+ Fortune::Pn_repetition(:n => 3, :k => 3) # => 27
141
+ is equal to
142
+ Fortune::Pnr(:groups => 3, :elements => 3) # => 27 (human like variant)
143
+ ```
144
+
145
+ #### Combinations
146
+
147
+ ##### Combinations without repetition (C)
148
+
149
+ Example: 10 different elements (students) and you need select 5 of them (how many ways of selection exists?). Elements does not ordered ([a,b,c] == [b,c,a])
150
+
151
+ ```ruby
152
+ Fortune::C.calc(:elements => 10, :select => 5) # => 252
153
+ ```
154
+
155
+ ##### Combination with repetition (Cr)
156
+
157
+ ```ruby
158
+ Fortune::C_repetition.calc(:elements => 10, :select => 5) # => 2002
159
+ same as
160
+ Fortune::Cr.calc(:elements => 10, :select => 5) # => 2002
161
+ ```
162
+
163
+ #### Addon Class Methods
164
+
165
+ `Math.factorial(n)`
166
+
167
+ `Event` class - under development
22
168
 
23
169
  ## Contributing
24
170
 
@@ -27,3 +173,7 @@ TODO: Write usage instructions here
27
173
  3. Commit your changes (`git commit -am 'Add some feature'`)
28
174
  4. Push to the branch (`git push origin my-new-feature`)
29
175
  5. Create new Pull Request
176
+
177
+ ## Authors
178
+
179
+ Personal blog author: [Malykh Oleg](http://es0.ru/) - blog in russian
@@ -1,3 +1,3 @@
1
1
  module Fortune
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
metadata CHANGED
@@ -1,18 +1,20 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fortune
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
+ prerelease:
5
6
  platform: ruby
6
7
  authors:
7
8
  - Malykh Oleg
8
9
  autorequire:
9
10
  bindir: bin
10
11
  cert_chain: []
11
- date: 2013-12-13 00:00:00.000000000 Z
12
+ date: 2013-12-16 00:00:00.000000000 Z
12
13
  dependencies:
13
14
  - !ruby/object:Gem::Dependency
14
15
  name: bundler
15
16
  requirement: !ruby/object:Gem::Requirement
17
+ none: false
16
18
  requirements:
17
19
  - - ~>
18
20
  - !ruby/object:Gem::Version
@@ -20,6 +22,7 @@ dependencies:
20
22
  type: :development
21
23
  prerelease: false
22
24
  version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
23
26
  requirements:
24
27
  - - ~>
25
28
  - !ruby/object:Gem::Version
@@ -27,29 +30,33 @@ dependencies:
27
30
  - !ruby/object:Gem::Dependency
28
31
  name: rake
29
32
  requirement: !ruby/object:Gem::Requirement
33
+ none: false
30
34
  requirements:
31
- - - '>='
35
+ - - ! '>='
32
36
  - !ruby/object:Gem::Version
33
37
  version: '0'
34
38
  type: :development
35
39
  prerelease: false
36
40
  version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
37
42
  requirements:
38
- - - '>='
43
+ - - ! '>='
39
44
  - !ruby/object:Gem::Version
40
45
  version: '0'
41
46
  - !ruby/object:Gem::Dependency
42
47
  name: rspec
43
48
  requirement: !ruby/object:Gem::Requirement
49
+ none: false
44
50
  requirements:
45
- - - '>='
51
+ - - ! '>='
46
52
  - !ruby/object:Gem::Version
47
53
  version: '0'
48
54
  type: :development
49
55
  prerelease: false
50
56
  version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
51
58
  requirements:
52
- - - '>='
59
+ - - ! '>='
53
60
  - !ruby/object:Gem::Version
54
61
  version: '0'
55
62
  description: Ruby gem for calculate probability, odds and combinations. Mostly helpfull
@@ -95,26 +102,33 @@ files:
95
102
  homepage: https://github.com/Fotom/Fortune
96
103
  licenses:
97
104
  - MIT
98
- metadata: {}
99
105
  post_install_message:
100
106
  rdoc_options: []
101
107
  require_paths:
102
108
  - lib
103
109
  required_ruby_version: !ruby/object:Gem::Requirement
110
+ none: false
104
111
  requirements:
105
- - - '>='
112
+ - - ! '>='
106
113
  - !ruby/object:Gem::Version
107
114
  version: '0'
115
+ segments:
116
+ - 0
117
+ hash: 3317073877623941602
108
118
  required_rubygems_version: !ruby/object:Gem::Requirement
119
+ none: false
109
120
  requirements:
110
- - - '>='
121
+ - - ! '>='
111
122
  - !ruby/object:Gem::Version
112
123
  version: '0'
124
+ segments:
125
+ - 0
126
+ hash: 3317073877623941602
113
127
  requirements: []
114
128
  rubyforge_project:
115
- rubygems_version: 2.1.11
129
+ rubygems_version: 1.8.25
116
130
  signing_key:
117
- specification_version: 4
131
+ specification_version: 3
118
132
  summary: Lib for get changes, odds, probability and combinations for given values
119
133
  test_files:
120
134
  - spec/fortune/a_spec.rb
checksums.yaml DELETED
@@ -1,7 +0,0 @@
1
- ---
2
- SHA1:
3
- metadata.gz: 9fc004785ef01cd70f23583a76c0d36cfd7d3607
4
- data.tar.gz: eb71d50d976e4d0a78f582e2db63dc5a6947f9c3
5
- SHA512:
6
- metadata.gz: df3008f56c4471c58cad733994baec963df15e064c80c8ddc0a46700417a571031c874e42e1220e4d75e1f396a962ac50cd75283d1cb30b20c3e990a9a0f4e32
7
- data.tar.gz: 19695ff2b954f96f74ef008954690d18088d1116d13811db7dae5b54ffc634bc4fa3c05ffff2c515e9eb15c67fb082291db4d2a05185bc0e6663f819be84d10c