montecarlo-rb 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +1 -0
- data/.ruby-version +1 -0
- data/README.md +44 -0
- data/Rakefile +2 -0
- data/lib/montecarlo/histogram_random_picker.rb +27 -0
- data/lib/montecarlo/simulator.rb +16 -0
- data/lib/montecarlo.rb +5 -0
- data/montecarlo-rb.gemspec +12 -0
- metadata +52 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 59915309cb7225c37c317e61bc1a7de751857336315ee50a3615564c952a9093
|
4
|
+
data.tar.gz: 3406685ce78de34fd11deb0042d8dcdc132b5fac6590f0f4cdc862c6f580470b
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: df94327d2fd1607a645d93371d94b8ce37bb530c5b0ef90e1e29e3707c5add4100106505fac7ed48088760075bb6f7c29d062eae08196a9f965a6b23cdcba29c
|
7
|
+
data.tar.gz: 2104f42d6007eb72806405d7cae0c26c4a44936a21d6055ff014a3cd39708e6c093db9dd1ae2aa88cc3a3c7470d0bb4ab3d01155d2dc2a07adc3d2c998bee35a
|
data/.gitignore
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
*.gem
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
3.3.5
|
data/README.md
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
# Montecarlo simulation
|
2
|
+
|
3
|
+
MonteCarloRB is a Ruby library designed to perform Monte Carlo simulations based on historical data distributions. It allows you to input probabilities with frequencies and simulate outcomes thousands of times. This is especially useful for scenarios where future outcomes depend on historical trends or distributions.
|
4
|
+
|
5
|
+
## Features
|
6
|
+
|
7
|
+
Monte Carlo simulation: Run thousands of simulations for any probability-based scenario.
|
8
|
+
Customizable probabilities: Input historical data with frequencies to shape the simulation.
|
9
|
+
|
10
|
+
Installation
|
11
|
+
Add this line to your application's Gemfile:
|
12
|
+
|
13
|
+
```ruby
|
14
|
+
gem 'monte_carlo_rb'
|
15
|
+
```
|
16
|
+
|
17
|
+
And then execute:
|
18
|
+
|
19
|
+
```bash
|
20
|
+
bundle install
|
21
|
+
```
|
22
|
+
|
23
|
+
Or install it yourself as:
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
gem install monte_carlo_rb
|
27
|
+
```
|
28
|
+
|
29
|
+
## Usage
|
30
|
+
|
31
|
+
## Basic Simulation Example
|
32
|
+
|
33
|
+
Suppose you have historical data representing daily engineering throughput (e.g., number of tasks completed per day) over the past few months. You can use this data to simulate possible throughput for the next 30 days.
|
34
|
+
|
35
|
+
DiceRoller is a Ruby class that simulates rolling a six-sided die. It has a method called `roll` that returns a random number between 1 and 6.
|
36
|
+
|
37
|
+
```ruby
|
38
|
+
require 'montecarlo'
|
39
|
+
dice_picker = MonteCarlo::HistogramRandomPicker.new({ 1 => 1, 2 => 1, 3 => 1, 4 => 1, 5 => 1, 6 => 1 })
|
40
|
+
simulator = MonteCarlo::Simulator.new(dice_picker)
|
41
|
+
results = simulator.run(1_000_000)
|
42
|
+
puts results
|
43
|
+
# => {4=>167583, 2=>166213, 5=>165950, 3=>167050, 6=>167021, 1=>166183}
|
44
|
+
```
|
data/Rakefile
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
module MonteCarlo
|
2
|
+
class HistogramRandomPicker
|
3
|
+
# histogram is a hash where the keys are the possible outcomes and the values are the number of occurrences of each outcome.
|
4
|
+
# For example, { 1 => 2, 2 => 3, 3 => 1 } means that the possible outcomes are 1, 2 and 3, and that 1 occurs twice, 2 occurs three times and 3 occurs once.
|
5
|
+
def initialize(histogram)
|
6
|
+
@histogram = histogram
|
7
|
+
calculate_cumulative_occurrences!
|
8
|
+
end
|
9
|
+
|
10
|
+
def pick
|
11
|
+
random_number = rand(@total_occurrences)
|
12
|
+
found_cumulative = @cumulative_occurrences.bsearch { |cumulative| random_number < cumulative[:cumulative] }
|
13
|
+
found_cumulative[:throughput]
|
14
|
+
end
|
15
|
+
|
16
|
+
private
|
17
|
+
|
18
|
+
def calculate_cumulative_occurrences!
|
19
|
+
@total_occurrences = 0
|
20
|
+
@cumulative_occurrences = []
|
21
|
+
@histogram.each do |outcome, occurrences|
|
22
|
+
@total_occurrences += occurrences
|
23
|
+
@cumulative_occurrences << { throughput: outcome, cumulative: @total_occurrences }
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module MonteCarlo
|
2
|
+
class Simulator
|
3
|
+
def initialize(picker)
|
4
|
+
@picker = picker
|
5
|
+
end
|
6
|
+
|
7
|
+
def run(n_times = 10_000)
|
8
|
+
result_counts = Hash.new(0)
|
9
|
+
n_times.times do
|
10
|
+
result = @picker.pick
|
11
|
+
result_counts[result] += 1
|
12
|
+
end
|
13
|
+
result_counts
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
data/lib/montecarlo.rb
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.name = "montecarlo-rb"
|
3
|
+
s.version = "0.0.3"
|
4
|
+
s.required_ruby_version = ">= 2.0.0"
|
5
|
+
s.summary = "MonteCarloRB is a Ruby library designed to perform Monte Carlo simulations based on historical data distributions."
|
6
|
+
s.description = "MonteCarloRB is a Ruby library designed to perform Monte Carlo simulations based on historical data distributions."
|
7
|
+
s.authors = ["Jiazhen Xie"]
|
8
|
+
s.email = "jiazhenxie515@gmail.com"
|
9
|
+
s.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test)/}) }
|
10
|
+
s.homepage = "https://github.com/JIAZHEN/montecarlo-rb"
|
11
|
+
s.license = "MIT"
|
12
|
+
end
|
metadata
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: montecarlo-rb
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.3
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jiazhen Xie
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2024-10-20 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: MonteCarloRB is a Ruby library designed to perform Monte Carlo simulations
|
14
|
+
based on historical data distributions.
|
15
|
+
email: jiazhenxie515@gmail.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- ".gitignore"
|
21
|
+
- ".ruby-version"
|
22
|
+
- README.md
|
23
|
+
- Rakefile
|
24
|
+
- lib/montecarlo.rb
|
25
|
+
- lib/montecarlo/histogram_random_picker.rb
|
26
|
+
- lib/montecarlo/simulator.rb
|
27
|
+
- montecarlo-rb.gemspec
|
28
|
+
homepage: https://github.com/JIAZHEN/montecarlo-rb
|
29
|
+
licenses:
|
30
|
+
- MIT
|
31
|
+
metadata: {}
|
32
|
+
post_install_message:
|
33
|
+
rdoc_options: []
|
34
|
+
require_paths:
|
35
|
+
- lib
|
36
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 2.0.0
|
41
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
42
|
+
requirements:
|
43
|
+
- - ">="
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
requirements: []
|
47
|
+
rubygems_version: 3.5.16
|
48
|
+
signing_key:
|
49
|
+
specification_version: 4
|
50
|
+
summary: MonteCarloRB is a Ruby library designed to perform Monte Carlo simulations
|
51
|
+
based on historical data distributions.
|
52
|
+
test_files: []
|