-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimulateDist.java
650 lines (540 loc) · 18.4 KB
/
simulateDist.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
import java.util.*;
import java.util.Random;
public class simulateDist {
public static void main(String[] args) {
String x = args[2];
if(x.compareTo("binomial") == 0)
{
int n = Integer.parseInt(args[1]);
//System.out.println(n);
double p = Double.parseDouble(args[3]);
//System.out.println(p);
Binomial(n, p);
}
else if(x.compareTo("bernoulli") == 0)
{
int n = Integer.parseInt(args[1]);
double p = Double.parseDouble(args[3]);
System.out.println(" The randomnly generated sample are: ");
bernoulli_distribution(p, n);
}
else if(x.compareTo("poisson") == 0)
{
int n = Integer.parseInt(args[1]);
double lambda = Double.parseDouble(args[3]);
poisson(n, lambda);
}
else if(x.compareTo("uniform") == 0)
{
int n = Integer.parseInt(args[1]);
float a = Float.parseFloat(args[3]);
float b = Float.parseFloat(args[4]);
Uniform(a, b, n);
}
else if(x.compareTo("normal") == 0)
{
int n = Integer.parseInt(args[1]);
double mu = Double.parseDouble(args[3]);
double sigma = Double.parseDouble(args[4]);
Normal(mu, sigma, n);
}
else if(x.compareTo("exponential") == 0)
{
int n = Integer.parseInt(args[1]);
double lambda = Double.parseDouble(args[3]);
exponential(lambda, n);
}
else if(x.compareTo("geometric") == 0)
{
int n = Integer.parseInt(args[1]);
double p = Double.parseDouble(args[3]);
geometric(p, n);
}
else if(x.compareTo("neg-binomial") == 0)
{
int n = Integer.parseInt(args[1]);
int k = Integer.parseInt(args[3]);
double p = Double.parseDouble(args[4]);
negativebinomial(p, k, n);
}
else if(x.compareTo("gamma") == 0)
{
int n = Integer.parseInt(args[1]);
double alpha = Double.parseDouble(args[3]);
double lambda = Double.parseDouble(args[4]);
Gamma(alpha, lambda, n);
}
else if(x.compareTo("arb-discrete") == 0)
{
int n = Integer.parseInt(args[1]);
String temp = " ";
for(int i=3; i<args.length; i++)
{
temp = temp +" "+ args[i];
}
String[] temp1 = temp.split(" ");
double[] pr = new double[temp1.length];
int count =0;
int iterator =0;
for(int i=0; i<temp1.length;i++)
{
if (temp1[i].isEmpty())
{
pr[i] = -1.0;
count++;
}
else {
pr[i] = Double.parseDouble(temp1[i]);
}
}
double[] p = new double[temp1.length - count];
for(int j=0; j<pr.length; j++)
{
if(pr[j] == -1)
{
}
else
{
p[iterator] = pr[j];
iterator++;
}
}
for(int z=0; z<p.length; z++)
{
System.out.println(p[z]);
}
Arb_disc(p, n);
}
}
// binomial starts here
public static void Binomial (int trials, double p)
{
//Binomial distribution is the probability of exactly x success in n trials
// The range of each sample element is taken as n
// elements in the random sample range from 0 to n
// Function to calculate nCr
Random rn = new Random();
int[] succs = new int[trials];
int min = 0;
int max = trials;
System.out.println(" The random generated numbers are: ");
for (int i = 0; i < (succs.length); i++) {
succs[i] = rn.nextInt(max - min + 1) + min;
System.out.print(succs[i]);
System.out.print(" ");
}
System.out.println(" ");
generateCombo(trials, succs, p);
}
/* Binomial */
public static void generateCombo(int trials, int[] succ, double probsucc){
int printtrials = trials;//vi p
int[] succfacts = new int[succ.length];
int[] diffFacts = new int[succ.length];
int [] diff = new int[succ.length];
double[] combos = new double[succ.length];
int trialfact = 1;
int succfact = 1;
int diffFact =1;
// generates factorial for total number of trials
for (int i=trials; i>0; i--){
trialfact = trialfact * i;
}
// generate factorial for each possible success
for (int i=0; i<succ.length; i++){
if (succ[i] == 0){
succfacts[i] = 1;
}
else{
succfact =1;
for(int j=1; j<=succ[i]; j++)
{
succfact = succfact * j;
succfacts[i] = succfact;
}
}
}
for(int i=0; i<diff.length; i++)
{
diff[i] = trials - succ[i];
}
for (int i=0; i<diff.length; i++){
if (diff[i] == 0){
diffFacts[i] = 1;
}
else{
diffFact =1;
for(int j=1; j<=diff[i]; j++)
{
diffFact = diffFact * j;
diffFacts[i] = diffFact;
}
}
}
// generate combinations for each success and trial number
for (int i=0; i<succ.length; i++){
double combo = (trialfact / (succfacts[i] * diffFacts[i])); // added -1
combos[i] = combo;
}
prob(combos, probsucc, trials, succ);
}
/* Binomial */
public static void prob(double[] combos, double probsucc, int trials, int[] succ){
int printtrials = trials;
double prob;
double[] probs = new double[combos.length];
System.out.println(" ");
//generates probabilities for each success
System.out.println(" probability of each are:");
for (int i=0; i<combos.length; i++){
prob = (combos[i] * (Math.pow(probsucc, succ[i])) * (Math.pow((1 - probsucc), ( trials - succ[i]))));
probs[i] = prob;
System.out.print(probs[i]);
System.out.print(" ");
}
}
// Binomial ends here
// Bernoulli starts here
public static void bernoulli_distribution(double p, int n)
{
long [] result = new long[n];
double count =0;
// Generating random samples of successes and faliures
for(int i=0; i<n; i++)
{
result[i] = Math.round(Math.random());
if (result[i] == 1)
{
count ++;
}
}
// distribution list maintains p% 1s and (1-p)% 0s
if(p>0.1 || p<0.9) {
if ((count / n) >= (p - 0.1) && (count / n) <= (p + 0.1)) {
for (int i = 0; i < n; i++) {
System.out.print(result[i]);
System.out.print(" ");
}
} else {
bernoulli_distribution(p, n);
}
}
else
{
if ((count / n) >= (p - 0.01) && (count / n) <= (p + 0.01)) {
for (int i = 0; i < n; i++) {
System.out.print(result[i]);
System.out.print(" ");
}
} else {
bernoulli_distribution(p, n);
}
}
}
// Bernoulli ends here
/* Poisson */
public static void poisson(int n, double lambda) {
// Number of x rare events happen in lambda_ time
// The range of each sample element is taken as 10
// elements in the random sample range from 1 to 10, since the range is not specified for each sample
double[] sample = new double[n];
Random rn = new Random();
int max=n;
int min =0;
System.out.println("The randomly generated numbers are");
for(int i=0; i<n; i++)
{
sample[i] = rn.nextInt(max - min + 1) + min;
System.out.print(sample[i]);
System.out.print(" ");
}
System.out.println(" ");
System.out.println("The probabilities of each are:");
double[] result = new double[n];
double exp =0;
double lamba_power =0;
double fact=0;
for(int j=0; j<n; j++)
{
exp = Math.exp(-(lambda));
lamba_power = Math.pow(lambda, sample[j]);
fact = fact_poi(sample[j]);
result[j] = ((exp * lamba_power)/ fact);
System.out.print(result[j]);
System.out.print(" ");
}
}
public static double fact_poi(double n)
{
double res = 1;
for (int i = 2; i <= n; i++)
res = res * i;
return res;
}
// Poisson ends here
// Uniform starts here
public static void Uniform(float a, float b, int n)
{
//Number of x rare events happen in lambda_ time
// The range of each sample element is taken as 10
// elements in the random sample range from 1 to 10, since the range is not specified for each sample
Random rn = new Random();
double[] sample = new double[n];
int max = Math.round(b-1);
int min = Math.round(a+1);
System.out.println("The randomly generated numbers are:");
for(int i=0; i<n; i++)
{
sample[i] = rn.nextInt(max - min + 1) + min;
System.out.print(sample[i]);
System.out.print(" ");
}
double[] result = new double[n];
double f_x =0;
System.out.println(" ");
System.out.println(" The probability of each are:");
for(int j=0; j<n; j++)
{
f_x=0;
f_x = sample[j]/(b-a);
result[j] = f_x;
System.out.print(result[j]);
System.out.print(" ");
}
}
// Uniform ends here
// exponential starts here
public static void exponential(double lambda, int n)
{
//Exponential dsitributon is F(x) = (1-e^(-lambda x) for x > 0
// The range of each sample element is taken as n
// elements in the random sample range from 1 to n, since the range is not specified for each sample
//Calculates P(X<x)
Random rn = new Random();
double[] sample = new double[n];
int max = n;
int min = 1;
System.out.println(" The randomnly generated samples are: ");
for(int i=0; i<n; i++)
{
sample[i] = rn.nextInt(max - min + 1) + min;
System.out.print(sample[i]);
System.out.print(" ");
}
System.out.println(" ");
System.out.println(" Their Probabilities are: ");
double[] result = new double[n];
double exp =0;
for(int j=0; j<n; j++)
{
exp =0;
exp = 1- Math.exp((-lambda) * sample[j]);
result[j] = exp;
System.out.print(result[j]);
System.out.print(" ");
}
}
// exponential ends here
// Geometric starts here
public static void geometric(double p, int n)
{
//Geometric distribution calculates the probability of first success on the xth trial
// The range of each sample element is taken as 10
// elements in the random sample range from 1 to 10, since the range is not specified for each sample
Random rn = new Random();
int max = n;
int min =0;
double[] samples = new double[n];
System.out.println(" The randomnly generated samples are: ");
for(int i=0; i<n; i++)
{
samples[i] = rn.nextInt(max - min + 1) + min;
System.out.print(samples[i]);
System.out.print(" ");
}
double[] result = new double[n];
System.out.println(" ");
System.out.println(" Their Probabilities");
for(int i=0; i<n; i++)
{
result[i] = Math.pow((1-p), (samples[i] -1)) * p;
}
for(int z=0; z<n; z++)
{
System.out.print(result[z]);
System.out.print(" ");
}
}
// Geometric ends here
// Negativebinomial starts here
public static void negativebinomial(double p, int k, int n)
{
//The xth trial result in the kth success
//x can vary from k to some number, x>k
// The range of each sample element is taken as 10
// elements in the random sample range from k to 10, since the range is not specified for each sample
int[] sample = new int[n];
Random rn = new Random();
int max =n;
int min=k;
System.out.println(" The randomnly generated samples are: ");
for(int i=0; i<n; i++)
{
sample[i] = rn.nextInt(max - min + 1) + min;
System.out.print(sample[i]);
System.out.print(" ");
}
System.out.println(" ");
double[] result = new double[n];
int combination =0;
double failure =0;
double success =0;
double z = Double.valueOf(k);
System.out.println(" Their Probability: ");
for(int j=0; j<n; j++)
{
combination = nCr(sample[j]-1, k-1);
failure = Math.pow((1-p), (sample[j] -z));
success = Math.pow(p,z);
result[j] = (combination * failure * success);
System.out.print(result[j]);
System.out.print(" ");
}
}
static int nCr(int n, int r)
{
return fact_CR(n) / (fact_CR(r) *
fact_CR(n - r));
}
// Returns factorial of n
static int fact_CR(int n)
{
int res = 1;
for (int i = 2; i <= n; i++)
res = res * i;
return res;
}
// Negative Binomial ends here
// Normal starts here
public static void Normal(double mu, double sigma, int n)
{
//Sample size is given as sample
// elements in the random sample range from -2 to 2, since the range is not specified for each sample
double[] samples = new double[n];
double max =2.0;
double min = -2.0;
System.out.println(" The randomnly generated samples are:");
for(int i=0; i<n; i++)
{
samples[i] = (Math.random()*((max-min)+1))+min;
System.out.print(samples[i]);
System.out.print(" ");
}
System.out.println(" ");
double[] result = new double[n];
double z=0;
System.out.println(" Their Probabilities: ");
for(int j=0; j<n; j++)
{
z = (samples[j] - mu) / sigma;
result[j] = ((1.0 + erf((z/Math.sqrt(2.0)))) / 2.0);
System.out.print(result[j]);
System.out.print(" ");
}
}
public static double erf(double z) {
double t = 1.0 / (1.0 + 0.5 * Math.abs(z));
// use Horner's method
double ans = 1 - t * Math.exp( -z*z - 1.26551223 + t * ( 1.00002368 + t * ( 0.37409196 + t * ( 0.09678418 + t * (-0.18628806 + t * ( 0.27886807 + t * (-1.13520398 + t * ( 1.48851587 + t * (-0.82215223 + t * ( 0.17087277))))))))));
if (z >= 0) return ans;
else return -ans;
}
// Normal ends here
// Gamma starts here
public static void Gamma(double alpha, double lambda, int n)
{
double[] samples = new double[n];
Random rn = new Random();
int max = 10;
int min = 0;
System.out.println(" The randomnly generated samples are: ");
for(int i=0; i<n; i++)
{
samples[i] = rn.nextInt(max - min + 1) + min;
System.out.print(samples[i]);
System.out.print(" ");
}
System.out.println(" ");
System.out.println(" Their Probabilities: ");
double[] result = new double[n];
double temp =0;
double temp1 =0;
for(int j=0; j<n; j++)
{
temp = ((Math.pow(lambda, alpha)) / gamma(alpha));
temp1 = Math.pow(samples[j], (alpha-1)) * Math.exp((-lambda) * samples[j]);
result[j] = temp * temp1;
System.out.print(result[j]);
System.out.print(" ");
}
}
public static double logGamma(double x) {
double tmp = (x - 0.5) * Math.log(x + 4.5) - (x + 4.5);
double ser = 1.0 + 76.18009173 / (x + 0) - 86.50532033 / (x + 1)
+ 24.01409822 / (x + 2) - 1.231739516 / (x + 3)
+ 0.00120858003 / (x + 4) - 0.00000536382 / (x + 5);
return tmp + Math.log(ser * Math.sqrt(2 * Math.PI));
}
public static double gamma(double x) { return Math.exp(logGamma(x)); }
// Gamma ends here
// Arb_disc starts Here
public static void Arb_disc(double[] p, int n)
{
Random rn = new Random();
double[] samples = new double[n];
double[] count = new double[n];
//int max = p.length-1;
int min =0;
double elements =0;
for(int i=0; i<p.length; i++)
{
elements = (p[i] * n);
if(elements<1)
{
elements = Math.ceil(elements);
}
else {
elements = Math.floor(elements);
}
for(int j=0; j< elements; j++)
{
count[j] = min;
}
min++;
}
System.out.println(" ");
RandomizeArray(count);
double[] result = new double[n];
System.out.println(" The randomnly generated sample is: ");
for(int z=0; z<result.length; z++)
{
getRandom(count);
}
}
public static double[] RandomizeArray(double[] array){
Random rgen = new Random(); // Random number generator
for (int i=0; i<array.length; i++) {
int randomPosition = rgen.nextInt(array.length);
double temp = array[i];
array[i] = array[randomPosition];
array[randomPosition] = temp;
}
return array;
}
public static void getRandom(double[] array) {
int rnd = new Random().nextInt(array.length);
System.out.print(array[rnd]);
System.out.print(" ");
}
// Arb_disc ends here
}