mc_forecast 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4f0d344256cf08700cd6d8475d0ac19ec44079cae9699f7a037fc1b934ee5c4d
4
- data.tar.gz: dca662a52e4bc902ddf944e47e59882f43d86629cf5792aa19d737fa430f8d59
3
+ metadata.gz: 387e653e97a687f2ab7618e2612c38fae72ddb5bc87485b44998005cab548d3d
4
+ data.tar.gz: 161aca02d0cb2ded0c143908ee9d4747078bd73888974d026b08d8039ccb117a
5
5
  SHA512:
6
- metadata.gz: a066871adf87c23610e0f08938934f5e1abdb630e477aea65e86195446952e279df54028c9ea12017e37669371203d6f22c53d21c1433aa69cf88c51ccb7b6b1
7
- data.tar.gz: 3c294be97dc9040d7cde19ae90c0508d02fe2f6eb777cb38ff109ac7a7971ca94b25b1fbf47cd53190cdcb5eb2f2a3558198361ffa54d6d9fdd43b7004d63982
6
+ metadata.gz: f075afc3d8b5c2c234c50fc2b58ea66da44a6bb6cfbc3058982c6a131aa374576b1ec7c953bb1b21f81c0cddba46233be67c8fddc09887049f060db37f903848
7
+ data.tar.gz: c0e17a7551d1ba892b7d372e1a1424eda9b8ee3231b68420e6a03e773cde672d6fad125529f99e72d7a1c810aed6b30b6a6ac4e12ebbfa9adfb06a96b8174454
data/README.md CHANGED
@@ -5,7 +5,7 @@
5
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
6
  [![Code Climate maintainability](https://img.shields.io/codeclimate/maintainability/DaanVanVugt/ruby-mc)](https://codeclimate.com/github/DaanVanVugt/ruby-mc)
7
7
 
8
- TODO: Description of this gem goes here.
8
+ Use Monte-Carlo methods for business forecasting. Define transition methods (for example a month-based one) and keep track of events you are interested in. Automatically generates a 95% confidence interval and mean values.
9
9
 
10
10
  ---
11
11
 
@@ -23,6 +23,17 @@ gem install mc_forecast
23
23
 
24
24
  ```ruby
25
25
  require "mc_forecast"
26
+ # all arguments optional
27
+ e = McForecast::Simulation.new.run(init_state: nil, steps: 1, trials: 1_000) do |_state, _step, _trial|
28
+ events = {}
29
+ events[:coin] = rand > 0.5 ? 1 : 0
30
+
31
+ # block should return a new state and a hash of events
32
+ [nil, events]
33
+ end
34
+ # e[:coin][:mean][0] ~ 0.5
35
+ # e[:coin][:quantiles][0.025][0] ~ 0
36
+ # e[:coin][:quantiles][0.975][0] ~ 1
26
37
  ```
27
38
 
28
39
  ## Support
@@ -1,9 +1,11 @@
1
+ require "deep_dup"
2
+
1
3
  module McForecast
2
4
  class Simulation
3
5
  def run(init_state: nil, trials: 1_000, steps: 1, quantiles: [0.025, 0.975])
4
6
  events = {}
5
7
  (0..trials - 1).each do |trial|
6
- state = init_state.dup # this is not a deepcopy!
8
+ state = DeepDup.deep_dup(init_state)
7
9
  (0..steps - 1).each do |step|
8
10
  state, e = yield state, step, trial
9
11
  e.each_pair do |k, v|
@@ -29,15 +31,20 @@ module McForecast
29
31
  # }}}
30
32
  def analyze(events, quantiles)
31
33
  events.transform_values do |steps| # array(steps) of arrays(trials)
34
+ a = if quantiles.any?
35
+ # only need to sort if we request answers on any quantiles
36
+ steps.map do |trials|
37
+ # could avoid sorting with some creativity, but probably fine for our data lengths so far
38
+ trials.sort.values_at(*quantile_indices(trials.length, quantiles))
39
+ end.transpose # a[step][]
40
+ else
41
+ []
42
+ end
43
+
32
44
  {
33
45
  mean: steps.map { |trials| Rational(trials.sum || 0, trials.length) },
34
- quantiles: Array.new(steps.length)
46
+ quantiles: quantiles.zip(a).to_h
35
47
  }
36
-
37
- require 'byebug'
38
- puts(steps.map do |trials|
39
- [quantiles, trials.sort.values_at(*quantile_indices(trials.length, quantiles))]
40
- end)
41
48
  end
42
49
  end
43
50
 
@@ -1,3 +1,3 @@
1
1
  module McForecast
2
- VERSION = "0.1.0".freeze
2
+ VERSION = "0.1.1".freeze
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mc_forecast
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daan van Vugt
@@ -9,7 +9,21 @@ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
11
  date: 2023-12-08 00:00:00.000000000 Z
12
- dependencies: []
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: deep_dup
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
13
27
  description:
14
28
  email:
15
29
  - dvanvugt@ignitioncomputing.com