Skip to content

Commit f013d07

Browse files
committed
cleanup distribution samples
1 parent dda7ac3 commit f013d07

File tree

1 file changed

+124
-103
lines changed

1 file changed

+124
-103
lines changed

src/distribution.js

Lines changed: 124 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ jStat.extend( jStat.beta, {
1717
return jStat.incompleteBeta( x, alpha, beta );
1818
},
1919

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 );
2222
},
2323

2424
mean : function( alpha, beta ) {
@@ -33,26 +33,27 @@ jStat.extend( jStat.beta, {
3333
return ( alpha * beta ) / ( Math.pow( alpha + beta, 2 ) * ( alpha + beta + 1 ));
3434
},
3535

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 ));
4840
},
4941

5042
variance : function( alpha, beta ) {
5143
return ( alpha * beta ) / ( Math.pow( alpha + beta, 2 ) * ( alpha + beta + 1 ) );
5244
}
5345
});
5446

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+
};
5657
(function( vals ) {
5758
for ( var item in vals ) (function( item ) {
5859
jStat.beta.prototype[ item ] = function( x ) {
@@ -103,22 +104,25 @@ jStat.extend( jStat.cauchy, {
103104
return local;
104105
},
105106

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;
114109
},
115110

116111
variance : function( local, scale ) {
117112
// TODO: implement this
118113
}
119114
});
120115

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+
};
122126
(function( vals ) {
123127
for ( var item in vals ) (function( item ) {
124128
jStat.cauchy.prototype[ item ] = function( x ) {
@@ -169,24 +173,25 @@ jStat.extend( jStat.chisquare, {
169173
return ( dof - 2 > 0 ) ? dof - 2 : 0;
170174
},
171175

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;
182178
},
183179

184180
variance: function( dof ) {
185181
return 2 * dof;
186182
}
187183
});
188184

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+
};
190195
(function( vals ) {
191196
for ( var item in vals ) (function( item ) {
192197
jStat.chisquare.prototype[ item ] = function( x ) {
@@ -236,22 +241,25 @@ jStat.extend( jStat.exponential, {
236241
return 0;
237242
},
238243

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() );
247246
},
248247

249248
variance : function( rate ) {
250249
return Math.pow( rate, -2 );
251250
}
252251
});
253252

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+
};
255263
(function( vals ) {
256264
for ( var item in vals ) (function( item ) {
257265
jStat.exponential.prototype[ item ] = function( x ) {
@@ -299,24 +307,25 @@ jStat.extend( jStat.gamma, {
299307
return undefined;
300308
},
301309

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;
312312
},
313313

314314
variance: function( shape, scale ) {
315315
return shape * scale * scale;
316316
}
317317
});
318318

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+
};
320329
(function( vals ) {
321330
for ( var item in vals ) (function( item ) {
322331
jStat.gamma.prototype[ item ] = function( x ) {
@@ -418,22 +427,25 @@ jStat.extend( jStat.lognormal, {
418427
return Math.exp( mu - sigma*sigma );
419428
},
420429

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 );
429432
},
430433

431434
variance : function( mu, sigma ) {
432435
return ( Math.exp( sigma*sigma ) - 1 ) * Math.exp( 2 * mu + sigma*sigma );
433436
}
434437
});
435438

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+
};
437449
(function( vals ) {
438450
for ( var item in vals ) (function( item ) {
439451
jStat.lognormal.prototype[ item ] = function( x ) {
@@ -484,24 +496,25 @@ jStat.extend( jStat.normal, {
484496
return mean;
485497
},
486498

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;
497501
},
498502

499503
variance : function( mean, std ) {
500504
return std * std;
501505
}
502506
});
503507

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+
};
505518
(function( vals ) {
506519
for ( var item in vals ) (function( item ) {
507520
jStat.normal.prototype[ item ] = function( x ) {
@@ -606,23 +619,25 @@ jStat.extend( jStat.studentt, {
606619
return 0;
607620
},
608621

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) ) );
618624
},
619625

620626
variance : function( dof ) {
621627
return ( dof > 2 ) ? dof / ( dof - 2 ) : ( dof > 1 ) ? Infinity : undefined;
622628
}
623629
});
624630

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+
};
626641
(function( vals ) {
627642
for ( var item in vals ) (function( item ) {
628643
jStat.studentt.prototype[ item ] = function( x ) {
@@ -673,22 +688,25 @@ jStat.extend( jStat.weibull, {
673688
return ( shape > 1 ) ? scale * Math.pow(( shape - 1 ) / shape, 1 / shape ) : undefined;
674689
},
675690

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 );
684693
},
685694

686695
variance : function( scale, shape ) {
687696
return scale * scale * jStat.gammafn( 1 + 2 / shape ) - Math.pow( this.mean( scale, shape ), 2 );
688697
}
689698
});
690699

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+
};
692710
(function( vals ) {
693711
for ( var item in vals ) (function( item ) {
694712
jStat.weibull.prototype[ item ] = function( x ) {
@@ -740,22 +758,25 @@ jStat.extend( jStat.uniform, {
740758

741759
},
742760

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);
751763
},
752764

753765
variance : function( a, b ) {
754766
return 0.08333333333333333 * Math.pow( b - a, 2 );
755767
}
756768
});
757769

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+
};
759780
(function( vals ) {
760781
for ( var item in vals ) (function( item ) {
761782
jStat.uniform.prototype[ item ] = function( x ) {

0 commit comments

Comments
 (0)