@@ -17,8 +17,8 @@ jStat.extend( jStat.beta, {
17
17
return jStat . incompleteBeta ( x , alpha , beta ) ;
18
18
} ,
19
19
20
- inv : function ( p , alpha , beta ) {
21
- return jStat . incompleteBetaInv ( p , alpha , beta ) ;
20
+ inv : function ( x , alpha , beta ) {
21
+ return jStat . incompleteBetaInv ( x , alpha , beta ) ;
22
22
} ,
23
23
24
24
mean : function ( alpha , beta ) {
@@ -33,26 +33,27 @@ jStat.extend( jStat.beta, {
33
33
return ( alpha * beta ) / ( Math . pow ( alpha + beta , 2 ) * ( alpha + beta + 1 ) ) ;
34
34
} ,
35
35
36
- sample : function ( x , alpha , beta ) {
37
- if ( x ) {
38
- // return a jstat object filled with random samples
39
- return x . alter ( function ( ) {
40
- var u = jStat . randg ( alpha ) ;
41
- return u / ( u + jStat . randg ( beta ) ) ;
42
- } ) ;
43
- } else {
44
- // return a random sample
45
- var u = jStat . randg ( alpha ) ;
46
- return u / ( u + jStat . randg ( beta ) ) ;
47
- }
36
+ // return a random sample
37
+ sample : function ( alpha , beta ) {
38
+ var u = jStat . randg ( alpha ) ;
39
+ return u / ( u + jStat . randg ( beta ) ) ;
48
40
} ,
49
41
50
42
variance : function ( alpha , beta ) {
51
43
return ( alpha * beta ) / ( Math . pow ( alpha + beta , 2 ) * ( alpha + beta + 1 ) ) ;
52
44
}
53
45
} ) ;
54
46
55
- // extend the beta objects prototype
47
+ // extend the beta distribution prototype
48
+ jStat . beta . prototype . sample = function ( arr ) {
49
+ if ( arr ) {
50
+ return jStat . alter ( arr , function ( ) {
51
+ return jStat . beta . sample ( this . alpha , this . beta ) ;
52
+ } ) ;
53
+ } else {
54
+ return jStat . beta . sample ( this . alpha , this . beta ) ;
55
+ } ;
56
+ } ;
56
57
( function ( vals ) {
57
58
for ( var item in vals ) ( function ( item ) {
58
59
jStat . beta . prototype [ item ] = function ( x ) {
@@ -103,22 +104,25 @@ jStat.extend( jStat.cauchy, {
103
104
return local ;
104
105
} ,
105
106
106
- sample : function ( x , local , scale ) {
107
- if ( x ) {
108
- return x . alter ( function ( ) {
109
- return jStat . randn ( ) * Math . sqrt ( 1 / ( 2 * jStat . randg ( 0.5 ) ) ) * scale + local ;
110
- } ) ;
111
- } else {
112
- return jStat . randn ( ) * Math . sqrt ( 1 / ( 2 * jStat . randg ( 0.5 ) ) ) * scale + local ;
113
- }
107
+ sample : function ( local , scale ) {
108
+ return jStat . randn ( ) * Math . sqrt ( 1 / ( 2 * jStat . randg ( 0.5 ) ) ) * scale + local ;
114
109
} ,
115
110
116
111
variance : function ( local , scale ) {
117
112
// TODO: implement this
118
113
}
119
114
} ) ;
120
115
121
- // extend the cauchy objects prototype
116
+ // extend the cauchy distribution prototype
117
+ jStat . cauchy . prototype . sample = function ( arr ) {
118
+ if ( arr ) {
119
+ return jStat . alter ( arr , function ( ) {
120
+ return jStat . cauchy . sample ( this . local , this . scale ) ;
121
+ } ) ;
122
+ } else {
123
+ return jStat . cauchy . sample ( this . local , this . scale ) ;
124
+ } ;
125
+ } ;
122
126
( function ( vals ) {
123
127
for ( var item in vals ) ( function ( item ) {
124
128
jStat . cauchy . prototype [ item ] = function ( x ) {
@@ -169,24 +173,25 @@ jStat.extend( jStat.chisquare, {
169
173
return ( dof - 2 > 0 ) ? dof - 2 : 0 ;
170
174
} ,
171
175
172
- sample : function ( x , dof ) {
173
- if ( x ) {
174
- // return a jstat object filled with random samples
175
- return x . alter ( function ( ) {
176
- return jStat . randg ( dof / 2 ) * 2 ;
177
- } ) ;
178
- } else {
179
- // return a random sample
180
- return jStat . randg ( dof / 2 ) * 2 ;
181
- }
176
+ sample : function ( dof ) {
177
+ return jStat . randg ( dof / 2 ) * 2 ;
182
178
} ,
183
179
184
180
variance : function ( dof ) {
185
181
return 2 * dof ;
186
182
}
187
183
} ) ;
188
184
189
- // extend the chisquare objects prototype
185
+ // extend the chisquare distribution prototype
186
+ jStat . chisquare . prototype . sample = function ( arr ) {
187
+ if ( arr ) {
188
+ return jStat . alter ( arr , function ( ) {
189
+ return jStat . chisquare . sample ( this . dof ) ;
190
+ } ) ;
191
+ } else {
192
+ return jStat . chisquare . sample ( this . dof ) ;
193
+ } ;
194
+ } ;
190
195
( function ( vals ) {
191
196
for ( var item in vals ) ( function ( item ) {
192
197
jStat . chisquare . prototype [ item ] = function ( x ) {
@@ -236,22 +241,25 @@ jStat.extend( jStat.exponential, {
236
241
return 0 ;
237
242
} ,
238
243
239
- sample : function ( x , rate ) {
240
- if ( x ) {
241
- return x . alter ( function ( ) {
242
- return - 1 / rate * Math . log ( Math . random ( ) ) ;
243
- } ) ;
244
- } else {
245
- return - 1 / rate * Math . log ( Math . random ( ) ) ;
246
- }
244
+ sample : function ( rate ) {
245
+ return - 1 / rate * Math . log ( Math . random ( ) ) ;
247
246
} ,
248
247
249
248
variance : function ( rate ) {
250
249
return Math . pow ( rate , - 2 ) ;
251
250
}
252
251
} ) ;
253
252
254
- // extend the exponential objects prototype
253
+ // extend the exponential distribution prototype
254
+ jStat . exponential . prototype . sample = function ( arr ) {
255
+ if ( arr ) {
256
+ return jStat . alter ( arr , function ( ) {
257
+ return jStat . exponential . sample ( this . rate ) ;
258
+ } ) ;
259
+ } else {
260
+ return jStat . exponential . sample ( this . rate ) ;
261
+ } ;
262
+ } ;
255
263
( function ( vals ) {
256
264
for ( var item in vals ) ( function ( item ) {
257
265
jStat . exponential . prototype [ item ] = function ( x ) {
@@ -299,24 +307,25 @@ jStat.extend( jStat.gamma, {
299
307
return undefined ;
300
308
} ,
301
309
302
- sample : function ( x , shape , scale ) {
303
- if ( x ) {
304
- // return a jstat object filled with random samples
305
- return x . alter ( function ( ) {
306
- return jStat . randg ( shape ) * scale ;
307
- } ) ;
308
- } else {
309
- // return a random sample
310
- return jStat . randg ( shape ) * scale ;
311
- }
310
+ sample : function ( shape , scale ) {
311
+ return jStat . randg ( shape ) * scale ;
312
312
} ,
313
313
314
314
variance : function ( shape , scale ) {
315
315
return shape * scale * scale ;
316
316
}
317
317
} ) ;
318
318
319
- // extend the gamma objects prototype
319
+ // extend the gamma distribution prototype
320
+ jStat . gamma . prototype . sample = function ( arr ) {
321
+ if ( arr ) {
322
+ return jStat . alter ( arr , function ( ) {
323
+ return jStat . gamma . sample ( this . shape , this . scale ) ;
324
+ } ) ;
325
+ } else {
326
+ return jStat . gamma . sample ( this . shape , this . scale ) ;
327
+ } ;
328
+ } ;
320
329
( function ( vals ) {
321
330
for ( var item in vals ) ( function ( item ) {
322
331
jStat . gamma . prototype [ item ] = function ( x ) {
@@ -418,22 +427,25 @@ jStat.extend( jStat.lognormal, {
418
427
return Math . exp ( mu - sigma * sigma ) ;
419
428
} ,
420
429
421
- sample : function ( x , mu , sigma ) {
422
- if ( x ) {
423
- return x . alter ( function ( ) {
424
- return Math . exp ( jStat . randn ( ) * sigma + mu ) ;
425
- } ) ;
426
- } else {
427
- return Math . exp ( jStat . randn ( ) * sigma + mu ) ;
428
- }
430
+ sample : function ( mu , sigma ) {
431
+ return Math . exp ( jStat . randn ( ) * sigma + mu ) ;
429
432
} ,
430
433
431
434
variance : function ( mu , sigma ) {
432
435
return ( Math . exp ( sigma * sigma ) - 1 ) * Math . exp ( 2 * mu + sigma * sigma ) ;
433
436
}
434
437
} ) ;
435
438
436
- // extend the lognormal objects prototype
439
+ // extend the lognormal distribution prototype
440
+ jStat . lognormal . prototype . sample = function ( arr ) {
441
+ if ( arr ) {
442
+ return jStat . alter ( arr , function ( ) {
443
+ return jStat . lognormal . sample ( this . mu , this . sigma ) ;
444
+ } ) ;
445
+ } else {
446
+ return jStat . lognormal . sample ( this . mu , this . sigma ) ;
447
+ } ;
448
+ } ;
437
449
( function ( vals ) {
438
450
for ( var item in vals ) ( function ( item ) {
439
451
jStat . lognormal . prototype [ item ] = function ( x ) {
@@ -484,24 +496,25 @@ jStat.extend( jStat.normal, {
484
496
return mean ;
485
497
} ,
486
498
487
- sample : function ( x , mean , std ) {
488
- if ( x ) {
489
- // return a jstat object filled with random samples
490
- return x . alter ( function ( ) {
491
- return jStat . randn ( ) * std + mean ;
492
- } ) ;
493
- } else {
494
- // return a random sample
495
- return jStat . randn ( ) * std + mean ;
496
- }
499
+ sample : function ( mean , std ) {
500
+ return jStat . randn ( ) * std + mean ;
497
501
} ,
498
502
499
503
variance : function ( mean , std ) {
500
504
return std * std ;
501
505
}
502
506
} ) ;
503
507
504
- // extend the normal objects prototype
508
+ // extend the normal distribution prototype
509
+ jStat . normal . prototype . sample = function ( arr ) {
510
+ if ( arr ) {
511
+ return jStat . alter ( arr , function ( ) {
512
+ return jStat . normal . sample ( this . mean , this . std ) ;
513
+ } ) ;
514
+ } else {
515
+ return jStat . normal . sample ( this . mean , this . std ) ;
516
+ } ;
517
+ } ;
505
518
( function ( vals ) {
506
519
for ( var item in vals ) ( function ( item ) {
507
520
jStat . normal . prototype [ item ] = function ( x ) {
@@ -606,23 +619,25 @@ jStat.extend( jStat.studentt, {
606
619
return 0 ;
607
620
} ,
608
621
609
- sample : function ( x , dof ) {
610
- if ( x ) {
611
- return x . alter ( function ( ) {
612
- return jStat . randn ( ) * Math . sqrt ( dof / ( 2 * jStat . randg ( dof / 2 ) ) ) ;
613
- } ) ;
614
- } else {
615
- // return a random sample
616
- return jStat . randn ( ) * Math . sqrt ( dof / ( 2 * jStat . randg ( dof / 2 ) ) ) ;
617
- }
622
+ sample : function ( dof ) {
623
+ return jStat . randn ( ) * Math . sqrt ( dof / ( 2 * jStat . randg ( dof / 2 ) ) ) ;
618
624
} ,
619
625
620
626
variance : function ( dof ) {
621
627
return ( dof > 2 ) ? dof / ( dof - 2 ) : ( dof > 1 ) ? Infinity : undefined ;
622
628
}
623
629
} ) ;
624
630
625
- // extend the studentt objects prototype
631
+ // extend the studentt distribution prototype
632
+ jStat . studentt . prototype . sample = function ( arr ) {
633
+ if ( arr ) {
634
+ return jStat . alter ( arr , function ( ) {
635
+ return jStat . studentt . sample ( this . dof ) ;
636
+ } ) ;
637
+ } else {
638
+ return jStat . studentt . sample ( this . dof ) ;
639
+ } ;
640
+ } ;
626
641
( function ( vals ) {
627
642
for ( var item in vals ) ( function ( item ) {
628
643
jStat . studentt . prototype [ item ] = function ( x ) {
@@ -673,22 +688,25 @@ jStat.extend( jStat.weibull, {
673
688
return ( shape > 1 ) ? scale * Math . pow ( ( shape - 1 ) / shape , 1 / shape ) : undefined ;
674
689
} ,
675
690
676
- sample : function ( x , scale , shape ) {
677
- if ( x ) {
678
- return x . alter ( function ( ) {
679
- return scale * Math . pow ( - Math . log ( Math . random ( ) ) , 1 / shape ) ;
680
- } ) ;
681
- } else {
682
- return scale * Math . pow ( - Math . log ( Math . random ( ) ) , 1 / shape ) ;
683
- }
691
+ sample : function ( scale , shape ) {
692
+ return scale * Math . pow ( - Math . log ( Math . random ( ) ) , 1 / shape ) ;
684
693
} ,
685
694
686
695
variance : function ( scale , shape ) {
687
696
return scale * scale * jStat . gammafn ( 1 + 2 / shape ) - Math . pow ( this . mean ( scale , shape ) , 2 ) ;
688
697
}
689
698
} ) ;
690
699
691
- // extend the weibull objects prototype
700
+ // extend the weibull distribution prototype
701
+ jStat . weibull . prototype . sample = function ( arr ) {
702
+ if ( arr ) {
703
+ return jStat . alter ( arr , function ( ) {
704
+ return jStat . weibull . sample ( this . scale , this . shape ) ;
705
+ } ) ;
706
+ } else {
707
+ return jStat . weibull . sample ( this . scale , this . shape ) ;
708
+ } ;
709
+ } ;
692
710
( function ( vals ) {
693
711
for ( var item in vals ) ( function ( item ) {
694
712
jStat . weibull . prototype [ item ] = function ( x ) {
@@ -740,22 +758,25 @@ jStat.extend( jStat.uniform, {
740
758
741
759
} ,
742
760
743
- sample : function ( x , a , b ) {
744
- if ( x ) {
745
- return x . alter ( function ( ) {
746
- return ( a / 2 + b / 2 ) + ( b / 2 - a / 2 ) * ( 2 * Math . random ( ) - 1 ) ;
747
- } ) ;
748
- } else {
749
- return ( a / 2 + b / 2 ) + ( b / 2 - a / 2 ) * ( 2 * Math . random ( ) - 1 ) ;
750
- }
761
+ sample : function ( a , b ) {
762
+ return ( a / 2 + b / 2 ) + ( b / 2 - a / 2 ) * ( 2 * Math . random ( ) - 1 ) ;
751
763
} ,
752
764
753
765
variance : function ( a , b ) {
754
766
return 0.08333333333333333 * Math . pow ( b - a , 2 ) ;
755
767
}
756
768
} ) ;
757
769
758
- // extend the uniform objects prototype
770
+ // extend the uniform distribution prototype
771
+ jStat . uniform . prototype . sample = function ( arr ) {
772
+ if ( arr ) {
773
+ return jStat . alter ( arr , function ( ) {
774
+ return jStat . uniform . sample ( this . a , this . b ) ;
775
+ } ) ;
776
+ } else {
777
+ return jStat . uniform . sample ( this . a , this . b ) ;
778
+ } ;
779
+ } ;
759
780
( function ( vals ) {
760
781
for ( var item in vals ) ( function ( item ) {
761
782
jStat . uniform . prototype [ item ] = function ( x ) {
0 commit comments