random_variable 0.0.2.pre → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -59,13 +59,18 @@
59
59
  /******************************************************************************/
60
60
  /* class objects */
61
61
  /******************************************************************************/
62
- static VALUE rb_cRandomVariable;
62
+ static VALUE rb_mRandomVariable;
63
+ static VALUE rb_cGenericRV;
63
64
  static VALUE rb_cBernoulliRV;
65
+ static VALUE rb_cBinomialRV;
64
66
  static VALUE rb_cPoissonRV;
65
67
  static VALUE rb_cNormalRV;
66
68
  static VALUE rb_cExponentialRV;
67
69
  static VALUE rb_cRayleighRV;
68
-
70
+ static VALUE rb_cContinuousUniformRV;
71
+ static VALUE rb_cDiscreteUniformRV;
72
+ static VALUE rb_cBetaRV;
73
+ static VALUE rb_cFRV;
69
74
  /******************************************************************************/
70
75
  /* Common Functions */
71
76
  /******************************************************************************/
@@ -77,7 +82,8 @@ static inline void check_not_nan(double x, const char *str)
77
82
  rb_raise(rb_eArgError, "%s", str);
78
83
  }
79
84
 
80
- #define CHECK_NOT_NAN(x) check_not_nan((x),"not a number " #x " parameter")
85
+ #define CHECK_NOT_NAN(x) check_not_nan((x), \
86
+ "not a number (NaN) " #x " parameter")
81
87
 
82
88
  static inline void check_not_infinite(double x, const char *str)
83
89
  {
@@ -88,14 +94,81 @@ static inline void check_not_infinite(double x, const char *str)
88
94
 
89
95
  #define CHECK_NOT_INFINITE(x) check_not_infinite((x),"infinite " #x " paramter")
90
96
 
91
- static inline void check_positive(double x, const char *str)
97
+
98
+ static inline void dbl_check_positive(double x, const char *str)
92
99
  {
93
100
  if (x > 0.0)
94
101
  return;
95
102
  rb_raise(rb_eArgError, "%s", str);
96
103
  }
97
104
 
98
- #define CHECK_POSITIVE(x) check_positive((x), "non-positive " #x " parameter")
105
+ #define DBL_CHECK_POSITIVE(x) dbl_check_positive((x), \
106
+ "non-positive " #x " parameter")
107
+
108
+
109
+ static inline void long_check_positive(long x, const char *str)
110
+ {
111
+ if (x > 0)
112
+ return;
113
+ rb_raise(rb_eArgError, "%s", str);
114
+ }
115
+
116
+ #define LONG_CHECK_POSITIVE(x) long_check_positive((x), \
117
+ "non-positive " #x " parameter")
118
+
119
+ static inline void dbl_check_non_neg(double x, const char *str)
120
+ {
121
+ if (x >= 0.0)
122
+ return;
123
+ rb_raise(rb_eArgError, "%s", str);
124
+ }
125
+
126
+ #define DBL_CHECK_NON_NEG(x) long_check_positive((x), \
127
+ "negative " #x " parameter")
128
+
129
+ static inline void long_check_non_neg(long x, const char *str)
130
+ {
131
+ if (x >= 0)
132
+ return;
133
+ rb_raise(rb_eArgError, "%s", str);
134
+ }
135
+
136
+ #define LONG_CHECK_NON_NEG(x) long_check_non_neg((x), \
137
+ "negative " #x " parameter")
138
+
139
+ static inline int
140
+ dbl_check_cinterval(double x, double min, double max,
141
+ const char *lower, const char *greater)
142
+ {
143
+ if (x < min)
144
+ rb_raise(rb_eArgError, "%s", lower);
145
+ else if (x > max)
146
+ rb_raise(rb_eArgError, "%s", greater);
147
+ return;
148
+ }
149
+
150
+ #define DBL_CHECK_CINTERVAL(x,min,max) \
151
+ (dbl_check_cinterval((x), (min), (max), \
152
+ #x " parameter out of bounds (" #x " < " #min ")", \
153
+ #x " parameter out of bounds (" #x " > " #max ")"))
154
+
155
+ #define CHECK_PROBABILITY(x) DBL_CHECK_CINTERVAL(x,0.0,1.0)
156
+
157
+ static inline int
158
+ dbl_check_ointerval(double x, double min, double max,
159
+ const char *lower, const char *greater)
160
+ {
161
+ if (x <= min)
162
+ rb_raise(rb_eArgError, "%s", lower);
163
+ else if (x >= max)
164
+ rb_raise(rb_eArgError, "%s", greater);
165
+ return;
166
+ }
167
+
168
+ #define DBL_CHECK_OINTERVAL(x,min,max) \
169
+ (dbl_check_ointerval((x), (min), (max), \
170
+ #x " parameter out of bounds (" #x " <= " #min ")", \
171
+ #x " parameter out of bounds (" #x " >= " #max ")"))
99
172
 
100
173
  static inline long get_times(VALUE times)
101
174
  {
@@ -186,6 +259,78 @@ VALUE BernoulliRV_outcomes(VALUE self, VALUE times)
186
259
  return _RV_outcomes(rv, CAST(_BernoulliRV_outcome), times);
187
260
  }
188
261
 
262
+ /******************************************************************************/
263
+ /* Binomial Random Variable */
264
+ /******************************************************************************/
265
+ typedef struct {
266
+ long n;
267
+ double p;
268
+ } binomial_rv_t;
269
+
270
+ binomial_rv_t *binomial_rv_create(long n, double p)
271
+ {
272
+ binomial_rv_t *rv;
273
+
274
+ LONG_CHECK_NON_NEG(n);
275
+
276
+
277
+ CHECK_NOT_NAN(p);
278
+ CHECK_NOT_INFINITE(p);
279
+ CHECK_PROBABILITY(p);
280
+
281
+ /* n & p parameters correct */
282
+ rv = ALLOC(binomial_rv_t);
283
+ rv->n = n;
284
+ rv->p = p;
285
+ return rv;
286
+ }
287
+
288
+ VALUE BinomialRV_new(VALUE self, VALUE n, VALUE p)
289
+ {
290
+ binomial_rv_t *rv;
291
+ VALUE rb_obj;
292
+
293
+ Check_Type(n, T_FIXNUM);
294
+ rv = binomial_rv_create(NUM2LONG(n), NUM2DBL(p));
295
+ rb_obj = Data_Wrap_Struct(rb_cBinomialRV, NULL, xfree, rv);
296
+ return rb_obj;
297
+ }
298
+
299
+ static inline binomial_rv_t *_BinomialRV(VALUE BinomialRV_obj)
300
+ {
301
+ binomial_rv_t *rv;
302
+ Data_Get_Struct(BinomialRV_obj, binomial_rv_t, rv);
303
+ return rv;
304
+ }
305
+
306
+ VALUE BinomialRV_n(VALUE self)
307
+ {
308
+ return LONG2NUM(_BinomialRV(self)->n);
309
+ }
310
+
311
+ VALUE BinomialRV_p(VALUE self)
312
+ {
313
+ return rb_float_new(_BinomialRV(self)->p);
314
+ }
315
+
316
+ static inline VALUE _BinomialRV_outcome(binomial_rv_t *rv)
317
+ {
318
+ return INT2FIX(ignbin(rv->n, rv->p));
319
+ }
320
+
321
+ VALUE BinomialRV_outcome(VALUE self)
322
+ {
323
+ return _BinomialRV_outcome(_BinomialRV(self));
324
+ }
325
+
326
+ VALUE BinomialRV_outcomes(VALUE self, VALUE times)
327
+ {
328
+ binomial_rv_t *rv;
329
+
330
+ rv = _BinomialRV(self);
331
+ return _RV_outcomes(rv, CAST(_BinomialRV_outcome), times);
332
+ }
333
+
189
334
  /******************************************************************************/
190
335
  /* Poisson Random Variable */
191
336
  /******************************************************************************/
@@ -193,6 +338,7 @@ typedef struct {
193
338
  double lambda;
194
339
  } poisson_rv_t;
195
340
 
341
+ /* avoid overflow */
196
342
  #define POISSON_LAMBDA_MAX (LONG_MAX - (long)(0.00001*LONG_MAX))
197
343
 
198
344
  static inline poisson_rv_t *poisson_rv_create(double lambda)
@@ -477,23 +623,350 @@ VALUE RayleighRV_outcomes(VALUE self, VALUE times)
477
623
  return _RV_outcomes(rv, CAST(_RayleighRV_outcome), times);
478
624
  }
479
625
 
626
+ /******************************************************************************/
627
+ /* Continuous Uniform Random Variable */
628
+ /******************************************************************************/
629
+ typedef struct {
630
+ double a;
631
+ double b;
632
+ } continuous_uniform_rv_t;
633
+
634
+ static inline
635
+ continuous_uniform_rv_t *continuous_uniform_rv_create(double a, double b)
636
+ {
637
+ continuous_uniform_rv_t *rv;
638
+
639
+ check_not_nan(a, "not a number (NaN) a parameter");
640
+ check_not_infinite(a, "infinite a parameter");
641
+
642
+ check_not_nan(b, "not a number (NaN) b parameter");
643
+ check_not_infinite(b, "infinite b parameter");
644
+
645
+ if (a >= b) {
646
+ rb_raise(rb_eArgError, "a < b");
647
+ return NULL;
648
+ }
649
+
650
+ rv = ALLOC(continuous_uniform_rv_t);
651
+
652
+ rv->a = a;
653
+ rv->b = b;
654
+ return rv;
655
+ }
656
+
657
+ VALUE ContinuousUniformRV_new(VALUE self, VALUE a, VALUE b)
658
+ {
659
+ continuous_uniform_rv_t *rv;
660
+ VALUE ContinuousUniformRV_obj;
661
+
662
+ rv = continuous_uniform_rv_create(NUM2DBL(a), NUM2DBL(b));
663
+ ContinuousUniformRV_obj =
664
+ Data_Wrap_Struct(rb_cContinuousUniformRV, NULL, xfree, rv);
665
+ return ContinuousUniformRV_obj;
666
+ }
667
+
668
+ static inline
669
+ continuous_uniform_rv_t *_ContinuousUniformRV(VALUE ContinuousUniformRV_obj)
670
+ {
671
+ continuous_uniform_rv_t *rv;
672
+ Data_Get_Struct(ContinuousUniformRV_obj, continuous_uniform_rv_t, rv);
673
+ return rv;
674
+ }
675
+
676
+ VALUE ContinuousUniformRV_a(VALUE self)
677
+ {
678
+ return DBL2NUM(_ContinuousUniformRV(self)->a);
679
+ }
680
+
681
+ VALUE ContinuousUniformRV_b(VALUE self)
682
+ {
683
+ return DBL2NUM(_ContinuousUniformRV(self)->b);
684
+ }
685
+
686
+ static inline VALUE _ContinuousUniformRV_outcome(continuous_uniform_rv_t *rv)
687
+ {
688
+ return rb_float_new(genunf(rv->a, rv->b));
689
+ }
690
+
691
+ VALUE ContinuousUniformRV_outcome(VALUE self)
692
+ {
693
+ return _ContinuousUniformRV_outcome(_ContinuousUniformRV(self));
694
+ }
695
+
696
+ VALUE ContinuousUniformRV_outcomes(VALUE self, VALUE times)
697
+ {
698
+ continuous_uniform_rv_t *rv;
699
+
700
+ rv = _ContinuousUniformRV(self);
701
+ return _RV_outcomes(rv, CAST(_ContinuousUniformRV_outcome), times);
702
+ }
703
+
704
+ /******************************************************************************/
705
+ /* Discrete Uniform Random Variable */
706
+ /******************************************************************************/
707
+ typedef struct {
708
+ long a;
709
+ long b;
710
+ } discrete_uniform_rv_t;
711
+
712
+ static inline
713
+ discrete_uniform_rv_t *discrete_uniform_rv_create(long a, long b)
714
+ {
715
+ discrete_uniform_rv_t *rv;
716
+
717
+ if (a >= b) {
718
+ rb_raise(rb_eArgError, "not a < b");
719
+ return NULL;
720
+ }
721
+
722
+ rv = ALLOC(discrete_uniform_rv_t);
723
+
724
+ rv->a = a;
725
+ rv->b = b;
726
+ return rv;
727
+ }
728
+
729
+ VALUE DiscreteUniformRV_new(VALUE self, VALUE a, VALUE b)
730
+ {
731
+ discrete_uniform_rv_t *rv;
732
+ VALUE DiscreteUniformRV_obj;
733
+
734
+ /* Check both parameters are integers only */
735
+ VALUE a_is_integer, b_is_integer;
736
+ a_is_integer = rb_obj_is_kind_of(a, rb_cInteger);
737
+ b_is_integer = rb_obj_is_kind_of(b, rb_cInteger);
738
+
739
+ if (!a_is_integer && b_is_integer)
740
+ rb_raise(rb_eArgError, "not integer a parameter");
741
+ else if (a_is_integer && !b_is_integer)
742
+ rb_raise(rb_eArgError, "not integer b parameter");
743
+ else if (!a_is_integer && !b_is_integer)
744
+ rb_raise(rb_eArgError, "not integer a and b parameters");
745
+
746
+
747
+ rv = discrete_uniform_rv_create(NUM2LONG(a), NUM2LONG(b));
748
+ DiscreteUniformRV_obj =
749
+ Data_Wrap_Struct(rb_cDiscreteUniformRV, NULL, xfree, rv);
750
+ return DiscreteUniformRV_obj;
751
+ }
752
+
753
+ static inline
754
+ discrete_uniform_rv_t *_DiscreteUniformRV(VALUE DiscreteUniformRV_obj)
755
+ {
756
+ discrete_uniform_rv_t *rv;
757
+ Data_Get_Struct(DiscreteUniformRV_obj, discrete_uniform_rv_t, rv);
758
+ return rv;
759
+ }
760
+
761
+ VALUE DiscreteUniformRV_a(VALUE self)
762
+ {
763
+ return LONG2NUM(_DiscreteUniformRV(self)->a);
764
+ }
765
+
766
+ VALUE DiscreteUniformRV_b(VALUE self)
767
+ {
768
+ return LONG2NUM(_DiscreteUniformRV(self)->b);
769
+ }
770
+
771
+ static inline VALUE _DiscreteUniformRV_outcome(discrete_uniform_rv_t *rv)
772
+ {
773
+ return rb_float_new(genunf(rv->a, rv->b));
774
+ }
775
+
776
+ VALUE DiscreteUniformRV_outcome(VALUE self)
777
+ {
778
+ return _DiscreteUniformRV_outcome(_DiscreteUniformRV(self));
779
+ }
780
+
781
+ VALUE DiscreteUniformRV_outcomes(VALUE self, VALUE times)
782
+ {
783
+ discrete_uniform_rv_t *rv;
784
+
785
+ rv = _DiscreteUniformRV(self);
786
+ return _RV_outcomes(rv, CAST(_DiscreteUniformRV_outcome), times);
787
+ }
788
+
789
+ /******************************************************************************/
790
+ /* Beta Random Variable */
791
+ /******************************************************************************/
792
+ typedef struct {
793
+ double alpha;
794
+ double beta;
795
+ } beta_rv_t;
796
+
797
+ static inline
798
+ beta_rv_t *beta_rv_create(double alpha, double beta)
799
+ {
800
+ beta_rv_t *rv;
801
+
802
+ CHECK_NOT_NAN(alpha);
803
+ CHECK_NOT_INFINITE(alpha);
804
+
805
+ CHECK_NOT_NAN(beta);
806
+ CHECK_NOT_INFINITE(beta);
807
+
808
+ DBL_CHECK_POSITIVE(alpha);
809
+ DBL_CHECK_POSITIVE(beta);
810
+
811
+ rv = ALLOC(beta_rv_t);
812
+
813
+ rv->alpha = alpha;
814
+ rv->beta = beta;
815
+ return rv;
816
+ }
817
+
818
+ VALUE BetaRV_new(VALUE self, VALUE alpha, VALUE beta)
819
+ {
820
+ beta_rv_t *rv;
821
+ VALUE BetaRV_obj;
822
+
823
+ rv = beta_rv_create(NUM2DBL(alpha), NUM2DBL(beta));
824
+ BetaRV_obj =
825
+ Data_Wrap_Struct(rb_cBetaRV, NULL, xfree, rv);
826
+ return BetaRV_obj;
827
+ }
828
+
829
+ static inline
830
+ beta_rv_t *_BetaRV(VALUE BetaRV_obj)
831
+ {
832
+ beta_rv_t *rv;
833
+ Data_Get_Struct(BetaRV_obj, beta_rv_t, rv);
834
+ return rv;
835
+ }
836
+
837
+ VALUE BetaRV_alpha(VALUE self)
838
+ {
839
+ return DBL2NUM(_BetaRV(self)->alpha);
840
+ }
841
+
842
+ VALUE BetaRV_beta(VALUE self)
843
+ {
844
+ return DBL2NUM(_BetaRV(self)->beta);
845
+ }
846
+
847
+ static inline VALUE _BetaRV_outcome(beta_rv_t *rv)
848
+ {
849
+ return rb_float_new(genbet(rv->alpha, rv->beta));
850
+ }
851
+
852
+ VALUE BetaRV_outcome(VALUE self)
853
+ {
854
+ return _BetaRV_outcome(_BetaRV(self));
855
+ }
856
+
857
+ VALUE BetaRV_outcomes(VALUE self, VALUE times)
858
+ {
859
+ beta_rv_t *rv;
860
+
861
+ rv = _BetaRV(self);
862
+ return _RV_outcomes(rv, CAST(_BetaRV_outcome), times);
863
+ }
864
+
865
+ /******************************************************************************/
866
+ /* F Random Variable */
867
+ /******************************************************************************/
868
+ typedef struct {
869
+ double d1;
870
+ double d2;
871
+ } f_rv_t;
872
+
873
+ static inline
874
+ f_rv_t *f_rv_create(double d1, double d2)
875
+ {
876
+ f_rv_t *rv;
877
+
878
+ CHECK_NOT_NAN(d1);
879
+ CHECK_NOT_INFINITE(d1);
880
+
881
+ CHECK_NOT_NAN(d2);
882
+ CHECK_NOT_INFINITE(d2);
883
+
884
+ DBL_CHECK_POSITIVE(d1);
885
+ DBL_CHECK_POSITIVE(d2);
886
+
887
+ rv = ALLOC(f_rv_t);
888
+
889
+ rv->d1 = d1;
890
+ rv->d2 = d2;
891
+ return rv;
892
+ }
893
+
894
+ VALUE FRV_new(VALUE self, VALUE d1, VALUE d2)
895
+ {
896
+ f_rv_t *rv;
897
+ VALUE FRV_obj;
898
+
899
+ rv = f_rv_create(NUM2DBL(d1), NUM2DBL(d2));
900
+ FRV_obj =
901
+ Data_Wrap_Struct(rb_cFRV, NULL, xfree, rv);
902
+ return FRV_obj;
903
+ }
904
+
905
+ static inline
906
+ f_rv_t *_FRV(VALUE FRV_obj)
907
+ {
908
+ f_rv_t *rv;
909
+ Data_Get_Struct(FRV_obj, f_rv_t, rv);
910
+ return rv;
911
+ }
912
+
913
+ VALUE FRV_d1(VALUE self)
914
+ {
915
+ return DBL2NUM(_FRV(self)->d1);
916
+ }
917
+
918
+ VALUE FRV_d2(VALUE self)
919
+ {
920
+ return DBL2NUM(_FRV(self)->d2);
921
+ }
922
+
923
+ static inline VALUE _FRV_outcome(f_rv_t *rv)
924
+ {
925
+ return rb_float_new(genf(rv->d1, rv->d2));
926
+ }
927
+
928
+ VALUE FRV_outcome(VALUE self)
929
+ {
930
+ return _FRV_outcome(_FRV(self));
931
+ }
932
+
933
+ VALUE FRV_outcomes(VALUE self, VALUE times)
934
+ {
935
+ f_rv_t *rv;
936
+
937
+ rv = _FRV(self);
938
+ return _RV_outcomes(rv, CAST(_FRV_outcome), times);
939
+ }
940
+
480
941
  /******************************************************************************/
481
942
  /* Extension Initialization */
482
943
  /******************************************************************************/
483
944
  void Init_random_variable(void)
484
945
  {
485
946
  /* RandomVariable */
486
- rb_cRandomVariable = rb_define_class("RandomVariable", rb_cObject);
947
+ rb_mRandomVariable = rb_define_module("RandomVariable");
487
948
 
488
- /* BernoulliRV */
489
- rb_cBernoulliRV = rb_define_class("BernoulliRV", rb_cRandomVariable);
949
+ /* Generic */
950
+ rb_cGenericRV = rb_define_class_under(rb_mRandomVariable, "Generic",
951
+ rb_cObject);
952
+
953
+ /* Bernoulli */
954
+ rb_cBernoulliRV = rb_define_class_under(rb_mRandomVariable,"Bernoulli", rb_cGenericRV);
490
955
  rb_define_singleton_method(rb_cBernoulliRV, "new", BernoulliRV_new, 1);
491
956
  rb_define_method(rb_cBernoulliRV, "p", BernoulliRV_p, 0);
492
957
  rb_define_method(rb_cBernoulliRV, "outcome", BernoulliRV_outcome, 0);
493
958
  rb_define_method(rb_cBernoulliRV, "outcomes", BernoulliRV_outcomes, 1);
494
959
 
495
- /* PoissonRV */
496
- rb_cPoissonRV = rb_define_class("PoissonRV", rb_cRandomVariable);
960
+ /* Binomial */
961
+ rb_cBinomialRV = rb_define_class_under(rb_mRandomVariable,"Binomial", rb_cGenericRV);
962
+ rb_define_singleton_method(rb_cBinomialRV, "new", BinomialRV_new, 2);
963
+ rb_define_method(rb_cBinomialRV, "n", BinomialRV_n, 0);
964
+ rb_define_method(rb_cBinomialRV, "p", BinomialRV_p, 0);
965
+ rb_define_method(rb_cBinomialRV, "outcome", BinomialRV_outcome, 0);
966
+ rb_define_method(rb_cBinomialRV, "outcomes", BinomialRV_outcomes, 1);
967
+
968
+ /* Poisson */
969
+ rb_cPoissonRV = rb_define_class_under(rb_mRandomVariable,"Poisson", rb_cGenericRV);
497
970
  rb_define_singleton_method(rb_cPoissonRV, "new", PoissonRV_new, 1);
498
971
  rb_define_singleton_method(rb_cPoissonRV,
499
972
  "lambda_max", PoissonRV_max_lambda, 0);
@@ -503,15 +976,14 @@ void Init_random_variable(void)
503
976
  rb_define_method(rb_cPoissonRV, "outcome", PoissonRV_outcome, 0);
504
977
  rb_define_method(rb_cPoissonRV, "outcomes", PoissonRV_outcomes, 1);
505
978
 
506
- /* NormalRV */
507
- rb_cNormalRV = rb_define_class("NormalRV", rb_cRandomVariable);
979
+ /* Normal */
980
+ rb_cNormalRV = rb_define_class_under(rb_mRandomVariable,"Normal", rb_cGenericRV);
508
981
  rb_define_singleton_method(rb_cNormalRV, "new", NormalRV_new, 2);
509
982
  rb_define_method(rb_cNormalRV, "outcome", NormalRV_outcome, 0);
510
983
  rb_define_method(rb_cNormalRV, "outcomes", NormalRV_outcomes, 1);
511
984
 
512
- /* ExponentialRV */
513
- rb_cExponentialRV = rb_define_class("ExponentialRV",
514
- rb_cRandomVariable);
985
+ /* Exponential */
986
+ rb_cExponentialRV = rb_define_class_under(rb_mRandomVariable,"Exponential", rb_cGenericRV);
515
987
  rb_define_singleton_method(rb_cExponentialRV, "new",
516
988
  ExponentialRV_new, 1);
517
989
  rb_define_method(rb_cExponentialRV, "lambda", ExponentialRV_lambda, 0);
@@ -522,7 +994,7 @@ void Init_random_variable(void)
522
994
  ExponentialRV_outcomes, 1);
523
995
 
524
996
  /* Rayleigh */
525
- rb_cRayleighRV = rb_define_class("Rayleigh", rb_cRandomVariable);
997
+ rb_cRayleighRV = rb_define_class_under(rb_mRandomVariable,"Rayleigh", rb_cGenericRV);
526
998
  rb_define_singleton_method(rb_cRayleighRV, "new",
527
999
  RayleighRV_new, 1);
528
1000
  rb_define_method(rb_cRayleighRV, "sigma", RayleighRV_sigma, 0);
@@ -530,6 +1002,62 @@ void Init_random_variable(void)
530
1002
  RayleighRV_outcome, 0);
531
1003
  rb_define_method(rb_cRayleighRV, "outcomes",
532
1004
  RayleighRV_outcomes, 1);
1005
+
1006
+ /* Continuous Uniform */
1007
+ rb_cContinuousUniformRV =
1008
+ rb_define_class_under(rb_mRandomVariable,"ContinuousUniform", rb_cGenericRV);
1009
+ rb_define_singleton_method(rb_cContinuousUniformRV, "new",
1010
+ ContinuousUniformRV_new, 2);
1011
+ rb_define_method(rb_cContinuousUniformRV,
1012
+ "a", ContinuousUniformRV_a, 0);
1013
+ rb_define_method(rb_cContinuousUniformRV,
1014
+ "b", ContinuousUniformRV_b, 0);
1015
+ rb_define_method(rb_cContinuousUniformRV, "outcome",
1016
+ ContinuousUniformRV_outcome, 0);
1017
+ rb_define_method(rb_cContinuousUniformRV, "outcomes",
1018
+ ContinuousUniformRV_outcomes, 1);
1019
+
1020
+ /* Discrete Uniform */
1021
+ rb_cDiscreteUniformRV =
1022
+ rb_define_class_under(rb_mRandomVariable,"DiscreteUniform", rb_cGenericRV);
1023
+ rb_define_singleton_method(rb_cDiscreteUniformRV, "new",
1024
+ DiscreteUniformRV_new, 2);
1025
+ rb_define_method(rb_cDiscreteUniformRV,
1026
+ "a", DiscreteUniformRV_a, 0);
1027
+ rb_define_method(rb_cDiscreteUniformRV,
1028
+ "b", DiscreteUniformRV_b, 0);
1029
+ rb_define_method(rb_cDiscreteUniformRV, "outcome",
1030
+ DiscreteUniformRV_outcome, 0);
1031
+ rb_define_method(rb_cDiscreteUniformRV, "outcomes",
1032
+ DiscreteUniformRV_outcomes, 1);
1033
+
1034
+ /* Beta */
1035
+ rb_cBetaRV =
1036
+ rb_define_class_under(rb_mRandomVariable,"Beta", rb_cGenericRV);
1037
+ rb_define_singleton_method(rb_cBetaRV, "new",
1038
+ BetaRV_new, 2);
1039
+ rb_define_method(rb_cBetaRV,
1040
+ "alpha", BetaRV_alpha, 0);
1041
+ rb_define_method(rb_cBetaRV,
1042
+ "beta", BetaRV_beta, 0);
1043
+ rb_define_method(rb_cBetaRV, "outcome",
1044
+ BetaRV_outcome, 0);
1045
+ rb_define_method(rb_cBetaRV, "outcomes",
1046
+ BetaRV_outcomes, 1);
1047
+
1048
+ /* F */
1049
+ rb_cFRV =
1050
+ rb_define_class_under(rb_mRandomVariable,"F", rb_cGenericRV);
1051
+ rb_define_singleton_method(rb_cFRV, "new",
1052
+ FRV_new, 2);
1053
+ rb_define_method(rb_cFRV,
1054
+ "d1", FRV_d1, 0);
1055
+ rb_define_method(rb_cFRV,
1056
+ "d2", FRV_d2, 0);
1057
+ rb_define_method(rb_cFRV, "outcome",
1058
+ FRV_outcome, 0);
1059
+ rb_define_method(rb_cFRV, "outcomes",
1060
+ FRV_outcomes, 1);
533
1061
 
534
1062
  return;
535
1063
  }
data/lib/ext/xrandlib.c CHANGED
@@ -54,4 +54,47 @@ double genray(double sigma)
54
54
  return sqrt( -2 * sigma * sigma * log(1.0 - ranf()) );
55
55
  }
56
56
 
57
+ /* Arcsine */
58
+ double gen_arcsine(void)
59
+ {
60
+ return pow(sin(M_2_PI * ranf() ), 2);
61
+ }
62
+
63
+ /* Rectangular */
64
+ double gen_rectangular(void)
65
+ {
66
+ return (ranf() - 0.5);
67
+ }
68
+
69
+ /* Irwin-Hall */
70
+ double gen_irwin_hall(long n)
71
+ {
72
+ long i;
73
+ double sum;
74
+ for (i = 0, sum = 0; i < n; i++)
75
+ sum += ranf();
76
+ return sum;
77
+ }
78
+
79
+ /* Bates */
80
+ double gen_bates(long n)
81
+ {
82
+ return genirwinhall(n) / n;
83
+ }
84
+
85
+ /* Triangular */
86
+ double gen_triangular(double a, double b, double c)
87
+ {
88
+ double y = ranf();
89
+ if (y <= (c-a) / (b-a) )
90
+ return a + sqrt(y*(b-a)*(c-a));
91
+ return b - sqrt((1-y)*(b-a)*(b-c));
92
+ }
93
+
94
+ /* Truncated Normal */
95
+ double gen_truncated_normal(double mu, double sigma, double a, double b)
96
+ {
97
+ return 0.0;
98
+ }
99
+
57
100
 
@@ -33,7 +33,19 @@
33
33
 
34
34
  require_relative 'ext/random_variable'
35
35
 
36
- class RandomVariable
36
+ module RandomVariable
37
+ def self.list
38
+ distros = []
39
+ self.constants.each do |c|
40
+ c = self.const_get(c)
41
+ if c.is_a? Class and c != self::Generic then
42
+ distros << c
43
+ end
44
+ end
45
+ distros
46
+ end
47
+
48
+ class Generic
37
49
  klass = self
38
50
 
39
51
  def initialize(&blk)
@@ -67,7 +79,9 @@ class RandomVariable
67
79
  end
68
80
  ary
69
81
  end
82
+
70
83
 
84
+ end
71
85
  end
72
86
 
73
87
  module Math
data/lib/test.rb CHANGED
@@ -6,3 +6,4 @@ require 'test/unit'
6
6
 
7
7
  # unary tests...
8
8
  require 'test/test_poisson_rv'
9
+ require 'test/test_rayleigh_rv'
metadata CHANGED
@@ -1,17 +1,39 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: random_variable
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2.pre
5
- prerelease: 6
4
+ version: 0.1.0
5
+ prerelease:
6
6
  platform: ruby
7
7
  authors:
8
8
  - Jorge Fco. Madronal Rinaldi
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-10-27 00:00:00.000000000 Z
12
+ date: 2012-11-17 00:00:00.000000000 Z
13
13
  dependencies: []
14
- description: Bernoulli, Normal, Poisson, Exponential
14
+ description: ! 'Create and operate with Random Variables in Ruby
15
+
16
+ To date, the supported distributions are:
17
+
18
+ Bernoulli
19
+
20
+ Beta
21
+
22
+ Binomial
23
+
24
+ Continous Uniform
25
+
26
+ Discrete Uniform
27
+
28
+ Exponential
29
+
30
+ F
31
+
32
+ Normal
33
+
34
+ Poisson
35
+
36
+ Rayleigh'
15
37
  email: jorge.madronal.rinaldi@gmail.com
16
38
  executables: []
17
39
  extensions:
@@ -47,13 +69,13 @@ required_ruby_version: !ruby/object:Gem::Requirement
47
69
  required_rubygems_version: !ruby/object:Gem::Requirement
48
70
  none: false
49
71
  requirements:
50
- - - ! '>'
72
+ - - ! '>='
51
73
  - !ruby/object:Gem::Version
52
- version: 1.3.1
74
+ version: '0'
53
75
  requirements: []
54
76
  rubyforge_project:
55
77
  rubygems_version: 1.8.24
56
78
  signing_key:
57
79
  specification_version: 3
58
- summary: For creating Random Variables in Ruby
80
+ summary: Random Variables in Ruby
59
81
  test_files: []