mc_forecast 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 4f0d344256cf08700cd6d8475d0ac19ec44079cae9699f7a037fc1b934ee5c4d
4
+ data.tar.gz: dca662a52e4bc902ddf944e47e59882f43d86629cf5792aa19d737fa430f8d59
5
+ SHA512:
6
+ metadata.gz: a066871adf87c23610e0f08938934f5e1abdb630e477aea65e86195446952e279df54028c9ea12017e37669371203d6f22c53d21c1433aa69cf88c51ccb7b6b1
7
+ data.tar.gz: 3c294be97dc9040d7cde19ae90c0508d02fe2f6eb777cb38ff109ac7a7971ca94b25b1fbf47cd53190cdcb5eb2f2a3558198361ffa54d6d9fdd43b7004d63982
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023 Daan van Vugt
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,42 @@
1
+ # mc_forecast
2
+
3
+ [![Gem Version](https://img.shields.io/gem/v/mc_forecast)](https://rubygems.org/gems/mc_forecast)
4
+ [![Gem Downloads](https://img.shields.io/gem/dt/mc_forecast)](https://www.ruby-toolbox.com/projects/mc_forecast)
5
+ [![GitHub Workflow Status](https://img.shields.io/github/actions/workflow/status/DaanVanVugt/ruby-mc/ci.yml)](https://github.com/DaanVanVugt/ruby-mc/actions/workflows/ci.yml)
6
+ [![Code Climate maintainability](https://img.shields.io/codeclimate/maintainability/DaanVanVugt/ruby-mc)](https://codeclimate.com/github/DaanVanVugt/ruby-mc)
7
+
8
+ TODO: Description of this gem goes here.
9
+
10
+ ---
11
+
12
+ - [Quick start](#quick-start)
13
+ - [Support](#support)
14
+ - [License](#license)
15
+ - [Code of conduct](#code-of-conduct)
16
+ - [Contribution guide](#contribution-guide)
17
+
18
+ ## Quick start
19
+
20
+ ```
21
+ gem install mc_forecast
22
+ ```
23
+
24
+ ```ruby
25
+ require "mc_forecast"
26
+ ```
27
+
28
+ ## Support
29
+
30
+ If you want to report a bug, or have ideas, feedback or questions about the gem, [let me know via GitHub issues](https://github.com/DaanVanVugt/ruby-mc/issues/new) and I will do my best to provide a helpful answer. Happy hacking!
31
+
32
+ ## License
33
+
34
+ The gem is available as open source under the terms of the [MIT License](LICENSE.txt).
35
+
36
+ ## Code of conduct
37
+
38
+ Everyone interacting in this project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](CODE_OF_CONDUCT.md).
39
+
40
+ ## Contribution guide
41
+
42
+ Pull requests are welcome!
@@ -0,0 +1,50 @@
1
+ module McForecast
2
+ class Simulation
3
+ def run(init_state: nil, trials: 1_000, steps: 1, quantiles: [0.025, 0.975])
4
+ events = {}
5
+ (0..trials - 1).each do |trial|
6
+ state = init_state.dup # this is not a deepcopy!
7
+ (0..steps - 1).each do |step|
8
+ state, e = yield state, step, trial
9
+ e.each_pair do |k, v|
10
+ # We explicitly store all the events, because we expect the block
11
+ # to be relatively expensive this will not be so bad.
12
+ # could replace with a quantile estimator like CKMS later
13
+ events[k] ||= Array.new(steps) { Array.new(trials) }
14
+ events[k][step][trial] = v
15
+ end
16
+ end
17
+ end
18
+ analyze(events, quantiles)
19
+ end
20
+
21
+ private
22
+
23
+ # Return an analysis of the events, containing:
24
+ # { event_name:
25
+ # { mean: [...], # per step
26
+ # quantiles:
27
+ # { 0.025: [...],
28
+ # 0.975: [...]
29
+ # }}}
30
+ def analyze(events, quantiles)
31
+ events.transform_values do |steps| # array(steps) of arrays(trials)
32
+ {
33
+ mean: steps.map { |trials| Rational(trials.sum || 0, trials.length) },
34
+ quantiles: Array.new(steps.length)
35
+ }
36
+
37
+ require 'byebug'
38
+ puts(steps.map do |trials|
39
+ [quantiles, trials.sort.values_at(*quantile_indices(trials.length, quantiles))]
40
+ end)
41
+ end
42
+ end
43
+
44
+ def quantile_indices(n_trials, quantiles)
45
+ quantiles.map do |q|
46
+ (q * (n_trials - 1)).round.to_i.clamp(0, n_trials - 1)
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,3 @@
1
+ module McForecast
2
+ VERSION = "0.1.0".freeze
3
+ end
@@ -0,0 +1,5 @@
1
+ require "mc_forecast/simulation"
2
+
3
+ module McForecast
4
+ autoload :VERSION, "mc_forecast/version"
5
+ end
metadata ADDED
@@ -0,0 +1,53 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mc_forecast
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Daan van Vugt
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2023-12-08 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description:
14
+ email:
15
+ - dvanvugt@ignitioncomputing.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - LICENSE.txt
21
+ - README.md
22
+ - lib/mc_forecast.rb
23
+ - lib/mc_forecast/simulation.rb
24
+ - lib/mc_forecast/version.rb
25
+ homepage: https://github.com/DaanVanVugt/ruby-mc
26
+ licenses:
27
+ - MIT
28
+ metadata:
29
+ bug_tracker_uri: https://github.com/DaanVanVugt/ruby-mc/issues
30
+ changelog_uri: https://github.com/DaanVanVugt/ruby-mc/releases
31
+ source_code_uri: https://github.com/DaanVanVugt/ruby-mc
32
+ homepage_uri: https://github.com/DaanVanVugt/ruby-mc
33
+ rubygems_mfa_required: 'true'
34
+ post_install_message:
35
+ rdoc_options: []
36
+ require_paths:
37
+ - lib
38
+ required_ruby_version: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: '3.0'
43
+ required_rubygems_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ requirements: []
49
+ rubygems_version: 3.4.10
50
+ signing_key:
51
+ specification_version: 4
52
+ summary: Forecast processes using monte-carlo simulation
53
+ test_files: []