random_variable 0.0.1.pre
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/com.c +373 -0
- data/lib/ext/extconf.rb +7 -0
- data/lib/ext/linpack.c +91 -0
- data/lib/ext/randlib.c +2162 -0
- data/lib/ext/randlib.h +38 -0
- data/lib/ext/random_variable.c +414 -0
- data/lib/ext/xrandlib.c +28 -0
- data/lib/ext/xrandlib.h +22 -0
- data/lib/random_variable.rb +76 -0
- data/lib/test/test_poisson_rv.rb +33 -0
- data/lib/test.rb +8 -0
- metadata +57 -0
data/lib/ext/randlib.h
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
#ifndef __RANDLIB_H_
|
2
|
+
#define __RANDLIB_H_
|
3
|
+
|
4
|
+
/* Prototypes for all user accessible RANDLIB routines */
|
5
|
+
|
6
|
+
extern void advnst(long k);
|
7
|
+
extern double genbet(double aa,double bb);
|
8
|
+
extern double genchi(double df);
|
9
|
+
extern double genexp(double av);
|
10
|
+
extern double genf(double dfn, double dfd);
|
11
|
+
extern double gengam(double a,double r);
|
12
|
+
extern void genmn(double *parm,double *x,double *work);
|
13
|
+
extern void genmul(long n,double *p,long ncat,long *ix);
|
14
|
+
extern double gennch(double df,double xnonc);
|
15
|
+
extern double gennf(double dfn, double dfd, double xnonc);
|
16
|
+
extern double gennor(double av,double sd);
|
17
|
+
extern void genprm(long *iarray,int larray);
|
18
|
+
extern double genunf(double low,double high);
|
19
|
+
extern void getsd(long *iseed1,long *iseed2);
|
20
|
+
extern void gscgn(long getset,long *g);
|
21
|
+
extern long ignbin(long n,double pp);
|
22
|
+
extern long ignnbn(long n,double p);
|
23
|
+
extern long ignlgi(void);
|
24
|
+
extern long ignpoi(double mu);
|
25
|
+
extern long ignuin(long low,long high);
|
26
|
+
extern void initgn(long isdtyp);
|
27
|
+
extern long mltmod(long a,long s,long m);
|
28
|
+
extern void phrtsd(char* phrase,long* seed1,long* seed2);
|
29
|
+
extern double ranf(void);
|
30
|
+
extern void setall(long iseed1,long iseed2);
|
31
|
+
extern void setant(long qvalue);
|
32
|
+
extern void setgmn(double *meanv,double *covm,long p,double *parm);
|
33
|
+
extern void setsd(long iseed1,long iseed2);
|
34
|
+
extern double sexpo(void);
|
35
|
+
extern double sgamma(double a);
|
36
|
+
extern double snorm(void);
|
37
|
+
|
38
|
+
#endif /* __RANDLIB_H_ */
|
@@ -0,0 +1,414 @@
|
|
1
|
+
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
2
|
+
// //
|
3
|
+
// File: random_variable.c //
|
4
|
+
// //
|
5
|
+
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
6
|
+
// //
|
7
|
+
// Author: Jorge F.M. Rinaldi //
|
8
|
+
// Contact: jorge.madronal.rinaldi@gmail.com //
|
9
|
+
// //
|
10
|
+
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
11
|
+
// //
|
12
|
+
// Date: 2012/10/11 //
|
13
|
+
// //
|
14
|
+
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
15
|
+
|
16
|
+
#ifdef HAVE_RUBY_H
|
17
|
+
#include <ruby.h>
|
18
|
+
#else
|
19
|
+
#error "Need ruby.h header"
|
20
|
+
#endif /* HAVE_RUBY_H */
|
21
|
+
|
22
|
+
#ifdef HAVE_MATH_H
|
23
|
+
#include <math.h>
|
24
|
+
#else
|
25
|
+
#error "Need math.h header"
|
26
|
+
#endif /* HAVE_MATH_H */
|
27
|
+
|
28
|
+
#ifdef HAVE_LIMITS_H
|
29
|
+
#include <limits.h>
|
30
|
+
#else
|
31
|
+
#error "Need limits.h header"
|
32
|
+
#endif /* HAVE_LIMITS_H */
|
33
|
+
|
34
|
+
#include "randlib.h"
|
35
|
+
#include "xrandlib.h"
|
36
|
+
|
37
|
+
/******************************************************************************/
|
38
|
+
/* class objects */
|
39
|
+
/******************************************************************************/
|
40
|
+
static VALUE rb_cRandomVariable;
|
41
|
+
static VALUE rb_cBernoulliRV;
|
42
|
+
static VALUE rb_cPoissonRV;
|
43
|
+
static VALUE rb_cNormalRV;
|
44
|
+
static VALUE rb_cExponentialRV;
|
45
|
+
|
46
|
+
/******************************************************************************/
|
47
|
+
/* Common Functions */
|
48
|
+
/******************************************************************************/
|
49
|
+
#define CAST(f) ((VALUE (*)(void *))(f))
|
50
|
+
|
51
|
+
static inline void check_not_infinite(double x, const char *str)
|
52
|
+
{
|
53
|
+
if (isfinite(x))
|
54
|
+
return;
|
55
|
+
rb_raise(rb_eArgError, "%s", str);
|
56
|
+
}
|
57
|
+
|
58
|
+
static inline void check_not_nan(double x, const char *str)
|
59
|
+
{
|
60
|
+
if (isnan(x))
|
61
|
+
rb_raise(rb_eArgError, "%s", str);
|
62
|
+
}
|
63
|
+
|
64
|
+
|
65
|
+
static inline long get_times(VALUE times)
|
66
|
+
{
|
67
|
+
long nr_times;
|
68
|
+
Check_Type(times, T_FIXNUM);
|
69
|
+
if ( (nr_times = NUM2LONG(times)) < 0)
|
70
|
+
rb_raise(rb_eArgError, "negative number of outcomes");
|
71
|
+
return nr_times;
|
72
|
+
}
|
73
|
+
|
74
|
+
static inline VALUE
|
75
|
+
_RV_outcomes(void *rv, VALUE (*_RV_outcome)(void *rv), VALUE times)
|
76
|
+
{
|
77
|
+
long repeat;
|
78
|
+
VALUE outcomes_ary;
|
79
|
+
|
80
|
+
repeat = get_times(times);
|
81
|
+
|
82
|
+
outcomes_ary = rb_ary_new();
|
83
|
+
for (; repeat > 0; --repeat)
|
84
|
+
rb_ary_push(outcomes_ary, _RV_outcome(rv));
|
85
|
+
return outcomes_ary;
|
86
|
+
}
|
87
|
+
|
88
|
+
/******************************************************************************/
|
89
|
+
/* Bernoulli Random Variable */
|
90
|
+
/******************************************************************************/
|
91
|
+
typedef struct {
|
92
|
+
double p;
|
93
|
+
} bernoulli_rv_t;
|
94
|
+
|
95
|
+
bernoulli_rv_t *bernoulli_rv_create(double p)
|
96
|
+
{
|
97
|
+
bernoulli_rv_t *rv;
|
98
|
+
|
99
|
+
check_not_nan(p, "not a number (NaN) p parameter");
|
100
|
+
check_not_infinite(p, "infinite p parameter");
|
101
|
+
|
102
|
+
/* Check p parameter */
|
103
|
+
if (p <= 0.0 || p >= 1.0) {
|
104
|
+
rb_raise(rb_eArgError, "wrong parameter value (0 < p < 1)");
|
105
|
+
return NULL;
|
106
|
+
}
|
107
|
+
|
108
|
+
/* p parameter correct */
|
109
|
+
rv = ALLOC(bernoulli_rv_t);
|
110
|
+
rv->p = p;
|
111
|
+
return rv;
|
112
|
+
}
|
113
|
+
|
114
|
+
VALUE BernoulliRV_new(VALUE self, VALUE p)
|
115
|
+
{
|
116
|
+
bernoulli_rv_t *rv;
|
117
|
+
VALUE rb_obj;
|
118
|
+
|
119
|
+
rv = bernoulli_rv_create(NUM2DBL(p));
|
120
|
+
rb_obj = Data_Wrap_Struct(rb_cBernoulliRV, NULL, xfree, rv);
|
121
|
+
return rb_obj;
|
122
|
+
}
|
123
|
+
|
124
|
+
static inline bernoulli_rv_t *_BernoulliRV(VALUE BernoulliRV_obj)
|
125
|
+
{
|
126
|
+
bernoulli_rv_t *rv;
|
127
|
+
Data_Get_Struct(BernoulliRV_obj, bernoulli_rv_t, rv);
|
128
|
+
return rv;
|
129
|
+
}
|
130
|
+
|
131
|
+
VALUE BernoulliRV_p(VALUE self)
|
132
|
+
{
|
133
|
+
return rb_float_new(_BernoulliRV(self)->p);
|
134
|
+
}
|
135
|
+
|
136
|
+
static inline VALUE _BernoulliRV_outcome(bernoulli_rv_t *rv)
|
137
|
+
{
|
138
|
+
return INT2FIX(genbern(rv->p));
|
139
|
+
}
|
140
|
+
|
141
|
+
VALUE BernoulliRV_outcome(VALUE self)
|
142
|
+
{
|
143
|
+
return _BernoulliRV_outcome(_BernoulliRV(self));
|
144
|
+
}
|
145
|
+
|
146
|
+
VALUE BernoulliRV_outcomes(VALUE self, VALUE times)
|
147
|
+
{
|
148
|
+
bernoulli_rv_t *rv;
|
149
|
+
|
150
|
+
rv = _BernoulliRV(self);
|
151
|
+
return _RV_outcomes(rv, CAST(_BernoulliRV_outcome), times);
|
152
|
+
}
|
153
|
+
|
154
|
+
/******************************************************************************/
|
155
|
+
/* Poisson Random Variable */
|
156
|
+
/******************************************************************************/
|
157
|
+
typedef struct {
|
158
|
+
double lambda;
|
159
|
+
} poisson_rv_t;
|
160
|
+
|
161
|
+
#define POISSON_LAMBDA_MAX (LONG_MAX - (long)(0.00001*LONG_MAX))
|
162
|
+
|
163
|
+
static inline poisson_rv_t *poisson_rv_create(double lambda)
|
164
|
+
{
|
165
|
+
poisson_rv_t *rv;
|
166
|
+
|
167
|
+
check_not_nan(lambda, "not a number (NaN) lambda parameter");
|
168
|
+
check_not_infinite(lambda, "infinite lambda parameter");
|
169
|
+
|
170
|
+
/* Lambda has to be positive */
|
171
|
+
if (lambda <= 0.0) {
|
172
|
+
rb_raise(rb_eArgError, "non-positive lambda parameter");
|
173
|
+
return NULL;
|
174
|
+
}
|
175
|
+
|
176
|
+
/* Chech lambda parameter limits */
|
177
|
+
if (lambda > POISSON_LAMBDA_MAX) {
|
178
|
+
rb_raise(rb_eArgError, "too high lambda parameter");
|
179
|
+
return NULL;
|
180
|
+
}
|
181
|
+
|
182
|
+
/* Lambda parameter correct */
|
183
|
+
rv = ALLOC(poisson_rv_t);
|
184
|
+
rv->lambda = lambda;
|
185
|
+
return rv;
|
186
|
+
}
|
187
|
+
|
188
|
+
VALUE PoissonRV_new(VALUE self, VALUE lambda)
|
189
|
+
{
|
190
|
+
poisson_rv_t *rv;
|
191
|
+
VALUE obj;
|
192
|
+
|
193
|
+
rv = poisson_rv_create(NUM2DBL(lambda));
|
194
|
+
obj = Data_Wrap_Struct(rb_cPoissonRV, NULL, xfree, rv);
|
195
|
+
return obj;
|
196
|
+
}
|
197
|
+
|
198
|
+
static inline poisson_rv_t *_PoissonRV(VALUE PoissonRV_obj)
|
199
|
+
{
|
200
|
+
poisson_rv_t *rv;
|
201
|
+
Data_Get_Struct(PoissonRV_obj, poisson_rv_t, rv);
|
202
|
+
return rv;
|
203
|
+
}
|
204
|
+
|
205
|
+
VALUE PoissonRV_max_lambda(VALUE self)
|
206
|
+
{
|
207
|
+
return DBL2NUM(POISSON_LAMBDA_MAX);
|
208
|
+
}
|
209
|
+
|
210
|
+
VALUE PoissonRV_lambda(VALUE self)
|
211
|
+
{
|
212
|
+
return rb_float_new(_PoissonRV(self)->lambda);
|
213
|
+
}
|
214
|
+
|
215
|
+
static inline VALUE _PoissonRV_outcome(poisson_rv_t *rv)
|
216
|
+
{
|
217
|
+
return LONG2NUM(ignpoi(rv->lambda));
|
218
|
+
}
|
219
|
+
|
220
|
+
VALUE PoissonRV_outcome(VALUE self)
|
221
|
+
{
|
222
|
+
return _PoissonRV_outcome(_PoissonRV(self));
|
223
|
+
}
|
224
|
+
|
225
|
+
VALUE PoissonRV_outcomes(VALUE self, VALUE times)
|
226
|
+
{
|
227
|
+
poisson_rv_t *rv;
|
228
|
+
|
229
|
+
rv = _PoissonRV(self);
|
230
|
+
return _RV_outcomes(rv, CAST(_PoissonRV_outcome), times);
|
231
|
+
}
|
232
|
+
|
233
|
+
|
234
|
+
/******************************************************************************/
|
235
|
+
/* Normal Random Variable */
|
236
|
+
/******************************************************************************/
|
237
|
+
typedef struct {
|
238
|
+
double mu;
|
239
|
+
double sigma;
|
240
|
+
} normal_rv_t;
|
241
|
+
|
242
|
+
static inline normal_rv_t *normal_rv_create(double mu, double sigma)
|
243
|
+
{
|
244
|
+
normal_rv_t *rv;
|
245
|
+
|
246
|
+
if (sigma <= 0.0) {
|
247
|
+
rb_raise(rb_eArgError, "non-positive sigma parameter");
|
248
|
+
return NULL;
|
249
|
+
}
|
250
|
+
|
251
|
+
rv = ALLOC(normal_rv_t);
|
252
|
+
rv->mu = mu;
|
253
|
+
rv->sigma = sigma;
|
254
|
+
return rv;
|
255
|
+
}
|
256
|
+
|
257
|
+
VALUE NormalRV_new(VALUE self, VALUE mu, VALUE sigma)
|
258
|
+
{
|
259
|
+
normal_rv_t *rv;
|
260
|
+
VALUE rb_obj;
|
261
|
+
|
262
|
+
rv = normal_rv_create(NUM2DBL(mu), NUM2DBL(sigma));
|
263
|
+
rb_obj = Data_Wrap_Struct(rb_cNormalRV, NULL, xfree, rv);
|
264
|
+
return rb_obj;
|
265
|
+
}
|
266
|
+
|
267
|
+
static inline normal_rv_t *_NormalRV(VALUE NormalRV_obj)
|
268
|
+
{
|
269
|
+
normal_rv_t *rv;
|
270
|
+
Data_Get_Struct(NormalRV_obj, normal_rv_t, rv);
|
271
|
+
return rv;
|
272
|
+
}
|
273
|
+
|
274
|
+
VALUE NormalRV_mu(VALUE self)
|
275
|
+
{
|
276
|
+
normal_rv_t *rv;
|
277
|
+
return _NormalRV(self)->mu;
|
278
|
+
}
|
279
|
+
|
280
|
+
VALUE NormalRV_sigma(VALUE self)
|
281
|
+
{
|
282
|
+
normal_rv_t *rv;
|
283
|
+
return _NormalRV(self)->sigma;
|
284
|
+
}
|
285
|
+
|
286
|
+
static VALUE _NormalRV_outcome(normal_rv_t *rv)
|
287
|
+
{
|
288
|
+
return rb_float_new(
|
289
|
+
gennor(rv->mu, rv->sigma)
|
290
|
+
);
|
291
|
+
}
|
292
|
+
|
293
|
+
VALUE NormalRV_outcome(VALUE self)
|
294
|
+
{
|
295
|
+
return _NormalRV_outcome(_NormalRV(self));
|
296
|
+
}
|
297
|
+
|
298
|
+
VALUE NormalRV_outcomes(VALUE self, VALUE times)
|
299
|
+
{
|
300
|
+
normal_rv_t *rv;
|
301
|
+
rv = _NormalRV(self);
|
302
|
+
return _RV_outcomes(rv, CAST(_NormalRV_outcome), times);
|
303
|
+
}
|
304
|
+
|
305
|
+
|
306
|
+
/******************************************************************************/
|
307
|
+
/* Exponential Random Variable */
|
308
|
+
/******************************************************************************/
|
309
|
+
typedef struct {
|
310
|
+
double mean;
|
311
|
+
} exponential_rv_t;
|
312
|
+
|
313
|
+
static inline exponential_rv_t *exponential_rv_create(double lambda)
|
314
|
+
{
|
315
|
+
exponential_rv_t *rv;
|
316
|
+
|
317
|
+
if (lambda <= 0) {
|
318
|
+
rb_raise(rb_eArgError, "non-positive parameter value");
|
319
|
+
return NULL;
|
320
|
+
}
|
321
|
+
|
322
|
+
rv = ALLOC(exponential_rv_t);
|
323
|
+
|
324
|
+
rv->mean = 1.0 / lambda;
|
325
|
+
return rv;
|
326
|
+
}
|
327
|
+
|
328
|
+
VALUE ExponentialRV_new(VALUE self, VALUE lambda)
|
329
|
+
{
|
330
|
+
exponential_rv_t *rv;
|
331
|
+
VALUE ExponentialRV_obj;
|
332
|
+
|
333
|
+
rv = exponential_rv_create(NUM2DBL(lambda));
|
334
|
+
ExponentialRV_obj = Data_Wrap_Struct(rb_cExponentialRV, NULL, xfree, rv);
|
335
|
+
return ExponentialRV_obj;
|
336
|
+
}
|
337
|
+
|
338
|
+
static inline exponential_rv_t *_ExponentialRV(VALUE ExponentialRV_obj)
|
339
|
+
{
|
340
|
+
exponential_rv_t *rv;
|
341
|
+
Data_Get_Struct(ExponentialRV_obj, exponential_rv_t, rv);
|
342
|
+
return rv;
|
343
|
+
}
|
344
|
+
|
345
|
+
VALUE ExponentialRV_lambda(VALUE self)
|
346
|
+
{
|
347
|
+
return 1.0 / _ExponentialRV(self)->mean;
|
348
|
+
}
|
349
|
+
|
350
|
+
static inline VALUE _ExponentialRV_outcome(exponential_rv_t *rv)
|
351
|
+
{
|
352
|
+
return rb_float_new(genexp(rv->mean));
|
353
|
+
}
|
354
|
+
|
355
|
+
VALUE ExponentialRV_outcome(VALUE self)
|
356
|
+
{
|
357
|
+
return _ExponentialRV_outcome(_ExponentialRV(self));
|
358
|
+
}
|
359
|
+
|
360
|
+
VALUE ExponentialRV_outcomes(VALUE self, VALUE times)
|
361
|
+
{
|
362
|
+
exponential_rv_t *rv;
|
363
|
+
|
364
|
+
rv = _ExponentialRV(self);
|
365
|
+
return _RV_outcomes(rv, CAST(_ExponentialRV_outcome), times);
|
366
|
+
}
|
367
|
+
|
368
|
+
|
369
|
+
/******************************************************************************/
|
370
|
+
/* Extension Initialization */
|
371
|
+
/******************************************************************************/
|
372
|
+
void Init_random_variable(void)
|
373
|
+
{
|
374
|
+
/* RandomVariable */
|
375
|
+
rb_cRandomVariable = rb_define_class("RandomVariable", rb_cObject);
|
376
|
+
|
377
|
+
/* BernoulliRV */
|
378
|
+
rb_cBernoulliRV = rb_define_class("BernoulliRV", rb_cRandomVariable);
|
379
|
+
rb_define_singleton_method(rb_cBernoulliRV, "new", BernoulliRV_new, 1);
|
380
|
+
rb_define_method(rb_cBernoulliRV, "p", BernoulliRV_p, 0);
|
381
|
+
rb_define_method(rb_cBernoulliRV, "outcome", BernoulliRV_outcome, 0);
|
382
|
+
rb_define_method(rb_cBernoulliRV, "outcomes", BernoulliRV_outcomes, 1);
|
383
|
+
|
384
|
+
/* PoissonRV */
|
385
|
+
rb_cPoissonRV = rb_define_class("PoissonRV", rb_cRandomVariable);
|
386
|
+
rb_define_singleton_method(rb_cPoissonRV, "new", PoissonRV_new, 1);
|
387
|
+
rb_define_singleton_method(rb_cPoissonRV,
|
388
|
+
"lambda_max", PoissonRV_max_lambda, 0);
|
389
|
+
rb_define_method(rb_cPoissonRV, "lambda", PoissonRV_lambda, 0);
|
390
|
+
rb_define_method(rb_cPoissonRV, "mean", PoissonRV_lambda, 0);
|
391
|
+
rb_define_method(rb_cPoissonRV, "var", PoissonRV_lambda, 0);
|
392
|
+
rb_define_method(rb_cPoissonRV, "outcome", PoissonRV_outcome, 0);
|
393
|
+
rb_define_method(rb_cPoissonRV, "outcomes", PoissonRV_outcomes, 1);
|
394
|
+
|
395
|
+
/* NormalRV */
|
396
|
+
rb_cNormalRV = rb_define_class("NormalRV", rb_cRandomVariable);
|
397
|
+
rb_define_singleton_method(rb_cNormalRV, "new", NormalRV_new, 2);
|
398
|
+
rb_define_method(rb_cNormalRV, "outcome", NormalRV_outcome, 0);
|
399
|
+
rb_define_method(rb_cNormalRV, "outcomes", NormalRV_outcomes, 1);
|
400
|
+
|
401
|
+
/* ExponentialRV */
|
402
|
+
rb_cExponentialRV = rb_define_class("ExponentialRV",
|
403
|
+
rb_cRandomVariable);
|
404
|
+
rb_define_singleton_method(rb_cExponentialRV, "new",
|
405
|
+
ExponentialRV_new, 1);
|
406
|
+
rb_define_method(rb_cExponentialRV, "lambda", ExponentialRV_lambda, 0);
|
407
|
+
rb_define_method(rb_cExponentialRV, "rate", ExponentialRV_lambda, 0);
|
408
|
+
rb_define_method(rb_cExponentialRV, "outcome",
|
409
|
+
ExponentialRV_outcome, 0);
|
410
|
+
rb_define_method(rb_cExponentialRV, "outcomes",
|
411
|
+
ExponentialRV_outcomes, 1);
|
412
|
+
|
413
|
+
return;
|
414
|
+
}
|
data/lib/ext/xrandlib.c
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
////////////////////////////////////////////////////////////////////////////////
|
2
|
+
// //
|
3
|
+
// File: xrandlib.c //
|
4
|
+
// //
|
5
|
+
////////////////////////////////////////////////////////////////////////////////
|
6
|
+
// //
|
7
|
+
// Author: Jorge F.M. Rinaldi //
|
8
|
+
// Contact: jorge.madronal.rinaldi@gmail.com //
|
9
|
+
// //
|
10
|
+
////////////////////////////////////////////////////////////////////////////////
|
11
|
+
// //
|
12
|
+
// Date: 2012/10/12 //
|
13
|
+
// //
|
14
|
+
////////////////////////////////////////////////////////////////////////////////
|
15
|
+
|
16
|
+
|
17
|
+
#include "xrandlib.h"
|
18
|
+
#include "randlib.h"
|
19
|
+
|
20
|
+
int genbern(double p)
|
21
|
+
{
|
22
|
+
if (ranf() > p)
|
23
|
+
return 0;
|
24
|
+
return 1;
|
25
|
+
}
|
26
|
+
|
27
|
+
|
28
|
+
|
data/lib/ext/xrandlib.h
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
////////////////////////////////////////////////////////////////////////////////
|
2
|
+
// //
|
3
|
+
// File: xrandlib.h //
|
4
|
+
// //
|
5
|
+
////////////////////////////////////////////////////////////////////////////////
|
6
|
+
// //
|
7
|
+
// Author: Jorge F.M. Rinaldi //
|
8
|
+
// Contact: jorge.madronal.rinaldi@gmail.com //
|
9
|
+
// //
|
10
|
+
////////////////////////////////////////////////////////////////////////////////
|
11
|
+
// //
|
12
|
+
// Date: 2012/10/12 //
|
13
|
+
// //
|
14
|
+
////////////////////////////////////////////////////////////////////////////////
|
15
|
+
|
16
|
+
|
17
|
+
#ifndef _XRANDLIB_H_
|
18
|
+
#define _XRANDLIB_H_
|
19
|
+
|
20
|
+
extern int genbern(double p);
|
21
|
+
|
22
|
+
#endif /* _XRANDLIB_H_ */
|
@@ -0,0 +1,76 @@
|
|
1
|
+
################################################################################
|
2
|
+
# #
|
3
|
+
# File: random_variable.rb #
|
4
|
+
# #
|
5
|
+
################################################################################
|
6
|
+
# #
|
7
|
+
# Author: Jorge F.M. Rinaldi #
|
8
|
+
# Contact: jorge.madronal.rinaldi@gmail.com #
|
9
|
+
# #
|
10
|
+
################################################################################
|
11
|
+
# #
|
12
|
+
# Date: 2012/10/13 #
|
13
|
+
# #
|
14
|
+
################################################################################
|
15
|
+
|
16
|
+
|
17
|
+
require_relative 'ext/random_variable'
|
18
|
+
|
19
|
+
class RandomVariable
|
20
|
+
klass = self
|
21
|
+
|
22
|
+
def initialize(&blk)
|
23
|
+
@blk = blk
|
24
|
+
end
|
25
|
+
|
26
|
+
|
27
|
+
operators = %w(+ - * / % **)
|
28
|
+
|
29
|
+
operators.each do |operator|
|
30
|
+
define_method(operator) do |arg|
|
31
|
+
if arg.is_a? klass then
|
32
|
+
return klass.new {
|
33
|
+
self.outcome.send(operator, arg.outcome)
|
34
|
+
}
|
35
|
+
end
|
36
|
+
klass.new {
|
37
|
+
self.outcome.send(operator, arg)
|
38
|
+
}
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def outcome
|
43
|
+
@blk.call
|
44
|
+
end
|
45
|
+
|
46
|
+
def outcomes(times)
|
47
|
+
ary = []
|
48
|
+
times.times do
|
49
|
+
ary << @blk.call
|
50
|
+
end
|
51
|
+
ary
|
52
|
+
end
|
53
|
+
|
54
|
+
end
|
55
|
+
|
56
|
+
module Math
|
57
|
+
|
58
|
+
functions = [:acos, :acosh, :asin, :asinh, :atan, :atanh, :cbrt,
|
59
|
+
:cos, :cosh, :erf, :erfc, :exp, :frexp, :gamma,
|
60
|
+
:lgamma, :log, :log10, :log2, :sin, :sinh, :sqrt,
|
61
|
+
:tan, :tanh]
|
62
|
+
klass = RandomVariable
|
63
|
+
|
64
|
+
functions.each do |method_name|
|
65
|
+
original_method = self.method method_name
|
66
|
+
|
67
|
+
define_singleton_method(method_name) do |arg|
|
68
|
+
unless arg.is_a? klass then
|
69
|
+
return original_method.call arg
|
70
|
+
end
|
71
|
+
|
72
|
+
return klass.new { original_method.call(arg.outcome) }
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
class TestPoissonRV < Test::Unit::TestCase
|
2
|
+
def test_no_parameter
|
3
|
+
assert_raise(ArgumentError) { PoissonRV.new }
|
4
|
+
end
|
5
|
+
|
6
|
+
def test_infinite_parameter
|
7
|
+
assert_raise(ArgumentError) { PoissonRV.new(113.0 / 0) }
|
8
|
+
x = 2**65536
|
9
|
+
assert_raise(ArgumentError) { PoissonRV.new(x) }
|
10
|
+
end
|
11
|
+
|
12
|
+
def test_nan_parameter
|
13
|
+
assert_raise(ArgumentError) { PoissonRV.new(0.0 / 0) }
|
14
|
+
end
|
15
|
+
|
16
|
+
def test_invalid_finite_paramter
|
17
|
+
assert_raise(ArgumentError) { PoissonRV.new(-7) }
|
18
|
+
assert_raise(ArgumentError) { PoissonRV.new(-0.0000001) }
|
19
|
+
assert_raise(ArgumentError) { PoissonRV.new(0) }
|
20
|
+
lambda_param = 2 * PoissonRV.lambda_max
|
21
|
+
assert_raise(ArgumentError) { PoissonRV.new(lambda_param) }
|
22
|
+
|
23
|
+
end
|
24
|
+
|
25
|
+
def test_max_lambda_produces_good_results
|
26
|
+
lambda_max = PoissonRV.lambda_max
|
27
|
+
x = PoissonRV.new(lambda_max)
|
28
|
+
outcomes = x.outcomes(10_000_000)
|
29
|
+
outcomes.each do |e|
|
30
|
+
assert(e > 0)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
data/lib/test.rb
ADDED
metadata
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: random_variable
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1.pre
|
5
|
+
prerelease: 6
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Jorge Fco. Madronal Rinaldi
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-10-27 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: Bernoulli, Normal, Poisson, Exponential
|
15
|
+
email: jorge.madronal.rinaldi@gmail.com
|
16
|
+
executables: []
|
17
|
+
extensions:
|
18
|
+
- lib/ext/extconf.rb
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- lib/random_variable.rb
|
22
|
+
- lib/ext/extconf.rb
|
23
|
+
- lib/test.rb
|
24
|
+
- lib/ext/random_variable.c
|
25
|
+
- lib/ext/randlib.c
|
26
|
+
- lib/ext/randlib.h
|
27
|
+
- lib/ext/xrandlib.c
|
28
|
+
- lib/ext/xrandlib.h
|
29
|
+
- lib/ext/linpack.c
|
30
|
+
- lib/ext/com.c
|
31
|
+
- lib/test/test_poisson_rv.rb
|
32
|
+
homepage: http://mrinaldi.net/random_variable
|
33
|
+
licenses:
|
34
|
+
- GPLv3
|
35
|
+
post_install_message:
|
36
|
+
rdoc_options: []
|
37
|
+
require_paths:
|
38
|
+
- lib
|
39
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
40
|
+
none: false
|
41
|
+
requirements:
|
42
|
+
- - ! '>='
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
version: '0'
|
45
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
46
|
+
none: false
|
47
|
+
requirements:
|
48
|
+
- - ! '>'
|
49
|
+
- !ruby/object:Gem::Version
|
50
|
+
version: 1.3.1
|
51
|
+
requirements: []
|
52
|
+
rubyforge_project:
|
53
|
+
rubygems_version: 1.8.24
|
54
|
+
signing_key:
|
55
|
+
specification_version: 3
|
56
|
+
summary: For creating Random Variables in Ruby
|
57
|
+
test_files: []
|