random_variable 1.1.0 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/distros.rb CHANGED
@@ -38,6 +38,14 @@ module RandomVariable
38
38
  end
39
39
  end
40
40
 
41
+ class ChiSquared < Generic
42
+ # create a <i>Chi-Squared Random Variable</i> with +k+ degrees
43
+ # of freedom
44
+ def self.new(k)
45
+ intern_new(k)
46
+ end
47
+ end
48
+
41
49
  class ContinuousUniform < Generic
42
50
  # create a new <i>Continuous Uniform Random Variable</i> with
43
51
  # parameters +a+ and +b+
@@ -71,13 +79,21 @@ module RandomVariable
71
79
  end
72
80
 
73
81
  class Normal < Generic
74
- # create a new <i>Normal Random Variable</i> with parameters
75
- # +mu+ and +sigma+
82
+ # create a new <i>Normal (aka Gaussian) Random Variable</i>
83
+ # with parameters +mu+ and +sigma+
76
84
  def self.new(mu = 0.0, sigma = 1.0)
77
85
  intern_new(mu, sigma)
78
86
  end
79
87
  end
80
88
 
89
+ class Pareto < Generic
90
+ # create a new <i>Pareto Random Variable</i> where +a+ is the
91
+ # shape and +m+ is the location
92
+ def self.new(a, m = 1.0)
93
+ intern_new(a, m)
94
+ end
95
+ end
96
+
81
97
  class Poisson < Generic
82
98
  # create a new <i>Poisson Random Variable</i> with a mean of
83
99
  # +mean+
@@ -86,6 +102,13 @@ module RandomVariable
86
102
  end
87
103
  end
88
104
 
105
+ class Rademacher < Generic
106
+ # create a new <i>Rademacher Random Variable</i>
107
+ def self.new
108
+ intern_new
109
+ end
110
+ end
111
+
89
112
  class Rayleigh < Generic
90
113
  # create a new <i>Rayleigh Random Variable</i> with parameter
91
114
  # +sigma+
@@ -93,4 +116,11 @@ module RandomVariable
93
116
  intern_new(sigma)
94
117
  end
95
118
  end
119
+
120
+ class Rectangular < Generic
121
+ # create a <i>Rectangular Random Variable</i>
122
+ def self.new
123
+ intern_new
124
+ end
125
+ end
96
126
  end
data/lib/ext/randlib.c CHANGED
@@ -1060,6 +1060,11 @@ S170:
1060
1060
  }
1061
1061
 
1062
1062
  long ignnbn(long n,double p)
1063
+ /*
1064
+ return the number of failures
1065
+ n -> number of successful events
1066
+ p -> probability of event
1067
+ */
1063
1068
  /*
1064
1069
  **********************************************************************
1065
1070
 
@@ -68,13 +68,18 @@ typedef enum {
68
68
  rv_type_bernoulli,
69
69
  rv_type_beta,
70
70
  rv_type_binomial,
71
+ rv_type_chi_squared,
71
72
  rv_type_continuous_uniform,
72
73
  rv_type_discrete_uniform,
73
74
  rv_type_exponential,
74
75
  rv_type_f,
76
+ rv_type_negative_binomial,
75
77
  rv_type_normal,
78
+ rv_type_pareto,
76
79
  rv_type_poisson,
80
+ rv_type_rademacher,
77
81
  rv_type_rayleigh,
82
+ rv_type_rectangular,
78
83
 
79
84
  /* end of random variable types */
80
85
 
@@ -92,13 +97,18 @@ typedef struct {
92
97
  struct { double p; } bernoulli;
93
98
  struct { double alpha, beta; } beta;
94
99
  struct { long n; double p; } binomial;
100
+ struct { long k; } chi_squared;
95
101
  struct { double a,b; } continuous_uniform;
96
102
  struct { long a,b; } discrete_uniform;
97
103
  struct { double mean; } exponential;
98
104
  struct { double d1, d2; } f;
105
+ struct { long r; double p; } negative_binomial;
99
106
  struct { double mu, sigma; } normal;
107
+ struct { double a, m; } pareto;
100
108
  struct { double mean; } poisson;
109
+ struct { /* no params */ } rademacher;
101
110
  struct { double sigma; } rayleigh;
111
+ struct { /* no params */ } rectangular;
102
112
  } RANDVAR_DATA; /* union */
103
113
  } randvar_t;
104
114
  #define RANDVAR_ALLOC() ALLOC(randvar_t)
@@ -118,6 +128,13 @@ typedef struct {
118
128
  rv->RANDVAR_DATA . name . param = param; \
119
129
  }
120
130
 
131
+ #define CREATE_RANDVAR_OUTCOME_FUNC0(name, func, type) \
132
+ static inline type \
133
+ randvar_##name ##_ ##outcome(randvar_t *rv) \
134
+ { \
135
+ return func(); \
136
+ }
137
+
121
138
  #define CREATE_RANDVAR_OUTCOME_FUNC1(name, func, type, param) \
122
139
  static inline type \
123
140
  randvar_##name ##_ ##outcome(randvar_t *rv) \
@@ -160,6 +177,11 @@ CREATE_RANDVAR_ACCESSOR(binomial, n, long)
160
177
  CREATE_RANDVAR_ACCESSOR(binomial, p, double)
161
178
  CREATE_RANDVAR_OUTCOME_FUNC2(binomial, ignbin, long, n, p)
162
179
  CREATE_RANDVAR_RB_OUTCOME(binomial, LONG2NUM)
180
+ /* chi-squared */
181
+ RV_NR_PARAMS(chi_squared, 1)
182
+ CREATE_RANDVAR_ACCESSOR(chi_squared, k, long)
183
+ CREATE_RANDVAR_OUTCOME_FUNC1(chi_squared, gen_chi_squared, double, k)
184
+ CREATE_RANDVAR_RB_OUTCOME(chi_squared, DBL2NUM)
163
185
  /* continuous uniform */
164
186
  RV_NR_PARAMS(continuous_uniform, 2)
165
187
  CREATE_RANDVAR_ACCESSOR(continuous_uniform, a, double)
@@ -170,7 +192,7 @@ CREATE_RANDVAR_RB_OUTCOME(continuous_uniform, DBL2NUM)
170
192
  RV_NR_PARAMS(discrete_uniform, 2)
171
193
  CREATE_RANDVAR_ACCESSOR(discrete_uniform, a, long)
172
194
  CREATE_RANDVAR_ACCESSOR(discrete_uniform, b, long)
173
- CREATE_RANDVAR_OUTCOME_FUNC2(discrete_uniform, ignuin, long, a, b)
195
+ CREATE_RANDVAR_OUTCOME_FUNC2(discrete_uniform, gen_discrete_uniform, long, a, b)
174
196
  CREATE_RANDVAR_RB_OUTCOME(discrete_uniform, LONG2NUM)
175
197
  /* exponential */
176
198
  RV_NR_PARAMS(exponential, 1)
@@ -183,22 +205,42 @@ CREATE_RANDVAR_ACCESSOR(f, d1, double)
183
205
  CREATE_RANDVAR_ACCESSOR(f, d2, double)
184
206
  CREATE_RANDVAR_OUTCOME_FUNC2(f, genf, double, d1, d2)
185
207
  CREATE_RANDVAR_RB_OUTCOME(f, DBL2NUM)
208
+ /* negative binomial */
209
+ RV_NR_PARAMS(negative_binomial, 2)
210
+ CREATE_RANDVAR_ACCESSOR(negative_binomial, r, long)
211
+ CREATE_RANDVAR_ACCESSOR(negative_binomial, p, double)
212
+ CREATE_RANDVAR_OUTCOME_FUNC2(negative_binomial, ignnbn, long, r, p)
213
+ CREATE_RANDVAR_RB_OUTCOME(negative_binomial, LONG2NUM)
186
214
  /* normal */
187
215
  RV_NR_PARAMS(normal, 2)
188
216
  CREATE_RANDVAR_ACCESSOR(normal, mu, double)
189
217
  CREATE_RANDVAR_ACCESSOR(normal, sigma, double)
190
218
  CREATE_RANDVAR_OUTCOME_FUNC2(normal, gennor, double, mu, sigma)
191
219
  CREATE_RANDVAR_RB_OUTCOME(normal, DBL2NUM)
220
+ /* pareto */
221
+ RV_NR_PARAMS(pareto, 2)
222
+ CREATE_RANDVAR_ACCESSOR(pareto, a, double)
223
+ CREATE_RANDVAR_ACCESSOR(pareto, m, double)
224
+ CREATE_RANDVAR_OUTCOME_FUNC2(pareto, gen_pareto, double, a, m)
225
+ CREATE_RANDVAR_RB_OUTCOME(pareto, DBL2NUM)
192
226
  /* poisson */
193
227
  RV_NR_PARAMS(poisson, 1)
194
228
  CREATE_RANDVAR_ACCESSOR(poisson, mean, double)
195
229
  CREATE_RANDVAR_OUTCOME_FUNC1(poisson, ignpoi, long, mean)
196
230
  CREATE_RANDVAR_RB_OUTCOME(poisson, LONG2NUM)
231
+ /* rademacher */
232
+ RV_NR_PARAMS(rademacher, 0)
233
+ CREATE_RANDVAR_OUTCOME_FUNC0(rademacher, gen_rademacher, int)
234
+ CREATE_RANDVAR_RB_OUTCOME(rademacher, INT2FIX)
197
235
  /* rayleigh */
198
236
  RV_NR_PARAMS(rayleigh, 1)
199
237
  CREATE_RANDVAR_ACCESSOR(rayleigh, sigma, double)
200
238
  CREATE_RANDVAR_OUTCOME_FUNC1(rayleigh, gen_rayleigh, double, sigma)
201
239
  CREATE_RANDVAR_RB_OUTCOME(rayleigh, DBL2NUM)
240
+ /* rectangular */
241
+ RV_NR_PARAMS(rectangular, 0)
242
+ CREATE_RANDVAR_OUTCOME_FUNC0(rectangular, gen_rectangular, double)
243
+ CREATE_RANDVAR_RB_OUTCOME(rectangular, DBL2NUM)
202
244
 
203
245
  /******************************************************************************/
204
246
  /* class and module objects */
@@ -267,6 +309,15 @@ static type_t type(VALUE rb_obj)
267
309
  #x " parameter " \
268
310
  "is not a " \
269
311
  "probability"); \
312
+ } while (0)
313
+
314
+ #define CHECK_PROBABILITY_EXCL(x) \
315
+ do { \
316
+ if (x >= 1.0 || x <= 0.0) \
317
+ rb_raise(rb_eArgError, \
318
+ #x " parameter " \
319
+ "must be " \
320
+ "0 < " #x " < 1"); \
270
321
  } while (0)
271
322
 
272
323
  #define VAR_DECLARATIONS va_list ap; \
@@ -275,6 +326,15 @@ static type_t type(VALUE rb_obj)
275
326
  VALUE klass = 0;
276
327
  #define SET_KLASS(name) \
277
328
  (klass = rb_cRandomVariables[rv_type_ ##name])
329
+
330
+ #define ASSERT_KLASS_IS_SET \
331
+ do { \
332
+ if (0 == klass) \
333
+ rb_bug("forgot to set klass variable " \
334
+ "in %s()", __func__); \
335
+ } while (0)
336
+
337
+
278
338
  #define GET_NEXT_ARG(ap) va_arg((ap), VALUE)
279
339
  #define CREATE_WRAPPING(rv) Data_Wrap_Struct(klass, NULL, xfree, (rv))
280
340
 
@@ -294,6 +354,7 @@ static type_t type(VALUE rb_obj)
294
354
  {
295
355
 
296
356
  #define CASE_END \
357
+ ASSERT_KLASS_IS_SET; \
297
358
  break; \
298
359
  }
299
360
  /******************************************************************************/
@@ -375,6 +436,25 @@ VALUE rb_create_instance(VALUE rb_obj, ...)
375
436
  SET_PARAM(binomial, p);
376
437
  CASE_END
377
438
 
439
+ CASE(chi_squared)
440
+ VALUE rb_k;
441
+ long k;
442
+
443
+ SET_KLASS(chi_squared);
444
+
445
+ rb_k = GET_NEXT_ARG(ap);
446
+ CHECK_RB_INTEGER(rb_k, "k");
447
+
448
+ k = NUM2LONG(rb_k);
449
+
450
+ /* k > 0 */
451
+ CHECK_POSITIVE(k);
452
+
453
+ /* k parameter correct */
454
+ RANDVAR_INIT(chi_squared);
455
+ SET_PARAM(chi_squared, k);
456
+ CASE_END
457
+
378
458
  CASE(continuous_uniform)
379
459
  VALUE rb_a, rb_b;
380
460
  double a,b;
@@ -460,6 +540,34 @@ VALUE rb_create_instance(VALUE rb_obj, ...)
460
540
  SET_PARAM(f, d2);
461
541
  CASE_END
462
542
 
543
+ CASE(negative_binomial)
544
+ VALUE rb_r, rb_p;
545
+ long r;
546
+ double p;
547
+
548
+ SET_KLASS(negative_binomial);
549
+
550
+ rb_r = GET_NEXT_ARG(ap);
551
+ rb_p = GET_NEXT_ARG(ap);
552
+
553
+ CHECK_RB_INTEGER(rb_r, "r");
554
+
555
+ r = NUM2LONG(rb_r);
556
+ p = NUM2DBL(rb_p);
557
+
558
+ /* r > 0 */
559
+ CHECK_POSITIVE(r);
560
+ /* 0 < p < 0 */
561
+ CHECK_PROBABILITY_EXCL(p);
562
+
563
+ /* r and p parameters correct */
564
+ RANDVAR_INIT(negative_binomial);
565
+ SET_PARAM(negative_binomial, r);
566
+ SET_PARAM(negative_binomial, p);
567
+
568
+
569
+ CASE_END
570
+
463
571
  CASE(normal)
464
572
  VALUE rb_mu, rb_sigma;
465
573
  double mu, sigma;
@@ -478,6 +586,30 @@ VALUE rb_create_instance(VALUE rb_obj, ...)
478
586
  RANDVAR_INIT(normal);
479
587
  SET_PARAM(normal, mu);
480
588
  SET_PARAM(normal, sigma);
589
+ CASE_END
590
+
591
+ CASE(pareto)
592
+ VALUE rb_a, rb_m;
593
+ double a, m;
594
+
595
+ SET_KLASS(pareto);
596
+
597
+ rb_a = GET_NEXT_ARG(ap);
598
+ rb_m = GET_NEXT_ARG(ap);
599
+
600
+ a = NUM2DBL(rb_a);
601
+ m = NUM2DBL(rb_m);
602
+
603
+ /* a > 0 */
604
+ CHECK_POSITIVE(a);
605
+
606
+ /* m > 0 */
607
+ CHECK_POSITIVE(m);
608
+
609
+ /* a and m parameters correct */
610
+ RANDVAR_INIT(pareto);
611
+ SET_PARAM(pareto, a);
612
+ SET_PARAM(pareto, m);
481
613
  CASE_END
482
614
 
483
615
  CASE(poisson)
@@ -489,11 +621,17 @@ VALUE rb_create_instance(VALUE rb_obj, ...)
489
621
  rb_mean = GET_NEXT_ARG(ap);
490
622
  mean = NUM2DBL(rb_mean);
491
623
  /* mean > 0 */
492
- CHECK_POSITIVE(mean);;
624
+ CHECK_POSITIVE(mean);
493
625
 
494
626
  /* mean parameter correct */
495
627
  RANDVAR_INIT(poisson);
496
- SET_PARAM(poisson, mean);;
628
+ SET_PARAM(poisson, mean);
629
+ CASE_END
630
+
631
+ CASE(rademacher)
632
+ SET_KLASS(rademacher);
633
+
634
+ RANDVAR_INIT(rademacher);
497
635
  CASE_END
498
636
 
499
637
  CASE(rayleigh)
@@ -512,6 +650,12 @@ VALUE rb_create_instance(VALUE rb_obj, ...)
512
650
  SET_PARAM(rayleigh, sigma);
513
651
  CASE_END
514
652
 
653
+ CASE(rectangular)
654
+ SET_KLASS(rectangular);
655
+
656
+ RANDVAR_INIT(rectangular);
657
+ CASE_END
658
+
515
659
  default:
516
660
  rb_rv = Qnil;
517
661
 
@@ -607,11 +751,12 @@ static VALUE rb_seed_set(VALUE self, VALUE rb_seed)
607
751
  (VALUE (*) (ANYARGS)) rb_create_instance, \
608
752
  rv_ ##name ##_nr_params); \
609
753
  \
610
- rb_define_method(*rb_objp, "outcome" , rb_outcome, 0); \
611
- rb_define_alias(*rb_objp, "sample", "outcome"); \
754
+ rb_define_private_method(*rb_objp, \
755
+ "intern_outcome", rb_outcome, 0); \
756
+ \
757
+ rb_define_private_method(*rb_objp, \
758
+ "intern_outcomes", rb_outcomes, 1); \
612
759
  \
613
- rb_define_method(*rb_objp, "outcomes", rb_outcomes, 1); \
614
- rb_define_alias(*rb_objp, "samples", "outcomes"); \
615
760
  outcome_func[rv_type_ ##name] = \
616
761
  randvar_ ##name ##_rb_outcome; \
617
762
  } while (0)
@@ -637,13 +782,18 @@ void Init_random_variable(void)
637
782
  CREATE_RANDOM_VARIABLE_CLASS("Bernoulli", bernoulli);
638
783
  CREATE_RANDOM_VARIABLE_CLASS("Beta", beta);
639
784
  CREATE_RANDOM_VARIABLE_CLASS("Binomial", binomial);
785
+ CREATE_RANDOM_VARIABLE_CLASS("ChiSquared", chi_squared);
640
786
  CREATE_RANDOM_VARIABLE_CLASS("ContinuousUniform", continuous_uniform);
641
787
  CREATE_RANDOM_VARIABLE_CLASS("DiscreteUniform", discrete_uniform);
642
788
  CREATE_RANDOM_VARIABLE_CLASS("Exponential", exponential);
643
789
  CREATE_RANDOM_VARIABLE_CLASS("F", f);
790
+ CREATE_RANDOM_VARIABLE_CLASS("NegativeBinomial", negative_binomial);
644
791
  CREATE_RANDOM_VARIABLE_CLASS("Normal", normal);
792
+ CREATE_RANDOM_VARIABLE_CLASS("Pareto", pareto);
645
793
  CREATE_RANDOM_VARIABLE_CLASS("Poisson", poisson);
794
+ CREATE_RANDOM_VARIABLE_CLASS("Rademacher", rademacher);
646
795
  CREATE_RANDOM_VARIABLE_CLASS("Rayleigh", rayleigh);
796
+ CREATE_RANDOM_VARIABLE_CLASS("Rectangular", rectangular);
647
797
 
648
798
  /* initialize the random number generator */
649
799
  rv_init_gen();
data/lib/ext/xrandlib.c CHANGED
@@ -40,6 +40,19 @@
40
40
  #include "xrandlib.h"
41
41
  #include "randlib.h"
42
42
 
43
+ /* Arcsine */
44
+ double gen_arcsine(void)
45
+ {
46
+ return pow(sin(M_2_PI * ranf() ), 2);
47
+ }
48
+
49
+ /* Bates */
50
+ double gen_bates(long n)
51
+ {
52
+ return genirwinhall(n) / n;
53
+ }
54
+
55
+ /* Bernoulli */
43
56
  int gen_bernoulli(double p)
44
57
  {
45
58
  if (ranf() > p)
@@ -47,22 +60,35 @@ int gen_bernoulli(double p)
47
60
  return 1;
48
61
  }
49
62
 
50
- /* Rayleigh */
51
- double gen_rayleigh(double sigma)
52
- {
53
- return sigma * sqrt(-2 * log(1.0 - ranf()));
54
- }
55
-
56
- /* Arcsine */
57
- double gen_arcsine(void)
63
+ /* Chi-Squared */
64
+ double gen_chi_squared(long k)
58
65
  {
59
- return pow(sin(M_2_PI * ranf() ), 2);
66
+ return genchi((double) k);
60
67
  }
61
68
 
62
- /* Rectangular */
63
- double gen_rectangular(void)
69
+ /* Discrete Uniform */
70
+ long gen_discrete_uniform(long a, long b)
64
71
  {
65
- return (ranf() - 0.5);
72
+ /* assumptions:
73
+ 1) a != b
74
+ 2) a < b
75
+ */
76
+ double step;
77
+ long step_nr;
78
+ double y, y0;
79
+
80
+ step = 1.0 / (b - a + 1);
81
+ y = step;
82
+ y0 = ranf();
83
+ step_nr = 0;
84
+
85
+ do {
86
+ if (y0 <= y)
87
+ break;
88
+ y += step;
89
+ ++step_nr;
90
+ } while (1);
91
+ return a + step_nr;
66
92
  }
67
93
 
68
94
  /* Irwin-Hall */
@@ -75,10 +101,30 @@ double gen_irwin_hall(long n)
75
101
  return sum;
76
102
  }
77
103
 
78
- /* Bates */
79
- double gen_bates(long n)
104
+ /* Rademacher */
105
+ int gen_rademacher(void)
80
106
  {
81
- return genirwinhall(n) / n;
107
+ if (ranf() <= 0.5)
108
+ return -1;
109
+ return 1;
110
+ }
111
+
112
+ /* Rayleigh */
113
+ double gen_rayleigh(double sigma)
114
+ {
115
+ return sigma * sqrt(-2 * log(1.0 - ranf()));
116
+ }
117
+
118
+ /* Pareto */
119
+ double gen_pareto(double a, double m)
120
+ {
121
+ return m * pow(1 - ranf(), 1.0 / a);
122
+ }
123
+
124
+ /* Rectangular */
125
+ double gen_rectangular(void)
126
+ {
127
+ return (ranf() - 0.5);
82
128
  }
83
129
 
84
130
  /* Triangular */
data/lib/ext/xrandlib.h CHANGED
@@ -17,8 +17,13 @@
17
17
  #ifndef _XRANDLIB_H_
18
18
  #define _XRANDLIB_H_
19
19
 
20
- extern int gen_bernoulli(double);
21
- extern double gen_exponential(double);
22
- extern double gen_rayleigh(double);
20
+ extern int gen_bernoulli(double);
21
+ extern double gen_chi_squared(long);
22
+ extern long gen_discrete_uniform(long a, long b);
23
+ extern double gen_exponential(double);
24
+ extern double gen_pareto(double, double);
25
+ extern int gen_rademacher(void);
26
+ extern double gen_rayleigh(double);
27
+ extern double gen_rectangular(void);
23
28
 
24
29
  #endif /* _XRANDLIB_H_ */
@@ -32,6 +32,7 @@
32
32
  =end
33
33
 
34
34
  require_relative 'ext/random_variable'
35
+ require_relative 'sampleable'
35
36
 
36
37
  module RandomVariable
37
38
  # obtain the current seed in use
@@ -63,14 +64,34 @@ module RandomVariable
63
64
  distros
64
65
  end
65
66
 
66
- class Generic
67
+ class Generic
68
+ include Sampleable
69
+
67
70
  klass = self
68
71
 
69
72
  def initialize(&blk)
70
73
  @blk = blk
74
+
75
+ class << self
76
+ def outcome
77
+ @blk.call
78
+ end
79
+ alias :sample :outcome
80
+
81
+ def outcomes(nr_samples)
82
+ ary = []
83
+ nr_samples.times do
84
+ ary << @blk.call
85
+ end
86
+ class << ary
87
+ include RandomVariable::Samples
88
+ end
89
+ ary
90
+ end
91
+ alias :samples :outcomes
92
+ end
71
93
  end
72
94
 
73
-
74
95
  operators = %w(+ - * / % **)
75
96
 
76
97
  operators.each do |op|
@@ -85,22 +106,6 @@ module RandomVariable
85
106
  end
86
107
  end
87
108
 
88
- # obtain a single outcome from the random variable
89
- def outcome
90
- @blk.call
91
- end
92
- alias :sample :outcome
93
-
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
101
- end
102
- alias :samples :outcomes
103
-
104
109
  end
105
110
  end
106
111
 
data/lib/sampleable.rb ADDED
@@ -0,0 +1,32 @@
1
+ ################################################################################
2
+ # #
3
+ # File: sampleable.rb #
4
+ # #
5
+ ################################################################################
6
+ # #
7
+ # Author: Jorge F.M. Rinaldi #
8
+ # Contact: jorge.madronal.rinaldi@gmail.com #
9
+ # #
10
+ ################################################################################
11
+ # #
12
+ # Date: 2012/12/02 #
13
+ # #
14
+ ################################################################################
15
+
16
+ require_relative 'samples'
17
+
18
+ module RandomVariable::Sampleable
19
+ def outcome
20
+ intern_outcome
21
+ end
22
+ alias :sample :outcome
23
+
24
+ def outcomes(nr_samples)
25
+ samples_ary = intern_outcomes(nr_samples)
26
+ class << samples_ary
27
+ include RandomVariable::Samples
28
+ end
29
+ samples_ary
30
+ end
31
+ alias :samples :outcomes
32
+ end
data/lib/samples.rb ADDED
@@ -0,0 +1,61 @@
1
+ ################################################################################
2
+ # #
3
+ # File: samples.rb #
4
+ # #
5
+ ################################################################################
6
+ # #
7
+ # Author: Jorge F.M. Rinaldi #
8
+ # Contact: jorge.madronal.rinaldi@gmail.com #
9
+ # #
10
+ ################################################################################
11
+ # #
12
+ # Date: 2012/12/02 #
13
+ # #
14
+ ################################################################################
15
+
16
+ module RandomVariable::Samples
17
+ def iterate_samples
18
+ self.each_with_index do |sample, i|
19
+ if block_given? then
20
+ yield(sample, i)
21
+ end
22
+ end
23
+ end
24
+
25
+ def max
26
+ cur_max = self[0]
27
+ self.iterate_samples do |sample|
28
+ cur_max = sample if sample > cur_max
29
+ end
30
+ cur_max
31
+ end
32
+
33
+ def min
34
+ cur_min = self[0]
35
+ self.iterate_samples do |sample|
36
+ cur_min = sample if sample < cur_min
37
+ end
38
+ cur_min
39
+ end
40
+
41
+ def mean
42
+ return nil if 0 == self.size
43
+ acc = 0.0
44
+ self.iterate_samples do |sample|
45
+ acc += sample
46
+ end
47
+ acc / self.size
48
+ end
49
+
50
+ def median
51
+ return nil if 0 == self.size
52
+ return self[0] if 1 == self.size
53
+ ary = self.sort
54
+ i = ary.len / 2
55
+ if ary.len.even? then
56
+ return (ary[i] + ary[i-1]) / 2.0
57
+ else
58
+ return ary[i]
59
+ end
60
+ end
61
+ 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.1.0
4
+ version: 1.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,9 +9,10 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-12-02 00:00:00.000000000 Z
12
+ date: 2012-12-15 00:00:00.000000000 Z
13
13
  dependencies: []
14
- description: Create and operate with random variables in Ruby
14
+ description: Probability distributions such as the Binomial, Beta, Chi-Squared, Normal,
15
+ Pareto, Poisson, etc.
15
16
  email: jorge.madronal.rinaldi@gmail.com
16
17
  executables: []
17
18
  extensions:
@@ -20,6 +21,8 @@ extra_rdoc_files: []
20
21
  files:
21
22
  - lib/random_variable.rb
22
23
  - lib/ext/extconf.rb
24
+ - lib/samples.rb
25
+ - lib/sampleable.rb
23
26
  - lib/distros.rb
24
27
  - LICENSE
25
28
  - COPYING
@@ -56,5 +59,5 @@ rubyforge_project:
56
59
  rubygems_version: 1.8.24
57
60
  signing_key:
58
61
  specification_version: 3
59
- summary: Random Variables in Ruby
62
+ summary: Random Variables in Ruby for a wide variety of probability distributions
60
63
  test_files: []