random_variable 1.0.0 → 1.0.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.
data/lib/distros.rb ADDED
@@ -0,0 +1,96 @@
1
+ ################################################################################
2
+ # #
3
+ # File: distros.rb #
4
+ # #
5
+ ################################################################################
6
+ # #
7
+ # Author: Jorge F.M. Rinaldi #
8
+ # Contact: jorge.madronal.rinaldi@gmail.com #
9
+ # #
10
+ ################################################################################
11
+ # #
12
+ # Date: 2012/11/28 #
13
+ # #
14
+ ################################################################################
15
+
16
+ module RandomVariable
17
+ class Bernoulli
18
+ # create a new <i>Bernoulli Random Variable</i> with
19
+ # parameter +p+
20
+ def self.new(p)
21
+ intern_new(p)
22
+ end
23
+ end
24
+
25
+ class Beta
26
+ # create a new <i>Beta Random Variable</i> with parameters
27
+ # +alpha+ and +beta+
28
+ def self.new(alpha, beta)
29
+ intern_new(alpha, beta)
30
+ end
31
+ end
32
+
33
+ class Binomial
34
+ # create a new <i>Binomial Random Variable</i> with parameters
35
+ # +n+ and +p+
36
+ def self.new(n, p)
37
+ intern_new(n, p)
38
+ end
39
+ end
40
+
41
+ class ContinuousUniform
42
+ # create a new <i>Continuous Uniform Random Variable</i> with
43
+ # parameters +a+ and +b+
44
+ def self.new(a, b)
45
+ intern_new(a, b)
46
+ end
47
+ end
48
+
49
+ class DiscreteUniform
50
+ # create a new <i>Discrete Uniform Random Variable</i> with
51
+ # parameters +a+ and +b+
52
+ def self.new(a, b)
53
+ intern_new(a, b)
54
+ end
55
+ end
56
+
57
+ class Exponential
58
+ # create a new <i>Exponential Random Variable</i> with a mean of
59
+ # +mean+
60
+ def self.new(mean)
61
+ intern_new(mean)
62
+ end
63
+ end
64
+
65
+ class F
66
+ # create a new <i>F Random Variable</i> with parameters
67
+ # +d1+ and +d2+
68
+ def self.new(d1, d2)
69
+ intern_new(d1, d2)
70
+ end
71
+ end
72
+
73
+ class Normal
74
+ # create a new <i>Normal Random Variable</i> with parameters
75
+ # +mu+ and +sigma+
76
+ def self.new(mu = 0.0, sigma = 1.0)
77
+ intern_new(mu, sigma)
78
+ end
79
+ end
80
+
81
+ class Poisson
82
+ # create a new <i>Poisson Random Variable</i> with a mean of
83
+ # +mean+
84
+ def self.new(mean)
85
+ intern_new(mean)
86
+ end
87
+ end
88
+
89
+ class Rayleigh
90
+ # create a new <i>Rayleigh Random Variable</i> with parameter
91
+ # +sigma+
92
+ def self.new(sigma)
93
+ intern_new(sigma)
94
+ end
95
+ end
96
+ end
@@ -97,10 +97,10 @@ typedef struct {
97
97
  struct { long n; double p; } binomial;
98
98
  struct { double a,b; } continuous_uniform;
99
99
  struct { long a,b; } discrete_uniform;
100
- struct { double lambda; } exponential;
100
+ struct { double mean; } exponential;
101
101
  struct { double d1, d2; } f;
102
102
  struct { double mu, sigma; } normal;
103
- struct { double lambda; } poisson;
103
+ struct { double mean; } poisson;
104
104
  struct { double sigma; } rayleigh;
105
105
  } RANDVAR_DATA; /* union */
106
106
  } randvar_t;
@@ -177,8 +177,8 @@ CREATE_RANDVAR_OUTCOME_FUNC2(discrete_uniform, ignuin, long, a, b)
177
177
  CREATE_RANDVAR_RB_OUTCOME(discrete_uniform, LONG2NUM)
178
178
  /* exponential */
179
179
  RV_NR_PARAMS(exponential, 1)
180
- CREATE_RANDVAR_ACCESSOR(exponential, lambda, double)
181
- CREATE_RANDVAR_OUTCOME_FUNC1(exponential, gen_exponential , double, lambda)
180
+ CREATE_RANDVAR_ACCESSOR(exponential, mean, double)
181
+ CREATE_RANDVAR_OUTCOME_FUNC1(exponential, genexp , double, mean)
182
182
  CREATE_RANDVAR_RB_OUTCOME(exponential, DBL2NUM)
183
183
  /* f */
184
184
  RV_NR_PARAMS(f, 2)
@@ -194,8 +194,8 @@ CREATE_RANDVAR_OUTCOME_FUNC2(normal, gennor, double, mu, sigma)
194
194
  CREATE_RANDVAR_RB_OUTCOME(normal, DBL2NUM)
195
195
  /* poisson */
196
196
  RV_NR_PARAMS(poisson, 1)
197
- CREATE_RANDVAR_ACCESSOR(poisson,lambda, double)
198
- CREATE_RANDVAR_OUTCOME_FUNC1(poisson, ignpoi, long, lambda)
197
+ CREATE_RANDVAR_ACCESSOR(poisson, mean, double)
198
+ CREATE_RANDVAR_OUTCOME_FUNC1(poisson, ignpoi, long, mean)
199
199
  CREATE_RANDVAR_RB_OUTCOME(poisson, LONG2NUM)
200
200
  /* rayleigh */
201
201
  RV_NR_PARAMS(rayleigh, 1)
@@ -423,20 +423,20 @@ VALUE rb_create_instance(VALUE rb_obj, ...)
423
423
  CASE_END
424
424
 
425
425
  CASE(exponential)
426
- VALUE rb_lambda;
427
- double lambda;
426
+ VALUE rb_mean;
427
+ double mean;
428
428
 
429
429
  SET_KLASS(exponential);
430
430
 
431
- rb_lambda = GET_NEXT_ARG(ap);
432
- lambda = NUM2DBL(rb_lambda);
431
+ rb_mean = GET_NEXT_ARG(ap);
432
+ mean = NUM2DBL(rb_mean);
433
433
 
434
- /* lambda > 0 */
435
- CHECK_POSITIVE(lambda);
434
+ /* mean > 0 */
435
+ CHECK_POSITIVE(mean);
436
436
 
437
- /* lambda parameter correct */
437
+ /* mean parameter correct */
438
438
  RANDVAR_INIT(exponential);
439
- SET_PARAM(exponential, lambda);
439
+ SET_PARAM(exponential, mean);
440
440
  CASE_END
441
441
 
442
442
  CASE(f)
@@ -483,19 +483,19 @@ VALUE rb_create_instance(VALUE rb_obj, ...)
483
483
  CASE_END
484
484
 
485
485
  CASE(poisson)
486
- VALUE rb_lambda;
487
- double lambda;
486
+ VALUE rb_mean;
487
+ double mean;
488
488
 
489
489
  SET_KLASS(poisson);
490
490
 
491
- rb_lambda = GET_NEXT_ARG(ap);
492
- lambda = NUM2DBL(rb_lambda);
493
- /* lambda > 0 */
494
- CHECK_POSITIVE(lambda);
491
+ rb_mean = GET_NEXT_ARG(ap);
492
+ mean = NUM2DBL(rb_mean);
493
+ /* mean > 0 */
494
+ CHECK_POSITIVE(mean);;
495
495
 
496
- /* lambda parameter correct */
496
+ /* mean parameter correct */
497
497
  RANDVAR_INIT(poisson);
498
- SET_PARAM(poisson, lambda);
498
+ SET_PARAM(poisson, mean);;
499
499
  CASE_END
500
500
 
501
501
  CASE(rayleigh)
@@ -584,17 +584,25 @@ VALUE rb_outcomes(VALUE rb_obj, VALUE rb_nr_times)
584
584
  /******************************************************************************/
585
585
  #define CREATE_RANDOM_VARIABLE_CLASS(rb_name, name) \
586
586
  do { \
587
- VALUE *rb_objp = &rb_cRandomVariables[rv_type_ ##name]; \
587
+ VALUE *rb_objp; \
588
+ VALUE rb_metaclass; \
589
+ \
590
+ rb_objp = &rb_cRandomVariables[rv_type_ ##name]; \
588
591
  \
589
592
  *rb_objp = rb_define_class_under(rb_mRandomVariable, \
590
593
  rb_name, rb_cRandomVariables[rv_type_generic]); \
591
594
  \
592
- rb_define_singleton_method(*rb_objp, "new", \
595
+ rb_metaclass = rb_singleton_class(*rb_objp); \
596
+ \
597
+ rb_define_private_method(rb_metaclass, "intern_new", \
593
598
  (VALUE (*) (ANYARGS)) rb_create_instance, \
594
599
  rv_ ##name ##_nr_params); \
595
600
  \
596
601
  rb_define_method(*rb_objp, "outcome" , rb_outcome, 0); \
602
+ rb_define_alias(*rb_objp, "sample", "outcome"); \
603
+ \
597
604
  rb_define_method(*rb_objp, "outcomes", rb_outcomes, 1); \
605
+ rb_define_alias(*rb_objp, "samples", "outcomes"); \
598
606
  outcome_func[rv_type_ ##name] = \
599
607
  randvar_ ##name ##_rb_outcome; \
600
608
  } while (0)
data/lib/ext/xrandlib.c CHANGED
@@ -47,11 +47,6 @@ int gen_bernoulli(double p)
47
47
  return 1;
48
48
  }
49
49
 
50
- double gen_exponential(double lambda)
51
- {
52
- return genexp(1.0 / lambda);
53
- }
54
-
55
50
  /* Rayleigh */
56
51
  double gen_rayleigh(double sigma)
57
52
  {
@@ -68,22 +68,29 @@ module RandomVariable
68
68
  end
69
69
  end
70
70
 
71
+ # obtain a single outcome from the random variable
71
72
  def outcome
72
73
  @blk.call
73
74
  end
75
+ alias :sample :outcome
74
76
 
75
- def outcomes(times)
77
+ # obtain +number+ outcomes from the random variable
78
+ def outcomes(number)
76
79
  ary = []
77
- times.times do
80
+ number.times do
78
81
  ary << @blk.call
79
82
  end
80
83
  ary
81
84
  end
85
+ alias :samples :outcomes
82
86
 
83
87
 
84
88
  end
85
89
  end
86
90
 
91
+ require_relative 'distros.rb'
92
+
93
+ =begin
87
94
  module Math
88
95
 
89
96
  functions = [:acos, :acosh, :asin, :asinh, :atan, :atanh, :cbrt,
@@ -105,5 +112,6 @@ module Math
105
112
  end
106
113
 
107
114
  end
115
+ =end
108
116
 
109
117
 
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.0
4
+ version: 1.0.1
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-11-27 00:00:00.000000000 Z
12
+ date: 2012-11-28 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
@@ -20,9 +20,9 @@ extra_rdoc_files: []
20
20
  files:
21
21
  - lib/random_variable.rb
22
22
  - lib/ext/extconf.rb
23
+ - lib/distros.rb
23
24
  - LICENSE
24
25
  - COPYING
25
- - lib/test.rb
26
26
  - lib/ext/random_variable.c
27
27
  - lib/ext/randlib.c
28
28
  - lib/ext/randlib.h
@@ -30,7 +30,6 @@ files:
30
30
  - lib/ext/xrandlib.h
31
31
  - lib/ext/linpack.c
32
32
  - lib/ext/com.c
33
- - lib/test/test_poisson_rv.rb
34
33
  homepage: http://mrinaldi.net/random_variable
35
34
  licenses:
36
35
  - GPLv3
@@ -43,7 +42,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
43
42
  requirements:
44
43
  - - ! '>='
45
44
  - !ruby/object:Gem::Version
46
- version: '0'
45
+ version: 1.9.3
47
46
  required_rubygems_version: !ruby/object:Gem::Requirement
48
47
  none: false
49
48
  requirements:
@@ -1,48 +0,0 @@
1
- ################################################################################
2
- # #
3
- # File: test_poisson_rv.rb #
4
- # #
5
- ################################################################################
6
- # #
7
- # Author: Jorge F.M. Rinaldi #
8
- # Contact: jorge.madronal.rinaldi@gmail.com #
9
- # #
10
- ################################################################################
11
- # #
12
- # Date: 2012/10/27 #
13
- # #
14
- ################################################################################
15
-
16
- class TestPoissonRV < Test::Unit::TestCase
17
- def test_no_parameter
18
- assert_raise(ArgumentError) { PoissonRV.new }
19
- end
20
-
21
- def test_infinite_parameter
22
- assert_raise(ArgumentError) { PoissonRV.new(113.0 / 0) }
23
- x = 2**65536
24
- assert_raise(ArgumentError) { PoissonRV.new(x) }
25
- end
26
-
27
- def test_nan_parameter
28
- assert_raise(ArgumentError) { PoissonRV.new(0.0 / 0) }
29
- end
30
-
31
- def test_invalid_finite_paramter
32
- assert_raise(ArgumentError) { PoissonRV.new(-7) }
33
- assert_raise(ArgumentError) { PoissonRV.new(-0.0000001) }
34
- assert_raise(ArgumentError) { PoissonRV.new(0) }
35
- lambda_param = 2 * PoissonRV.lambda_max
36
- assert_raise(ArgumentError) { PoissonRV.new(lambda_param) }
37
-
38
- end
39
-
40
- def test_max_lambda_produces_good_results
41
- lambda_max = PoissonRV.lambda_max
42
- x = PoissonRV.new(lambda_max)
43
- outcomes = x.outcomes(10_000_000)
44
- outcomes.each do |e|
45
- assert(e > 0)
46
- end
47
- end
48
- end
data/lib/test.rb DELETED
@@ -1,9 +0,0 @@
1
- # whole project
2
- require 'random_variable'
3
-
4
- # gems for testing
5
- require 'test/unit'
6
-
7
- # unary tests...
8
- require 'test/test_poisson_rv'
9
- require 'test/test_rayleigh_rv'