tiny_monte 1.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 +7 -0
- data/lib/tiny_monte.rb +36 -0
- metadata +43 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 1f15b124f2589421ca38c236a7d8558a536b501f8254c66b16ad76c2dff114d9
|
4
|
+
data.tar.gz: dd593fc0df44e0e4eb0acecf0770f4e5045337788a45a45af6a18f65ea51d407
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: f0cebd496e957049c19f21f60fe6c91ba23923a6b8861dee401241709e58735f14ab74c8c5880b95bd0f3b265fa40819e8a4b3ff7b5287f5c0c23e1b891ab3da
|
7
|
+
data.tar.gz: d08c6ef6814568585474d8aa1e8f1fafd087d75ab873d2dad10d3fa051c86dd73a75f9a000f8bbeb27c7c79b78002b7edd3c9f3821e1996a96bd5e3050e84005
|
data/lib/tiny_monte.rb
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
# a Monte Carlo simulator
|
2
|
+
module TinyMonte
|
3
|
+
class << self
|
4
|
+
# returns the result set from running the specified number of Monte Carlo
|
5
|
+
# simulations with the specified probability.
|
6
|
+
#
|
7
|
+
# trials: the number of trials to perform
|
8
|
+
# samples_per_trial: the number of samples to generate per trial
|
9
|
+
# probability: the probability of success in each trial
|
10
|
+
#
|
11
|
+
# ex:
|
12
|
+
# TinyMonte.monte(1000, 0.5) # => [51, 48, 52, ...]
|
13
|
+
def monte(trials, samples_per_trial, probability)
|
14
|
+
(1..trials).map{|i| _trial(samples_per_trial, probability).count(1) }
|
15
|
+
end
|
16
|
+
|
17
|
+
# returns the median result from the output of the ::monte method.
|
18
|
+
#
|
19
|
+
# ex:
|
20
|
+
# TinyMonte.median_result(TinyMonte.monte(1000, 0.5)) # => 50
|
21
|
+
def median_result(results)
|
22
|
+
sorted = results.sort
|
23
|
+
sorted_count = sorted.count
|
24
|
+
working_median = sorted_count.odd? ? sorted[((sorted_count + 1) / 2) - 1] : (sorted[(sorted_count - 1) / 2] + sorted[((sorted_count) / 2)] / 2.0)
|
25
|
+
working_median == working_median.to_i ? working_median.to_i : working_median
|
26
|
+
end
|
27
|
+
|
28
|
+
# returns an array of samples constituting a single trial run
|
29
|
+
#
|
30
|
+
# ex:
|
31
|
+
# _trial(100, 0.5) # => [0, 1, 1, 0, ...] <100 times long>
|
32
|
+
def _trial(samples_per_trial, probability)
|
33
|
+
(1..samples_per_trial).map{|i| rand() <= probability ? 1 : 0 }
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
metadata
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: tiny_monte
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jeff Lunt
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2023-12-21 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: a monte carlo simulator
|
14
|
+
email: jefflunt@gmail.com
|
15
|
+
executables: []
|
16
|
+
extensions: []
|
17
|
+
extra_rdoc_files: []
|
18
|
+
files:
|
19
|
+
- lib/tiny_monte.rb
|
20
|
+
homepage: https://github.com/jefflunt/tiny_monte
|
21
|
+
licenses:
|
22
|
+
- MIT
|
23
|
+
metadata: {}
|
24
|
+
post_install_message:
|
25
|
+
rdoc_options: []
|
26
|
+
require_paths:
|
27
|
+
- lib
|
28
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
29
|
+
requirements:
|
30
|
+
- - ">="
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
34
|
+
requirements:
|
35
|
+
- - ">="
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
requirements: []
|
39
|
+
rubygems_version: 3.3.7
|
40
|
+
signing_key:
|
41
|
+
specification_version: 4
|
42
|
+
summary: a monte carlo simulator
|
43
|
+
test_files: []
|