monte_carlo 0.0.3 → 0.0.4
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/.travis.yml +3 -0
- data/README.md +17 -3
- data/lib/monte_carlo.rb +1 -0
- data/lib/monte_carlo/experiment.rb +4 -2
- data/lib/monte_carlo/experiment_dsl.rb +29 -0
- data/lib/monte_carlo/version.rb +1 -1
- data/spec/lib/monte_carlo/experiment_dsl_spec.rb +66 -0
- metadata +6 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4ef1f90ac61fa117853dcb291543d633d0e926b3
|
4
|
+
data.tar.gz: ed41e0348dab9bd8b128dc37eaea97619e34e1f9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1df342cb801a0d8b842fd830f93c1010517a3b95d6b4e0e811867639689b0d6d67310d0b4647d08e5c718ed6d70ea789561b17a3815222653ffcbdd53510010e
|
7
|
+
data.tar.gz: 52cfaed3705d375fff447c49cfaf4fb4928538869a696c1972c7c3cee31b041b7d74eb7ec791f4d46b78231e3f5fac2ddaf5425a9a41ef18d0937997e72603de
|
data/.travis.yml
ADDED
data/README.md
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
# MonteCarlo
|
1
|
+
# MonteCarlo [](https://travis-ci.org/agelber/monte_carlo)
|
2
2
|
|
3
3
|
A utility to write quick [Monte Carlo Method](http://en.wikipedia.org/wiki/Monte_Carlo_method) experiments.
|
4
4
|
|
@@ -42,7 +42,21 @@ experiment.computation = -> (sample) { sample > 0.5 }
|
|
42
42
|
results = experiment.run
|
43
43
|
```
|
44
44
|
|
45
|
-
|
45
|
+
Another options is to use the configuration DSL, like so:
|
46
|
+
|
47
|
+
```ruby
|
48
|
+
# Create an experiment and pass it a configuration block
|
49
|
+
experiment = MonteCarlo::Experiment.new do
|
50
|
+
times 1000000
|
51
|
+
sample_method { rand }
|
52
|
+
compuation { |sample| sample > 0.5 }
|
53
|
+
end
|
54
|
+
|
55
|
+
# And run it normally
|
56
|
+
results = experiment.run
|
57
|
+
```
|
58
|
+
|
59
|
+
Alternatively, you can write your sample and computation method as one with the shorthand block syntax and get the restults straight away:
|
46
60
|
|
47
61
|
```ruby
|
48
62
|
results = MonteCarlo::Experiment.run(100000) { rand > 0.5 }
|
@@ -59,7 +73,7 @@ If no computation method was given, `value` and `sample_value` will be the same.
|
|
59
73
|
|
60
74
|
## Contributing
|
61
75
|
|
62
|
-
1. Fork it ( https://github.com/
|
76
|
+
1. Fork it ( https://github.com/agelber/monte_carlo/fork )
|
63
77
|
2. Create your feature branch (`git checkout -b my-new-feature`)
|
64
78
|
3. Commit your changes (`git commit -am 'Add some feature'`)
|
65
79
|
4. Push to the branch (`git push origin my-new-feature`)
|
data/lib/monte_carlo.rb
CHANGED
@@ -6,12 +6,14 @@ module MonteCarlo
|
|
6
6
|
attr_accessor :times, :sample_method, :computation, :setup, :reset
|
7
7
|
|
8
8
|
def self.run(times = DEFAULT_TIMES, &block)
|
9
|
-
MonteCarlo::Experiment.new(times
|
9
|
+
experiment = MonteCarlo::Experiment.new(times)
|
10
|
+
experiment.sample_method = block
|
11
|
+
experiment.run
|
10
12
|
end
|
11
13
|
|
12
14
|
def initialize(times = DEFAULT_TIMES, &block)
|
13
15
|
@times = times
|
14
|
-
|
16
|
+
MonteCarlo::ExperimentDSL.new(self).instance_eval(&block) if block_given?
|
15
17
|
end
|
16
18
|
|
17
19
|
def run
|
@@ -0,0 +1,29 @@
|
|
1
|
+
module MonteCarlo
|
2
|
+
class ExperimentDSL
|
3
|
+
|
4
|
+
def initialize(experiment)
|
5
|
+
@experiment = experiment
|
6
|
+
end
|
7
|
+
|
8
|
+
def times(times)
|
9
|
+
@experiment.times = times
|
10
|
+
end
|
11
|
+
|
12
|
+
def sample_method(&block)
|
13
|
+
@experiment.sample_method = block
|
14
|
+
end
|
15
|
+
|
16
|
+
def computation(&block)
|
17
|
+
@experiment.computation = block
|
18
|
+
end
|
19
|
+
|
20
|
+
def setup(&block)
|
21
|
+
@experiment.setup = block
|
22
|
+
end
|
23
|
+
|
24
|
+
def reset(&block)
|
25
|
+
@experiment.reset = block
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
29
|
+
end
|
data/lib/monte_carlo/version.rb
CHANGED
@@ -0,0 +1,66 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe MonteCarlo::ExperimentDSL do
|
4
|
+
let(:experiment) { MonteCarlo::Experiment.new }
|
5
|
+
|
6
|
+
def eval_in_dsl(&block)
|
7
|
+
MonteCarlo::ExperimentDSL.new(experiment).instance_eval(&block)
|
8
|
+
end
|
9
|
+
|
10
|
+
describe :times do
|
11
|
+
it 'should set the amount of times' do
|
12
|
+
expect(experiment).to receive(:times=)
|
13
|
+
|
14
|
+
eval_in_dsl do
|
15
|
+
times 1
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
describe :sample_method do
|
21
|
+
it 'should set the sample method' do
|
22
|
+
expect(experiment).to receive(:sample_method=)
|
23
|
+
|
24
|
+
eval_in_dsl do
|
25
|
+
sample_method { }
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
describe :computation do
|
31
|
+
let(:test_computation) { -> { } }
|
32
|
+
|
33
|
+
it 'should set the computation method' do
|
34
|
+
expect(experiment).to receive(:computation=)
|
35
|
+
|
36
|
+
eval_in_dsl do
|
37
|
+
computation { }
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
describe :setup do
|
43
|
+
let(:test_setup) { -> { } }
|
44
|
+
|
45
|
+
it 'should set the setup method' do
|
46
|
+
expect(experiment).to receive(:setup=)
|
47
|
+
|
48
|
+
eval_in_dsl do
|
49
|
+
setup { }
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
describe :reset do
|
55
|
+
let(:test_reset) { -> { } }
|
56
|
+
|
57
|
+
it 'should set the reset method' do
|
58
|
+
expect(experiment).to receive(:reset=)
|
59
|
+
|
60
|
+
eval_in_dsl do
|
61
|
+
reset { }
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: monte_carlo
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Assaf Gelber
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-11-
|
11
|
+
date: 2014-11-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -74,6 +74,7 @@ extensions: []
|
|
74
74
|
extra_rdoc_files: []
|
75
75
|
files:
|
76
76
|
- .gitignore
|
77
|
+
- .travis.yml
|
77
78
|
- Gemfile
|
78
79
|
- LICENSE.txt
|
79
80
|
- README.md
|
@@ -81,10 +82,12 @@ files:
|
|
81
82
|
- lib/monte_carlo.rb
|
82
83
|
- lib/monte_carlo/errors.rb
|
83
84
|
- lib/monte_carlo/experiment.rb
|
85
|
+
- lib/monte_carlo/experiment_dsl.rb
|
84
86
|
- lib/monte_carlo/experiment_results.rb
|
85
87
|
- lib/monte_carlo/result.rb
|
86
88
|
- lib/monte_carlo/version.rb
|
87
89
|
- monte_carlo.gemspec
|
90
|
+
- spec/lib/monte_carlo/experiment_dsl_spec.rb
|
88
91
|
- spec/lib/monte_carlo/experiment_results_spec.rb
|
89
92
|
- spec/lib/monte_carlo/experiment_spec.rb
|
90
93
|
- spec/spec_helper.rb
|
@@ -113,6 +116,7 @@ signing_key:
|
|
113
116
|
specification_version: 4
|
114
117
|
summary: A small gem to help with Monte Carlo Method experiments in ruby.
|
115
118
|
test_files:
|
119
|
+
- spec/lib/monte_carlo/experiment_dsl_spec.rb
|
116
120
|
- spec/lib/monte_carlo/experiment_results_spec.rb
|
117
121
|
- spec/lib/monte_carlo/experiment_spec.rb
|
118
122
|
- spec/spec_helper.rb
|