random_variable 1.0.1 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/distros.rb +10 -10
- data/lib/ext/gen.c +52 -0
- data/lib/ext/gen.h +27 -0
- data/lib/ext/randlib.c +3 -0
- data/lib/ext/random_variable.c +23 -4
- data/lib/ext/xrandlib.c +1 -1
- data/lib/random_variable.rb +46 -30
- metadata +4 -2
data/lib/distros.rb
CHANGED
@@ -14,7 +14,7 @@
|
|
14
14
|
################################################################################
|
15
15
|
|
16
16
|
module RandomVariable
|
17
|
-
class Bernoulli
|
17
|
+
class Bernoulli < Generic
|
18
18
|
# create a new <i>Bernoulli Random Variable</i> with
|
19
19
|
# parameter +p+
|
20
20
|
def self.new(p)
|
@@ -22,7 +22,7 @@ module RandomVariable
|
|
22
22
|
end
|
23
23
|
end
|
24
24
|
|
25
|
-
class Beta
|
25
|
+
class Beta < Generic
|
26
26
|
# create a new <i>Beta Random Variable</i> with parameters
|
27
27
|
# +alpha+ and +beta+
|
28
28
|
def self.new(alpha, beta)
|
@@ -30,7 +30,7 @@ module RandomVariable
|
|
30
30
|
end
|
31
31
|
end
|
32
32
|
|
33
|
-
class Binomial
|
33
|
+
class Binomial < Generic
|
34
34
|
# create a new <i>Binomial Random Variable</i> with parameters
|
35
35
|
# +n+ and +p+
|
36
36
|
def self.new(n, p)
|
@@ -38,7 +38,7 @@ module RandomVariable
|
|
38
38
|
end
|
39
39
|
end
|
40
40
|
|
41
|
-
class ContinuousUniform
|
41
|
+
class ContinuousUniform < Generic
|
42
42
|
# create a new <i>Continuous Uniform Random Variable</i> with
|
43
43
|
# parameters +a+ and +b+
|
44
44
|
def self.new(a, b)
|
@@ -46,7 +46,7 @@ module RandomVariable
|
|
46
46
|
end
|
47
47
|
end
|
48
48
|
|
49
|
-
class DiscreteUniform
|
49
|
+
class DiscreteUniform < Generic
|
50
50
|
# create a new <i>Discrete Uniform Random Variable</i> with
|
51
51
|
# parameters +a+ and +b+
|
52
52
|
def self.new(a, b)
|
@@ -54,7 +54,7 @@ module RandomVariable
|
|
54
54
|
end
|
55
55
|
end
|
56
56
|
|
57
|
-
class Exponential
|
57
|
+
class Exponential < Generic
|
58
58
|
# create a new <i>Exponential Random Variable</i> with a mean of
|
59
59
|
# +mean+
|
60
60
|
def self.new(mean)
|
@@ -62,7 +62,7 @@ module RandomVariable
|
|
62
62
|
end
|
63
63
|
end
|
64
64
|
|
65
|
-
class F
|
65
|
+
class F < Generic
|
66
66
|
# create a new <i>F Random Variable</i> with parameters
|
67
67
|
# +d1+ and +d2+
|
68
68
|
def self.new(d1, d2)
|
@@ -70,7 +70,7 @@ module RandomVariable
|
|
70
70
|
end
|
71
71
|
end
|
72
72
|
|
73
|
-
class Normal
|
73
|
+
class Normal < Generic
|
74
74
|
# create a new <i>Normal Random Variable</i> with parameters
|
75
75
|
# +mu+ and +sigma+
|
76
76
|
def self.new(mu = 0.0, sigma = 1.0)
|
@@ -78,7 +78,7 @@ module RandomVariable
|
|
78
78
|
end
|
79
79
|
end
|
80
80
|
|
81
|
-
class Poisson
|
81
|
+
class Poisson < Generic
|
82
82
|
# create a new <i>Poisson Random Variable</i> with a mean of
|
83
83
|
# +mean+
|
84
84
|
def self.new(mean)
|
@@ -86,7 +86,7 @@ module RandomVariable
|
|
86
86
|
end
|
87
87
|
end
|
88
88
|
|
89
|
-
class Rayleigh
|
89
|
+
class Rayleigh < Generic
|
90
90
|
# create a new <i>Rayleigh Random Variable</i> with parameter
|
91
91
|
# +sigma+
|
92
92
|
def self.new(sigma)
|
data/lib/ext/gen.c
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
////////////////////////////////////////////////////////////////////////////////
|
2
|
+
// //
|
3
|
+
// File: gen.c //
|
4
|
+
// //
|
5
|
+
////////////////////////////////////////////////////////////////////////////////
|
6
|
+
// //
|
7
|
+
// Author: Jorge F.M. Rinaldi //
|
8
|
+
// Contact: jorge.madronal.rinaldi@gmail.com //
|
9
|
+
// //
|
10
|
+
////////////////////////////////////////////////////////////////////////////////
|
11
|
+
// //
|
12
|
+
// Date: 2012/12/01 //
|
13
|
+
// //
|
14
|
+
////////////////////////////////////////////////////////////////////////////////
|
15
|
+
|
16
|
+
#include <ruby.h>
|
17
|
+
|
18
|
+
extern VALUE rb_cRandom;
|
19
|
+
|
20
|
+
/* the random number generator */
|
21
|
+
static VALUE rb_RandGen = Qnil;
|
22
|
+
|
23
|
+
#define GET_RB_RANF rb_funcall(rb_RandGen, rb_intern("rand"), 0)
|
24
|
+
double ranf(void)
|
25
|
+
{
|
26
|
+
return NUM2DBL(GET_RB_RANF);
|
27
|
+
}
|
28
|
+
|
29
|
+
VALUE rb_ranf(void)
|
30
|
+
{
|
31
|
+
return GET_RB_RANF;
|
32
|
+
}
|
33
|
+
#undef GET_RB_RANF
|
34
|
+
|
35
|
+
VALUE rv_gen_get_seed(void)
|
36
|
+
{
|
37
|
+
return rb_funcall(rb_RandGen, rb_intern("seed"), 0);
|
38
|
+
}
|
39
|
+
|
40
|
+
void rv_gen_set_seed(VALUE rb_seed)
|
41
|
+
{
|
42
|
+
rb_RandGen = rb_funcall(rb_cRandom, rb_intern("new"), 1, rb_seed);
|
43
|
+
}
|
44
|
+
|
45
|
+
/* Must be called before any kind ranf() call */
|
46
|
+
void rv_init_gen(void)
|
47
|
+
{
|
48
|
+
rb_RandGen = rb_funcall(rb_cRandom, rb_intern("new"), 0);
|
49
|
+
|
50
|
+
/* tell the GC not to collect the random generator */
|
51
|
+
rb_gc_register_address(&rb_RandGen);
|
52
|
+
}
|
data/lib/ext/gen.h
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
////////////////////////////////////////////////////////////////////////////////
|
2
|
+
// //
|
3
|
+
// File: gen.h //
|
4
|
+
// //
|
5
|
+
////////////////////////////////////////////////////////////////////////////////
|
6
|
+
// //
|
7
|
+
// Author: Jorge F.M. Rinaldi //
|
8
|
+
// Contact: jorge.madronal.rinaldi@gmail.com //
|
9
|
+
// //
|
10
|
+
////////////////////////////////////////////////////////////////////////////////
|
11
|
+
// //
|
12
|
+
// Date: 2012/12/01 //
|
13
|
+
// //
|
14
|
+
////////////////////////////////////////////////////////////////////////////////
|
15
|
+
|
16
|
+
|
17
|
+
#ifndef __GEN_H__
|
18
|
+
#define __GEN_H__
|
19
|
+
|
20
|
+
#include <ruby.h>
|
21
|
+
|
22
|
+
void rv_init_gen(void);
|
23
|
+
double ranf(void);
|
24
|
+
void rv_gen_set_seed(VALUE);
|
25
|
+
VALUE rv_gen_get_seed(void);
|
26
|
+
|
27
|
+
#endif /* __GEN_H__ */
|
data/lib/ext/randlib.c
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
#include "randlib.h"
|
2
|
+
#include "gen.h"
|
2
3
|
#include <stdio.h>
|
3
4
|
#include <math.h>
|
4
5
|
#include <stdlib.h>
|
@@ -1597,6 +1598,7 @@ extern long lennob(char *str);
|
|
1597
1598
|
#endif
|
1598
1599
|
}
|
1599
1600
|
|
1601
|
+
#if 0
|
1600
1602
|
double ranf(void)
|
1601
1603
|
/*
|
1602
1604
|
**********************************************************************
|
@@ -1622,6 +1624,7 @@ static double ranf;
|
|
1622
1624
|
ranf = ignlgi()*4.65661305739177E-10;
|
1623
1625
|
return ranf;
|
1624
1626
|
}
|
1627
|
+
#endif
|
1625
1628
|
|
1626
1629
|
void setgmn(double *meanv,double *covm,long p,double *parm)
|
1627
1630
|
/*
|
data/lib/ext/random_variable.c
CHANGED
@@ -1,21 +1,17 @@
|
|
1
1
|
////////////////////////////////////////////////////////////////////////////////
|
2
|
-
////////////////////////////////////////////////////////////////////////////////
|
3
2
|
// //
|
4
3
|
// File: random_variable.c //
|
5
4
|
// //
|
6
5
|
////////////////////////////////////////////////////////////////////////////////
|
7
|
-
////////////////////////////////////////////////////////////////////////////////
|
8
6
|
// //
|
9
7
|
// Author: Jorge F.M. Rinaldi //
|
10
8
|
// Contact: jorge.madronal.rinaldi@gmail.com //
|
11
9
|
// //
|
12
10
|
////////////////////////////////////////////////////////////////////////////////
|
13
|
-
////////////////////////////////////////////////////////////////////////////////
|
14
11
|
// //
|
15
12
|
// Date: 2012/10/11 //
|
16
13
|
// //
|
17
14
|
////////////////////////////////////////////////////////////////////////////////
|
18
|
-
////////////////////////////////////////////////////////////////////////////////
|
19
15
|
|
20
16
|
/*******************************************************************************
|
21
17
|
random_variable gem for the creation or random variables in Ruby
|
@@ -59,6 +55,7 @@
|
|
59
55
|
#error "No limits.h header found"
|
60
56
|
#endif /* HAVE_LIMITS_H */
|
61
57
|
|
58
|
+
#include "gen.h"
|
62
59
|
#include "randlib.h"
|
63
60
|
#include "xrandlib.h"
|
64
61
|
|
@@ -206,6 +203,7 @@ CREATE_RANDVAR_RB_OUTCOME(rayleigh, DBL2NUM)
|
|
206
203
|
/******************************************************************************/
|
207
204
|
/* class and module objects */
|
208
205
|
static VALUE rb_mRandomVariable;
|
206
|
+
static VALUE rb_mGenerator;
|
209
207
|
static VALUE rb_cRandomVariables[NR_RANDOM_VARIABLES];
|
210
208
|
/******************************************************************************/
|
211
209
|
|
@@ -579,6 +577,17 @@ VALUE rb_outcomes(VALUE rb_obj, VALUE rb_nr_times)
|
|
579
577
|
}
|
580
578
|
#undef GET_DATA
|
581
579
|
|
580
|
+
static VALUE rb_seed_get(VALUE self)
|
581
|
+
{
|
582
|
+
return rv_gen_get_seed();
|
583
|
+
}
|
584
|
+
|
585
|
+
static VALUE rb_seed_set(VALUE self, VALUE rb_seed)
|
586
|
+
{
|
587
|
+
rv_gen_set_seed(rb_seed);
|
588
|
+
return rb_seed;
|
589
|
+
}
|
590
|
+
|
582
591
|
/******************************************************************************/
|
583
592
|
/* macros for the extension entry point */
|
584
593
|
/******************************************************************************/
|
@@ -614,10 +623,17 @@ void Init_random_variable(void)
|
|
614
623
|
{
|
615
624
|
/* the RandomVariable module */
|
616
625
|
rb_mRandomVariable = rb_define_module("RandomVariable");
|
626
|
+
|
627
|
+
/* Generator */
|
628
|
+
rb_mGenerator = rb_define_module_under(rb_mRandomVariable, "Generator");
|
629
|
+
rb_define_singleton_method(rb_mGenerator, "seed", rb_seed_get, 0);
|
630
|
+
rb_define_singleton_method(rb_mGenerator, "seed=", rb_seed_set, 1);
|
631
|
+
|
617
632
|
/* Generic */
|
618
633
|
rb_cRandomVariables[rv_type_generic] =
|
619
634
|
rb_define_class_under(rb_mRandomVariable,
|
620
635
|
"Generic", rb_cObject);
|
636
|
+
/* random distributions */
|
621
637
|
CREATE_RANDOM_VARIABLE_CLASS("Bernoulli", bernoulli);
|
622
638
|
CREATE_RANDOM_VARIABLE_CLASS("Beta", beta);
|
623
639
|
CREATE_RANDOM_VARIABLE_CLASS("Binomial", binomial);
|
@@ -628,6 +644,9 @@ void Init_random_variable(void)
|
|
628
644
|
CREATE_RANDOM_VARIABLE_CLASS("Normal", normal);
|
629
645
|
CREATE_RANDOM_VARIABLE_CLASS("Poisson", poisson);
|
630
646
|
CREATE_RANDOM_VARIABLE_CLASS("Rayleigh", rayleigh);
|
647
|
+
|
648
|
+
/* initialize the random number generator */
|
649
|
+
rv_init_gen();
|
631
650
|
}
|
632
651
|
#undef CREATE_RANDOM_VARIABLE_CLASS
|
633
652
|
|
data/lib/ext/xrandlib.c
CHANGED
data/lib/random_variable.rb
CHANGED
@@ -34,6 +34,24 @@
|
|
34
34
|
require_relative 'ext/random_variable'
|
35
35
|
|
36
36
|
module RandomVariable
|
37
|
+
# obtain the current seed in use
|
38
|
+
#
|
39
|
+
# @return [Numeric] the current seed
|
40
|
+
def self.seed
|
41
|
+
Generator::seed
|
42
|
+
end
|
43
|
+
|
44
|
+
# set and reset the seed to a new seed
|
45
|
+
#
|
46
|
+
# @param [Numeric] new_seed new seed
|
47
|
+
# @return [Numeric] the new just set seed
|
48
|
+
def self.seed=(new_seed)
|
49
|
+
Generator::seed = new_seed
|
50
|
+
end
|
51
|
+
|
52
|
+
# obtain a list of the different available random variable class
|
53
|
+
# objects
|
54
|
+
# @return [Array] list of random variable class objects
|
37
55
|
def self.list
|
38
56
|
distros = []
|
39
57
|
self.constants.each do |c|
|
@@ -46,46 +64,44 @@ module RandomVariable
|
|
46
64
|
end
|
47
65
|
|
48
66
|
class Generic
|
49
|
-
|
67
|
+
klass = self
|
50
68
|
|
51
|
-
|
52
|
-
|
53
|
-
|
69
|
+
def initialize(&blk)
|
70
|
+
@blk = blk
|
71
|
+
end
|
54
72
|
|
55
73
|
|
56
|
-
|
74
|
+
operators = %w(+ - * / % **)
|
57
75
|
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
76
|
+
operators.each do |op|
|
77
|
+
define_method(op) do |arg|
|
78
|
+
if arg.is_a? klass then
|
79
|
+
return klass.new {
|
80
|
+
self.outcome.send(op,
|
81
|
+
arg.outcome)
|
82
|
+
}
|
83
|
+
end
|
84
|
+
klass.new { self.outcome.send(op, arg) }
|
64
85
|
end
|
65
|
-
klass.new {
|
66
|
-
self.outcome.send(operator, arg)
|
67
|
-
}
|
68
86
|
end
|
69
|
-
end
|
70
87
|
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
88
|
+
# obtain a single outcome from the random variable
|
89
|
+
def outcome
|
90
|
+
@blk.call
|
91
|
+
end
|
92
|
+
alias :sample :outcome
|
76
93
|
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
94
|
+
# obtain +number+ outcomes from the random variable
|
95
|
+
def outcomes(number)
|
96
|
+
ary = []
|
97
|
+
number.times do
|
98
|
+
ary << @blk.call
|
99
|
+
end
|
100
|
+
ary
|
82
101
|
end
|
83
|
-
|
84
|
-
end
|
85
|
-
alias :samples :outcomes
|
102
|
+
alias :samples :outcomes
|
86
103
|
|
87
|
-
|
88
|
-
end
|
104
|
+
end
|
89
105
|
end
|
90
106
|
|
91
107
|
require_relative 'distros.rb'
|
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.0
|
4
|
+
version: 1.1.0
|
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
|
+
date: 2012-12-02 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description: Create and operate with random variables in Ruby
|
15
15
|
email: jorge.madronal.rinaldi@gmail.com
|
@@ -24,6 +24,8 @@ files:
|
|
24
24
|
- LICENSE
|
25
25
|
- COPYING
|
26
26
|
- lib/ext/random_variable.c
|
27
|
+
- lib/ext/gen.c
|
28
|
+
- lib/ext/gen.h
|
27
29
|
- lib/ext/randlib.c
|
28
30
|
- lib/ext/randlib.h
|
29
31
|
- lib/ext/xrandlib.c
|