mc_forecast 0.1.0 → 0.1.1
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 +4 -4
 - data/README.md +12 -1
 - data/lib/mc_forecast/simulation.rb +14 -7
 - data/lib/mc_forecast/version.rb +1 -1
 - metadata +16 -2
 
    
        checksums.yaml
    CHANGED
    
    | 
         @@ -1,7 +1,7 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            ---
         
     | 
| 
       2 
2 
     | 
    
         
             
            SHA256:
         
     | 
| 
       3 
     | 
    
         
            -
              metadata.gz:  
     | 
| 
       4 
     | 
    
         
            -
              data.tar.gz:  
     | 
| 
      
 3 
     | 
    
         
            +
              metadata.gz: 387e653e97a687f2ab7618e2612c38fae72ddb5bc87485b44998005cab548d3d
         
     | 
| 
      
 4 
     | 
    
         
            +
              data.tar.gz: 161aca02d0cb2ded0c143908ee9d4747078bd73888974d026b08d8039ccb117a
         
     | 
| 
       5 
5 
     | 
    
         
             
            SHA512:
         
     | 
| 
       6 
     | 
    
         
            -
              metadata.gz:  
     | 
| 
       7 
     | 
    
         
            -
              data.tar.gz:  
     | 
| 
      
 6 
     | 
    
         
            +
              metadata.gz: f075afc3d8b5c2c234c50fc2b58ea66da44a6bb6cfbc3058982c6a131aa374576b1ec7c953bb1b21f81c0cddba46233be67c8fddc09887049f060db37f903848
         
     | 
| 
      
 7 
     | 
    
         
            +
              data.tar.gz: c0e17a7551d1ba892b7d372e1a1424eda9b8ee3231b68420e6a03e773cde672d6fad125529f99e72d7a1c810aed6b30b6a6ac4e12ebbfa9adfb06a96b8174454
         
     | 
    
        data/README.md
    CHANGED
    
    | 
         @@ -5,7 +5,7 @@ 
     | 
|
| 
       5 
5 
     | 
    
         
             
            [](https://github.com/DaanVanVugt/ruby-mc/actions/workflows/ci.yml)
         
     | 
| 
       6 
6 
     | 
    
         
             
            [](https://codeclimate.com/github/DaanVanVugt/ruby-mc)
         
     | 
| 
       7 
7 
     | 
    
         | 
| 
       8 
     | 
    
         
            -
             
     | 
| 
      
 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 
     | 
| 
      
 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:  
     | 
| 
      
 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 
     | 
    
         | 
    
        data/lib/mc_forecast/version.rb
    CHANGED
    
    
    
        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. 
     | 
| 
      
 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
         
     |