random_variable 1.2.1 → 1.2.2
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.
- data/lib/ext/gen.c +22 -3
- data/lib/ext/gen.h +1 -0
- data/lib/ext/random_variable.c +11 -0
- data/lib/random_variable.rb +7 -0
- data/lib/test.rb +21 -0
- data/lib/tests/common.rb +27 -0
- data/lib/tests/poisson.rb +87 -0
- metadata +5 -2
data/lib/ext/gen.c
CHANGED
@@ -15,37 +15,56 @@
|
|
15
15
|
|
16
16
|
#include <ruby.h>
|
17
17
|
|
18
|
+
/* The Ruby "Random" class */
|
18
19
|
extern VALUE rb_cRandom;
|
19
20
|
|
20
|
-
/* the random number generator */
|
21
|
+
/* the random number generator (instance) */
|
21
22
|
static VALUE rb_RandGen = Qnil;
|
22
23
|
|
24
|
+
/* For returning a random number at the C level */
|
23
25
|
#define GET_RB_RANF rb_funcall(rb_RandGen, rb_intern("rand"), 0)
|
24
26
|
double ranf(void)
|
25
27
|
{
|
26
28
|
return NUM2DBL(GET_RB_RANF);
|
27
29
|
}
|
28
30
|
|
31
|
+
/* For returning a random number at the Ruby level */
|
29
32
|
VALUE rb_ranf(void)
|
30
33
|
{
|
31
34
|
return GET_RB_RANF;
|
32
35
|
}
|
33
36
|
#undef GET_RB_RANF
|
34
37
|
|
38
|
+
/******************************************************************************/
|
39
|
+
/* Functions at C level */
|
40
|
+
/******************************************************************************/
|
41
|
+
|
42
|
+
/* get the current seed */
|
35
43
|
VALUE rv_gen_get_seed(void)
|
36
44
|
{
|
37
45
|
return rb_funcall(rb_RandGen, rb_intern("seed"), 0);
|
38
46
|
}
|
39
47
|
|
48
|
+
/* set the current seed */
|
40
49
|
void rv_gen_set_seed(VALUE rb_seed)
|
41
50
|
{
|
51
|
+
/* memory leak?! */
|
42
52
|
rb_RandGen = rb_funcall(rb_cRandom, rb_intern("new"), 1, rb_seed);
|
43
53
|
}
|
44
54
|
|
45
|
-
|
46
|
-
void rv_init_gen(void)
|
55
|
+
VALUE rv_gen_new_seed(void)
|
47
56
|
{
|
48
57
|
rb_RandGen = rb_funcall(rb_cRandom, rb_intern("new"), 0);
|
58
|
+
return rv_gen_get_seed();
|
59
|
+
}
|
60
|
+
/******************************************************************************/
|
61
|
+
|
62
|
+
|
63
|
+
/* Must be called BEFORE any kind ranf() call !! */
|
64
|
+
void rv_init_gen(void)
|
65
|
+
{
|
66
|
+
/* create an instance of the random generator */
|
67
|
+
rv_gen_new_seed();
|
49
68
|
|
50
69
|
/* tell the GC not to collect the random generator */
|
51
70
|
rb_gc_register_address(&rb_RandGen);
|
data/lib/ext/gen.h
CHANGED
data/lib/ext/random_variable.c
CHANGED
@@ -768,6 +768,14 @@ VALUE rb_outcomes(VALUE rb_obj, VALUE rb_nr_times)
|
|
768
768
|
}
|
769
769
|
#undef GET_DATA
|
770
770
|
|
771
|
+
/******************************************************************************/
|
772
|
+
/* set and geet the seed at Ruby level */
|
773
|
+
/******************************************************************************/
|
774
|
+
static VALUE rb_seed_new(VALUE self)
|
775
|
+
{
|
776
|
+
return rv_gen_new_seed();
|
777
|
+
}
|
778
|
+
|
771
779
|
static VALUE rb_seed_get(VALUE self)
|
772
780
|
{
|
773
781
|
return rv_gen_get_seed();
|
@@ -778,6 +786,8 @@ static VALUE rb_seed_set(VALUE self, VALUE rb_seed)
|
|
778
786
|
rv_gen_set_seed(rb_seed);
|
779
787
|
return rb_seed;
|
780
788
|
}
|
789
|
+
/******************************************************************************/
|
790
|
+
|
781
791
|
|
782
792
|
/******************************************************************************/
|
783
793
|
/* macros for the extension entry point */
|
@@ -818,6 +828,7 @@ void Init_random_variable(void)
|
|
818
828
|
|
819
829
|
/* Generator */
|
820
830
|
rb_mGenerator = rb_define_module_under(rb_mRandomVariable, "Generator");
|
831
|
+
rb_define_singleton_method(rb_mGenerator, "new_seed", rb_seed_new, 0);
|
821
832
|
rb_define_singleton_method(rb_mGenerator, "seed", rb_seed_get, 0);
|
822
833
|
rb_define_singleton_method(rb_mGenerator, "seed=", rb_seed_set, 1);
|
823
834
|
|
data/lib/random_variable.rb
CHANGED
@@ -35,6 +35,13 @@ require_relative 'ext/random_variable'
|
|
35
35
|
require_relative 'sampleable'
|
36
36
|
|
37
37
|
module RandomVariable
|
38
|
+
# obtain and set an arbitrary new seed
|
39
|
+
#
|
40
|
+
# @return [Numeric] the new seed
|
41
|
+
def self.new_seed
|
42
|
+
Generator::new_seed
|
43
|
+
end
|
44
|
+
|
38
45
|
# obtain the current seed in use
|
39
46
|
#
|
40
47
|
# @return [Numeric] the current seed
|
data/lib/test.rb
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
################################################################################
|
2
|
+
# #
|
3
|
+
# File: test.rb #
|
4
|
+
# #
|
5
|
+
################################################################################
|
6
|
+
# #
|
7
|
+
# Author: Jorge F.M. Rinaldi #
|
8
|
+
# Contact: jorge.madronal.rinaldi@gmail.com #
|
9
|
+
# #
|
10
|
+
################################################################################
|
11
|
+
# #
|
12
|
+
# Date: 2012/12/22 #
|
13
|
+
# #
|
14
|
+
################################################################################
|
15
|
+
|
16
|
+
require 'random_variable'
|
17
|
+
|
18
|
+
module RandomVariable::Tests
|
19
|
+
end
|
20
|
+
|
21
|
+
require_relative 'tests/poisson.rb'
|
data/lib/tests/common.rb
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
################################################################################
|
2
|
+
# #
|
3
|
+
# File: common.rb #
|
4
|
+
# #
|
5
|
+
################################################################################
|
6
|
+
# #
|
7
|
+
# Author: Jorge F.M. Rinaldi #
|
8
|
+
# Contact: jorge.madronal.rinaldi@gmail.com #
|
9
|
+
# #
|
10
|
+
################################################################################
|
11
|
+
# #
|
12
|
+
# Date: 2012/12/22 #
|
13
|
+
# #
|
14
|
+
################################################################################
|
15
|
+
|
16
|
+
# Attemps to require the gem denoted by +name+, otherwise it displays an
|
17
|
+
# error message and exits
|
18
|
+
def required_gem(name)
|
19
|
+
begin
|
20
|
+
require name
|
21
|
+
rescue LoadError # 'rescue' only rescues StandardError without args
|
22
|
+
abort("Gem '#{name}' required in order to work properly, " +
|
23
|
+
"please install the required gem and try again.\n" +
|
24
|
+
"\tThe installation is usually done as follows:\n" +
|
25
|
+
"\t\tgem install #{name}");
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,87 @@
|
|
1
|
+
################################################################################
|
2
|
+
# #
|
3
|
+
# File: poisson.rb #
|
4
|
+
# #
|
5
|
+
################################################################################
|
6
|
+
# #
|
7
|
+
# Author: Jorge F.M. Rinaldi #
|
8
|
+
# Contact: jorge.madronal.rinaldi@gmail.com #
|
9
|
+
# #
|
10
|
+
################################################################################
|
11
|
+
# #
|
12
|
+
# Date: 2012/12/22 #
|
13
|
+
# #
|
14
|
+
################################################################################
|
15
|
+
|
16
|
+
require_relative 'common.rb'
|
17
|
+
|
18
|
+
require 'test/unit'
|
19
|
+
|
20
|
+
required_gem 'shoulda'
|
21
|
+
required_gem 'random_variable'
|
22
|
+
|
23
|
+
class RandomVariable::Tests::Poisson < Test::Unit::TestCase
|
24
|
+
include RandomVariable
|
25
|
+
|
26
|
+
should "fail instantiating with no parameter" do
|
27
|
+
assert_raise(ArgumentError) { Poisson.new }
|
28
|
+
end
|
29
|
+
|
30
|
+
should "fail instantiating with a NaN parameter" do
|
31
|
+
assert_raise(ArgumentError) { Poisson.new(0.0/0) }
|
32
|
+
assert_raise(ArgumentError) { Poisson.new(0/0.0) }
|
33
|
+
end
|
34
|
+
|
35
|
+
should "fail instantiating with a Infinty parameter" do
|
36
|
+
assert_raise(ArgumentError) { Poisson.new(0.0/1) }
|
37
|
+
assert_raise(ArgumentError) { Poisson.new(-0.0/1) }
|
38
|
+
|
39
|
+
huge_number = 2**1024
|
40
|
+
assert_raise(ArgumentError) { Poisson.new(huge_number) }
|
41
|
+
end
|
42
|
+
|
43
|
+
should "fail instantiating with a negative parameter" do
|
44
|
+
assert_raise(ArgumentError) { Poisson.new(-0.0001) }
|
45
|
+
assert_raise(ArgumentError) { Poisson.new(-1) }
|
46
|
+
assert_raise(ArgumentError) { Poisson.new(-113) }
|
47
|
+
assert_raise(ArgumentError) { Poisson.new(-10_000) }
|
48
|
+
end
|
49
|
+
|
50
|
+
should "fail instantiating with zero as the parameter" do
|
51
|
+
assert_raise(ArgumentError) { Poisson.new(0) }
|
52
|
+
assert_raise(ArgumentError) { Poisson.new(-0) }
|
53
|
+
assert_raise(ArgumentError) { Poisson.new(0.0) }
|
54
|
+
assert_raise(ArgumentError) { Poisson.new(-0.0) }
|
55
|
+
end
|
56
|
+
|
57
|
+
should "instantiate with a valid parameter" do
|
58
|
+
Poisson.new 0.001
|
59
|
+
Poisson.new 0.1
|
60
|
+
Poisson.new 1
|
61
|
+
Poisson.new 100
|
62
|
+
Poisson.new 10_000
|
63
|
+
end
|
64
|
+
|
65
|
+
should "provide greater or equal to zero outcomes" do
|
66
|
+
[0.001, 0.1, 1, 100, 10_000, 10_000_000].each do |param|
|
67
|
+
x = Poisson.new param
|
68
|
+
samples = x.outcomes 100_000
|
69
|
+
samples.each do |sample|
|
70
|
+
assert(sample >= 0)
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
should "reproduce the same outcomes for the same seed value" do
|
76
|
+
seed = RandomVariable::new_seed
|
77
|
+
x = Poisson.new 113
|
78
|
+
samples = x.outcomes 10
|
79
|
+
RandomVariable::seed = seed
|
80
|
+
new_samples = x.outcomes 10
|
81
|
+
samples.length.times do |i|
|
82
|
+
puts "#{samples[i]} ?= #{new_samples[i]}"
|
83
|
+
assert_equal(samples[i], new_samples[i])
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: random_variable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.2.
|
4
|
+
version: 1.2.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-12-
|
12
|
+
date: 2012-12-23 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description: ! 'random variables for a wide variety of probability distributions such
|
15
15
|
as: Binomial, Beta, Chi-Squared, Normal, Pareto, Poisson, etc.'
|
@@ -26,6 +26,9 @@ files:
|
|
26
26
|
- lib/distros.rb
|
27
27
|
- LICENSE
|
28
28
|
- COPYING
|
29
|
+
- lib/test.rb
|
30
|
+
- lib/tests/common.rb
|
31
|
+
- lib/tests/poisson.rb
|
29
32
|
- lib/ext/random_variable.c
|
30
33
|
- lib/ext/gen.c
|
31
34
|
- lib/ext/gen.h
|