random-walk 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +4 -13
- data/VERSION +1 -1
- data/lib/random-walk.rb +4 -22
- data/random-walk.gemspec +1 -1
- data/spec/random-walk_spec.rb +21 -48
- metadata +2 -4
data/README.rdoc
CHANGED
@@ -1,26 +1,17 @@
|
|
1
1
|
= random-walk
|
2
2
|
|
3
|
-
This is an extremely simple gem for generating random-walk test data in an array. It's useful for statistics testing, where random data falls short for
|
3
|
+
This is an extremely simple gem for generating random-walk test data in an array. It's useful for statistics or finance testing, where random data falls short for something like simple regression or the movement of stocks.
|
4
4
|
|
5
|
-
The resulting array will always stay within the range provided (inclusive). The random-walk adds +1, 0, or -1 on each iteration.
|
5
|
+
The resulting array will always stay within the range provided (inclusive). The random-walk adds +1, 0, or -1 on each iteration by default.
|
6
6
|
|
7
7
|
== Usage
|
8
8
|
|
9
9
|
require 'random-walk'
|
10
10
|
|
11
|
-
# RandomWalk.generate(range, array_length)
|
11
|
+
# RandomWalk.generate(range, array_length, step=1)
|
12
12
|
RandomWalk.generate(10..40, 5) # [11, 12, 11, 10, 10]
|
13
13
|
RandomWalk.generate(10..40, 5) # [27, 28, 29, 30, 29]
|
14
|
-
|
15
|
-
# generate_vectors(x_range, y_range, vector_length)
|
16
|
-
rw = RandomWalk.new
|
17
|
-
rw.generate_vectors(0..99, 0..1023, 5)
|
18
|
-
rw.x # [78, 79, 80, 81, 81]
|
19
|
-
rw.y # [700, 701, 702, 702, 701]
|
20
|
-
|
21
|
-
# fit_y_to_x(fit_chance)
|
22
|
-
rw.fit_y_to_x(100) # Perfect fit. If x goes up by 1, y goes up by 1
|
23
|
-
rw.fit_y_to_x(50) # 50% chance. If x goes up by 1, y has a 50 percent chance of going up by 1
|
14
|
+
RandomWalk.generate(10..40, 5, 2) # [34, 36, 36, 38, 36]
|
24
15
|
|
25
16
|
== Contributing to random-walk
|
26
17
|
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.3
|
data/lib/random-walk.rb
CHANGED
@@ -1,11 +1,10 @@
|
|
1
1
|
class RandomWalk
|
2
|
-
|
3
|
-
|
4
|
-
def self.generate(limits, array_length)
|
2
|
+
def self.generate(limits, array_length, step = 1)
|
5
3
|
starting_point = Random.rand(limits)
|
6
4
|
output_array = [starting_point]
|
5
|
+
|
7
6
|
(array_length - 1).times do
|
8
|
-
add_or_subtract = Random.rand(-
|
7
|
+
add_or_subtract = Random.rand(-step..step)
|
9
8
|
end_point = output_array.last + add_or_subtract
|
10
9
|
if end_point <= limits.max && end_point >= limits.min
|
11
10
|
output_array << end_point
|
@@ -13,24 +12,7 @@ class RandomWalk
|
|
13
12
|
output_array << output_array.last
|
14
13
|
end
|
15
14
|
end
|
16
|
-
return output_array
|
17
|
-
end
|
18
15
|
|
19
|
-
|
20
|
-
self.x = RandomWalk.generate(x_limits, vector_length)
|
21
|
-
self.y = RandomWalk.generate(y_limits, vector_length)
|
22
|
-
end
|
23
|
-
|
24
|
-
def fit_y_to_x(fit_chance)
|
25
|
-
raise 'Fit chance must be between 0 and 100' unless fit_chance <= 100 && fit_chance >= 0
|
26
|
-
self.x.each_with_index do |value, i|
|
27
|
-
if self.x[i] && self.x[i+1]
|
28
|
-
x_diff = self.x[i] - self.x[i+1]
|
29
|
-
rand = Random.rand(100 / fit_chance)
|
30
|
-
if rand == 0
|
31
|
-
self.y[i+1] = self.y[i] - x_diff
|
32
|
-
end
|
33
|
-
end
|
34
|
-
end
|
16
|
+
return output_array
|
35
17
|
end
|
36
18
|
end
|
data/random-walk.gemspec
CHANGED
data/spec/random-walk_spec.rb
CHANGED
@@ -1,62 +1,35 @@
|
|
1
1
|
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
2
|
|
3
3
|
describe "RandomWalk" do
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
result.should be >= 0
|
10
|
-
result.should be <= 10
|
11
|
-
end
|
4
|
+
it "returns results between given limits" do
|
5
|
+
5.times do
|
6
|
+
results = RandomWalk.generate(0..10, 1000)
|
7
|
+
results.each do |result|
|
8
|
+
(result.should be >= 0) && (result.should be <= 10)
|
12
9
|
end
|
13
10
|
end
|
14
|
-
|
15
|
-
it "returns an array of the correct length" do
|
16
|
-
results = RandomWalk.generate(100..200, 250)
|
17
|
-
results.length.should == 250
|
18
|
-
end
|
19
11
|
end
|
20
12
|
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
5.times do
|
26
|
-
rw.generate_vectors(0..10, 0..10, 1000)
|
27
|
-
rw.x.each do |result|
|
28
|
-
result.should be >= 0
|
29
|
-
result.should be <= 10
|
30
|
-
end
|
31
|
-
rw.y.each do |result|
|
32
|
-
result.should be >= 0
|
33
|
-
result.should be <= 10
|
34
|
-
end
|
35
|
-
end
|
36
|
-
end
|
13
|
+
it "returns an array of the correct length" do
|
14
|
+
results = RandomWalk.generate(100..200, 250)
|
15
|
+
results.length.should == 250
|
16
|
+
end
|
37
17
|
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
18
|
+
it "uses a default step size of 1" do
|
19
|
+
results = RandomWalk.generate(0..10, 1000, 1)
|
20
|
+
previous_result = results.first
|
21
|
+
results.each do |result|
|
22
|
+
(previous_result-1..previous_result+1).should cover(result)
|
23
|
+
previous_result = result
|
42
24
|
end
|
43
25
|
end
|
44
26
|
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
rw.fit_y_to_x(fit_chance)
|
52
|
-
diff_x, diff_y = 0, 0
|
53
|
-
rw.x.each_with_index do |result, i|
|
54
|
-
if rw.x[i+1]
|
55
|
-
diff_x += rw.x[i] - rw.x[i+1]
|
56
|
-
diff_y += rw.y[i] - rw.y[i+1]
|
57
|
-
end
|
58
|
-
end
|
59
|
-
diff_x.should == diff_y
|
27
|
+
it "uses steps of the specified size" do
|
28
|
+
results = RandomWalk.generate(0..10, 1000, 5)
|
29
|
+
previous_result = results.first
|
30
|
+
results.each do |result|
|
31
|
+
(previous_result-5..previous_result+5).should cover(result)
|
32
|
+
previous_result = result
|
60
33
|
end
|
61
34
|
end
|
62
35
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: random-walk
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -109,9 +109,6 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
109
109
|
- - ! '>='
|
110
110
|
- !ruby/object:Gem::Version
|
111
111
|
version: '0'
|
112
|
-
segments:
|
113
|
-
- 0
|
114
|
-
hash: -3032803507122637849
|
115
112
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
116
113
|
none: false
|
117
114
|
requirements:
|
@@ -125,3 +122,4 @@ signing_key:
|
|
125
122
|
specification_version: 3
|
126
123
|
summary: Simple random-walk generator
|
127
124
|
test_files: []
|
125
|
+
has_rdoc:
|