source: src/main/java/agents/anac/y2015/agentBuyogV2/flanagan/analysis/Stat.java

Last change on this file was 1, checked in by Wouter Pasman, 6 years ago

Initial import : Genius 9.0.0

File size: 567.5 KB
Line 
1/*
2* Class Stat
3*
4* USAGE: Statistical functions
5*
6* WRITTEN BY: Dr Michael Thomas Flanagan
7*
8* DATE: June 2002 as part of Fmath
9* AMENDED: 12 May 2003 Statistics separated out from Fmath as a new class
10* DATE: 18 June 2005, 5 January 2006, 25 April 2006, 12, 21 November 2006
11* 4 December 2006 (renaming of cfd and pdf methods - older version also retained)
12* 31 December 2006, March 2007, 14 April 2007, 19 October 2007, 27 February 2008
13* 29 March 2008, 7 April 2008, 29 April 2008 - 13 May 2008, 22-31 May 2008,
14* 4-10 June 2008, 27 June 2008, 2-5 July 2008, 23 July 2008, 31 July 2008,
15* 2-4 August 2008, 20 August 2008, 5-10 September 2008, 19 September 2008,
16* 28-30 September 2008 (probability Plot moved to separate class, ProbabilityPlot)
17* 4-5 October 2008, 8-13 December 2008, 14 June 2009, 13-23 October 2009,
18* 8 February 2010, 18-25 May 2010, 2 November 2010, 4 December 2010, 19-25 January 2011
19*
20* DOCUMENTATION:
21* See Michael Thomas Flanagan's Java library on-line web page:
22* http://www.ee.ucl.ac.uk/~mflanaga/java/Stat.html
23* http://www.ee.ucl.ac.uk/~mflanaga/java/
24*
25* Copyright (c) 2002 - 2011 Michael Thomas Flanagan
26*
27* PERMISSION TO COPY:
28*
29* Permission to use, copy and modify this software and its documentation for NON-COMMERCIAL purposes is granted, without fee,
30* provided that an acknowledgement to the author, Dr Michael Thomas Flanagan at www.ee.ucl.ac.uk/~mflanaga, appears in all copies
31* and associated documentation or publications.
32*
33* Redistributions of the source code of this source code, or parts of the source codes, must retain the above copyright notice,
34+ this list of conditions and the following disclaimer and requires written permission from the Michael Thomas Flanagan:
35*
36* Redistribution in binary form of all or parts of this class must reproduce the above copyright notice, this list of conditions and
37* the following disclaimer in the documentation and/or other materials provided with the distribution and requires written permission
38* from the Michael Thomas Flanagan:
39*
40* Dr Michael Thomas Flanagan makes no representations about the suitability or fitness of the software for any or for a particular purpose.
41* Dr Michael Thomas Flanagan shall not be liable for any damages suffered as a result of using, modifying or distributing this software
42* or its derivatives.
43*
44***************************************************************************************/
45
46package agents.anac.y2015.agentBuyogV2.flanagan.analysis;
47
48import java.util.*;
49
50import agents.anac.y2015.agentBuyogV2.flanagan.circuits.Phasor;
51import agents.anac.y2015.agentBuyogV2.flanagan.complex.*;
52import agents.anac.y2015.agentBuyogV2.flanagan.integration.IntegralFunction;
53import agents.anac.y2015.agentBuyogV2.flanagan.integration.Integration;
54import agents.anac.y2015.agentBuyogV2.flanagan.io.*;
55import agents.anac.y2015.agentBuyogV2.flanagan.math.*;
56import agents.anac.y2015.agentBuyogV2.flanagan.plot.PlotGraph;
57import agents.anac.y2015.agentBuyogV2.flanagan.roots.*;
58
59import java.math.*;
60
61
62public class Stat extends ArrayMaths{
63
64 // INSTANCE VARIABLES
65 private boolean nFactorOptionI = false; // = true varaiance, covariance and standard deviation denominator = n
66 // = false varaiance, covariance and standard deviation denominator = n-1
67 private boolean nFactorReset = false; // = true when instance method resetting the denominator is called
68
69 private boolean nEffOptionI = true; // = true n replaced by effective sample number
70 // = false n used as sample number
71 private boolean nEffReset = false; // = true when instance method resetting the nEff choice called
72
73 private boolean weightingOptionI = true; // = true 'little w' weights (uncertainties) used
74 // = false 'big W' weights (multiplicative factors) used
75 private boolean weightingReset = false; // = true when instance method resetting the nEff choice called
76
77 private ArrayMaths amWeights = null; // weights as ArrayMaths
78 private boolean weightsSupplied = false; // = true if weights entered
79
80 private ArrayList<Object> upperOutlierDetails = new ArrayList<Object>(); // upper outlier search details
81 // element 0 - number of ouliers (Integer)
82 // element 1 - outliers (double[])
83 // element 2 - outlier indices (inmoved
84 private boolean upperDone = false; // = true when upper oulier search ct[])
85 // element 3 - array with ouliers reompleted even if no upper outliers found
86 private ArrayList<Object> lowerOutlierDetails = new ArrayList<Object>(); // lower outlier search details
87 // element 0 - number of ouliers (Integer)
88 // element 1 - outliers (double[])
89 // element 2 - outlier indices (int[])
90 // element 3 - array with ouliers removed
91 private boolean lowerDone = false; // = true when lower oulier search completed even if no upper outliers found
92
93
94 // STATIC VARIABLES
95 private static boolean nFactorOptionS = false; // = true varaiance, covariance and standard deviation denominator = n
96 // = false varaiance and standard deviation denominator = n-1
97 private static boolean nEffOptionS = true; // = true n replaced by effective sample number
98 // = false n used as sample number
99
100 private static boolean weightingOptionS= true; // = true 'little w' weights (uncertainties) used
101 // = false 'big W' weights (multiplicative factors) used
102
103 // maximum number of iterations allowed in the contFract method
104 private static int cfMaxIter = 500;
105
106 // tolerance used in the contFract method
107 private static double cfTol = 1.0e-8;
108
109 // A small number close to the smallest representable floating point number
110 public static final double FPMIN = 1e-300;
111
112 private static boolean igSupress = false; // if true error messages in incompleteGammaSeries
113 // and incompleteGammaFract supressed
114
115
116 // PRIVATE MEMBERS FOR USE IN GAMMA FUNCTION METHODS AND HISTOGRAM CONSTRUCTION METHODS
117
118 // GAMMA FUNCTIONS
119 // Lanczos Gamma Function approximation - N (number of coefficients -1)
120 private static int lgfN = 6;
121 // Lanczos Gamma Function approximation - Coefficients
122 private static double[] lgfCoeff = {1.000000000190015, 76.18009172947146, -86.50532032941677, 24.01409824083091, -1.231739572450155, 0.1208650973866179E-2, -0.5395239384953E-5};
123 // Lanczos Gamma Function approximation - small gamma
124 private static double lgfGamma = 5.0;
125 // Maximum number of iterations allowed in Incomplete Gamma Function calculations
126 private static int igfiter = 1000;
127 // Tolerance used in terminating series in Incomplete Gamma Function calculations
128 private static double igfeps = 1e-8;
129
130 // HISTOGRAM CONSTRUCTION
131 // Tolerance used in including an upper point in last histogram bin when it is outside due to rounding erors
132 private static double histTol = 1.0001D;
133
134 // CONSTRUCTORS
135 public Stat(){
136 super();
137 }
138
139 public Stat(double[] xx){
140 super(xx);
141 this.convertToHighest();
142 }
143
144 public Stat(Double[] xx){
145 super(xx);
146 this.convertToHighest();
147 }
148
149 public Stat(float[] xx){
150 super(xx);
151 this.convertToHighest();
152 }
153
154 public Stat(Float[] xx){
155 super(xx);
156 this.convertToHighest();
157 }
158
159 public Stat(long[] xx){
160 super(xx);
161 this.convertToHighest();
162 }
163
164 public Stat(Long[] xx){
165 super(xx);
166 this.convertToHighest();
167 }
168
169 public Stat(int[] xx){
170 super(xx);
171 this.convertToHighest();
172 }
173
174 public Stat(Integer[] xx){
175 super(xx);
176 this.convertToHighest();
177 }
178
179 public Stat(short[] xx){
180 super(xx);
181 this.convertToHighest();
182 }
183
184 public Stat(Short[] xx){
185 super(xx);
186 this.convertToHighest();
187 }
188
189 public Stat(byte[] xx){
190 super(xx);
191 this.convertToHighest();
192 }
193
194 public Stat(Byte[] xx){
195 super(xx);
196 this.convertToHighest();
197 }
198
199 public Stat(BigDecimal[] xx){
200 super(xx);
201 }
202
203 public Stat(BigInteger[] xx){
204 super(xx);
205 this.convertToHighest();
206 }
207
208 public Stat(Complex[] xx){
209 super(xx);
210 this.convertToHighest();
211 }
212
213 public Stat(Phasor[] xx){
214 super(xx);
215 this.convertToHighest();
216 }
217
218 public Stat(String[] xx){
219 super(xx);
220 this.convertToHighest();
221 }
222
223 public Stat(Object[] xx){
224 super(xx);
225 this.convertToHighest();
226 }
227
228 public Stat(Vector<Object> xx){
229 super(xx);
230 this.convertToHighest();
231 }
232
233 public Stat(ArrayList<Object> xx){
234 super(xx);
235 this.convertToHighest();
236 }
237
238 // Convert array to Double if not Complex, Phasor, BigDecimal or BigInteger
239 // Convert to BigDecimal if BigInteger
240 // Convert Phasor to Complex
241 public void convertToHighest(){
242
243 switch(this.type){
244 case 0:
245 case 1:
246 case 2:
247 case 3:
248 case 4:
249 case 5:
250 case 6:
251 case 7:
252 case 8:
253 case 9:
254 case 10:
255 case 11:
256 case 16:
257 case 17:
258 case 18: Double[] dd = this.getArray_as_Double();
259 this.array.clear();
260 for(int i=0; i<this.length; i++)this.array.add(dd[i]);
261 double[] ww = new double[this.length];
262 for(int i=0; i<this.length; i++)ww[i]=1.0D;
263 amWeights = new ArrayMaths(ww);
264 this.type = 1;
265 break;
266 case 12:
267 case 13: BigDecimal[] bd = this.getArray_as_BigDecimal();
268 this.array.clear();
269 for(int i=0; i<this.length; i++)this.array.add(bd[i]);
270 BigDecimal[] wd = new BigDecimal[this.length];
271 for(int i=0; i<this.length; i++)wd[i]=BigDecimal.ONE;
272 amWeights = new ArrayMaths(wd);
273 this.type = 12;
274 bd = null;
275 break;
276 case 14:
277 case 15: Complex[] cc = this.getArray_as_Complex();
278 this.array.clear();
279 for(int i=0; i<this.length; i++)this.array.add(cc[i]);
280 Complex[] wc = new Complex[this.length];
281 for(int i=0; i<this.length; i++)wc[i]=Complex.plusOne();
282 amWeights = new ArrayMaths(wc);
283 this.type = 14;
284 break;
285 default: throw new IllegalArgumentException("This type number, " + this.type +", should not be possible here!!!!");
286 }
287 }
288
289
290 // INSTANCE METHODS
291 // Set weights to 'big W' - multiplicative factor
292 public void setWeightsToBigW(){
293 this.weightingOptionI = false;
294 this.weightingReset = true;
295 }
296
297 // Set weights to 'little w' - uncertainties
298 public void setWeightsToLittleW(){
299 this.weightingOptionI = true;
300 this.weightingReset = true;
301 }
302
303 // Set standard deviation, variance and covariance denominators to n
304 public void setDenominatorToN(){
305 this.nFactorOptionI = true;
306 this.nFactorReset = true;
307 }
308
309 // Set standard deviation, variance and covariance denominators to n-1
310 public void setDenominatorToNminusOne(){
311 this.nFactorOptionI = false;
312 this.nFactorReset = true;
313 }
314
315 // Repalce number of data points to the effective sample number in weighted calculations
316 public void useEffectiveN(){
317 this.nEffOptionI = true;
318 this.nEffReset = true;
319 }
320
321 // Repalce the effective sample number in weighted calculations by the number of data points
322 public void useTrueN(){
323 this.nEffOptionI = false;
324 this.nEffReset = true;
325 }
326
327 // Return the effective sample number
328 public double effectiveSampleNumber(){
329 return this.effectiveSampleNumber_as_double();
330
331 }
332
333 public double effectiveSampleNumber_as_double(){
334 boolean holdW = Stat.weightingOptionS;
335 if(this.weightingReset){
336 if(this.weightingOptionI){
337 Stat.weightingOptionS = true;
338 }
339 else{
340 Stat.weightingOptionS = false;
341 }
342 }
343 double nEff = 0.0D;
344 switch(this.type){
345 case 1: double[] dd = this.getArray_as_double();
346 nEff = Stat.effectiveSampleNumber(dd);
347 break;
348 case 12: BigDecimal[] bd = this.getArray_as_BigDecimal();
349 nEff = Stat.effectiveSampleNumber(bd).doubleValue();
350 bd = null;
351 break;
352 case 14: throw new IllegalArgumentException("Complex cannot be converted to double");
353 default: throw new IllegalArgumentException("This type number, " + this.type +", should not be possible here!!!!");
354 }
355 Stat.weightingOptionS = holdW;
356 return nEff;
357 }
358
359 public BigDecimal effectiveSampleNumber_as_BigDecimal(){
360 boolean holdW = Stat.weightingOptionS;
361 if(this.weightingReset){
362 if(this.weightingOptionI){
363 Stat.weightingOptionS = true;
364 }
365 else{
366 Stat.weightingOptionS = false;
367 }
368 }
369 BigDecimal nEff = BigDecimal.ZERO;
370 switch(this.type){
371 case 1:
372 case 12: BigDecimal[] bd = this.getArray_as_BigDecimal();
373 nEff = Stat.effectiveSampleNumber(bd);
374 bd = null;
375 break;
376 case 14: throw new IllegalArgumentException("Complex cannot be converted to BigDecimal");
377 default: throw new IllegalArgumentException("This type number, " + this.type +", should not be possible here!!!!");
378 }
379 Stat.weightingOptionS = holdW;
380 return nEff;
381 }
382
383 public Complex effectiveSampleNumber_as_Complex(){
384 boolean holdW = Stat.weightingOptionS;
385 if(this.weightingReset){
386 if(this.weightingOptionI){
387 Stat.weightingOptionS = true;
388 }
389 else{
390 Stat.weightingOptionS = false;
391 }
392 }
393 Complex nEff = Complex.zero();
394 switch(this.type){
395 case 1:
396 case 12:
397 case 14: Complex[] cc = this.getArray_as_Complex();
398 nEff = Stat.effectiveSampleNumber(cc);
399 break;
400 default: throw new IllegalArgumentException("This type number, " + this.type +", should not be possible here!!!!");
401 }
402 Stat.weightingOptionS = holdW;
403 return nEff;
404 }
405
406 // Return the true sample number
407 public int trueSampleNumber(){
408 return this.length;
409
410 }
411
412 public int trueSampleNumber_as_int(){
413 return this.length;
414 }
415
416 public double trueSampleNumber_as_double(){
417 return (double)this.length;
418 }
419
420 public BigDecimal trueSampleNumber_as_BigDecimal(){
421 return new BigDecimal(new Integer(this.length).toString());
422 }
423
424 public Complex trueSampleNumber_as_Complex(){
425 return new Complex((double)this.length, 0.0);
426 }
427
428
429 // CONVERSION OF WEIGHTING FACTORS (INSTANCE)
430 // Converts weighting facors Wi to wi, i.e. to 1/sqrt(Wi)
431 // DEPRECATED !!!
432 public void convertBigWtoLittleW(){
433 if(!this.weightsSupplied){
434 System.out.println("convertBigWtoLittleW: no weights have been supplied - all weights set to unity");
435 }
436 else{
437 amWeights = amWeights.oneOverSqrt();
438 }
439 }
440
441 // ENTER AN ARRAY OF WEIGHTS
442 public void setWeights(double[] xx){
443 if(this.length!=xx.length)throw new IllegalArgumentException("Length of weights array, " + xx.length + ", must be the same as the length of the instance internal array, " + this.length);
444 ArrayMaths wm = new ArrayMaths(xx);
445 this.convertWeights(wm);
446 this.weightsSupplied = true;
447 }
448
449 public void setWeights(Double[] xx){
450 if(this.length!=xx.length)throw new IllegalArgumentException("Length of weights array, " + xx.length + ", must be the same as the length of the instance internal array, " + this.length);
451 ArrayMaths wm = new ArrayMaths(xx);
452 this.convertWeights(wm);
453 this.weightsSupplied = true;
454 }
455
456 public void setWeights(float[] xx){
457 if(this.length!=xx.length)throw new IllegalArgumentException("Length of weights array, " + xx.length + ", must be the same as the length of the instance internal array, " + this.length);
458 ArrayMaths wm = new ArrayMaths(xx);
459 this.convertWeights(wm);
460 this.weightsSupplied = true;
461 }
462
463 public void setWeights(Float[] xx){
464 if(this.length!=xx.length)throw new IllegalArgumentException("Length of weights array, " + xx.length + ", must be the same as the length of the instance internal array, " + this.length);
465 ArrayMaths wm = new ArrayMaths(xx);
466 this.convertWeights(wm);
467 this.weightsSupplied = true;
468 }
469
470 public void setWeights(long[] xx){
471 if(this.length!=xx.length)throw new IllegalArgumentException("Length of weights array, " + xx.length + ", must be the same as the length of the instance internal array, " + this.length);
472 ArrayMaths wm = new ArrayMaths(xx);
473 this.convertWeights(wm);
474 this.weightsSupplied = true;
475 }
476
477 public void setWeights(Long[] xx){
478 if(this.length!=xx.length)throw new IllegalArgumentException("Length of weights array, " + xx.length + ", must be the same as the length of the instance internal array, " + this.length);
479 ArrayMaths wm = new ArrayMaths(xx);
480 this.convertWeights(wm);
481 this.weightsSupplied = true;
482 }
483
484 public void setWeights(int[] xx){
485 if(this.length!=xx.length)throw new IllegalArgumentException("Length of weights array, " + xx.length + ", must be the same as the length of the instance internal array, " + this.length);
486 ArrayMaths wm = new ArrayMaths(xx);
487 this.convertWeights(wm);
488 this.weightsSupplied = true;
489 }
490
491 public void setWeights(Integer[] xx){
492 if(this.length!=xx.length)throw new IllegalArgumentException("Length of weights array, " + xx.length + ", must be the same as the length of the instance internal array, " + this.length);
493 ArrayMaths wm = new ArrayMaths(xx);
494 this.convertWeights(wm);
495 this.weightsSupplied = true;
496 }
497
498 public void setWeights(short[] xx){
499 if(this.length!=xx.length)throw new IllegalArgumentException("Length of weights array, " + xx.length + ", must be the same as the length of the instance internal array, " + this.length);
500 ArrayMaths wm = new ArrayMaths(xx);
501 this.convertWeights(wm);
502 this.weightsSupplied = true;
503 }
504
505 public void setWeights(Short[] xx){
506 if(this.length!=xx.length)throw new IllegalArgumentException("Length of weights array, " + xx.length + ", must be the same as the length of the instance internal array, " + this.length);
507 ArrayMaths wm = new ArrayMaths(xx);
508 this.convertWeights(wm);
509 this.weightsSupplied = true;
510 }
511
512 public void setWeights(byte[] xx){
513 if(this.length!=xx.length)throw new IllegalArgumentException("Length of weights array, " + xx.length + ", must be the same as the length of the instance internal array, " + this.length);
514 ArrayMaths wm = new ArrayMaths(xx);
515 this.convertWeights(wm);
516 this.weightsSupplied = true;
517 }
518
519 public void setWeights(Byte[] xx){
520 if(this.length!=xx.length)throw new IllegalArgumentException("Length of weights array, " + xx.length + ", must be the same as the length of the instance internal array, " + this.length);
521 ArrayMaths wm = new ArrayMaths(xx);
522 this.convertWeights(wm);
523 this.weightsSupplied = true;
524 }
525
526 public void setWeights(BigDecimal[] xx){
527 if(this.length!=xx.length)throw new IllegalArgumentException("Length of weights array, " + xx.length + ", must be the same as the length of the instance internal array, " + this.length);
528 ArrayMaths wm = new ArrayMaths(xx);
529 this.convertWeights(wm);
530 this.weightsSupplied = true;
531 }
532
533 public void setWeights(BigInteger[] xx){
534 if(this.length!=xx.length)throw new IllegalArgumentException("Length of weights array, " + xx.length + ", must be the same as the length of the instance internal array, " + this.length);
535 ArrayMaths wm = new ArrayMaths(xx);
536 this.convertWeights(wm);
537 this.weightsSupplied = true;
538 }
539
540 public void setWeights(Complex[] xx){
541 if(this.length!=xx.length)throw new IllegalArgumentException("Length of weights array, " + xx.length + ", must be the same as the length of the instance internal array, " + this.length);
542 ArrayMaths wm = new ArrayMaths(xx);
543 this.convertWeights(wm);
544 this.weightsSupplied = true;
545 }
546
547 public void setWeights(Phasor[] xx){
548 if(this.length!=xx.length)throw new IllegalArgumentException("Length of weights array, " + xx.length + ", must be the same as the length of the instance internal array, " + this.length);
549 ArrayMaths wm = new ArrayMaths(xx);
550 this.convertWeights(wm);
551 this.weightsSupplied = true;
552 }
553
554 public void setWeights(Object[] xx){
555 if(this.length!=xx.length)throw new IllegalArgumentException("Length of weights array, " + xx.length + ", must be the same as the length of the instance internal array, " + this.length);
556 ArrayMaths wm = new ArrayMaths(xx);
557 this.convertWeights(wm);
558 this.weightsSupplied = true;
559 }
560
561 public void setWeights(Vector<Object> xx){
562 if(this.length!=xx.size())throw new IllegalArgumentException("Length of weights array, " + xx.size() + ", must be the same as the length of the instance internal array, " + this.length);
563 ArrayMaths wm = new ArrayMaths(xx);
564 this.convertWeights(wm);
565 this.weightsSupplied = true;
566 }
567
568 public void setWeights(ArrayList<Object> xx){
569 if(this.length!=xx.size())throw new IllegalArgumentException("Length of weights array, " + xx.size() + ", must be the same as the length of the instance internal array, " + this.length);
570 ArrayMaths wm = new ArrayMaths(xx);
571 this.convertWeights(wm);
572 this.weightsSupplied = true;
573 }
574
575 private void convertWeights(ArrayMaths wm){
576 switch(this.type){
577 case 1: switch(wm.typeIndex()){
578 case 0:
579 case 1:
580 case 2:
581 case 3:
582 case 4:
583 case 5:
584 case 6:
585 case 7:
586 case 8:
587 case 9:
588 case 10:
589 case 11: Double[] w1 = wm.getArray_as_Double();
590 this.amWeights = new ArrayMaths(w1);
591 break;
592 case 12:
593 case 13: BigDecimal[] a2 = this.getArray_as_BigDecimal();
594 for(int i=0; i<this.length; i++)this.array.add(a2[i]);
595 BigDecimal[] w2 = wm.getArray_as_BigDecimal();
596 this.amWeights = new ArrayMaths(w2);
597 a2 = null;
598 w2 = null;
599 break;
600 case 14:
601 case 15: Complex[] a3 = this.getArray_as_Complex();
602 for(int i=0; i<this.length; i++)this.array.add(a3[i]);
603 Complex[] w3 = wm.getArray_as_Complex();
604 this.amWeights = new ArrayMaths(w3);
605 break;
606 default: throw new IllegalArgumentException("This type number, " + this.type +", should not be possible here!!!!");
607
608 }
609 break;
610 case 12: switch(wm.typeIndex()){
611 case 0:
612 case 1:
613 case 2:
614 case 3:
615 case 4:
616 case 5:
617 case 6:
618 case 7:
619 case 8:
620 case 9:
621 case 10:
622 case 11: BigDecimal[] w4 = wm.getArray_as_BigDecimal();
623 this.amWeights = new ArrayMaths(w4);
624 w4 = null;
625 break;
626 case 12:
627 case 13: BigDecimal[] w5 = wm.getArray_as_BigDecimal();
628 this.amWeights = new ArrayMaths(w5);
629 w5 = null;
630 break;
631 case 14:
632 case 15: Complex[] a6 = this.getArray_as_Complex();
633 for(int i=0; i<this.length; i++)this.array.add(a6[i]);
634 Complex[] w6 = wm.getArray_as_Complex();
635 this.amWeights = new ArrayMaths(w6);
636 break;
637 default: throw new IllegalArgumentException("This type number, " + this.type +", should not be possible here!!!!");
638 }
639 break;
640 case 14: Complex[] a7 = this.getArray_as_Complex();
641 for(int i=0; i<this.length; i++)this.array.add(a7[i]);
642 Complex[] w7 = wm.getArray_as_Complex();
643 this.amWeights = new ArrayMaths(w7);
644 break;
645 default: throw new IllegalArgumentException("This type number, " + this.type +", should not be possible here!!!!");
646 }
647 }
648
649 // ARITMETIC MEANS (INSTANCE)
650 public double mean(){
651 return this.mean_as_double();
652
653 }
654
655 public double mean_as_double(){
656 double mean = 0.0D;
657 switch(this.type){
658 case 1: double[] dd = this.getArray_as_double();
659 for(int i=0; i<this.length; i++){
660 mean += dd[i];
661 }
662 mean /= (double)this.length;
663 break;
664 case 12: BigDecimal[] bd = this.getArray_as_BigDecimal();
665 BigDecimal meanbd = BigDecimal.ZERO;
666 for(int i=0; i<this.length; i++)meanbd = meanbd.add(bd[i]);
667 meanbd = meanbd.divide(new BigDecimal((double)this.length), BigDecimal.ROUND_HALF_UP);
668 mean = meanbd.doubleValue();
669 bd = null;
670 meanbd = null;
671 break;
672 case 14: throw new IllegalArgumentException("Complex cannot be converted to double");
673 default: throw new IllegalArgumentException("This type number, " + this.type +", should not be possible here!!!!");
674 }
675 return mean;
676 }
677
678 public BigDecimal mean_as_BigDecimal(){
679 BigDecimal mean = BigDecimal.ZERO;
680 switch(this.type){
681 case 1: double[] dd = this.getArray_as_double();
682 double meand= 0.0D;
683 for(int i=0; i<this.length; i++)meand += dd[i];
684 meand /= (double)this.length;
685 mean = new BigDecimal(meand);
686 break;
687 case 12: BigDecimal[] bd = this.getArray_as_BigDecimal();
688 for(int i=0; i<this.length; i++)mean = mean.add(bd[i]);
689 mean = mean.divide(new BigDecimal((double)this.length), BigDecimal.ROUND_HALF_UP);
690 bd = null;
691 break;
692 case 14: throw new IllegalArgumentException("Complex cannot be converted to BigDecimal");
693 default: throw new IllegalArgumentException("This type number, " + this.type +", should not be possible here!!!!");
694 }
695 return mean;
696 }
697
698 public Complex mean_as_Complex(){
699 Complex mean = Complex.zero();
700 switch(this.type){
701 case 1: double[] dd = this.getArray_as_double();
702 double meand= 0.0D;
703 for(int i=0; i<this.length; i++)meand += dd[i];
704 meand /= (double)this.length;
705 mean = new Complex(meand);
706 break;
707 case 12: BigDecimal[] bd = this.getArray_as_BigDecimal();
708 BigDecimal meanbd = BigDecimal.ZERO;
709 for(int i=0; i<this.length; i++)meanbd = meanbd.add(bd[i]);
710 meanbd = meanbd.divide(new BigDecimal((double)this.length), BigDecimal.ROUND_HALF_UP);
711 mean = new Complex(meanbd.doubleValue());
712 bd = null;
713 meanbd = null;
714 break;
715 case 14: Complex[] cc = this.getArray_as_Complex();
716 for(int i=0; i<this.length; i++)mean = mean.plus(cc[i]);
717 mean = mean.over(new Complex((double)this.length));
718 break;
719 default: throw new IllegalArgumentException("This type number, " + this.type +", should not be possible here!!!!");
720 }
721 return mean;
722 }
723
724
725 // WEIGHTED ARITMETIC MEANS (INSTANCE)
726 public double weightedMean(){
727 return this.weightedMean_as_double();
728 }
729
730 public double weightedMean_as_double(){
731 if(!this.weightsSupplied){
732 System.out.println("weightedMean_as_double: no weights supplied - unweighted mean returned");
733 return this.mean_as_double();
734 }
735 else{
736 boolean holdW = Stat.weightingOptionS;
737 if(this.weightingReset){
738 if(this.weightingOptionI){
739 Stat.weightingOptionS = true;
740 }
741 else{
742 Stat.weightingOptionS = false;
743 }
744 }
745 double mean = 0.0D;
746 switch(this.type){
747 case 1: double[] dd = this.getArray_as_double();
748 double[] wwd = this.amWeights.getArray_as_double();
749 mean = Stat.mean(dd, wwd);
750 break;
751 case 12: BigDecimal[] bd = this.getArray_as_BigDecimal();
752 BigDecimal[] wwb = this.amWeights.getArray_as_BigDecimal();
753 mean = (Stat.mean(bd, wwb)).doubleValue();
754 bd = null;
755 wwb = null;
756 break;
757 case 14: throw new IllegalArgumentException("Complex cannot be converted to double");
758 default: throw new IllegalArgumentException("This type number, " + this.type +", should not be possible here!!!!");
759 }
760 Stat.weightingOptionS = holdW;
761 return mean;
762 }
763 }
764
765 public BigDecimal weightedMean_as_BigDecimal(){
766 if(!this.weightsSupplied){
767 System.out.println("weightedMean_as_BigDecimal: no weights supplied - unweighted mean returned");
768 return this.mean_as_BigDecimal();
769 }
770 else{
771 boolean holdW = Stat.weightingOptionS;
772 if(this.weightingReset){
773 if(this.weightingOptionI){
774 Stat.weightingOptionS = true;
775 }
776 else{
777 Stat.weightingOptionS = false;
778 }
779 }
780 BigDecimal mean = BigDecimal.ZERO;
781 switch(this.type){
782 case 1:
783 case 12: BigDecimal[] bd = this.getArray_as_BigDecimal();
784 BigDecimal[] wwb = this.amWeights.getArray_as_BigDecimal();
785 mean = Stat.mean(bd, wwb);
786 bd = null;
787 wwb = null;
788 break;
789 case 14: throw new IllegalArgumentException("Complex cannot be converted to BigDecimal");
790 default: throw new IllegalArgumentException("This type number, " + this.type +", should not be possible here!!!!");
791 }
792 Stat.weightingOptionS = holdW;
793 return mean;
794 }
795 }
796
797 public Complex weightedMean_as_Complex(){
798 if(!this.weightsSupplied){
799 System.out.println("weightedMean_as_Complex: no weights supplied - unweighted mean returned");
800 return this.mean_as_Complex();
801 }
802 else{
803 boolean holdW = Stat.weightingOptionS;
804 if(this.weightingReset){
805 if(this.weightingOptionI){
806 Stat.weightingOptionS = true;
807 }
808 else{
809 Stat.weightingOptionS = false;
810 }
811 }
812 Complex mean = Complex.zero();
813 switch(this.type){
814 case 1: double[] dd = this.getArray_as_double();
815 double[] wwd = this.amWeights.getArray_as_double();
816 mean = new Complex(Stat.mean(dd, wwd));
817 break;
818 case 12: BigDecimal[] bd = this.getArray_as_BigDecimal();
819 BigDecimal[] wwb = this.amWeights.getArray_as_BigDecimal();
820 mean = new Complex((Stat.mean(bd, wwb)).doubleValue());
821 bd = null;
822 wwb = null;
823 break;
824 case 14: Complex[] cc = this.getArray_as_Complex();
825 Complex[] wwc = this.amWeights.getArray_as_Complex();
826 mean = Stat.mean(cc, wwc);
827 break;
828 default: throw new IllegalArgumentException("This type number, " + this.type +", should not be possible here!!!!");
829 }
830 Stat.weightingOptionS = holdW;
831 return mean;
832 }
833 }
834
835 // SUBTRACT AN ARITMETIC MEAN FROM AN ARRAY (INSTANCE)
836 public double[] subtractMean(){
837 return this.subtractMean_as_double();
838 }
839
840 public double[] subtractMean_as_double(){
841 double[] arrayminus = new double[this.length];
842 switch(this.type){
843 case 1: double meand = this.mean_as_double();
844 ArrayMaths amd = this.minus(meand);
845 arrayminus = amd.getArray_as_double();
846 break;
847 case 12: BigDecimal meanb = this.mean_as_BigDecimal();
848 ArrayMaths amb = this.minus(meanb);
849 arrayminus = amb.getArray_as_double();
850 meanb = null;
851 break;
852 case 14: throw new IllegalArgumentException("Complex cannot be converted to double");
853 default: throw new IllegalArgumentException("This type number, " + this.type +", should not be possible here!!!!");
854 }
855 return arrayminus;
856 }
857
858 public BigDecimal[] subtractMean_as_BigDecimal(){
859 BigDecimal[] arrayminus = new BigDecimal[this.length];
860 switch(this.type){
861 case 1:
862 case 12: BigDecimal meanb = this.mean_as_BigDecimal();
863 ArrayMaths amb = this.minus(meanb);
864 arrayminus = amb.getArray_as_BigDecimal();
865 meanb = null;
866 break;
867 case 14: throw new IllegalArgumentException("Complex cannot be converted to BigDecimal");
868 default: throw new IllegalArgumentException("This type number, " + this.type +", should not be possible here!!!!");
869 }
870 return arrayminus;
871 }
872
873 public Complex[] subtractMean_as_Complex(){
874 Complex[] arrayminus = new Complex[this.length];
875 switch(this.type){
876 case 1: double meand = this.mean_as_double();
877 ArrayMaths amd = this.minus(meand);
878 arrayminus = amd.getArray_as_Complex();
879 break;
880 case 12: BigDecimal meanb = this.mean_as_BigDecimal();
881 ArrayMaths amb = this.minus(meanb);
882 arrayminus = amb.getArray_as_Complex();
883 meanb = null;
884 break;
885 case 14: Complex meanc = this.mean_as_Complex();
886 ArrayMaths amc = this.minus(meanc);
887 arrayminus = amc.getArray_as_Complex();
888 break;
889 default: throw new IllegalArgumentException("This type number, " + this.type +", should not be possible here!!!!");
890 }
891 return arrayminus;
892 }
893
894 // SUBTRACT AN WEIGHTED ARITMETIC MEAN FROM AN ARRAY (INSTANCE)
895 public double[] subtractWeightedMean(){
896 return this.subtractWeightedMean_as_double();
897 }
898
899 public double[] subtractWeightedMean_as_double(){
900 if(!this.weightsSupplied){
901 System.out.println("subtractWeightedMean_as_double: no weights supplied - unweighted values returned");
902 return this.subtractMean_as_double();
903 }
904 else{
905 boolean holdW = Stat.weightingOptionS;
906 if(this.weightingReset){
907 if(this.weightingOptionI){
908 Stat.weightingOptionS = true;
909 }
910 else{
911 Stat.weightingOptionS = false;
912 }
913 }
914 double[] arrayminus = new double[this.length];
915 switch(this.type){
916 case 1: double meand = this.weightedMean_as_double();
917 ArrayMaths amd = this.minus(meand);
918 arrayminus = amd.getArray_as_double();
919 break;
920 case 12: BigDecimal meanb = this.weightedMean_as_BigDecimal();
921 ArrayMaths amb = this.minus(meanb);
922 arrayminus = amb.getArray_as_double();
923 meanb = null;
924 break;
925 case 14: throw new IllegalArgumentException("Complex cannot be converted to double");
926 default: throw new IllegalArgumentException("This type number, " + this.type +", should not be possible here!!!!");
927 }
928 Stat.weightingOptionS = holdW;
929 return arrayminus;
930 }
931 }
932
933 public BigDecimal[] subtractWeightedMean_as_BigDecimal(){
934 if(!this.weightsSupplied){
935 System.out.println("subtractWeightedMean_as_BigDecimal: no weights supplied - unweighted values returned");
936 return this.subtractMean_as_BigDecimal();
937 }
938 else{
939 boolean holdW = Stat.weightingOptionS;
940 if(this.weightingReset){
941 if(this.weightingOptionI){
942 Stat.weightingOptionS = true;
943 }
944 else{
945 Stat.weightingOptionS = false;
946 }
947 }
948 BigDecimal[] arrayminus = new BigDecimal[this.length];
949 switch(this.type){
950 case 1:
951 case 12: BigDecimal meanb = this.weightedMean_as_BigDecimal();
952 ArrayMaths amb = this.minus(meanb);
953 arrayminus = amb.getArray_as_BigDecimal();
954 meanb = null;
955 break;
956 case 14: throw new IllegalArgumentException("Complex cannot be converted to BigDecimal");
957 default: throw new IllegalArgumentException("This type number, " + this.type +", should not be possible here!!!!");
958 }
959 Stat.weightingOptionS = holdW;
960 return arrayminus;
961 }
962 }
963
964 public Complex[] subtractWeightedMean_as_Complex(){
965 if(!this.weightsSupplied){
966 System.out.println("subtractWeightedMean_as_Complex: no weights supplied - unweighted values returned");
967 return this.subtractMean_as_Complex();
968 }
969 else{
970 boolean holdW = Stat.weightingOptionS;
971 if(this.weightingReset){
972 if(this.weightingOptionI){
973 Stat.weightingOptionS = true;
974 }
975 else{
976 Stat.weightingOptionS = false;
977 }
978 }
979 Complex[] arrayminus = new Complex[this.length];
980 switch(this.type){
981 case 1: double meand = this.weightedMean_as_double();
982 ArrayMaths amd = this.minus(meand);
983 arrayminus = amd.getArray_as_Complex();
984 break;
985 case 12: BigDecimal meanb = this.weightedMean_as_BigDecimal();
986 ArrayMaths amb = this.minus(meanb);
987 arrayminus = amb.getArray_as_Complex();
988 meanb = null;
989 break;
990 case 14: Complex meanc = this.weightedMean_as_Complex();
991 ArrayMaths amc = this.minus(meanc);
992 arrayminus = amc.getArray_as_Complex();
993 break;
994 default: throw new IllegalArgumentException("This type number, " + this.type +", should not be possible here!!!!");
995 }
996 Stat.weightingOptionS = holdW;
997 return arrayminus;
998 }
999 }
1000
1001
1002 // GEOMETRIC MEAN(INSTANCE)
1003 public double geometricMean(){
1004 return this.geometricMean_as_double();
1005 }
1006
1007 public double geometricMean_as_double(){
1008 double gmean = 0.0D;
1009 switch(this.type){
1010 case 1: double[] dd = this.getArray_as_double();
1011 gmean = Stat.geometricMean(dd);
1012 break;
1013 case 12: BigDecimal[] bd = this.getArray_as_BigDecimal();
1014 gmean = Stat.geometricMean(bd);
1015 bd = null;
1016 break;
1017 case 14: throw new IllegalArgumentException("Complex cannot be converted to double");
1018 default: throw new IllegalArgumentException("This type number, " + this.type +", should not be possible here!!!!");
1019
1020 }
1021 return gmean;
1022 }
1023
1024 public Complex geometricMean_as_Complex(){
1025 Complex gmean = Complex.zero();
1026 switch(this.type){
1027 case 1:
1028 case 12:
1029 case 14: Complex[] cc = this.getArray_as_Complex();
1030 gmean = Stat.geometricMean(cc);
1031 break;
1032 default: throw new IllegalArgumentException("This type number, " + this.type +", should not be possible here!!!!");
1033 }
1034 return gmean;
1035 }
1036
1037
1038 // WEIGHTED GEOMETRIC MEAN(INSTANCE)
1039 public double weightedGeometricMean(){
1040 return this.weightedGeometricMean_as_double();
1041 }
1042
1043 public double weightedGeometricMean_as_double(){
1044 if(!this.weightsSupplied){
1045 System.out.println("weightedGeometricMean_as_double: no weights supplied - unweighted value returned");
1046 return this.geometricMean_as_double();
1047 }
1048 else{
1049 boolean holdW = Stat.weightingOptionS;
1050 if(this.weightingReset){
1051 if(this.weightingOptionI){
1052 Stat.weightingOptionS = true;
1053 }
1054 else{
1055 Stat.weightingOptionS = false;
1056 }
1057 }
1058 double gmean = 0.0D;
1059 switch(this.type){
1060 case 1: double[] dd = this.getArray_as_double();
1061 double[] ww = this.getArray_as_double();
1062 gmean = Stat.geometricMean(dd, ww);
1063 break;
1064 case 12: BigDecimal[] bd = this.getArray_as_BigDecimal();
1065 BigDecimal[] wd = this.getArray_as_BigDecimal();
1066 gmean = Stat.geometricMean(bd, wd);
1067 bd = null;
1068 wd = null;
1069 break;
1070 case 14: throw new IllegalArgumentException("Complex cannot be converted to double");
1071 default: throw new IllegalArgumentException("This type number, " + this.type +", should not be possible here!!!!");
1072
1073 }
1074 Stat.weightingOptionS = holdW;
1075 return gmean;
1076 }
1077 }
1078
1079 public Complex weightedGeometricMean_as_Complex(){
1080 if(!this.weightsSupplied){
1081 System.out.println("weightedGeometricMean_as_Complex: no weights supplied - unweighted value returned");
1082 return this.geometricMean_as_Complex();
1083 }
1084 else{
1085 boolean holdW = Stat.weightingOptionS;
1086 if(this.weightingReset){
1087 if(this.weightingOptionI){
1088 Stat.weightingOptionS = true;
1089 }
1090 else{
1091 Stat.weightingOptionS = false;
1092 }
1093 }
1094 Complex gmean = Complex.zero();
1095 switch(this.type){
1096 case 1:
1097 case 12:
1098 case 14: Complex[] cc = this.getArray_as_Complex();
1099 Complex[] ww = this.getArray_as_Complex();
1100 gmean = Stat.geometricMean(cc, ww);
1101 break;
1102 default: throw new IllegalArgumentException("This type number, " + this.type +", should not be possible here!!!!");
1103 }
1104 Stat.weightingOptionS = holdW;
1105 return gmean;
1106 }
1107 }
1108
1109 // HARMONIC MEANS (INSTANCE)
1110 public double harmonicMean(){
1111 return this.harmonicMean_as_double();
1112 }
1113
1114 public double harmonicMean_as_double(){
1115
1116 double mean = 0.0D;
1117 switch(this.type){
1118 case 1: double[] dd = this.getArray_as_double();
1119 mean = Stat.harmonicMean(dd);
1120 break;
1121 case 12: BigDecimal[] bd = this.getArray_as_BigDecimal();
1122 mean = (Stat.harmonicMean(bd)).doubleValue();
1123 bd = null;
1124 break;
1125 case 14: throw new IllegalArgumentException("Complex cannot be converted to double");
1126 default: throw new IllegalArgumentException("This type number, " + this.type +", should not be possible here!!!!");
1127 }
1128 return mean;
1129
1130 }
1131
1132 public BigDecimal harmonicMean_as_BigDecimal(){
1133
1134 BigDecimal mean = BigDecimal.ZERO;
1135 switch(this.type){
1136 case 1:
1137 case 12: BigDecimal[] bd = this.getArray_as_BigDecimal();
1138 mean = Stat.harmonicMean(bd);
1139 bd = null;
1140 break;
1141 case 14: throw new IllegalArgumentException("Complex cannot be converted to BigDecimal");
1142 default: throw new IllegalArgumentException("This type number, " + this.type +", should not be possible here!!!!");
1143 }
1144 return mean;
1145
1146 }
1147
1148 public Complex harmonicMean_as_Complex(){
1149
1150 Complex mean = Complex.zero();
1151 switch(this.type){
1152 case 1: double[] dd = this.getArray_as_double();
1153 mean = new Complex(Stat.harmonicMean(dd));
1154 break;
1155 case 12: BigDecimal[] bd = this.getArray_as_BigDecimal();
1156 mean = new Complex((Stat.harmonicMean(bd)).doubleValue());
1157 bd = null;
1158 break;
1159 case 14: Complex[] cc = this.getArray_as_Complex();
1160 mean = Stat.harmonicMean(cc);
1161 break;
1162 default: throw new IllegalArgumentException("This type number, " + this.type +", should not be possible here!!!!");
1163 }
1164 return mean;
1165
1166 }
1167
1168 // WEIGHTED HARMONIC MEANS (INSTANCE)
1169 public double weightedHarmonicMean(){
1170 return this.weightedHarmonicMean_as_double();
1171 }
1172
1173 public double weightedHarmonicMean_as_double(){
1174 if(!this.weightsSupplied){
1175 System.out.println("weightedHarmonicMean_as_double: no weights supplied - unweighted mean returned");
1176 return this.harmonicMean_as_double();
1177 }
1178 else{
1179 boolean holdW = Stat.weightingOptionS;
1180 if(this.weightingReset){
1181 if(this.weightingOptionI){
1182 Stat.weightingOptionS = true;
1183 }
1184 else{
1185 Stat.weightingOptionS = false;
1186 }
1187 }
1188 double mean = 0.0D;
1189 switch(this.type){
1190 case 1: double[] dd = this.getArray_as_double();
1191 double[] wwd = this.amWeights.getArray_as_double();
1192 mean = Stat.harmonicMean(dd, wwd);
1193 break;
1194 case 12: BigDecimal[] bd = this.getArray_as_BigDecimal();
1195 BigDecimal[] wwb = this.amWeights.getArray_as_BigDecimal();
1196 mean = (Stat.harmonicMean(bd, wwb)).doubleValue();
1197 bd = null;
1198 wwb = null;
1199 break;
1200 case 14: throw new IllegalArgumentException("Complex cannot be converted to double");
1201 default: throw new IllegalArgumentException("This type number, " + this.type +", should not be possible here!!!!");
1202 }
1203 Stat.weightingOptionS = holdW;
1204 return mean;
1205 }
1206 }
1207
1208 public BigDecimal weightedHarmonicMean_as_BigDecimal(){
1209 if(!this.weightsSupplied){
1210 System.out.println("weightedHarmonicMean_as_BigDecimal: no weights supplied - unweighted mean returned");
1211 return this.harmonicMean_as_BigDecimal();
1212 }
1213 else{
1214 boolean holdW = Stat.weightingOptionS;
1215 if(this.weightingReset){
1216 if(this.weightingOptionI){
1217 Stat.weightingOptionS = true;
1218 }
1219 else{
1220 Stat.weightingOptionS = false;
1221 }
1222 }
1223 BigDecimal mean = BigDecimal.ZERO;
1224 switch(this.type){
1225 case 1:
1226 case 12: BigDecimal[] bd = this.getArray_as_BigDecimal();
1227 BigDecimal[] wwb = this.amWeights.getArray_as_BigDecimal();
1228 mean = Stat.harmonicMean(bd, wwb);
1229 bd = null;
1230 wwb = null;
1231 break;
1232 case 14: throw new IllegalArgumentException("Complex cannot be converted to BigDecimal");
1233 default: throw new IllegalArgumentException("This type number, " + this.type +", should not be possible here!!!!");
1234 }
1235 Stat.weightingOptionS = holdW;
1236 return mean;
1237 }
1238 }
1239
1240 public Complex weightedHarmonicMean_as_Complex(){
1241 if(!this.weightsSupplied){
1242 System.out.println("weightedHarmonicMean_as_Complex: no weights supplied - unweighted mean returned");
1243 return this.harmonicMean_as_Complex();
1244 }
1245 else{
1246 boolean holdW = Stat.weightingOptionS;
1247 if(this.weightingReset){
1248 if(this.weightingOptionI){
1249 Stat.weightingOptionS = true;
1250 }
1251 else{
1252 Stat.weightingOptionS = false;
1253 }
1254 }
1255 Complex mean = Complex.zero();
1256 switch(this.type){
1257 case 1: double[] dd = this.getArray_as_double();
1258 double[] wwd = this.amWeights.getArray_as_double();
1259 mean = new Complex(Stat.harmonicMean(dd, wwd));
1260 break;
1261 case 12: BigDecimal[] bd = this.getArray_as_BigDecimal();
1262 BigDecimal[] wwb = this.amWeights.getArray_as_BigDecimal();
1263 mean = new Complex((Stat.harmonicMean(bd, wwb)).doubleValue());
1264 bd = null;
1265 wwb = null;
1266 break;
1267 case 14: Complex[] cc = this.getArray_as_Complex();
1268 Complex[] wwc = this.amWeights.getArray_as_Complex();
1269 mean = Stat.harmonicMean(cc, wwc);
1270 break;
1271 default: throw new IllegalArgumentException("This type number, " + this.type +", should not be possible here!!!!");
1272 }
1273 Stat.weightingOptionS = holdW;
1274 return mean;
1275 }
1276 }
1277
1278 // GENERALIZED MEANS [POWER MEANS](INSTANCE)
1279 public double generalizedMean(double m){
1280 return this.generalizedMean_as_double(m);
1281 }
1282
1283 public double generalizedMean_as_double(double m){
1284 double mean = 0.0D;
1285 switch(this.type){
1286 case 1: double[] dd = this.getArray_as_double();
1287 mean = Stat.generalizedMean(dd, m);
1288 break;
1289 case 12: BigDecimal[] bd = this.getArray_as_BigDecimal();
1290 mean = Stat.generalizedMean(bd, m);
1291 bd = null;
1292 break;
1293 case 14: throw new IllegalArgumentException("Complex cannot be converted to double");
1294 default: throw new IllegalArgumentException("This type number, " + this.type +", should not be possible here!!!!");
1295 }
1296 return mean;
1297
1298 }
1299
1300 public double generalizedMean(BigDecimal m){
1301 return this.generalizedMean_as_double(m);
1302 }
1303
1304 public double generalizedMean_as_double(BigDecimal m){
1305 double mean = 0.0D;
1306 switch(this.type){
1307 case 1:
1308 case 12: BigDecimal[] bd = this.getArray_as_BigDecimal();
1309 mean = Stat.generalizedMean(bd, m);
1310 bd = null;
1311 break;
1312 case 14: throw new IllegalArgumentException("Complex cannot be converted to BigDecimal");
1313 default: throw new IllegalArgumentException("This type number, " + this.type +", should not be possible here!!!!");
1314 }
1315 return mean;
1316 }
1317
1318 public Complex generalizedMean_as_Complex(double m){
1319 Complex mean = Complex.zero();
1320 switch(this.type){
1321 case 1: double[] dd = this.getArray_as_double();
1322 mean = new Complex(Stat.generalizedMean(dd, m));
1323 break;
1324 case 12: BigDecimal[] bd = this.getArray_as_BigDecimal();
1325 mean = new Complex(Stat.generalizedMean(bd, m));
1326 bd = null;
1327 break;
1328 case 14: Complex[] cc = this.getArray_as_Complex();
1329 mean = Stat.generalizedMean(cc, m);
1330 break;
1331 default: throw new IllegalArgumentException("This type number, " + this.type +", should not be possible here!!!!");
1332 }
1333 return mean;
1334 }
1335
1336 public Complex generalizedMean_as_Complex(Complex m){
1337 Complex mean = Complex.zero();
1338 switch(this.type){
1339 case 1:
1340 case 12:
1341 case 14: Complex[] cc = this.getArray_as_Complex();
1342 mean = Stat.generalizedMean(cc, m);
1343 break;
1344 default: throw new IllegalArgumentException("This type number, " + this.type +", should not be possible here!!!!");
1345 }
1346 return mean;
1347 }
1348
1349 public double generalisedMean(double m){
1350 return this.generalisedMean_as_double(m);
1351 }
1352
1353 public double generalisedMean_as_double(double m){
1354 double mean = 0.0D;
1355 switch(this.type){
1356 case 1: double[] dd = this.getArray_as_double();
1357 mean = Stat.generalisedMean(dd, m);
1358 break;
1359 case 12: BigDecimal[] bd = this.getArray_as_BigDecimal();
1360 mean = Stat.generalisedMean(bd, m);
1361 bd = null;
1362 break;
1363 case 14: throw new IllegalArgumentException("Complex cannot be converted to double");
1364 default: throw new IllegalArgumentException("This type number, " + this.type +", should not be possible here!!!!");
1365 }
1366 return mean;
1367 }
1368
1369 public double generalisedMean(BigDecimal m){
1370 return this.generalisedMean_as_double(m);
1371 }
1372
1373 public double generalisedMean_as_double(BigDecimal m){
1374 double mean = 0.0D;
1375 switch(this.type){
1376 case 1:
1377 case 12: BigDecimal[] bd = this.getArray_as_BigDecimal();
1378 mean = Stat.generalisedMean(bd, m);
1379 bd = null;
1380 break;
1381 case 14: throw new IllegalArgumentException("Complex cannot be converted to BigDecimal");
1382 default: throw new IllegalArgumentException("This type number, " + this.type +", should not be possible here!!!!");
1383 }
1384 return mean;
1385 }
1386
1387 public Complex generalisedMean_as_Complex(double m){
1388 Complex mean = Complex.zero();
1389 switch(this.type){
1390 case 1: double[] dd = this.getArray_as_double();
1391 mean = new Complex(Stat.generalisedMean(dd, m));
1392 break;
1393 case 12: BigDecimal[] bd = this.getArray_as_BigDecimal();
1394 mean = new Complex(Stat.generalisedMean(bd, m));
1395 bd = null;
1396 break;
1397 case 14: Complex[] cc = this.getArray_as_Complex();
1398 mean = Stat.generalisedMean(cc, m);
1399 break;
1400 default: throw new IllegalArgumentException("This type number, " + this.type +", should not be possible here!!!!");
1401 }
1402 return mean;
1403 }
1404
1405 public Complex generalisedMean_as_Complex(Complex m){
1406 Complex mean = Complex.zero();
1407 switch(this.type){
1408 case 1:
1409 case 12:
1410 case 14: Complex[] cc = this.getArray_as_Complex();
1411 mean = Stat.generalisedMean(cc, m);
1412 break;
1413 default: throw new IllegalArgumentException("This type number, " + this.type +", should not be possible here!!!!");
1414 }
1415 return mean;
1416 }
1417
1418
1419 // WEIGHTED GENERALIZED MEANS [WEIGHTED POWER MEANS](INSTANCE)
1420 public double weightedGeneralizedMean(double m){
1421 return this.weightedGeneralizedMean_as_double(m);
1422 }
1423
1424 public double weightedGeneralizedMean_as_double(double m){
1425 if(!this.weightsSupplied){
1426 System.out.println("weightedGeneralizedMean_as_double: no weights supplied - unweighted mean returned");
1427 return this.generalizedMean_as_double(m);
1428 }
1429 else{
1430 boolean holdW = Stat.weightingOptionS;
1431 if(this.weightingReset){
1432 if(this.weightingOptionI){
1433 Stat.weightingOptionS = true;
1434 }
1435 else{
1436 Stat.weightingOptionS = false;
1437 }
1438 }
1439 double mean = 0.0D;
1440 switch(this.type){
1441 case 1: double[] dd = this.getArray_as_double();
1442 double[] ww = this.amWeights.getArray_as_double();
1443 mean = Stat.generalisedMean(dd, ww, m);
1444 break;
1445 case 12: BigDecimal[] bd = this.getArray_as_BigDecimal();
1446 BigDecimal[] wd = this.amWeights.getArray_as_BigDecimal();
1447 mean = Stat.generalisedMean(bd, wd, m);
1448 bd = null;
1449 wd = null;
1450 break;
1451 case 14: throw new IllegalArgumentException("Complex cannot be converted to double");
1452 default: throw new IllegalArgumentException("This type number, " + this.type +", should not be possible here!!!!");
1453 }
1454 Stat.weightingOptionS = holdW;
1455 return mean;
1456 }
1457 }
1458
1459 public double weightedGeneralizedMean(BigDecimal m){
1460 return this.weightedGeneralizedMean_as_double(m);
1461 }
1462
1463 public double weightedGeneralizedMean_as_double(BigDecimal m){
1464 if(!this.weightsSupplied){
1465 System.out.println("weightedGeneralizedMean_as_double: no weights supplied - unweighted mean returned");
1466 return this.generalizedMean_as_double(m);
1467 }
1468 else{
1469 boolean holdW = Stat.weightingOptionS;
1470 if(this.weightingReset){
1471 if(this.weightingOptionI){
1472 Stat.weightingOptionS = true;
1473 }
1474 else{
1475 Stat.weightingOptionS = false;
1476 }
1477 }
1478 double mean = 0.0D;
1479 switch(this.type){
1480 case 1:
1481 case 12: BigDecimal[] bd = this.getArray_as_BigDecimal();
1482 BigDecimal[] wd = this.amWeights.getArray_as_BigDecimal();
1483 mean = Stat.generalisedMean(bd, wd, m);
1484 bd = null;
1485 break;
1486 case 14: throw new IllegalArgumentException("Complex cannot be converted to BigDecimal");
1487 default: throw new IllegalArgumentException("This type number, " + this.type +", should not be possible here!!!!");
1488 }
1489 Stat.weightingOptionS = holdW;
1490 return mean;
1491 }
1492 }
1493
1494 public Complex weightedGeneralizedMean_as_Complex(double m){
1495 if(!this.weightsSupplied){
1496 System.out.println("weightedGeneralizedMean_as_Complex: no weights supplied - unweighted mean returned");
1497 return this.generalizedMean_as_Complex(m);
1498 }
1499 else{
1500 boolean holdW = Stat.weightingOptionS;
1501 if(this.weightingReset){
1502 if(this.weightingOptionI){
1503 Stat.weightingOptionS = true;
1504 }
1505 else{
1506 Stat.weightingOptionS = false;
1507 }
1508 }
1509 Complex mean = Complex.zero();
1510 switch(this.type){
1511 case 1: double[] dd = this.getArray_as_double();
1512 double[] ww = this.amWeights.getArray_as_double();
1513 mean = new Complex(Stat.generalisedMean(dd, ww, m));
1514 break;
1515 case 12: BigDecimal[] bd = this.getArray_as_BigDecimal();
1516 BigDecimal[] wd = this.amWeights.getArray_as_BigDecimal();
1517 mean = new Complex(Stat.generalisedMean(bd, wd, m));
1518 bd = null;
1519 break;
1520 case 14: Complex[] cc = this.getArray_as_Complex();
1521 Complex[] cw = this.amWeights.getArray_as_Complex();
1522 mean = Stat.generalisedMean(cc, cw, m);
1523 break;
1524 default: throw new IllegalArgumentException("This type number, " + this.type +", should not be possible here!!!!");
1525 }
1526 Stat.weightingOptionS = holdW;
1527 return mean;
1528 }
1529 }
1530
1531 public Complex weightedGeneralizedMean_as_Complex(Complex m){
1532 Complex mean = Complex.zero();
1533 if(!this.weightsSupplied){
1534 System.out.println("weightedGeneralizedMean_as_dComplex: no weights supplied - unweighted mean returned");
1535 return this.generalizedMean_as_Complex(m);
1536 }
1537 else{
1538 boolean holdW = Stat.weightingOptionS;
1539 if(this.weightingReset){
1540 if(this.weightingOptionI){
1541 Stat.weightingOptionS = true;
1542 }
1543 else{
1544 Stat.weightingOptionS = false;
1545 }
1546 }
1547 switch(this.type){
1548 case 1:
1549 case 12:
1550 case 14: Complex[] cc = this.getArray_as_Complex();
1551 Complex[] cw = this.amWeights.getArray_as_Complex();
1552 mean = Stat.generalisedMean(cc, cw, m);
1553 break;
1554 default: throw new IllegalArgumentException("This type number, " + this.type +", should not be possible here!!!!");
1555 }
1556 Stat.weightingOptionS = holdW;
1557 return mean;
1558 }
1559 }
1560
1561 public double weightedGeneralisedMean(double m){
1562 return this.weightedGeneralizedMean_as_double(m);
1563 }
1564
1565 public double weightedGeneralisedMean_as_double(double m){
1566 return this.weightedGeneralizedMean_as_double(m);
1567 }
1568
1569 public double weightedGeneralisedMean(BigDecimal m){
1570 return this.weightedGeneralizedMean_as_double(m);
1571 }
1572
1573 public double weightedGeneralisedMean_as_double(BigDecimal m){
1574 return this.weightedGeneralizedMean_as_double(m);
1575 }
1576
1577 public Complex weightedGeneralisedMean_as_Complex(double m){
1578 return this.weightedGeneralizedMean_as_Complex(m);
1579 }
1580
1581 public Complex weightedGeneralisedMean_as_Complex(Complex m){
1582 return this.weightedGeneralizedMean_as_Complex(m);
1583 }
1584
1585 // INTERQUARTILE MEANS (INSTANCE)
1586 public double interQuartileMean(){
1587 return this.interQuartileMean_as_double();
1588 }
1589
1590 public double interQuartileMean_as_double(){
1591 double mean = 0.0D;
1592 switch(this.type){
1593 case 1: double[] dd = this.getArray_as_double();
1594 mean = Stat.interQuartileMean(dd);
1595 break;
1596 case 12: BigDecimal[] bd = this.getArray_as_BigDecimal();
1597 mean = (Stat.interQuartileMean(bd)).doubleValue();
1598 bd = null;
1599 break;
1600 case 14: throw new IllegalArgumentException("Complex interquartile mean is not supported");
1601 default: throw new IllegalArgumentException("This type number, " + this.type +", should not be possible here!!!!");
1602 }
1603 return mean;
1604 }
1605
1606 public BigDecimal interQuartileMean_as_BigDecimal(){
1607 BigDecimal mean = BigDecimal.ZERO;
1608 switch(this.type){
1609 case 1:
1610 case 12: BigDecimal[] bd = this.getArray_as_BigDecimal();
1611 mean = Stat.interQuartileMean(bd);
1612 bd = null;
1613 break;
1614 case 14: throw new IllegalArgumentException("Complex interquartile mean is not supported");
1615 default: throw new IllegalArgumentException("This type number, " + this.type +", should not be possible here!!!!");
1616 }
1617 return mean;
1618 }
1619
1620 // MEDIAN VALUE(INSTANCE)
1621 public double median(){
1622 return this.median_as_double();
1623 }
1624
1625 public double median_as_double(){
1626 double median = 0.0D;
1627 switch(this.type){
1628 case 1: double[] dd = this.getArray_as_double();
1629 median = Stat.median(dd);
1630 break;
1631 case 12: BigDecimal[] bd = this.getArray_as_BigDecimal();
1632 median = Stat.median(bd).doubleValue();
1633 bd = null;
1634 break;
1635 case 14: throw new IllegalArgumentException("Complex median value not supported");
1636 default: throw new IllegalArgumentException("This type number, " + this.type +", should not be possible here!!!!");
1637 }
1638 return median;
1639 }
1640
1641 public BigDecimal median_as_BigDecimal(){
1642 BigDecimal median = BigDecimal.ZERO;
1643 switch(this.type){
1644 case 1:
1645 case 12: BigDecimal[] bd = this.getArray_as_BigDecimal();
1646 median = Stat.median(bd);
1647 bd = null;
1648 break;
1649 case 14: throw new IllegalArgumentException("Complex median value not supported");
1650 default: throw new IllegalArgumentException("This type number, " + this.type +", should not be possible here!!!!");
1651 }
1652 return median;
1653 }
1654
1655 // ROOT MEAN SQUARE (INSTANCE METHODS)
1656 public double rms(){
1657 double rms = 0.0D;
1658 switch(this.type){
1659 case 1: double[] dd = this.getArray_as_double();
1660 rms = Stat.rms(dd);
1661 break;
1662 case 12: BigDecimal[] bd = this.getArray_as_BigDecimal();
1663 rms = Stat.rms(bd);
1664 bd = null;
1665 break;
1666 case 14: throw new IllegalArgumentException("Complex root mean square is not supported");
1667 default: throw new IllegalArgumentException("This type number, " + this.type +", should not be possible here!!!!");
1668 }
1669 return rms;
1670 }
1671
1672 // WEIGHTED ROOT MEAN SQUARE (INSTANCE METHODS)
1673 public double weightedRms(){
1674 if(!this.weightsSupplied){
1675 System.out.println("weightedRms: no weights supplied - unweighted rms returned");
1676 return this.rms();
1677 }
1678 else{
1679 boolean holdW = Stat.weightingOptionS;
1680 if(this.weightingReset){
1681 if(this.weightingOptionI){
1682 Stat.weightingOptionS = true;
1683 }
1684 else{
1685 Stat.weightingOptionS = false;
1686 }
1687 }
1688 double rms = 0.0D;
1689 switch(this.type){
1690 case 1: double[] dd = this.getArray_as_double();
1691 double[] ww = this.amWeights.getArray_as_double();
1692 rms = Stat.rms(dd, ww);
1693 break;
1694 case 12: BigDecimal[] bd = this.getArray_as_BigDecimal();
1695 BigDecimal[] wd = this.amWeights.getArray_as_BigDecimal();
1696 rms = Stat.rms(bd, wd);
1697 bd = null;
1698 wd = null;
1699 break;
1700 case 14: throw new IllegalArgumentException("Complex root mean square is not supported");
1701 default: throw new IllegalArgumentException("This type number, " + this.type +", should not be possible here!!!!");
1702 }
1703 Stat.weightingOptionS = holdW;
1704 return rms;
1705 }
1706 }
1707
1708
1709
1710 // SKEWNESS (INSTANCE METHODS)
1711 // Moment skewness
1712 public double momentSkewness(){
1713 boolean hold = Stat.nFactorOptionS;
1714 if(this.nFactorReset){
1715 if(this.nFactorOptionI){
1716 Stat.nFactorOptionS = true;
1717 }
1718 else{
1719 Stat.nFactorOptionS = false;
1720 }
1721 }
1722 double skewness = 0.0D;
1723 switch(this.type){
1724 case 1: double[] dd = this.getArray_as_double();
1725 skewness = Stat.momentSkewness(dd);
1726 break;
1727 case 12: BigDecimal[] bd = this.getArray_as_BigDecimal();
1728 skewness = Stat.momentSkewness(bd);
1729 bd = null;
1730 break;
1731 case 14: throw new IllegalArgumentException("Complex skewness is not supported");
1732 default: throw new IllegalArgumentException("This type number, " + this.type +", should not be possible here!!!!");
1733 }
1734 Stat.nFactorOptionS = hold;
1735 return skewness;
1736 }
1737
1738 public double momentSkewness_as_double(){
1739 return this.momentSkewness();
1740 }
1741
1742 // Median skewness
1743 public double medianSkewness(){
1744 boolean hold = Stat.nFactorOptionS;
1745 if(this.nFactorReset){
1746 if(this.nFactorOptionI){
1747 Stat.nFactorOptionS = true;
1748 }
1749 else{
1750 Stat.nFactorOptionS = false;
1751 }
1752 }
1753 double skewness = 0.0D;
1754 switch(this.type){
1755 case 1: double[] dd = this.getArray_as_double();
1756 skewness = Stat.medianSkewness(dd);
1757 break;
1758 case 12: BigDecimal[] bd = this.getArray_as_BigDecimal();
1759 skewness = Stat.medianSkewness(bd);
1760 bd = null;
1761 break;
1762 case 14: throw new IllegalArgumentException("Complex skewness is not supported");
1763 default: throw new IllegalArgumentException("This type number, " + this.type +", should not be possible here!!!!");
1764 }
1765 Stat.nFactorOptionS = hold;
1766 return skewness;
1767 }
1768
1769 public double medianSkewness_as_double(){
1770 return this.medianSkewness();
1771 }
1772
1773 // quartile skewness as double
1774 public double quartileSkewness(){
1775 boolean hold = Stat.nFactorOptionS;
1776 if(this.nFactorReset){
1777 if(this.nFactorOptionI){
1778 Stat.nFactorOptionS = true;
1779 }
1780 else{
1781 Stat.nFactorOptionS = false;
1782 }
1783 }
1784 double skewness = 0.0D;
1785 switch(this.type){
1786 case 1: double[] dd = this.getArray_as_double();
1787 skewness = Stat.quartileSkewness(dd);
1788 break;
1789 case 12: BigDecimal[] bd = this.getArray_as_BigDecimal();
1790 skewness = Stat.quartileSkewness(bd).doubleValue();
1791 bd = null;
1792 break;
1793 case 14: throw new IllegalArgumentException("Complex skewness is not supported");
1794 default: throw new IllegalArgumentException("This type number, " + this.type +", should not be possible here!!!!");
1795 }
1796 Stat.nFactorOptionS = hold;
1797 return skewness;
1798 }
1799
1800 public double quartileSkewness_as_double(){
1801 return this.quartileSkewness();
1802 }
1803
1804 // quartile skewness as BigDecimal
1805 public BigDecimal quartileSkewness_as_BigDecimal(){
1806 boolean hold = Stat.nFactorOptionS;
1807 if(this.nFactorReset){
1808 if(this.nFactorOptionI){
1809 Stat.nFactorOptionS = true;
1810 }
1811 else{
1812 Stat.nFactorOptionS = false;
1813 }
1814 }
1815 BigDecimal skewness = BigDecimal.ZERO;
1816 switch(this.type){
1817 case 1:
1818 case 12: BigDecimal[] bd = this.getArray_as_BigDecimal();
1819 skewness = Stat.quartileSkewness(bd);
1820 bd = null;
1821 break;
1822 case 14: throw new IllegalArgumentException("Complex skewness is not supported");
1823 default: throw new IllegalArgumentException("This type number, " + this.type +", should not be possible here!!!!");
1824 }
1825 Stat.nFactorOptionS = hold;
1826 return skewness;
1827 }
1828
1829
1830
1831 // KURTOSIS (INSTANCE METHODS)
1832 public double kurtosis(){
1833 return this.kurtosis_as_double();
1834 }
1835
1836 public double kurtosis_as_double(){
1837 boolean hold = Stat.nFactorOptionS;
1838 if(this.nFactorReset){
1839 if(this.nFactorOptionI){
1840 Stat.nFactorOptionS = true;
1841 }
1842 else{
1843 Stat.nFactorOptionS = false;
1844 }
1845 }
1846 double kurtosis = 0.0D;
1847 switch(this.type){
1848 case 1: double[] dd = this.getArray_as_double();
1849 kurtosis = Stat.kurtosis(dd);
1850 break;
1851 case 12: BigDecimal[] bd = this.getArray_as_BigDecimal();
1852 kurtosis = (Stat.kurtosis(bd)).doubleValue();
1853 bd = null;
1854 break;
1855 case 14: throw new IllegalArgumentException("Complex kurtosis is not supported");
1856 default: throw new IllegalArgumentException("This type number, " + this.type +", should not be possible here!!!!");
1857 }
1858 Stat.nFactorOptionS = hold;
1859 return kurtosis;
1860 }
1861
1862 public double curtosis(){
1863 return this.kurtosis_as_double();
1864 }
1865
1866 public double curtosis_as_double(){
1867 return this.kurtosis_as_double();
1868 }
1869
1870 public double kurtosisExcess(){
1871 return this.kurtosisExcess_as_double();
1872 }
1873
1874 public double excessKurtosis(){
1875 return this.kurtosisExcess_as_double();
1876 }
1877
1878 public double excessCurtosis(){
1879 return this.kurtosisExcess_as_double();
1880 }
1881
1882 public double kurtosisExcess_as_double(){
1883 boolean hold = Stat.nFactorOptionS;
1884 if(this.nFactorReset){
1885 if(this.nFactorOptionI){
1886 Stat.nFactorOptionS = true;
1887 }
1888 else{
1889 Stat.nFactorOptionS = false;
1890 }
1891 }
1892 double kurtosis = 0.0D;
1893 switch(this.type){
1894 case 1: double[] dd = this.getArray_as_double();
1895 kurtosis = Stat.kurtosisExcess(dd);
1896 break;
1897 case 12: BigDecimal[] bd = this.getArray_as_BigDecimal();
1898 kurtosis = (Stat.kurtosisExcess(bd)).doubleValue();
1899 bd = null;
1900 break;
1901 case 14: throw new IllegalArgumentException("Complex kurtosis is not supported");
1902 default: throw new IllegalArgumentException("This type number, " + this.type +", should not be possible here!!!!");
1903 }
1904 Stat.nFactorOptionS = hold;
1905 return kurtosis;
1906 }
1907
1908 public double excessKurtosis_as_double(){
1909 return kurtosisExcess_as_double();
1910 }
1911
1912
1913 public double curtosisExcess(){
1914 return this.kurtosisExcess_as_double();
1915 }
1916
1917 public double curtosisExcess_as_double(){
1918 return this.kurtosisExcess_as_double();
1919 }
1920
1921 public double excessCurtosis_as_double(){
1922 return this.kurtosisExcess_as_double();
1923 }
1924
1925 public BigDecimal kurtosis_as_BigDecimal(){
1926 boolean hold = Stat.nFactorOptionS;
1927 if(this.nFactorReset){
1928 if(this.nFactorOptionI){
1929 Stat.nFactorOptionS = true;
1930 }
1931 else{
1932 Stat.nFactorOptionS = false;
1933 }
1934 }
1935 BigDecimal kurtosis = BigDecimal.ZERO;
1936 switch(this.type){
1937 case 1:
1938 case 12: BigDecimal[] bd = this.getArray_as_BigDecimal();
1939 kurtosis = Stat.kurtosis(bd);
1940 bd = null;
1941 break;
1942 case 14: throw new IllegalArgumentException("Complex kurtosis is not supported");
1943 default: throw new IllegalArgumentException("This type number, " + this.type +", should not be possible here!!!!");
1944 }
1945 Stat.nFactorOptionS = hold;
1946 return kurtosis;
1947 }
1948
1949 public BigDecimal curtosis_as_BigDecimal(){
1950 return this.kurtosis_as_BigDecimal();
1951 }
1952
1953
1954 public BigDecimal kurtosisExcess_as_BigDecimal(){
1955 boolean hold = Stat.nFactorOptionS;
1956 if(this.nFactorReset){
1957 if(this.nFactorOptionI){
1958 Stat.nFactorOptionS = true;
1959 }
1960 else{
1961 Stat.nFactorOptionS = false;
1962 }
1963 }
1964 BigDecimal kurtosis = BigDecimal.ZERO;
1965 switch(this.type){
1966 case 1:
1967 case 12: BigDecimal[] bd = this.getArray_as_BigDecimal();
1968 kurtosis = Stat.kurtosisExcess(bd);
1969 bd = null;
1970 break;
1971 case 14: throw new IllegalArgumentException("Complex kurtosis is not supported");
1972 default: throw new IllegalArgumentException("This type number, " + this.type +", should not be possible here!!!!");
1973 }
1974 Stat.nFactorOptionS = hold;
1975 return kurtosis;
1976 }
1977
1978 public BigDecimal excessKurtosis_as_BigDecimal(){
1979 return this.kurtosisExcess_as_BigDecimal();
1980 }
1981
1982 public BigDecimal curtosisExcess_as_BigDecimal(){
1983 return this.kurtosisExcess_as_BigDecimal();
1984 }
1985
1986 public BigDecimal excessCurtosis_as_BigDecimal(){
1987 return this.kurtosisExcess_as_BigDecimal();
1988 }
1989
1990
1991
1992 // VARIANCES (INSTANCE METHODS)
1993 public double variance(){
1994 return this.variance_as_double();
1995 }
1996
1997 public double variance_as_double(){
1998 boolean hold = Stat.nFactorOptionS;
1999 if(this.nFactorReset){
2000 if(this.nFactorOptionI){
2001 Stat.nFactorOptionS = true;
2002 }
2003 else{
2004 Stat.nFactorOptionS = false;
2005 }
2006 }
2007 double variance = 0.0D;
2008 switch(this.type){
2009 case 1: double[] dd = this.getArray_as_double();
2010 variance = Stat.variance(dd);
2011 break;
2012 case 12: BigDecimal[] bd = this.getArray_as_BigDecimal();
2013 variance = (Stat.variance(bd)).doubleValue();
2014 bd = null;
2015 break;
2016 case 14: throw new IllegalArgumentException("Complex cannot be converted to double");
2017 default: throw new IllegalArgumentException("This type number, " + this.type +", should not be possible here!!!!");
2018 }
2019 Stat.nFactorOptionS = hold;
2020 return variance;
2021 }
2022
2023 public BigDecimal variance_as_BigDecimal(){
2024 boolean hold = Stat.nFactorOptionS;
2025 if(this.nFactorReset){
2026 if(this.nFactorOptionI){
2027 Stat.nFactorOptionS = true;
2028 }
2029 else{
2030 Stat.nFactorOptionS = false;
2031 }
2032 }
2033 BigDecimal variance = BigDecimal.ZERO;
2034 switch(this.type){
2035 case 1:
2036 case 12: BigDecimal[] bd = this.getArray_as_BigDecimal();
2037 variance = Stat.variance(bd);
2038 bd = null;
2039 break;
2040 case 14: throw new IllegalArgumentException("Complex cannot be converted to BigDecimal");
2041 default: throw new IllegalArgumentException("This type number, " + this.type +", should not be possible here!!!!");
2042 }
2043 Stat.nFactorOptionS = hold;
2044 return variance;
2045 }
2046
2047 public Complex variance_as_Complex(){
2048 boolean hold = Stat.nFactorOptionS;
2049 if(this.nFactorReset){
2050 if(this.nFactorOptionI){
2051 Stat.nFactorOptionS = true;
2052 }
2053 else{
2054 Stat.nFactorOptionS = false;
2055 }
2056 }
2057 Complex variance = Complex.zero();
2058 Complex[] cc = this.getArray_as_Complex();
2059 variance = Stat.variance(cc);
2060 Stat.nFactorOptionS = hold;
2061 return variance;
2062 }
2063
2064 public double variance_as_Complex_ConjugateCalcn(){
2065 boolean hold = Stat.nFactorOptionS;
2066 if(this.nFactorReset){
2067 if(this.nFactorOptionI){
2068 Stat.nFactorOptionS = true;
2069 }
2070 else{
2071 Stat.nFactorOptionS = false;
2072 }
2073 }
2074 Complex[] cc = this.getArray_as_Complex();
2075 double variance = Stat.varianceConjugateCalcn(cc);
2076 Stat.nFactorOptionS = hold;
2077 return variance;
2078 }
2079
2080 public double variance_of_ComplexModuli(){
2081 boolean hold = Stat.nFactorOptionS;
2082 if(this.nFactorReset){
2083 if(this.nFactorOptionI){
2084 Stat.nFactorOptionS = true;
2085 }
2086 else{
2087 Stat.nFactorOptionS = false;
2088 }
2089 }
2090 double[] re = this.array_as_modulus_of_Complex();
2091 double variance = Stat.variance(re);
2092 Stat.nFactorOptionS = hold;
2093 return variance;
2094 }
2095
2096 public double variance_of_ComplexRealParts(){
2097 boolean hold = Stat.nFactorOptionS;
2098 if(this.nFactorReset){
2099 if(this.nFactorOptionI){
2100 Stat.nFactorOptionS = true;
2101 }
2102 else{
2103 Stat.nFactorOptionS = false;
2104 }
2105 }
2106 double[] re = this.array_as_real_part_of_Complex();
2107 double variance = Stat.variance(re);
2108 Stat.nFactorOptionS = hold;
2109 return variance;
2110 }
2111
2112 public double variance_of_ComplexImaginaryParts(){
2113 boolean hold = Stat.nFactorOptionS;
2114 if(this.nFactorReset){
2115 if(this.nFactorOptionI){
2116 Stat.nFactorOptionS = true;
2117 }
2118 else{
2119 Stat.nFactorOptionS = false;
2120 }
2121 }
2122 double[] im = this.array_as_imaginary_part_of_Complex();
2123 double variance = Stat.variance(im);
2124 Stat.nFactorOptionS = hold;
2125 return variance;
2126 }
2127
2128 // WEIGHTED VARIANCES (INSTANCE METHODS)
2129 public double weightedVariance(){
2130 return this.weightedVariance_as_double();
2131 }
2132
2133 public double weightedVariance_as_double(){
2134 boolean hold = Stat.nFactorOptionS;
2135 if(this.nFactorReset){
2136 if(this.nFactorOptionI){
2137 Stat.nFactorOptionS = true;
2138 }
2139 else{
2140 Stat.nFactorOptionS = false;
2141 }
2142 }
2143 boolean hold2 = Stat.nEffOptionS;
2144 if(this.nEffReset){
2145 if(this.nEffOptionI){
2146 Stat.nEffOptionS = true;
2147 }
2148 else{
2149 Stat.nEffOptionS = false;
2150 }
2151 }
2152 boolean holdW = Stat.weightingOptionS;
2153 if(this.weightingReset){
2154 if(this.weightingOptionI){
2155 Stat.weightingOptionS = true;
2156 }
2157 else{
2158 Stat.weightingOptionS = false;
2159 }
2160 }
2161
2162 double varr = Double.NaN;
2163 if(!this.weightsSupplied){
2164 System.out.println("weightedVariance_as_double: no weights supplied - unweighted value returned");
2165 varr = this.variance_as_double();
2166 }
2167 else{
2168 double weightedVariance = 0.0D;
2169 switch(this.type){
2170 case 1: double[] dd = this.getArray_as_double();
2171 double[] ww = amWeights.getArray_as_double();
2172 weightedVariance = Stat.variance(dd, ww);
2173 break;
2174 case 12: BigDecimal[] bd = this.getArray_as_BigDecimal();
2175 BigDecimal[] wd = amWeights.getArray_as_BigDecimal();
2176 weightedVariance = (Stat.variance(bd, wd)).doubleValue();
2177 bd = null;
2178 wd = null;
2179 break;
2180 case 14: throw new IllegalArgumentException("Complex cannot be converted to double");
2181 default: throw new IllegalArgumentException("This type number, " + this.type +", should not be possible here!!!!");
2182 }
2183 varr = weightedVariance;
2184 }
2185 Stat.nFactorOptionS = hold;
2186 Stat.nEffOptionS = hold2;
2187 Stat.weightingOptionS = holdW;
2188 return varr;
2189
2190 }
2191
2192 public BigDecimal weightedVariance_as_BigDecimal(){
2193 boolean hold = Stat.nFactorOptionS;
2194 if(this.nFactorReset){
2195 if(this.nFactorOptionI){
2196 Stat.nFactorOptionS = true;
2197 }
2198 else{
2199 Stat.nFactorOptionS = false;
2200 }
2201 }
2202 boolean hold2 = Stat.nEffOptionS;
2203 if(this.nEffReset){
2204 if(this.nEffOptionI){
2205 Stat.nEffOptionS = true;
2206 }
2207 else{
2208 Stat.nEffOptionS = false;
2209 }
2210 }
2211 boolean holdW = Stat.weightingOptionS;
2212 if(this.weightingReset){
2213 if(this.weightingOptionI){
2214 Stat.weightingOptionS = true;
2215 }
2216 else{
2217 Stat.weightingOptionS = false;
2218 }
2219 }
2220 BigDecimal varr = BigDecimal.ZERO;
2221 if(!this.weightsSupplied){
2222 System.out.println("weightedVariance_as_BigDecimal: no weights supplied - unweighted value returned");
2223 varr = this.variance_as_BigDecimal();
2224 }
2225 else{
2226 BigDecimal weightedVariance = BigDecimal.ZERO;
2227 switch(this.type){
2228 case 1:
2229 case 12: BigDecimal[] bd = this.getArray_as_BigDecimal();
2230 BigDecimal[] wd = amWeights.getArray_as_BigDecimal();
2231 weightedVariance = Stat.variance(bd, wd);
2232 bd = null;
2233 wd = null;
2234 break;
2235 case 14: throw new IllegalArgumentException("Complex cannot be converted to BigDecimal");
2236 default: throw new IllegalArgumentException("This type number, " + this.type +", should not be possible here!!!!");
2237 }
2238 varr = weightedVariance;
2239 }
2240 Stat.nFactorOptionS = hold;
2241 Stat.nEffOptionS = hold2;
2242 Stat.weightingOptionS = holdW;
2243 return varr;
2244 }
2245
2246
2247 public Complex weightedVariance_as_Complex(){
2248 boolean hold = Stat.nFactorOptionS;
2249 if(this.nFactorReset){
2250 if(this.nFactorOptionI){
2251 Stat.nFactorOptionS = true;
2252 }
2253 else{
2254 Stat.nFactorOptionS = false;
2255 }
2256 }
2257 boolean hold2 = Stat.nEffOptionS;
2258 if(this.nEffReset){
2259 if(this.nEffOptionI){
2260 Stat.nEffOptionS = true;
2261 }
2262 else{
2263 Stat.nEffOptionS = false;
2264 }
2265 }
2266 boolean holdW = Stat.weightingOptionS;
2267 if(this.weightingReset){
2268 if(this.weightingOptionI){
2269 Stat.weightingOptionS = true;
2270 }
2271 else{
2272 Stat.weightingOptionS = false;
2273 }
2274 }
2275 Complex varr = Complex.zero();
2276 if(!this.weightsSupplied){
2277 System.out.println("weightedVariance_as_Complex: no weights supplied - unweighted value returned");
2278 varr = this.variance_as_Complex();
2279 }
2280 else{
2281 Complex weightedVariance = Complex.zero();
2282 Complex[] cc = this.getArray_as_Complex();
2283 Complex[] wc = amWeights.getArray_as_Complex();
2284 weightedVariance = Stat.variance(cc, wc);
2285 varr = weightedVariance;
2286 }
2287 Stat.nFactorOptionS = hold;
2288 Stat.nEffOptionS = hold2;
2289 Stat.weightingOptionS = holdW;
2290 return varr;
2291 }
2292
2293 public double weightedVariance_as_Complex_ConjugateCalcn(){
2294 boolean hold = Stat.nFactorOptionS;
2295 if(this.nFactorReset){
2296 if(this.nFactorOptionI){
2297 Stat.nFactorOptionS = true;
2298 }
2299 else{
2300 Stat.nFactorOptionS = false;
2301 }
2302 }
2303 boolean hold2 = Stat.nEffOptionS;
2304 if(this.nEffReset){
2305 if(this.nEffOptionI){
2306 Stat.nEffOptionS = true;
2307 }
2308 else{
2309 Stat.nEffOptionS = false;
2310 }
2311 }
2312 boolean holdW = Stat.weightingOptionS;
2313 if(this.weightingReset){
2314 if(this.weightingOptionI){
2315 Stat.weightingOptionS = true;
2316 }
2317 else{
2318 Stat.weightingOptionS = false;
2319 }
2320 }
2321 double varr = Double.NaN;
2322 if(!this.weightsSupplied){
2323 System.out.println("weightedVariance_as_Complex: no weights supplied - unweighted value returned");
2324 varr = this.variance_as_Complex_ConjugateCalcn();
2325 }
2326 else{
2327 Complex[] cc = this.getArray_as_Complex();
2328 Complex[] wc = amWeights.getArray_as_Complex();
2329 varr = Stat.varianceConjugateCalcn(cc, wc);
2330 }
2331 Stat.nFactorOptionS = hold;
2332 Stat.nEffOptionS = hold2;
2333 Stat.weightingOptionS = holdW;
2334 return varr;
2335 }
2336
2337 public double weightedVariance_of_ComplexModuli(){
2338 boolean hold = Stat.nFactorOptionS;
2339 if(this.nFactorReset){
2340 if(this.nFactorOptionI){
2341 Stat.nFactorOptionS = true;
2342 }
2343 else{
2344 Stat.nFactorOptionS = false;
2345 }
2346 }
2347 boolean hold2 = Stat.nEffOptionS;
2348 if(this.nEffReset){
2349 if(this.nEffOptionI){
2350 Stat.nEffOptionS = true;
2351 }
2352 else{
2353 Stat.nEffOptionS = false;
2354 }
2355 }
2356 boolean holdW = Stat.weightingOptionS;
2357 if(this.weightingReset){
2358 if(this.weightingOptionI){
2359 Stat.weightingOptionS = true;
2360 }
2361 else{
2362 Stat.weightingOptionS = false;
2363 }
2364 }
2365 double varr = Double.NaN;
2366 if(!this.weightsSupplied){
2367 System.out.println("weightedVariance_as_Complex: no weights supplied - unweighted value returned");
2368 varr = this.variance_of_ComplexModuli();
2369 }
2370 else{
2371 double[] cc = this.array_as_modulus_of_Complex();
2372 double[] wc = amWeights.array_as_modulus_of_Complex();
2373 varr = Stat.variance(cc, wc);
2374 }
2375 Stat.nFactorOptionS = hold;
2376 Stat.nEffOptionS = hold2;
2377 Stat.weightingOptionS = holdW;
2378 return varr;
2379 }
2380
2381 public double weightedVariance_of_ComplexRealParts(){
2382 boolean hold = Stat.nFactorOptionS;
2383 if(this.nFactorReset){
2384 if(this.nFactorOptionI){
2385 Stat.nFactorOptionS = true;
2386 }
2387 else{
2388 Stat.nFactorOptionS = false;
2389 }
2390 }
2391 boolean hold2 = Stat.nEffOptionS;
2392 if(this.nEffReset){
2393 if(this.nEffOptionI){
2394 Stat.nEffOptionS = true;
2395 }
2396 else{
2397 Stat.nEffOptionS = false;
2398 }
2399 }
2400 boolean holdW = Stat.weightingOptionS;
2401 if(this.weightingReset){
2402 if(this.weightingOptionI){
2403 Stat.weightingOptionS = true;
2404 }
2405 else{
2406 Stat.weightingOptionS = false;
2407 }
2408 }
2409 double varr = Double.NaN;
2410 if(!this.weightsSupplied){
2411 System.out.println("weightedVariance_as_Complex: no weights supplied - unweighted value returned");
2412 varr = this.variance_of_ComplexRealParts();
2413 }
2414 else{
2415 double[] cc = this.array_as_real_part_of_Complex();
2416 double[] wc = amWeights.array_as_real_part_of_Complex();
2417 varr = Stat.variance(cc, wc);
2418 }
2419 Stat.nFactorOptionS = hold;
2420 Stat.nEffOptionS = hold2;
2421 Stat.weightingOptionS = holdW;
2422 return varr;
2423 }
2424
2425 public double weightedVariance_of_ComplexImaginaryParts(){
2426 boolean hold = Stat.nFactorOptionS;
2427 if(this.nFactorReset){
2428 if(this.nFactorOptionI){
2429 Stat.nFactorOptionS = true;
2430 }
2431 else{
2432 Stat.nFactorOptionS = false;
2433 }
2434 }
2435 boolean hold2 = Stat.nEffOptionS;
2436 if(this.nEffReset){
2437 if(this.nEffOptionI){
2438 Stat.nEffOptionS = true;
2439 }
2440 else{
2441 Stat.nEffOptionS = false;
2442 }
2443 }
2444 boolean holdW = Stat.weightingOptionS;
2445 if(this.weightingReset){
2446 if(this.weightingOptionI){
2447 Stat.weightingOptionS = true;
2448 }
2449 else{
2450 Stat.weightingOptionS = false;
2451 }
2452 }
2453 double varr = Double.NaN;
2454 if(!this.weightsSupplied){
2455 System.out.println("weightedVariance_as_Complex: no weights supplied - unweighted value returned");
2456 varr = this.variance_of_ComplexImaginaryParts();
2457 }
2458 else{
2459 double[] cc = this.array_as_imaginary_part_of_Complex();
2460 double[] wc = amWeights.array_as_imaginary_part_of_Complex();
2461 varr = Stat.variance(cc, wc);
2462 }
2463 Stat.nFactorOptionS = hold;
2464 Stat.nEffOptionS = hold2;
2465 Stat.weightingOptionS = holdW;
2466 return varr;
2467 }
2468
2469
2470
2471 // STANDARD DEVIATIONS (INSTANCE METHODS)
2472 public double standardDeviation(){
2473 return this.standardDeviation_as_double();
2474 }
2475
2476 public double standardDeviation_as_double(){
2477 boolean hold = Stat.nFactorOptionS;
2478 if(this.nFactorReset){
2479 if(this.nFactorOptionI){
2480 Stat.nFactorOptionS = true;
2481 }
2482 else{
2483 Stat.nFactorOptionS = false;
2484 }
2485 }
2486
2487 double variance = 0.0D;
2488 switch(this.type){
2489 case 1: double[] dd = this.getArray_as_double();
2490 variance = Stat.variance(dd);
2491 break;
2492 case 12: BigDecimal[] bd = this.getArray_as_BigDecimal();
2493 variance = (Stat.variance(bd)).doubleValue();
2494 bd = null;
2495 break;
2496 case 14: throw new IllegalArgumentException("Complex cannot be converted to double");
2497 default: throw new IllegalArgumentException("This type number, " + this.type +", should not be possible here!!!!");
2498 }
2499 Stat.nFactorOptionS = hold;
2500 return Math.sqrt(variance);
2501 }
2502
2503 public Complex standardDeviation_as_Complex(){
2504 boolean hold = Stat.nFactorOptionS;
2505 if(this.nFactorReset){
2506 if(this.nFactorOptionI){
2507 Stat.nFactorOptionS = true;
2508 }
2509 else{
2510 Stat.nFactorOptionS = false;
2511 }
2512 }
2513
2514 Complex variance = Complex.zero();
2515 Complex[] cc = this.getArray_as_Complex();
2516 variance = Stat.variance(cc);
2517 Stat.nFactorOptionS = hold;
2518 return Complex.sqrt(variance);
2519 }
2520
2521 public double standardDeviation_as_Complex_ConjugateCalcn(){
2522 boolean hold = Stat.nFactorOptionS;
2523 if(this.nFactorReset){
2524 if(this.nFactorOptionI){
2525 Stat.nFactorOptionS = true;
2526 }
2527 else{
2528 Stat.nFactorOptionS = false;
2529 }
2530 }
2531
2532 Complex[] cc = this.getArray_as_Complex();
2533 double variance = Stat.varianceConjugateCalcn(cc);
2534 Stat.nFactorOptionS = hold;
2535 return Math.sqrt(variance);
2536 }
2537
2538 public double standardDeviation_of_ComplexModuli(){
2539 boolean hold = Stat.nFactorOptionS;
2540 if(this.nFactorReset){
2541 if(this.nFactorOptionI){
2542 Stat.nFactorOptionS = true;
2543 }
2544 else{
2545 Stat.nFactorOptionS = false;
2546 }
2547 }
2548 double[] re = this.array_as_modulus_of_Complex();
2549 double standardDeviation = Stat.standardDeviation(re);
2550 Stat.nFactorOptionS = hold;
2551 return standardDeviation;
2552 }
2553
2554 public double standardDeviation_of_ComplexRealParts(){
2555 boolean hold = Stat.nFactorOptionS;
2556 if(this.nFactorReset){
2557 if(this.nFactorOptionI){
2558 Stat.nFactorOptionS = true;
2559 }
2560 else{
2561 Stat.nFactorOptionS = false;
2562 }
2563 }
2564 double[] re = this.array_as_real_part_of_Complex();
2565 double standardDeviation = Stat.standardDeviation(re);
2566 Stat.nFactorOptionS = hold;
2567 return standardDeviation;
2568 }
2569
2570 public double standardDeviation_of_ComplexImaginaryParts(){
2571 boolean hold = Stat.nFactorOptionS;
2572 if(this.nFactorReset){
2573 if(this.nFactorOptionI){
2574 Stat.nFactorOptionS = true;
2575 }
2576 else{
2577 Stat.nFactorOptionS = false;
2578 }
2579 }
2580 double[] im = this.array_as_imaginary_part_of_Complex();
2581 double standardDeviation = Stat.standardDeviation(im);
2582 Stat.nFactorOptionS = hold;
2583 return standardDeviation;
2584 }
2585
2586 // WEIGHTED STANDARD DEVIATION (INSTANCE METHODS)
2587 public double weightedStandardDeviation(){
2588 return this.weightedStandardDeviation_as_double();
2589 }
2590
2591 public double weightedStandardDeviation_as_double(){
2592 boolean hold = Stat.nFactorOptionS;
2593 if(this.nFactorReset){
2594 if(this.nFactorOptionI){
2595 Stat.nFactorOptionS = true;
2596 }
2597 else{
2598 Stat.nFactorOptionS = false;
2599 }
2600 }
2601 boolean holdW = Stat.weightingOptionS;
2602 if(this.weightingReset){
2603 if(this.weightingOptionI){
2604 Stat.weightingOptionS = true;
2605 }
2606 else{
2607 Stat.weightingOptionS = false;
2608 }
2609 }
2610
2611 double varr = 0.0;
2612 if(!this.weightsSupplied){
2613 System.out.println("weightedStandardDeviation_as_double: no weights supplied - unweighted value returned");
2614 varr = this.standardDeviation_as_double();
2615 }
2616 else{
2617 double variance = 0.0D;
2618 switch(this.type){
2619 case 1: double[] dd = this.getArray_as_double();
2620 double[] ww = amWeights.getArray_as_double();
2621 variance = Stat.variance(dd, ww);
2622 break;
2623 case 12: BigDecimal[] bd = this.getArray_as_BigDecimal();
2624 BigDecimal[] wd = amWeights.getArray_as_BigDecimal();
2625 variance = (Stat.variance(bd, wd)).doubleValue();
2626 bd = null;
2627 wd = null;
2628 break;
2629 case 14: throw new IllegalArgumentException("Complex cannot be converted to double");
2630 default: throw new IllegalArgumentException("This type number, " + this.type +", should not be possible here!!!!");
2631 }
2632 varr = Math.sqrt(variance);
2633
2634 }
2635 Stat.nFactorOptionS = hold;
2636 Stat.weightingOptionS = holdW;
2637 return varr;
2638 }
2639
2640
2641 public Complex weightedStandardDeviation_as_Complex(){
2642 boolean hold = Stat.nFactorOptionS;
2643 if(this.nFactorReset){
2644 if(this.nFactorOptionI){
2645 Stat.nFactorOptionS = true;
2646 }
2647 else{
2648 Stat.nFactorOptionS = false;
2649 }
2650 }
2651 boolean holdW = Stat.weightingOptionS;
2652 if(this.weightingReset){
2653 if(this.weightingOptionI){
2654 Stat.weightingOptionS = true;
2655 }
2656 else{
2657 Stat.weightingOptionS = false;
2658 }
2659 }
2660
2661 Complex varr = Complex.zero();
2662 if(!this.weightsSupplied){
2663 System.out.println("weightedtandardDeviationS_as_Complex: no weights supplied - unweighted value returned");
2664 varr = this.standardDeviation_as_Complex();
2665 }
2666 else{
2667 Complex variance = Complex.zero();
2668 Complex[] cc = this.getArray_as_Complex();
2669 Complex[] wc = amWeights.getArray_as_Complex();
2670 variance = Stat.variance(cc, wc);
2671 varr = Complex.sqrt(variance);
2672 }
2673 Stat.nFactorOptionS = hold;
2674 Stat.weightingOptionS = holdW;
2675 return varr;
2676
2677 }
2678
2679 public double weightedStandardDeviation_as_Complex_ConjugateCalcn(){
2680 boolean hold = Stat.nFactorOptionS;
2681 if(this.nFactorReset){
2682 if(this.nFactorOptionI){
2683 Stat.nFactorOptionS = true;
2684 }
2685 else{
2686 Stat.nFactorOptionS = false;
2687 }
2688 }
2689 boolean holdW = Stat.weightingOptionS;
2690 if(this.weightingReset){
2691 if(this.weightingOptionI){
2692 Stat.weightingOptionS = true;
2693 }
2694 else{
2695 Stat.weightingOptionS = false;
2696 }
2697 }
2698 double varr = Double.NaN;
2699 if(!this.weightsSupplied){
2700 System.out.println("weightedtandardDeviationS_as_Complex: no weights supplied - unweighted value returned");
2701 varr = this.standardDeviation_as_Complex_ConjugateCalcn();
2702 }
2703 else{
2704 double variance = Double.NaN;
2705 Complex[] cc = this.getArray_as_Complex();
2706 Complex[] wc = amWeights.getArray_as_Complex();
2707 variance = Stat.varianceConjugateCalcn(cc, wc);
2708 varr = Math.sqrt(variance);
2709 }
2710 Stat.nFactorOptionS = hold;
2711 Stat.weightingOptionS = holdW;
2712 return varr;
2713
2714 }
2715
2716 public double weightedStandardDeviation_of_ComplexModuli(){
2717 boolean hold = Stat.nFactorOptionS;
2718 if(this.nFactorReset){
2719 if(this.nFactorOptionI){
2720 Stat.nFactorOptionS = true;
2721 }
2722 else{
2723 Stat.nFactorOptionS = false;
2724 }
2725 }
2726 boolean hold2 = Stat.nEffOptionS;
2727 if(this.nEffReset){
2728 if(this.nEffOptionI){
2729 Stat.nEffOptionS = true;
2730 }
2731 else{
2732 Stat.nEffOptionS = false;
2733 }
2734 }
2735 boolean holdW = Stat.weightingOptionS;
2736 if(this.weightingReset){
2737 if(this.weightingOptionI){
2738 Stat.weightingOptionS = true;
2739 }
2740 else{
2741 Stat.weightingOptionS = false;
2742 }
2743 }
2744 double varr = Double.NaN;
2745 if(!this.weightsSupplied){
2746 System.out.println("weightedStandardDeviation_as_Complex: no weights supplied - unweighted value returned");
2747 varr = this.standardDeviation_of_ComplexModuli();
2748 }
2749 else{
2750 double[] cc = this.array_as_modulus_of_Complex();
2751 double[] wc = amWeights.array_as_modulus_of_Complex();
2752 varr = Stat.standardDeviation(cc, wc);
2753 }
2754 Stat.nFactorOptionS = hold;
2755 Stat.nEffOptionS = hold2;
2756 Stat.weightingOptionS = holdW;
2757 return varr;
2758 }
2759
2760 public double weightedStandardDeviation_of_ComplexRealParts(){
2761 boolean hold = Stat.nFactorOptionS;
2762 if(this.nFactorReset){
2763 if(this.nFactorOptionI){
2764 Stat.nFactorOptionS = true;
2765 }
2766 else{
2767 Stat.nFactorOptionS = false;
2768 }
2769 }
2770 boolean hold2 = Stat.nEffOptionS;
2771 if(this.nEffReset){
2772 if(this.nEffOptionI){
2773 Stat.nEffOptionS = true;
2774 }
2775 else{
2776 Stat.nEffOptionS = false;
2777 }
2778 }
2779 boolean holdW = Stat.weightingOptionS;
2780 if(this.weightingReset){
2781 if(this.weightingOptionI){
2782 Stat.weightingOptionS = true;
2783 }
2784 else{
2785 Stat.weightingOptionS = false;
2786 }
2787 }
2788 double varr = Double.NaN;
2789 if(!this.weightsSupplied){
2790 System.out.println("weightedStandardDeviation_as_Complex: no weights supplied - unweighted value returned");
2791 varr = this.standardDeviation_of_ComplexRealParts();
2792 }
2793 else{
2794 double[] cc = this.array_as_real_part_of_Complex();
2795 double[] wc = amWeights.array_as_real_part_of_Complex();
2796 varr = Stat.standardDeviation(cc, wc);
2797 }
2798 Stat.nFactorOptionS = hold;
2799 Stat.nEffOptionS = hold2;
2800 Stat.weightingOptionS = holdW;
2801 return varr;
2802 }
2803
2804
2805 public double weightedStandardDeviation_of_ComplexImaginaryParts(){
2806 boolean hold = Stat.nFactorOptionS;
2807 if(this.nFactorReset){
2808 if(this.nFactorOptionI){
2809 Stat.nFactorOptionS = true;
2810 }
2811 else{
2812 Stat.nFactorOptionS = false;
2813 }
2814 }
2815 boolean hold2 = Stat.nEffOptionS;
2816 if(this.nEffReset){
2817 if(this.nEffOptionI){
2818 Stat.nEffOptionS = true;
2819 }
2820 else{
2821 Stat.nEffOptionS = false;
2822 }
2823 }
2824 boolean holdW = Stat.weightingOptionS;
2825 if(this.weightingReset){
2826 if(this.weightingOptionI){
2827 Stat.weightingOptionS = true;
2828 }
2829 else{
2830 Stat.weightingOptionS = false;
2831 }
2832 }
2833 double varr = Double.NaN;
2834 if(!this.weightsSupplied){
2835 System.out.println("weightedStandardDeviation_as_Complex: no weights supplied - unweighted value returned");
2836 varr = this.standardDeviation_of_ComplexImaginaryParts();
2837 }
2838 else{
2839 double[] cc = this.array_as_imaginary_part_of_Complex();
2840 double[] wc = amWeights.array_as_imaginary_part_of_Complex();
2841 varr = Stat.standardDeviation(cc, wc);
2842 }
2843 Stat.nFactorOptionS = hold;
2844 Stat.nEffOptionS = hold2;
2845 Stat.weightingOptionS = holdW;
2846 return varr;
2847 }
2848
2849
2850
2851
2852
2853 // STANDARD ERROR OF THE MEAN (INSTANCE METHODS)
2854 public double standardError(){
2855 return this.standardError_as_double();
2856 }
2857
2858 public double standardError_as_double(){
2859 boolean hold = Stat.nFactorOptionS;
2860 if(this.nFactorReset){
2861 if(this.nFactorOptionI){
2862 Stat.nFactorOptionS = true;
2863 }
2864 else{
2865 Stat.nFactorOptionS = false;
2866 }
2867 }
2868
2869 double standardError = 0.0D;
2870 switch(this.type){
2871 case 1: double[] dd = this.getArray_as_double();
2872 standardError = Stat.standardError(dd);
2873 break;
2874 case 12: BigDecimal[] bd = this.getArray_as_BigDecimal();
2875 standardError = Stat.standardError(bd);
2876 bd = null;
2877 break;
2878 case 14: throw new IllegalArgumentException("Complex cannot be converted to double");
2879 default: throw new IllegalArgumentException("This type number, " + this.type +", should not be possible here!!!!");
2880 }
2881 Stat.nFactorOptionS = hold;
2882 return standardError;
2883 }
2884
2885 public Complex standardError_as_Complex(){
2886 boolean hold = Stat.nFactorOptionS;
2887 if(this.nFactorReset){
2888 if(this.nFactorOptionI){
2889 Stat.nFactorOptionS = true;
2890 }
2891 else{
2892 Stat.nFactorOptionS = false;
2893 }
2894 }
2895
2896 Complex standardError = Complex.zero();
2897 Complex[] cc = this.getArray_as_Complex();
2898 standardError = Stat.standardError(cc);
2899 Stat.nFactorOptionS = hold;
2900 return standardError;
2901 }
2902
2903 public double standardError_as_Complex_ConjugateCalcn(){
2904 boolean hold = Stat.nFactorOptionS;
2905 if(this.nFactorReset){
2906 if(this.nFactorOptionI){
2907 Stat.nFactorOptionS = true;
2908 }
2909 else{
2910 Stat.nFactorOptionS = false;
2911 }
2912 }
2913 Complex[] cc = this.getArray_as_Complex();
2914 double standardError = Stat.standardErrorConjugateCalcn(cc);
2915 Stat.nFactorOptionS = hold;
2916 return standardError;
2917 }
2918
2919 public double standardError_of_ComplexModuli(){
2920 boolean hold = Stat.nFactorOptionS;
2921 if(this.nFactorReset){
2922 if(this.nFactorOptionI){
2923 Stat.nFactorOptionS = true;
2924 }
2925 else{
2926 Stat.nFactorOptionS = false;
2927 }
2928 }
2929 double[] re = this.array_as_modulus_of_Complex();
2930 double standardError = Stat.standardError(re);
2931 Stat.nFactorOptionS = hold;
2932 return standardError;
2933 }
2934
2935 public double standardError_of_ComplexRealParts(){
2936 boolean hold = Stat.nFactorOptionS;
2937 if(this.nFactorReset){
2938 if(this.nFactorOptionI){
2939 Stat.nFactorOptionS = true;
2940 }
2941 else{
2942 Stat.nFactorOptionS = false;
2943 }
2944 }
2945 double[] re = this.array_as_real_part_of_Complex();
2946 double standardError = Stat.standardError(re);
2947 Stat.nFactorOptionS = hold;
2948 return standardError;
2949 }
2950
2951 public double standardError_of_ComplexImaginaryParts(){
2952 boolean hold = Stat.nFactorOptionS;
2953 if(this.nFactorReset){
2954 if(this.nFactorOptionI){
2955 Stat.nFactorOptionS = true;
2956 }
2957 else{
2958 Stat.nFactorOptionS = false;
2959 }
2960 }
2961 double[] re = this.array_as_imaginary_part_of_Complex();
2962 double standardError = Stat.standardError(re);
2963 Stat.nFactorOptionS = hold;
2964 return standardError;
2965 }
2966
2967 // WEIGHTED STANDARD ERROR OF THE MEAN (INSTANCE METHODS)
2968 public double weightedStandardError(){
2969 return this.weightedStandardError_as_double();
2970 }
2971
2972 public double weightedStandardError_as_double(){
2973 boolean hold = Stat.nFactorOptionS;
2974 if(this.nFactorReset){
2975 if(this.nFactorOptionI){
2976 Stat.nFactorOptionS = true;
2977 }
2978 else{
2979 Stat.nFactorOptionS = false;
2980 }
2981 }
2982
2983 boolean hold2 = Stat.nEffOptionS;
2984 if(this.nEffReset){
2985 if(this.nEffOptionI){
2986 Stat.nEffOptionS = true;
2987 }
2988 else{
2989 Stat.nEffOptionS = false;
2990 }
2991 }
2992
2993 boolean holdW = Stat.weightingOptionS;
2994 if(this.weightingReset){
2995 if(this.weightingOptionI){
2996 Stat.weightingOptionS = true;
2997 }
2998 else{
2999 Stat.weightingOptionS = false;
3000 }
3001 }
3002
3003 double standardError = 0.0;
3004 if(!this.weightsSupplied){
3005 System.out.println("weightedStandardError_as_double: no weights supplied - unweighted value returned");
3006 standardError = this.standardError_as_double();
3007 }
3008 else{
3009 switch(this.type){
3010 case 1: double[] dd = this.getArray_as_double();
3011 double[] ww = amWeights.getArray_as_double();
3012 standardError = Stat.standardError(dd, ww);
3013 break;
3014 case 12: BigDecimal[] bd = this.getArray_as_BigDecimal();
3015 BigDecimal[] wd = amWeights.getArray_as_BigDecimal();
3016 standardError = Stat.standardError(bd, wd);
3017 bd = null;
3018 wd = null;
3019 break;
3020 case 14: throw new IllegalArgumentException("Complex cannot be converted to double");
3021 default: throw new IllegalArgumentException("This type number, " + this.type +", should not be possible here!!!!");
3022 }
3023 standardError = Math.sqrt(standardError);
3024 }
3025 Stat.nFactorOptionS = hold;
3026 Stat.nEffOptionS = hold2;
3027 Stat.weightingOptionS = holdW;
3028 return standardError;
3029 }
3030
3031
3032 public Complex weightedStandarError_as_Complex(){
3033 boolean hold = Stat.nFactorOptionS;
3034 if(this.nFactorReset){
3035 if(this.nFactorOptionI){
3036 Stat.nFactorOptionS = true;
3037 }
3038 else{
3039 Stat.nFactorOptionS = false;
3040 }
3041 }
3042
3043 boolean hold2 = Stat.nEffOptionS;
3044 if(this.nEffReset){
3045 if(this.nEffOptionI){
3046 Stat.nEffOptionS = true;
3047 }
3048 else{
3049 Stat.nEffOptionS = false;
3050 }
3051 }
3052
3053 boolean holdW = Stat.weightingOptionS;
3054 if(this.weightingReset){
3055 if(this.weightingOptionI){
3056 Stat.weightingOptionS = true;
3057 }
3058 else{
3059 Stat.weightingOptionS = false;
3060 }
3061 }
3062
3063 Complex standardError = Complex.zero();
3064 if(!this.weightsSupplied){
3065 System.out.println("weightedStandardError_as_Complex: no weights supplied - unweighted value returned");
3066 standardError = this.standardError_as_Complex();
3067 }
3068 else{
3069 Complex[] cc = this.getArray_as_Complex();
3070 Complex[] wc = amWeights.getArray_as_Complex();
3071 standardError = Stat.standardError(cc, wc);
3072 }
3073 Stat.nFactorOptionS = hold;
3074 Stat.nEffOptionS = hold2;
3075 Stat.weightingOptionS = holdW;
3076
3077 return standardError;
3078
3079 }
3080
3081
3082 public double weightedStandarError_as_Complex_ConjugateCalcn(){
3083 boolean hold = Stat.nFactorOptionS;
3084 if(this.nFactorReset){
3085 if(this.nFactorOptionI){
3086 Stat.nFactorOptionS = true;
3087 }
3088 else{
3089 Stat.nFactorOptionS = false;
3090 }
3091 }
3092
3093 boolean hold2 = Stat.nEffOptionS;
3094 if(this.nEffReset){
3095 if(this.nEffOptionI){
3096 Stat.nEffOptionS = true;
3097 }
3098 else{
3099 Stat.nEffOptionS = false;
3100 }
3101 }
3102
3103 boolean holdW = Stat.weightingOptionS;
3104 if(this.weightingReset){
3105 if(this.weightingOptionI){
3106 Stat.weightingOptionS = true;
3107 }
3108 else{
3109 Stat.weightingOptionS = false;
3110 }
3111 }
3112 double standardError = Double.NaN;
3113 if(!this.weightsSupplied){
3114 System.out.println("weightedStandardError_as_Complex: no weights supplied - unweighted value returned");
3115 standardError = this.standardError_as_Complex_ConjugateCalcn();
3116 }
3117 else{
3118 Complex[] cc = this.getArray_as_Complex();
3119 Complex[] wc = amWeights.getArray_as_Complex();
3120 standardError = Stat.standardErrorConjugateCalcn(cc, wc);
3121 }
3122 Stat.nFactorOptionS = hold;
3123 Stat.nEffOptionS = hold2;
3124 Stat.weightingOptionS = holdW;
3125
3126 return standardError;
3127
3128 }
3129
3130 public double weightedStandardError_of_ComplexModuli(){
3131 boolean hold = Stat.nFactorOptionS;
3132 if(this.nFactorReset){
3133 if(this.nFactorOptionI){
3134 Stat.nFactorOptionS = true;
3135 }
3136 else{
3137 Stat.nFactorOptionS = false;
3138 }
3139 }
3140 boolean hold2 = Stat.nEffOptionS;
3141 if(this.nEffReset){
3142 if(this.nEffOptionI){
3143 Stat.nEffOptionS = true;
3144 }
3145 else{
3146 Stat.nEffOptionS = false;
3147 }
3148 }
3149 boolean holdW = Stat.weightingOptionS;
3150 if(this.weightingReset){
3151 if(this.weightingOptionI){
3152 Stat.weightingOptionS = true;
3153 }
3154 else{
3155 Stat.weightingOptionS = false;
3156 }
3157 }
3158 double varr = Double.NaN;
3159 if(!this.weightsSupplied){
3160 System.out.println("weightedStandardError_as_Complex: no weights supplied - unweighted value returned");
3161 varr = this.standardError_of_ComplexModuli();
3162 }
3163 else{
3164 double[] cc = this.array_as_modulus_of_Complex();
3165 double[] wc = amWeights.array_as_modulus_of_Complex();
3166 varr = Stat.standardError(cc, wc);
3167 }
3168 Stat.nFactorOptionS = hold;
3169 Stat.nEffOptionS = hold2;
3170 Stat.weightingOptionS = holdW;
3171 return varr;
3172 }
3173
3174 public double weightedStandardError_of_ComplexRealParts(){
3175 boolean hold = Stat.nFactorOptionS;
3176 if(this.nFactorReset){
3177 if(this.nFactorOptionI){
3178 Stat.nFactorOptionS = true;
3179 }
3180 else{
3181 Stat.nFactorOptionS = false;
3182 }
3183 }
3184 boolean hold2 = Stat.nEffOptionS;
3185 if(this.nEffReset){
3186 if(this.nEffOptionI){
3187 Stat.nEffOptionS = true;
3188 }
3189 else{
3190 Stat.nEffOptionS = false;
3191 }
3192 }
3193 boolean holdW = Stat.weightingOptionS;
3194 if(this.weightingReset){
3195 if(this.weightingOptionI){
3196 Stat.weightingOptionS = true;
3197 }
3198 else{
3199 Stat.weightingOptionS = false;
3200 }
3201 }
3202 double varr = Double.NaN;
3203 if(!this.weightsSupplied){
3204 System.out.println("weightedStandardError_as_Complex: no weights supplied - unweighted value returned");
3205 varr = this.standardError_of_ComplexRealParts();
3206 }
3207 else{
3208 double[] cc = this.array_as_real_part_of_Complex();
3209 double[] wc = amWeights.array_as_real_part_of_Complex();
3210 varr = Stat.standardError(cc, wc);
3211 }
3212 Stat.nFactorOptionS = hold;
3213 Stat.nEffOptionS = hold2;
3214 Stat.weightingOptionS = holdW;
3215 return varr;
3216 }
3217
3218
3219 public double weightedStandardError_of_ComplexImaginaryParts(){
3220 boolean hold = Stat.nFactorOptionS;
3221 if(this.nFactorReset){
3222 if(this.nFactorOptionI){
3223 Stat.nFactorOptionS = true;
3224 }
3225 else{
3226 Stat.nFactorOptionS = false;
3227 }
3228 }
3229 boolean hold2 = Stat.nEffOptionS;
3230 if(this.nEffReset){
3231 if(this.nEffOptionI){
3232 Stat.nEffOptionS = true;
3233 }
3234 else{
3235 Stat.nEffOptionS = false;
3236 }
3237 }
3238 boolean holdW = Stat.weightingOptionS;
3239 if(this.weightingReset){
3240 if(this.weightingOptionI){
3241 Stat.weightingOptionS = true;
3242 }
3243 else{
3244 Stat.weightingOptionS = false;
3245 }
3246 }
3247 double varr = Double.NaN;
3248 if(!this.weightsSupplied){
3249 System.out.println("weightedStandardError_as_Complex: no weights supplied - unweighted value returned");
3250 varr = this.standardError_of_ComplexImaginaryParts();
3251 }
3252 else{
3253 double[] cc = this.array_as_imaginary_part_of_Complex();
3254 double[] wc = amWeights.array_as_imaginary_part_of_Complex();
3255 varr = Stat.standardError(cc, wc);
3256 }
3257 Stat.nFactorOptionS = hold;
3258 Stat.nEffOptionS = hold2;
3259 Stat.weightingOptionS = holdW;
3260 return varr;
3261 }
3262
3263
3264
3265
3266 // STANDARDIZE (INSTANCE METHODS)
3267 // Standardization of the internal array to a mean of 0 and a standard deviation of 1
3268 public double[] standardize(){
3269 double[] bb = null;
3270 switch(this.type){
3271 case 1:
3272 case 12: double[] dd = this.getArray_as_double();
3273 bb = Stat.standardize(dd);
3274 break;
3275 case 14: throw new IllegalArgumentException("Standardization of Complex is not supported");
3276 default: throw new IllegalArgumentException("This type number, " + this.type +", should not be possible here!!!!");
3277 }
3278 return bb;
3279 }
3280
3281 public double[] standardise(){
3282 return standardize();
3283 }
3284
3285
3286 // SCALE (INSTANCE METHODS)
3287 // Scale the internal array to a new mean and a new standard deviation
3288 public double[] scale(double mean, double sd){
3289 double[] bb = null;
3290 switch(this.type){
3291 case 1:
3292 case 12: double[] dd = this.getArray_as_double();
3293 bb = Stat.scale(dd, mean, sd);
3294 break;
3295 case 14: throw new IllegalArgumentException("Scaling of Complex is not supported");
3296 default: throw new IllegalArgumentException("This type number, " + this.type +", should not be possible here!!!!");
3297 }
3298 return bb;
3299 }
3300
3301
3302
3303 // VOLATILITY (INSTANCE METHODS)
3304 public double volatilityLogChange(){
3305 double volatilityLogChange = 0.0D;
3306 switch(this.type){
3307 case 1: double[] dd = this.getArray_as_double();
3308 volatilityLogChange = Stat.volatilityLogChange(dd);
3309 break;
3310 case 12: BigDecimal[] bd = this.getArray_as_BigDecimal();
3311 volatilityLogChange = Stat.volatilityLogChange(bd);
3312 bd = null;
3313 break;
3314 case 14: throw new IllegalArgumentException("Complex volatilty is not supported");
3315 default: throw new IllegalArgumentException("This type number, " + this.type +", should not be possible here!!!!");
3316 }
3317 return volatilityLogChange;
3318 }
3319
3320 public double volatilityPerCentChange(){
3321 double volatilityPerCentChange = 0.0D;
3322 switch(this.type){
3323 case 1: double[] dd = this.getArray_as_double();
3324 volatilityPerCentChange = Stat.volatilityPerCentChange(dd);
3325 break;
3326 case 12: BigDecimal[] bd = this.getArray_as_BigDecimal();
3327 volatilityPerCentChange = Stat.volatilityPerCentChange(bd);
3328 bd = null;
3329 break;
3330 case 14: throw new IllegalArgumentException("Complex volatilty is not supported");
3331 default: throw new IllegalArgumentException("This type number, " + this.type +", should not be possible here!!!!");
3332 }
3333 return volatilityPerCentChange;
3334 }
3335
3336 //COEFFICIENT OF VARIATION
3337 public double coefficientOfVariation(){
3338 boolean hold = Stat.nFactorOptionS;
3339 if(this.nFactorReset){
3340 if(this.nFactorOptionI){
3341 Stat.nFactorOptionS = true;
3342 }
3343 else{
3344 Stat.nFactorOptionS = false;
3345 }
3346 }
3347 double coefficientOfVariation = 0.0D;
3348 switch(this.type){
3349 case 1: double[] dd = this.getArray_as_double();
3350 coefficientOfVariation = Stat.coefficientOfVariation(dd);
3351 break;
3352 case 12: BigDecimal[] bd = this.getArray_as_BigDecimal();
3353 coefficientOfVariation = Stat.coefficientOfVariation(bd);
3354 bd = null;
3355 break;
3356 case 14: throw new IllegalArgumentException("Complex coefficient of variation is not supported");
3357 default: throw new IllegalArgumentException("This type number, " + this.type +", should not be possible here!!!!");
3358 }
3359 Stat.nFactorOptionS = hold;
3360 return coefficientOfVariation;
3361 }
3362
3363 public double weightedCoefficientOfVariation(){
3364 boolean hold = Stat.nFactorOptionS;
3365 if(this.nFactorReset){
3366 if(this.nFactorOptionI){
3367 Stat.nFactorOptionS = true;
3368 }
3369 else{
3370 Stat.nFactorOptionS = false;
3371 }
3372 }
3373 boolean holdW = Stat.weightingOptionS;
3374 if(this.weightingReset){
3375 if(this.weightingOptionI){
3376 Stat.weightingOptionS = true;
3377 }
3378 else{
3379 Stat.weightingOptionS = false;
3380 }
3381 }
3382 double coefficientOfVariation = 0.0D;
3383 switch(this.type){
3384 case 1: double[] dd = this.getArray_as_double();
3385 double[] wd = amWeights.getArray_as_double();
3386 coefficientOfVariation = Stat.coefficientOfVariation(dd, wd);
3387 break;
3388 case 12: BigDecimal[] bd = this.getArray_as_BigDecimal();
3389 BigDecimal[] bw = amWeights.getArray_as_BigDecimal();
3390 coefficientOfVariation = Stat.coefficientOfVariation(bd, bw);
3391 bd = null;
3392 bw = null;
3393 break;
3394 case 14: throw new IllegalArgumentException("Complex coefficient of variation is not supported");
3395 default: throw new IllegalArgumentException("This type number, " + this.type +", should not be possible here!!!!");
3396 }
3397 Stat.nFactorOptionS = hold;
3398 Stat.weightingOptionS = holdW;
3399 return coefficientOfVariation;
3400 }
3401
3402 // SHANNON ENTROPY (INSTANCE METHODS)
3403 // return Shannon entropy as bits
3404 public double shannonEntropy(){
3405 double entropy = 0.0D;
3406 switch(this.type){
3407 case 1:
3408 case 12: double[] dd = this.getArray_as_double();
3409 entropy = Stat.shannonEntropy(dd);
3410 break;
3411 case 14: throw new IllegalArgumentException("Complex Shannon Entropy is not meaningful");
3412 default: throw new IllegalArgumentException("This type number, " + this.type +", should not be possible here!!!!");
3413 }
3414 return entropy;
3415 }
3416
3417 // return Shannon entropy as bits
3418 public double shannonEntropyBit(){
3419 double entropy = 0.0D;
3420 switch(this.type){
3421 case 1:
3422 case 12: double[] dd = this.getArray_as_double();
3423 entropy = Stat.shannonEntropy(dd);
3424 break;
3425 case 14: throw new IllegalArgumentException("Complex Shannon Entropy is not meaningful");
3426 default: throw new IllegalArgumentException("This type number, " + this.type +", should not be possible here!!!!");
3427 }
3428 return entropy;
3429 }
3430
3431 // return Shannon entropy as nats
3432 public double shannonEntropyNat(){
3433 double entropy = 0.0D;
3434 switch(this.type){
3435 case 1:
3436 case 12: double[] dd = this.getArray_as_double();
3437 entropy = Stat.shannonEntropyNat(dd);
3438 break;
3439 case 14: throw new IllegalArgumentException("Complex Shannon Entropy is not meaningful");
3440 default: throw new IllegalArgumentException("This type number, " + this.type +", should not be possible here!!!!");
3441 }
3442 return entropy;
3443 }
3444
3445 // return Shannon entropy as dits
3446 public double shannonEntropyDit(){
3447 double entropy = 0.0D;
3448 switch(this.type){
3449 case 1:
3450 case 12: double[] dd = this.getArray_as_double();
3451 entropy = Stat.shannonEntropyDit(dd);
3452 break;
3453 case 14: throw new IllegalArgumentException("Complex Shannon Entropy is not meaningful");
3454 default: throw new IllegalArgumentException("This type number, " + this.type +", should not be possible here!!!!");
3455 }
3456 return entropy;
3457 }
3458
3459 // RENYI ENTROPY (INSTANCE METHODS)
3460 // return Renyi entropy as bits
3461 public double renyiEntropy(double alpha){
3462 double entropy = 0.0D;
3463 switch(this.type){
3464 case 1:
3465 case 12: double[] dd = this.getArray_as_double();
3466 entropy = Stat.renyiEntropy(dd, alpha);
3467 break;
3468 case 14: throw new IllegalArgumentException("Complex Renyi Entropy is not meaningful");
3469 default: throw new IllegalArgumentException("This type number, " + this.type +", should not be possible here!!!!");
3470 }
3471 return entropy;
3472 }
3473
3474 // return Renyi entropy as bits
3475 public double renyiEntropyBit(double alpha){
3476 double entropy = 0.0D;
3477 switch(this.type){
3478 case 1:
3479 case 12: double[] dd = this.getArray_as_double();
3480 entropy = Stat.renyiEntropy(dd, alpha);
3481 break;
3482 case 14: throw new IllegalArgumentException("Complex Renyi Entropy is not meaningful");
3483 default: throw new IllegalArgumentException("This type number, " + this.type +", should not be possible here!!!!");
3484 }
3485 return entropy;
3486 }
3487
3488 // return Renyi entropy as nats
3489 public double renyiEntropyNat(double alpha){
3490 double entropy = 0.0D;
3491 switch(this.type){
3492 case 1:
3493 case 12: double[] dd = this.getArray_as_double();
3494 entropy = Stat.renyiEntropyNat(dd, alpha);
3495 break;
3496 case 14: throw new IllegalArgumentException("Complex Renyi Entropy is not meaningful");
3497 default: throw new IllegalArgumentException("This type number, " + this.type +", should not be possible here!!!!");
3498 }
3499 return entropy;
3500 }
3501
3502 // return Renyi entropy as dits
3503 public double renyiEntropyDit(double alpha){
3504 double entropy = 0.0D;
3505 switch(this.type){
3506 case 1:
3507 case 12: double[] dd = this.getArray_as_double();
3508 entropy = Stat.renyiEntropyDit(dd, alpha);
3509 break;
3510 case 14: throw new IllegalArgumentException("Complex Renyi Entropy is not meaningful");
3511 default: throw new IllegalArgumentException("This type number, " + this.type +", should not be possible here!!!!");
3512 }
3513 return entropy;
3514 }
3515
3516 // TSALLIS ENTROPY (INSTANCE METHODS)
3517 // return Tsallis entropy
3518 public double tsallisEntropyNat(double q){
3519 double entropy = 0.0D;
3520 switch(this.type){
3521 case 1:
3522 case 12: double[] dd = this.getArray_as_double();
3523 entropy = Stat.tsallisEntropyNat(dd, q);
3524 break;
3525 case 14: throw new IllegalArgumentException("Complex Tsallis Entropy is not meaningful");
3526 default: throw new IllegalArgumentException("This type number, " + this.type +", should not be possible here!!!!");
3527 }
3528 return entropy;
3529 }
3530
3531
3532 // GENERALIZED ENTROPY (INSTANCE METHODS)
3533 // return generalised entropy
3534 public double generalizedEntropyOneNat(double q, double r){
3535 double entropy = 0.0D;
3536 switch(this.type){
3537 case 1:
3538 case 12: double[] dd = this.getArray_as_double();
3539 entropy = Stat.generalizedEntropyOneNat(dd, q, r);
3540 break;
3541 case 14: throw new IllegalArgumentException("Complex Generalized Entropy is not meaningful");
3542 default: throw new IllegalArgumentException("This type number, " + this.type +", should not be possible here!!!!");
3543 }
3544 return entropy;
3545 }
3546
3547 public double generalisedEntropyOneNat(double q, double r){
3548 return generalizedEntropyOneNat(q, r);
3549 }
3550
3551 // OUTLIER DETECTION (INSTANCE)
3552 // Anscombe test for a upper outlier
3553 public ArrayList<Object> upperOutliersAnscombe(double constant){
3554 return this.upperOutliersAnscombe_as_double(constant);
3555 }
3556
3557 // Anscombe test for a upper outlier
3558 public ArrayList<Object> upperOutliersAnscombe_as_double(double constant){
3559
3560 switch(this.type){
3561 case 1: double[] dd = this.getArray_as_double();
3562 this.upperOutlierDetails = upperOutliersAnscombeAsArrayList(dd, constant);
3563 break;
3564 case 12: BigDecimal[] bd = this.getArray_as_BigDecimal();
3565 ArrayList<Object> ret = new ArrayList<Object>();
3566 ret = upperOutliersAnscombeAsArrayList(bd, new BigDecimal(constant));
3567 this.upperOutlierDetails.add((Integer)ret.get(0));
3568 BigDecimal[] bd1 = (BigDecimal[])ret.get(1);
3569 ArrayMaths am1 = new ArrayMaths(bd1);
3570 this.upperOutlierDetails.add(am1.getArray_as_Double());
3571 this.upperOutlierDetails.add((int[])ret.get(2));
3572 BigDecimal[] bd2 = (BigDecimal[])ret.get(3);
3573 ArrayMaths am2 = new ArrayMaths(bd2);
3574 this.upperOutlierDetails.add(am2.getArray_as_Double());
3575 break;
3576 case 14: throw new IllegalArgumentException("Outlier detection of Complex is not supported");
3577 default: throw new IllegalArgumentException("This type number, " + this.type +", should not be possible here!!!!");
3578 }
3579 this.upperDone = true;
3580 return this.upperOutlierDetails;
3581 }
3582
3583 // Anscombe test for a upper outlier
3584 public ArrayList<Object> upperOutliersAnscombe(BigDecimal constant){
3585 return this.upperOutliersAnscombe_as_BigDecimal(constant);
3586 }
3587
3588 // Anscombe test for a upper outlier
3589 public ArrayList<Object> upperOutliersAnscombe_as_BigDecimal(BigDecimal constant){
3590
3591 switch(this.type){
3592 case 1: double[] dd = this.getArray_as_double();
3593 ArrayList<Object> ret = new ArrayList<Object>();
3594 ret = upperOutliersAnscombeAsArrayList(dd, constant.doubleValue());
3595 this.upperOutlierDetails.add((Integer)ret.get(0));
3596 Double[] dd1 = (Double[])ret.get(1);
3597 ArrayMaths am1 = new ArrayMaths(dd1);
3598 this.upperOutlierDetails.add(am1.getArray_as_BigDecimal());
3599 this.upperOutlierDetails.add((int[])ret.get(2));
3600 Double[] dd2 = (Double[])ret.get(3);
3601 ArrayMaths am2 = new ArrayMaths(dd2);
3602 this.upperOutlierDetails.add(am2.getArray_as_BigDecimal());
3603 break;
3604 case 12: BigDecimal[] bd = this.getArray_as_BigDecimal();
3605 this.upperOutlierDetails = upperOutliersAnscombeAsArrayList(bd, constant);
3606 break;
3607 case 14: throw new IllegalArgumentException("Outlier detection of Complex is not supported");
3608 default: throw new IllegalArgumentException("This type number, " + this.type +", should not be possible here!!!!");
3609 }
3610 this.upperDone = true;
3611 return this.upperOutlierDetails;
3612 }
3613
3614
3615 public ArrayList<Object> upperOutliersAnscombe(BigInteger constant){
3616 return this.upperOutliersAnscombe_as_BigDecimal(new BigDecimal(constant));
3617 }
3618
3619 public ArrayList<Object> upperOutliersAnscombe_as_BigDecimal(BigInteger constant){
3620 return this.upperOutliersAnscombe_as_BigDecimal(new BigDecimal(constant));
3621 }
3622
3623 // Anscombe test for a lower outlier
3624 public ArrayList<Object> lowerOutliersAnscombe(double constant){
3625 return this.lowerOutliersAnscombe_as_double(constant);
3626 }
3627
3628 // Anscombe test for a lower outlier
3629 public ArrayList<Object> lowerOutliersAnscombe_as_double(double constant){
3630
3631 switch(this.type){
3632 case 1: double[] dd = this.getArray_as_double();
3633 this.lowerOutlierDetails = lowerOutliersAnscombeAsArrayList(dd, constant);
3634 break;
3635 case 12: BigDecimal[] bd = this.getArray_as_BigDecimal();
3636 ArrayList<Object> ret = new ArrayList<Object>();
3637 ret = lowerOutliersAnscombeAsArrayList(bd, new BigDecimal(constant));
3638 this.lowerOutlierDetails.add((Integer)ret.get(0));
3639 BigDecimal[] bd1 = (BigDecimal[])ret.get(1);
3640 ArrayMaths am1 = new ArrayMaths(bd1);
3641 this.lowerOutlierDetails.add(am1.getArray_as_Double());
3642 this.lowerOutlierDetails.add((int[])ret.get(2));
3643 BigDecimal[] bd2 = (BigDecimal[])ret.get(3);
3644 ArrayMaths am2 = new ArrayMaths(bd2);
3645 this.lowerOutlierDetails.add(am2.getArray_as_Double());
3646 break;
3647 case 14: throw new IllegalArgumentException("Outlier detection of Complex is not supported");
3648 default: throw new IllegalArgumentException("This type number, " + this.type +", should not be possible here!!!!");
3649 }
3650 this.lowerDone = true;
3651 return this.lowerOutlierDetails;
3652 }
3653
3654 public ArrayList<Object> lowerOutliersAnscombe(BigDecimal constant){
3655 return this.lowerOutliersAnscombe_as_BigDecimal(constant);
3656 }
3657
3658
3659 public ArrayList<Object> lowerOutliersAnscombe_as_BigDecimal(BigDecimal constant){
3660
3661 switch(this.type){
3662 case 1: double[] dd = this.getArray_as_double();
3663 ArrayList<Object> ret = new ArrayList<Object>();
3664 ret = lowerOutliersAnscombeAsArrayList(dd, constant.doubleValue());
3665 this.lowerOutlierDetails.add((Integer)ret.get(0));
3666 Double[] dd1 = (Double[])ret.get(1);
3667 ArrayMaths am1 = new ArrayMaths(dd1);
3668 this.lowerOutlierDetails.add(am1.getArray_as_BigDecimal());
3669 this.lowerOutlierDetails.add((int[])ret.get(2));
3670 Double[] dd2 = (Double[])ret.get(3);
3671 ArrayMaths am2 = new ArrayMaths(dd2);
3672 this.lowerOutlierDetails.add(am2.getArray_as_BigDecimal());
3673 break;
3674 case 12: BigDecimal[] bd = this.getArray_as_BigDecimal();
3675 this.lowerOutlierDetails = lowerOutliersAnscombeAsArrayList(bd, constant);
3676 break;
3677 case 14: throw new IllegalArgumentException("Outlier detection of Complex is not supported");
3678 default: throw new IllegalArgumentException("This type number, " + this.type +", should not be possible here!!!!");
3679 }
3680 this.lowerDone = true;
3681 return this.lowerOutlierDetails;
3682 }
3683
3684 public ArrayList<Object> lowerOutliersAnscombe(BigInteger constant){
3685 return this.lowerOutliersAnscombe_as_BigDecimal(new BigDecimal(constant));
3686 }
3687
3688 public ArrayList<Object> lowerOutliersAnscombe_as_BigDecimal(BigInteger constant){
3689 return this.lowerOutliersAnscombe_as_BigDecimal(new BigDecimal(constant));
3690 }
3691
3692
3693 // STATIC METHODS
3694 // WEIGHTING CHOICE (STATIC)
3695 // Set weights to 'big W' - multiplicative factor
3696 public static void setStaticWeightsToBigW(){
3697 Stat.weightingOptionS = false;
3698 }
3699
3700 // Set weights to 'little w' - uncertainties
3701 public static void setStaticWeightsToLittleW(){
3702 Stat.weightingOptionS = true;
3703 }
3704
3705 // CONVERSION OF WEIGHTING FACTORS
3706 // Converts weighting facors Wi to wi, i.e. to 1/sqrt(Wi)
3707 public static double[] convertBigWtoLittleW(double[] bigW){
3708 ArrayMaths am1 = new ArrayMaths(bigW);
3709 ArrayMaths am2 = am1.oneOverSqrt();
3710 return am2.getArray_as_double();
3711 }
3712
3713 public static float[] convertBigWtoLittleW(float[] bigW){
3714 ArrayMaths am1 = new ArrayMaths(bigW);
3715 ArrayMaths am2 = am1.oneOverSqrt();
3716 return am2.getArray_as_float();
3717 }
3718
3719 public static Complex[] convertBigWtoLittleW(Complex[] bigW){
3720 ArrayMaths am1 = new ArrayMaths(bigW);
3721 ArrayMaths am2 = am1.oneOverSqrt();
3722 return am2.getArray_as_Complex();
3723 }
3724
3725 public static double[] convertBigWtoLittleW(BigDecimal[] bigW){
3726 ArrayMaths am1 = new ArrayMaths(bigW);
3727 ArrayMaths am2 = am1.oneOverSqrt();
3728 return am2.getArray_as_double();
3729 }
3730
3731 public static double[] convertBigWtoLittleW(BigInteger[] bigW){
3732 ArrayMaths am1 = new ArrayMaths(bigW);
3733 ArrayMaths am2 = am1.oneOverSqrt();
3734 return am2.getArray_as_double();
3735 }
3736
3737 // private weighting calculation
3738 // returns weight w
3739 // litte w to one over little w squared if uncertainties used
3740 private static double[] invertAndSquare(double[] ww){
3741 double[] weight = Conv.copy(ww);
3742 if(Stat.weightingOptionS){
3743 ArrayMaths am = new ArrayMaths(ww);
3744 am = am.pow(2);
3745 am = am.invert();
3746 weight = am.array();
3747 }
3748 return weight;
3749 }
3750
3751 private static float[] invertAndSquare(float[] ww){
3752 float[] weight = Conv.copy(ww);
3753 if(Stat.weightingOptionS){
3754 ArrayMaths am = new ArrayMaths(ww);
3755 am = am.pow(2);
3756 am = am.invert();
3757 weight = am.array_as_float();
3758 }
3759 return weight;
3760 }
3761
3762 private static Complex[] invertAndSquare(Complex[] ww){
3763 Complex[] weight = Conv.copy(ww);
3764 if(Stat.weightingOptionS){
3765 ArrayMaths am = new ArrayMaths(ww);
3766 am = am.pow(2);
3767 am = am.invert();
3768 weight = am.array_as_Complex();
3769 }
3770 return weight;
3771 }
3772
3773 private static BigDecimal[] invertAndSquare(BigDecimal[] ww){
3774 BigDecimal[] weight = Conv.copy(ww);
3775 if(Stat.weightingOptionS){
3776 ArrayMaths am = new ArrayMaths(ww);
3777 am = am.pow(2);
3778 am = am.invert();
3779 weight = am.array_as_BigDecimal();
3780 }
3781 return weight;
3782 }
3783
3784 private static BigDecimal[] invertAndSquare(BigInteger[] ww){
3785 ArrayMaths am = new ArrayMaths(ww);
3786 BigDecimal[] weight = am.array_as_BigDecimal();
3787 if(Stat.weightingOptionS){
3788 am = am.pow(2);
3789 am = am.invert();
3790 weight = am.array_as_BigDecimal();
3791 }
3792 return weight;
3793 }
3794
3795
3796
3797 // DENOMINATOR CHOICE (STATIC)
3798 // Set standard deviation, variance and covariance denominators to n
3799 public static void setStaticDenominatorToN(){
3800 Stat.nFactorOptionS = true;
3801 }
3802
3803 // Set standard deviation, variance and covariance denominators to n
3804 public static void setStaticDenominatorToNminusOne(){
3805 Stat.nFactorOptionS = false;
3806 }
3807
3808
3809 // EFFECTIVE SAMPLE NUMBER
3810 // Repalce number of data points to the effective sample number in weighted calculations
3811 public static void useStaticEffectiveN(){
3812 Stat.nEffOptionS = true;
3813 }
3814
3815 // Repalce the effective sample number in weighted calculations by the number of data points
3816 public static void useStaticTrueN(){
3817 Stat.nEffOptionS = false;
3818 }
3819
3820 // Calculation of the effective sample number (double)
3821 public static double effectiveSampleNumber(double[] ww){
3822 double[] weight = Conv.copy(ww);
3823 if(Stat.weightingOptionS){
3824 ArrayMaths am = new ArrayMaths(ww);
3825 am = am.pow(2);
3826 am = am.invert();
3827 weight = am.array();
3828 }
3829 int n = weight.length;
3830
3831 double nEff = n;
3832 if(Stat.nEffOptionS){
3833 double sum2w = 0.0D;
3834 double sumw2 = 0.0D;
3835 for(int i=0; i<n; i++){
3836 sum2w += weight[i];
3837 sumw2 += weight[i]*weight[i];
3838 }
3839 sum2w *= sum2w;
3840 nEff = sum2w/sumw2;
3841 }
3842 return nEff;
3843 }
3844
3845 // Calculation of the sample number (float)
3846 public static float effectiveSampleNumber(float[] ww){
3847 float[] weight = Conv.copy(ww);
3848 if(Stat.weightingOptionS){
3849 ArrayMaths am = new ArrayMaths(ww);
3850 am = am.pow(2);
3851 am = am.invert();
3852 weight = am.array_as_float();
3853 }
3854 int n = weight.length;
3855
3856 float nEff = n;
3857 if(Stat.nEffOptionS){
3858 float sum2w = 0.0F;
3859 float sumw2 = 0.0F;
3860 for(int i=0; i<n; i++){
3861 sum2w += weight[i];
3862 sumw2 += weight[i]*weight[i];
3863 }
3864 sum2w *= sum2w;
3865 nEff = sum2w/sumw2;
3866 }
3867 return nEff;
3868 }
3869
3870 // Calculation of the sample number (Complex)
3871 public static Complex effectiveSampleNumber(Complex[] ww){
3872 Complex[] weight = Conv.copy(ww);
3873 if(Stat.weightingOptionS){
3874 ArrayMaths am = new ArrayMaths(ww);
3875 am = am.pow(2);
3876 am = am.invert();
3877 weight = am.array_as_Complex();
3878 }
3879 int n = weight.length;
3880
3881 Complex nEff = new Complex(n, 0.0);
3882 if(Stat.nEffOptionS){
3883 Complex sumw2 = Complex.zero();
3884 Complex sum2w = Complex.zero();
3885 for(int i=0; i<n; i++){
3886 sum2w = sum2w.plus(weight[i]);
3887 sumw2 = sumw2.plus(weight[i].times(weight[i]));
3888 }
3889 sum2w = sum2w.times(sum2w);
3890 nEff = sum2w.over(sumw2);
3891 }
3892 return nEff;
3893 }
3894
3895 // Calculation of the sample number (Complex - Conjugate formula)
3896 public static double effectiveSampleNumberConjugateCalcn(Complex[] ww){
3897 Complex[] weight = Conv.copy(ww);
3898 if(Stat.weightingOptionS){
3899 ArrayMaths am = new ArrayMaths(ww);
3900 am = am.pow(2);
3901 am = am.invert();
3902 weight = am.array_as_Complex();
3903 }
3904 int n = weight.length;
3905
3906 double nEff = Double.NaN;
3907 if(Stat.nEffOptionS){
3908 Complex sumw2 = Complex.zero();
3909 Complex sum2w = Complex.zero();
3910 for(int i=0; i<n; i++){
3911 sum2w = sum2w.plus(weight[i]);
3912 sumw2 = sumw2.plus(weight[i].times(weight[i].conjugate()));
3913 }
3914 sum2w = sum2w.times(sum2w.conjugate());
3915 nEff = sum2w.getReal()/sumw2.getReal();
3916 }
3917 return nEff;
3918 }
3919
3920 // Calculation of the sample number (BigDecimal)
3921 public static BigDecimal effectiveSampleNumber(BigDecimal[] ww){
3922 BigDecimal[] weight = Conv.copy(ww);
3923 if(Stat.weightingOptionS){
3924 ArrayMaths am = new ArrayMaths(ww);
3925 am = am.pow(2);
3926 am = am.invert();
3927 weight = am.array_as_BigDecimal();
3928 }
3929 int n = weight.length;
3930
3931 BigDecimal nEff = new BigDecimal(new Integer(n).toString());
3932 if(Stat.nEffOptionS){
3933 BigDecimal sumw2 = BigDecimal.ZERO;
3934 BigDecimal sum2w = BigDecimal.ZERO;
3935 for(int i=0; i<n; i++){
3936 sum2w = sum2w.add(weight[i]);
3937 sumw2 = sumw2.add(weight[i].multiply(weight[i]));
3938 }
3939 sum2w = sum2w.multiply(sum2w);
3940 nEff = sum2w.divide(sumw2, BigDecimal.ROUND_HALF_UP);
3941 sumw2 = null;
3942 sum2w = null;
3943 weight = null;
3944 }
3945 return nEff;
3946 }
3947
3948 public static BigDecimal effectiveSampleNumber(BigInteger[] ww){
3949 ArrayMaths am = new ArrayMaths(ww);
3950 BigDecimal[] www = am.array_as_BigDecimal();
3951 return Stat.effectiveSampleNumber(www);
3952 }
3953
3954
3955 // ARITMETIC MEANS (STATIC)
3956
3957 // Arithmetic mean of a 1D array of doubles, aa
3958 public static double mean(double[] aa){
3959 int n = aa.length;
3960 double sum=0.0D;
3961 for(int i=0; i<n; i++){
3962 sum+=aa[i];
3963 }
3964 return sum/((double)n);
3965 }
3966
3967 // Arithmetic mean of a 1D array of floats, aa
3968 public static float mean(float[] aa){
3969 int n = aa.length;
3970 float sum=0.0F;
3971 for(int i=0; i<n; i++){
3972 sum+=aa[i];
3973 }
3974 return sum/((float)n);
3975 }
3976
3977 // Arithmetic mean of a 1D array of int, aa
3978 public static double mean(long[] aa){
3979 int n = aa.length;
3980 double sum=0.0D;
3981 for(int i=0; i<n; i++){
3982 sum+=(double)aa[i];
3983 }
3984 return sum/((double)n);
3985 }
3986
3987 // Arithmetic mean of a 1D array of int, aa
3988 public static double mean(int[] aa){
3989 int n = aa.length;
3990 double sum=0.0D;
3991 for(int i=0; i<n; i++){
3992 sum+=(double)aa[i];
3993 }
3994 return sum/((double)n);
3995 }
3996
3997 // Arithmetic mean of a 1D array of short, aa
3998 public static double mean(short[] aa){
3999 int n = aa.length;
4000 double sum=0.0D;
4001 for(int i=0; i<n; i++){
4002 sum+=(double)aa[i];
4003 }
4004 return sum/((double)n);
4005 }
4006
4007 // Arithmetic mean of a 1D array of byte, aa
4008 public static double mean(byte[] aa){
4009 int n = aa.length;
4010 double sum=0.0D;
4011 for(int i=0; i<n; i++){
4012 sum+=(double)aa[i];
4013 }
4014 return sum/((double)n);
4015 }
4016
4017 // Arithmetic mean of a 1D array of Complex, aa
4018 public static Complex mean(Complex[] aa){
4019 int n = aa.length;
4020 Complex sum = new Complex(0.0D, 0.0D);
4021 for(int i=0; i<n; i++){
4022 sum = sum.plus(aa[i]);
4023 }
4024 return sum.over((double)n);
4025 }
4026
4027 // Arithmetic mean of a 1D array of BigDecimal, aa
4028 public static BigDecimal mean(BigDecimal[] aa){
4029 int n = aa.length;
4030 BigDecimal sum = BigDecimal.ZERO;
4031 for(int i=0; i<n; i++){
4032 sum = sum.add(aa[i]);
4033 }
4034 return sum.divide(new BigDecimal((double)n), BigDecimal.ROUND_HALF_UP);
4035 }
4036
4037 // Arithmetic mean of a 1D array of BigInteger, aa
4038 public static BigDecimal mean(BigInteger[] aa){
4039 int n = aa.length;
4040 BigDecimal sum = BigDecimal.ZERO;
4041 BigDecimal bi = BigDecimal.ZERO;
4042 for(int i=0; i<n; i++){
4043 bi = new BigDecimal(aa[i]);
4044 sum = sum.add(bi);
4045 }
4046 bi = null;
4047 return sum.divide(new BigDecimal((double)n), BigDecimal.ROUND_HALF_UP);
4048 }
4049
4050
4051
4052
4053 // WEIGHTED ARITHMETIC MEANS (STATIC)
4054 // Weighted arithmetic mean of a 1D array of doubles, aa
4055 public static double mean(double[] aa, double[] ww){
4056 int n = aa.length;
4057 if(n!=ww.length)throw new IllegalArgumentException("length of variable array, " + n + " and length of weight array, " + ww.length + " are different");
4058 double[] weight = Conv.copy(ww);
4059 if(Stat.weightingOptionS){
4060 ArrayMaths am = new ArrayMaths(ww);
4061 am = am.pow(2);
4062 am = am.invert();
4063 weight = am.array();
4064 }
4065 double sumx=0.0D;
4066 double sumw=0.0D;
4067 for(int i=0; i<n; i++){
4068 sumx+=aa[i]*weight[i];
4069 sumw+=weight[i];
4070 }
4071 return sumx/sumw;
4072 }
4073
4074 // Weighted arithmetic mean of a 1D array of floats, aa
4075 public static float mean(float[] aa, float[] ww){
4076 int n = aa.length;
4077 if(n!=ww.length)throw new IllegalArgumentException("length of variable array, " + n + " and length of weight array, " + ww.length + " are different");
4078 float[] weight = Conv.copy(ww);
4079 if(Stat.weightingOptionS){
4080 ArrayMaths am = new ArrayMaths(ww);
4081 am = am.pow(2);
4082 am = am.invert();
4083 weight = am.array_as_float();
4084 }
4085
4086 float sumx=0.0F;
4087 float sumw=0.0F;
4088 for(int i=0; i<n; i++){
4089 sumx+=aa[i]*weight[i];
4090 sumw+=weight[i];
4091 }
4092 return sumx/sumw;
4093 }
4094
4095 // Weighted arithmetic mean of a 1D array of Complex, aa
4096 public static Complex mean(Complex[] aa, Complex[] ww){
4097 int n = aa.length;
4098 if(n!=ww.length)throw new IllegalArgumentException("length of variable array, " + n + " and length of weight array, " + ww.length + " are different");
4099 Complex[] weight = Conv.copy(ww);
4100 if(Stat.weightingOptionS){
4101 ArrayMaths am = new ArrayMaths(ww);
4102 am = am.pow(2);
4103 am = am.invert();
4104 weight = am.array_as_Complex();
4105 }
4106 Complex sumx=Complex.zero();
4107 Complex sumw=Complex.zero();
4108 for(int i=0; i<n; i++){
4109 sumx = sumx.plus(aa[i].times(weight[i]));
4110 sumw = sumw.plus(weight[i]);
4111 }
4112 return sumx.over(sumw);
4113 }
4114
4115 // Weighted arithmetic mean of a 1D array of BigDecimal, aa
4116 public static BigDecimal mean(BigDecimal[] aa, BigDecimal[] ww){
4117 int n = aa.length;
4118 if(n!=ww.length)throw new IllegalArgumentException("length of variable array, " + n + " and length of weight array, " + ww.length + " are different");
4119 BigDecimal[] weight = Conv.copy(ww);
4120 if(Stat.weightingOptionS){
4121 ArrayMaths am = new ArrayMaths(ww);
4122 am = am.pow(2);
4123 am = am.invert();
4124 weight = am.array_as_BigDecimal();
4125 }
4126
4127 BigDecimal sumx =BigDecimal.ZERO;
4128 BigDecimal sumw =BigDecimal.ZERO;
4129 for(int i=0; i<n; i++){
4130 sumx = sumx.add(aa[i].multiply(weight[i]));
4131 sumw = sumw.add(weight[i]);
4132 }
4133 sumx = sumx.divide(sumw, BigDecimal.ROUND_HALF_UP);
4134 sumw = null;
4135 weight = null;
4136 return sumx;
4137 }
4138
4139 // Weighted arithmetic mean of a 1D array of BigInteger, aa
4140 public static BigDecimal mean(BigInteger[] aa, BigInteger[] ww){
4141 ArrayMaths amaa = new ArrayMaths(aa);
4142 ArrayMaths amww = new ArrayMaths(ww);
4143
4144 return mean(amaa.array_as_BigDecimal(), amww.array_as_BigDecimal());
4145 }
4146
4147 // SUBTRACT THE MEAN (STATIC)
4148 // Subtract arithmetic mean of an array from data array elements
4149 public static double[] subtractMean(double[] array){
4150 int n = array.length;
4151 double mean = Stat.mean(array);
4152 double[] arrayMinusMean = new double[n];
4153 for(int i=0; i<n; i++)arrayMinusMean[i] = array[i] - mean;
4154
4155 return arrayMinusMean;
4156 }
4157
4158 // Subtract arithmetic mean of an array from data array elements
4159 public static float[] subtractMean(float[] array){
4160 int n = array.length;
4161 float mean = Stat.mean(array);
4162 float[] arrayMinusMean = new float[n];
4163 for(int i=0; i<n; i++)arrayMinusMean[i] = array[i] - mean;
4164
4165 return arrayMinusMean;
4166 }
4167
4168
4169 // Subtract arithmetic mean of an array from data array elements
4170 public static BigDecimal[] subtractMean(BigDecimal[] array){
4171 int n = array.length;
4172 BigDecimal mean = Stat.mean(array);
4173 BigDecimal[] arrayMinusMean = new BigDecimal[n];
4174 for(int i=0; i<n; i++)arrayMinusMean[i] = array[i].subtract(mean);
4175 mean = null;
4176 return arrayMinusMean;
4177 }
4178
4179 // Subtract arithmetic mean of an array from data array elements
4180 public static BigDecimal[] subtractMean(BigInteger[] array){
4181 int n = array.length;
4182 BigDecimal mean = Stat.mean(array);
4183 BigDecimal[] arrayMinusMean = new BigDecimal[n];
4184 for(int i=0; i<n; i++)arrayMinusMean[i] = (new BigDecimal(array[i])).subtract(mean);
4185 mean = null;
4186 return arrayMinusMean;
4187 }
4188
4189 // Subtract arithmetic mean of an array from data array elements
4190 public static Complex[] subtractMean(Complex[] array){
4191 int n = array.length;
4192 Complex mean = Stat.mean(array);
4193 Complex[] arrayMinusMean = new Complex[n];
4194 for(int i=0; i<n; i++)arrayMinusMean[i] = array[i].minus(mean);
4195
4196 return arrayMinusMean;
4197 }
4198
4199 // Subtract weighted arirhmetic mean of an array from data array elements
4200 public static double[] subtractMean(double[] array, double[] weights){
4201 int n = array.length;
4202 double mean = Stat.mean(array, weights);
4203 double[] arrayMinusMean = new double[n];
4204 for(int i=0; i<n; i++)arrayMinusMean[i] = array[i] - mean;
4205
4206 return arrayMinusMean;
4207 }
4208
4209 // Subtract weighted arirhmetic mean of an array from data array elements
4210 public static float[] subtractMean(float[] array, float[] weights){
4211 int n = array.length;
4212 float mean = Stat.mean(array, weights);
4213 float[] arrayMinusMean = new float[n];
4214 for(int i=0; i<n; i++)arrayMinusMean[i] = array[i] - mean;
4215
4216 return arrayMinusMean;
4217 }
4218
4219
4220 // Subtract weighted arirhmetic mean of an array from data array elements
4221 public static BigDecimal[] subtractMean(BigDecimal[] array, BigDecimal[] weights){
4222 int n = array.length;
4223 BigDecimal mean = Stat.mean(array, weights);
4224 BigDecimal[] arrayMinusMean = new BigDecimal[n];
4225 for(int i=0; i<n; i++)arrayMinusMean[i] = array[i].subtract(mean);
4226 mean = null;
4227 return arrayMinusMean;
4228 }
4229
4230 // Subtract weighted arirhmetic mean of an array from data array elements
4231 public static BigDecimal[] subtractMean(BigInteger[] array, BigInteger[] weights){
4232 int n = array.length;
4233 BigDecimal mean = Stat.mean(array, weights);
4234 BigDecimal[] arrayMinusMean = new BigDecimal[n];
4235 for(int i=0; i<n; i++)arrayMinusMean[i] = (new BigDecimal(array[i])).subtract(mean);
4236 mean = null;
4237 return arrayMinusMean;
4238 }
4239
4240 // Subtract weighted arirhmetic mean of an array from data array elements
4241 public static Complex[] subtractMean(Complex[] array, Complex[] weights){
4242 int n = array.length;
4243 Complex mean = Stat.mean(array, weights);
4244 Complex[] arrayMinusMean = new Complex[n];
4245 for(int i=0; i<n; i++)arrayMinusMean[i] = array[i].minus(mean);
4246
4247 return arrayMinusMean;
4248 }
4249
4250 // GEOMETRIC MEANS (STATIC)
4251
4252 // Geometric mean of a 1D array of BigDecimal, aa
4253 public static double geometricMean(BigDecimal[] aa){
4254 int n = aa.length;
4255 double sum = 0.0D;
4256 for(int i=0; i<n; i++)sum += Math.log(aa[i].doubleValue());
4257 return Math.exp(sum/(double)n);
4258 }
4259
4260 // Geometric mean of a 1D array of BigInteger, aa
4261 public static double geometricMean(BigInteger[] aa){
4262 int n = aa.length;
4263 double sum = 0.0D;
4264 for(int i=0; i<n; i++)sum += Math.log(aa[i].doubleValue());
4265 return Math.exp(sum/(double)n);
4266 }
4267
4268 // Geometric mean of a 1D array of Complex, aa
4269 public static Complex geometricMean(Complex[] aa){
4270 int n = aa.length;
4271 Complex sum = Complex.zero();
4272 for(int i=0; i<n; i++)sum = sum.plus(Complex.log(aa[i]));
4273 return Complex.exp(sum.over((double)n));
4274 }
4275
4276 // Geometric mean of a 1D array of doubles, aa
4277 public static double geometricMean(double[] aa){
4278 int n = aa.length;
4279 double sum=0.0D;
4280 for(int i=0; i<n; i++)sum += Math.log(aa[i]);
4281 return Math.exp(sum/(double)n);
4282 }
4283
4284 // Geometric mean of a 1D array of floats, aa
4285 public static float geometricMean(float[] aa){
4286 int n = aa.length;
4287 float sum=0.0F;
4288 for(int i=0; i<n; i++)sum += (float)Math.log(aa[i]);
4289 return (float)Math.exp(sum/(float)n);
4290 }
4291
4292 // Weighted geometric mean of a 1D array of Complexs, aa
4293 public static Complex geometricMean(Complex[] aa, Complex[] ww){
4294 int n = aa.length;
4295 if(n!=ww.length)throw new IllegalArgumentException("length of variable array, " + n + " and length of weight array, " + ww.length + " are different");
4296 Complex sumW = Complex.zero();
4297 Complex[] weight = Stat.invertAndSquare(ww);
4298 for(int i=0; i<n; i++){
4299 sumW = sumW.plus(weight[i]);
4300 }
4301 Complex sum = Complex.zero();
4302 for(int i=0; i<n; i++){
4303 sum = sum.plus(Complex.log(aa[i]).times(weight[i]));
4304 }
4305 return Complex.exp(sum.over(sumW));
4306 }
4307
4308 // Weighted geometric mean of a 1D array of BigDecimal, aa
4309 public static double geometricMean(BigDecimal[] aa, BigDecimal[] ww){
4310 int n = aa.length;
4311 if(n!=ww.length)throw new IllegalArgumentException("length of variable array, " + n + " and length of weight array, " + ww.length + " are different");
4312 ArrayMaths weighting = new ArrayMaths(Stat.invertAndSquare(ww));
4313 double[] weight = weighting.array();
4314
4315 double sumW = 0.0D;
4316 for(int i=0; i<n; i++){
4317 sumW += weight[i];
4318 }
4319 double sum=0.0D;
4320 for(int i=0; i<n; i++){
4321 sum += Math.log(aa[i].doubleValue())*weight[i];
4322 }
4323 return Math.exp(sum/sumW);
4324 }
4325
4326 // Weighted geometric mean of a 1D array of BigDecimal, aa
4327 public static double geometricMean(BigInteger[] aa, BigInteger[] ww){
4328 ArrayMaths amaa = new ArrayMaths(aa);
4329 ArrayMaths amww = new ArrayMaths(ww);
4330 return geometricMean(amaa.array_as_BigDecimal(), amww.array_as_BigDecimal());
4331 }
4332
4333 // Weighted geometric mean of a 1D array of double, aa
4334 public static double geometricMean(double[] aa, double[] ww){
4335 int n = aa.length;
4336 if(n!=ww.length)throw new IllegalArgumentException("length of variable array, " + n + " and length of weight array, " + ww.length + " are different");
4337 double sumW = 0.0D;
4338 double[] weight = Stat.invertAndSquare(ww);
4339 for(int i=0; i<n; i++){
4340 sumW += weight[i];
4341 }
4342 double sum=0.0D;
4343 for(int i=0; i<n; i++){
4344 sum += Math.log(aa[i])*weight[i];
4345 }
4346 return Math.exp(sum/sumW);
4347 }
4348
4349 // Weighted geometric mean of a 1D array of floats, aa
4350 public static float geometricMean(float[] aa, float[] ww){
4351 int n = aa.length;
4352 if(n!=ww.length)throw new IllegalArgumentException("length of variable array, " + n + " and length of weight array, " + ww.length + " are different");
4353 float sumW = 0.0F;
4354 float[] weight = Stat.invertAndSquare(ww);
4355 for(int i=0; i<n; i++){
4356 sumW += weight[i];
4357 }
4358 float sum=0.0F;
4359 for(int i=0; i<n; i++){
4360 sum += (float)Math.log(aa[i])*weight[i];
4361 }
4362 return (float)Math.exp(sum/sumW);
4363 }
4364
4365 // HARMONIC MEANS (STATIC)
4366
4367 // Harmonic mean of a 1D array of BigDecimal, aa
4368 public static BigDecimal harmonicMean(BigDecimal[] aa){
4369 int n = aa.length;
4370 BigDecimal sum = BigDecimal.ZERO;
4371 for(int i=0; i<n; i++)sum = sum.add(BigDecimal.ONE.divide(aa[i], BigDecimal.ROUND_HALF_UP));
4372 sum = (new BigDecimal((double)n)).divide(sum, BigDecimal.ROUND_HALF_UP);
4373 return sum;
4374 }
4375
4376 // Harmonic mean of a 1D array of BigInteger, aa
4377 public static BigDecimal harmonicMean(BigInteger[] aa){
4378 int n = aa.length;
4379 ArrayMaths am = new ArrayMaths(aa);
4380 BigDecimal[] bd = am.getArray_as_BigDecimal();
4381 BigDecimal sum = BigDecimal.ZERO;
4382 for(int i=0; i<n; i++)sum = sum.add(BigDecimal.ONE.divide(bd[i], BigDecimal.ROUND_HALF_UP));
4383 sum = (new BigDecimal((double)n)).divide(sum, BigDecimal.ROUND_HALF_UP);
4384 bd = null;
4385 return sum;
4386 }
4387
4388 // Harmonic mean of a 1D array of Complex, aa
4389 public static Complex harmonicMean(Complex[] aa){
4390 int n = aa.length;
4391 Complex sum = Complex.zero();
4392 for(int i=0; i<n; i++)sum = sum.plus(Complex.plusOne().over(aa[i]));
4393 sum = (new Complex((double)n)).over(sum);
4394 return sum;
4395 }
4396
4397 // Harmonic mean of a 1D array of doubles, aa
4398 public static double harmonicMean(double[] aa){
4399 int n = aa.length;
4400 double sum = 0.0D;
4401 for(int i=0; i<n; i++)sum += 1.0D/aa[i];
4402 return (double)n/sum;
4403 }
4404
4405 // Harmonic mean of a 1D array of floats, aa
4406 public static float harmonicMean(float[] aa){
4407 int n = aa.length;
4408 float sum = 0.0F;
4409 for(int i=0; i<n; i++)sum += 1.0F/aa[i];
4410 return (float)n/sum;
4411 }
4412
4413 // Weighted harmonic mean of a 1D array of BigDecimal, aa
4414 public static BigDecimal harmonicMean(BigDecimal[] aa, BigDecimal[] ww){
4415 int n = aa.length;
4416 if(n!=ww.length)throw new IllegalArgumentException("length of variable array, " + n + " and length of weight array, " + ww.length + " are different");
4417 BigDecimal sum = BigDecimal.ZERO;
4418 BigDecimal sumW = BigDecimal.ZERO;
4419 BigDecimal[] weight = Stat.invertAndSquare(ww);
4420 for(int i=0; i<n; i++){
4421 sumW = sumW.add(weight[i]);
4422 }
4423 for(int i=0; i<n; i++)sum = sum.add(weight[i].divide(aa[i], BigDecimal.ROUND_HALF_UP));
4424 sum = sumW.divide(sum, BigDecimal.ROUND_HALF_UP);
4425 sumW = null;
4426 weight = null;
4427 return sum;
4428 }
4429
4430 // Weighted harmonic mean of a 1D array of BigInteger, aa
4431 public static BigDecimal harmonicMean(BigInteger[] aa, BigInteger[] ww){
4432 int n = aa.length;
4433 if(n!=ww.length)throw new IllegalArgumentException("length of variable array, " + n + " and length of weight array, " + ww.length + " are different");
4434 ArrayMaths am = new ArrayMaths(aa);
4435 ArrayMaths wm = new ArrayMaths(ww);
4436 return harmonicMean(am.getArray_as_BigDecimal(), wm.getArray_as_BigDecimal());
4437 }
4438
4439 // Weighted harmonic mean of a 1D array of Complex, aa
4440 public static Complex harmonicMean(Complex[] aa, Complex[] ww){
4441 int n = aa.length;
4442 if(n!=ww.length)throw new IllegalArgumentException("length of variable array, " + n + " and length of weight array, " + ww.length + " are different");
4443 Complex sum = Complex.zero();
4444 Complex sumW = Complex.zero();
4445 Complex[] weight = Stat.invertAndSquare(ww);
4446 for(int i=0; i<n; i++){
4447 sumW = sumW.plus(weight[i]);
4448 }
4449 for(int i=0; i<n; i++)sum = sum.plus(weight[i].over(aa[i]));
4450 return sumW.over(sum);
4451 }
4452
4453 // Weighted harmonic mean of a 1D array of doubles, aa
4454 public static double harmonicMean(double[] aa, double[] ww){
4455 int n = aa.length;
4456 if(n!=ww.length)throw new IllegalArgumentException("length of variable array, " + n + " and length of weight array, " + ww.length + " are different");
4457 double sum = 0.0D;
4458 double sumW = 0.0D;
4459 double[] weight = Stat.invertAndSquare(ww);
4460 for(int i=0; i<n; i++){
4461 sumW += weight[i];
4462 }
4463 for(int i=0; i<n; i++)sum += weight[i]/aa[i];
4464 return sumW/sum;
4465 }
4466
4467 // Weighted harmonic mean of a 1D array of floats, aa
4468 public static float harmonicMean(float[] aa, float[] ww){
4469 int n = aa.length;
4470 if(n!=ww.length)throw new IllegalArgumentException("length of variable array, " + n + " and length of weight array, " + ww.length + " are different");
4471 float sum = 0.0F;
4472 float sumW = 0.0F;
4473 float[] weight = Stat.invertAndSquare(ww);
4474 for(int i=0; i<n; i++){
4475 sumW += weight[i];
4476 }
4477 for(int i=0; i<n; i++)sum += weight[i]/aa[i];
4478 return sumW/sum;
4479 }
4480
4481 // GENERALIZED MEANS [POWER MEANS] (STATIC METHODS)
4482
4483 // generalized mean of a 1D array of Complex, aa
4484 public static Complex generalizedMean(Complex[] aa, double m){
4485 int n = aa.length;
4486 Complex sum = Complex.zero();
4487 if(m==0.0D){
4488 for(int i=0; i<n; i++){
4489 sum = sum.plus(Complex.log(aa[i]));
4490 }
4491 return Complex.exp(sum);
4492 }
4493 else{
4494 for(int i=0; i<n; i++){
4495 sum = sum.plus(Complex.pow(aa[i],m));
4496 }
4497 return Complex.pow(sum.over((double)n), 1.0D/m);
4498 }
4499 }
4500
4501 // generalized mean of a 1D array of Complex, aa
4502 public static Complex generalizedMean(Complex[] aa, Complex m){
4503 int n = aa.length;
4504 Complex sum = Complex.zero();
4505 if(m.equals(Complex.zero())){
4506 for(int i=0; i<n; i++){
4507 sum = sum.plus(Complex.log(aa[i]));
4508 }
4509 return Complex.exp(sum);
4510 }
4511 else{
4512 for(int i=0; i<n; i++){
4513 sum = sum.plus(Complex.pow(aa[i],m));
4514 }
4515 return Complex.pow(sum.over((double)n), Complex.plusOne().over(m));
4516 }
4517 }
4518
4519 // generalized mean of a 1D array of BigDecimal, aa
4520 public static double generalizedMean(BigDecimal[] aa, double m){
4521 ArrayMaths am = new ArrayMaths(aa);
4522 double[] dd = am.getArray_as_double();
4523 return generalizedMean(dd, m);
4524 }
4525
4526 // generalized mean of a 1D array of BigDecimal, aa
4527 public static double generalizedMean(BigDecimal[] aa, BigDecimal m){
4528 ArrayMaths am = new ArrayMaths(aa);
4529 double[] dd = am.getArray_as_double();
4530 return generalizedMean(dd, m.doubleValue());
4531 }
4532
4533 // generalized mean of a 1D array of BigInteger, aa
4534 public static double generalizedMean(BigInteger[] aa, double m){
4535 ArrayMaths am = new ArrayMaths(aa);
4536 double[] dd = am.getArray_as_double();
4537 return generalizedMean(dd, m);
4538 }
4539
4540 // generalized mean of a 1D array of BigInteger, aa
4541 public static double generalizedMean(BigInteger[] aa, BigInteger m){
4542 ArrayMaths am = new ArrayMaths(aa);
4543 double[] dd = am.getArray_as_double();
4544 return generalizedMean(dd, m.doubleValue());
4545 }
4546
4547 // generalized mean of a 1D array of doubles, aa
4548 public static double generalizedMean(double[] aa, double m){
4549 int n = aa.length;
4550 double sum=0.0D;
4551 if(m==0){
4552 for(int i=0; i<n; i++){
4553 sum += Math.log(aa[i]);
4554 }
4555 return Math.exp(sum);
4556 }
4557 else{
4558 for(int i=0; i<n; i++){
4559 sum += Math.pow(aa[i],m);
4560 }
4561 return Math.pow(sum/((double)n), 1.0D/m);
4562 }
4563 }
4564
4565 // generalized mean of a 1D array of floats, aa
4566 public static float generalizedMean(float[] aa, float m){
4567 int n = aa.length;
4568 float sum=0.0F;
4569 if(m==0){
4570 for(int i=0; i<n; i++){
4571 sum += (float)Math.log(aa[i]);
4572 }
4573 return (float)Math.exp(sum);
4574 }
4575 else{
4576 for(int i=0; i<n; i++){
4577 sum += Math.pow(aa[i],m);
4578 }
4579 return (float)Math.pow(sum/((float)n), 1.0F/m);
4580 }
4581 }
4582
4583
4584 // Generalised mean of a 1D array of Complex, aa
4585 public static Complex generalisedMean(Complex[] aa, double m){
4586 int n = aa.length;
4587 Complex sum = Complex.zero();
4588 if(m==0.0D){
4589 for(int i=0; i<n; i++){
4590 sum = sum.plus(Complex.log(aa[i]));
4591 }
4592 return Complex.exp(sum);
4593 }
4594 else{
4595 for(int i=0; i<n; i++){
4596 sum = sum.plus(Complex.pow(aa[i],m));
4597 }
4598 return Complex.pow(sum.over((double)n), 1.0D/m);
4599 }
4600 }
4601
4602 // Generalised mean of a 1D array of Complex, aa
4603 public static Complex generalisedMean(Complex[] aa, Complex m){
4604 int n = aa.length;
4605 Complex sum = Complex.zero();
4606 if(m.equals(Complex.zero())){
4607 for(int i=0; i<n; i++){
4608 sum = sum.plus(Complex.log(aa[i]));
4609 }
4610 return Complex.exp(sum);
4611 }
4612 else{
4613 for(int i=0; i<n; i++){
4614 sum = sum.plus(Complex.pow(aa[i],m));
4615 }
4616 return Complex.pow(sum.over((double)n), Complex.plusOne().over(m));
4617 }
4618 }
4619
4620 // Generalised mean of a 1D array of BigDecimal, aa
4621 public static double generalisedMean(BigDecimal[] aa, double m){
4622 ArrayMaths am = new ArrayMaths(aa);
4623 double[] dd = am.getArray_as_double();
4624 return generalisedMean(dd, m);
4625 }
4626
4627 // Generalised mean of a 1D array of BigDecimal, aa
4628 public static double generalisedMean(BigDecimal[] aa, BigDecimal m){
4629 ArrayMaths am = new ArrayMaths(aa);
4630 double[] dd = am.getArray_as_double();
4631 return generalisedMean(dd, m.doubleValue());
4632 }
4633
4634 // Generalised mean of a 1D array of BigInteger, aa
4635 public static double generalisedMean(BigInteger[] aa, double m){
4636 ArrayMaths am = new ArrayMaths(aa);
4637 double[] dd = am.getArray_as_double();
4638 return generalisedMean(dd, m);
4639 }
4640
4641 // Generalised mean of a 1D array of BigInteger, aa
4642 public static double generalisedMean(BigInteger[] aa, BigInteger m){
4643 ArrayMaths am = new ArrayMaths(aa);
4644 double[] dd = am.getArray_as_double();
4645 return generalisedMean(dd, m.doubleValue());
4646 }
4647
4648 // Generalised mean of a 1D array of doubles, aa
4649 public static double generalisedMean(double[] aa, double m){
4650 int n = aa.length;
4651 double sum=0.0D;
4652 if(m==0){
4653 for(int i=0; i<n; i++){
4654 sum += Math.log(aa[i]);
4655 }
4656 return Math.exp(sum);
4657 }
4658 else{
4659 for(int i=0; i<n; i++){
4660 sum += Math.pow(aa[i],m);
4661 }
4662 return Math.pow(sum/((double)n), 1.0D/m);
4663 }
4664 }
4665
4666 // Generalised mean of a 1D array of floats, aa
4667 public static float generalisedMean(float[] aa, float m){
4668 int n = aa.length;
4669 float sum=0.0F;
4670 if(m==0){
4671 for(int i=0; i<n; i++){
4672 sum += (float)Math.log(aa[i]);
4673 }
4674 return (float)Math.exp(sum);
4675 }
4676 else{
4677 for(int i=0; i<n; i++){
4678 sum += Math.pow(aa[i],m);
4679 }
4680 return (float)Math.pow(sum/((float)n), 1.0F/m);
4681 }
4682 }
4683
4684 // WEIGHTED GENERALIZED MEANS
4685
4686 // weighted generalized mean of a 1D array of Complex, aa
4687 public static Complex generalisedMean(Complex[] aa, Complex[] ww, double m){
4688 int n = aa.length;
4689 if(n!=ww.length)throw new IllegalArgumentException("length of variable array, " + n + " and length of weight array, " + ww.length + " are different");
4690
4691 Complex sum = Complex.zero();
4692 Complex sumw = Complex.zero();
4693 Complex[] weight = Stat.invertAndSquare(ww);
4694 for(int i=0; i<n; i++){
4695 sumw = sumw.plus(weight[i]);
4696 }
4697
4698 if(m==0.0D){
4699 for(int i=0; i<n; i++){
4700 sum = sum.plus(Complex.log(weight[i].times(aa[i])).over(sumw));
4701 }
4702 return Complex.exp(sum);
4703 }
4704 else{
4705 for(int i=0; i<n; i++){
4706 sum = sum.plus(weight[i].times(Complex.pow(aa[i],m)));
4707 }
4708 return Complex.pow(sum.over(sumw), 1.0D/m);
4709 }
4710 }
4711
4712 // weighted generalized mean of a 1D array of Complex, aa
4713 public static Complex generalisedMean(Complex[] aa, Complex[] ww, Complex m){
4714 int n = aa.length;
4715 if(n!=ww.length)throw new IllegalArgumentException("length of variable array, " + n + " and length of weight array, " + ww.length + " are different");
4716
4717 Complex sum = Complex.zero();
4718 Complex sumw = Complex.zero();
4719 Complex[] weight = Stat.invertAndSquare(ww);
4720 for(int i=0; i<n; i++){
4721 sumw = sumw.plus(weight[i]);
4722 }
4723
4724 if(m.equals(Complex.zero())){
4725 for(int i=0; i<n; i++){
4726 sum = sum.plus(Complex.log(weight[i].times(aa[i])).over(sumw));
4727 }
4728 return Complex.exp(sum);
4729 }
4730 else{
4731 for(int i=0; i<n; i++){
4732 sum = sum.plus(weight[i].times(Complex.pow(aa[i],m)));
4733 }
4734 return Complex.pow(sum.over(sumw), Complex.plusOne().over(m));
4735 }
4736 }
4737
4738 // weighted generalized mean of a 1D array of BigDecimal, aa
4739 public static double generalisedMean(BigDecimal[] aa, BigDecimal[] ww, double m){
4740 int n = aa.length;
4741 if(n!=ww.length)throw new IllegalArgumentException("length of variable array, " + n + " and length of weight array, " + ww.length + " are different");
4742
4743 ArrayMaths am1 = new ArrayMaths(aa);
4744 double[] dd = am1.getArray_as_double();
4745 ArrayMaths am2 = new ArrayMaths(ww);
4746 double[] wd = am2.getArray_as_double();
4747 return generalisedMean(dd, wd, m);
4748 }
4749
4750 // weighted generalized mean of a 1D array of BigDecimal, aa
4751 public static double generalisedMean(BigDecimal[] aa, BigDecimal[] ww, BigDecimal m){
4752 int n = aa.length;
4753 if(n!=ww.length)throw new IllegalArgumentException("length of variable array, " + n + " and length of weight array, " + ww.length + " are different");
4754
4755 ArrayMaths am1 = new ArrayMaths(aa);
4756 double[] dd = am1.getArray_as_double();
4757 ArrayMaths am2 = new ArrayMaths(ww);
4758 double[] wd = am2.getArray_as_double();
4759 return generalisedMean(dd, wd, m.doubleValue());
4760 }
4761
4762 // weighted generalized mean of a 1D array of BigInteger, aa
4763 public static double generalisedMean(BigInteger[] aa, BigInteger[] ww, double m){
4764 int n = aa.length;
4765 if(n!=ww.length)throw new IllegalArgumentException("length of variable array, " + n + " and length of weight array, " + ww.length + " are different");
4766
4767 ArrayMaths am1 = new ArrayMaths(aa);
4768 double[] dd = am1.getArray_as_double();
4769 ArrayMaths am2 = new ArrayMaths(ww);
4770 double[] wd = am2.getArray_as_double();
4771 return generalisedMean(dd, wd, m);
4772 }
4773
4774 // weighted generalized mean of a 1D array of BigInteger, aa
4775 public static double generalisedMean(BigInteger[] aa, BigInteger[] ww, BigInteger m){
4776 int n = aa.length;
4777 if(n!=ww.length)throw new IllegalArgumentException("length of variable array, " + n + " and length of weight array, " + ww.length + " are different");
4778
4779 ArrayMaths am1 = new ArrayMaths(aa);
4780 double[] dd = am1.getArray_as_double();
4781 ArrayMaths am2 = new ArrayMaths(ww);
4782 double[] wd = am2.getArray_as_double();
4783 return generalisedMean(dd, wd, m.doubleValue());
4784 }
4785
4786 // weighted generalized mean of a 1D array of doubles, aa
4787 public static double generalisedMean(double[] aa, double[] ww, double m){
4788 int n = aa.length;
4789 if(n!=ww.length)throw new IllegalArgumentException("length of variable array, " + n + " and length of weight array, " + ww.length + " are different");
4790
4791 double sum=0.0D;
4792 double sumw=0.0D;
4793 double[] weight = Stat.invertAndSquare(ww);
4794 for(int i=0; i<n; i++){
4795 sumw += weight[i];
4796 }
4797
4798 if(m==0){
4799 for(int i=0; i<n; i++){
4800 sum += Math.log(aa[i]*weight[i]/sumw);
4801 }
4802 return Math.exp(sum);
4803 }
4804 else{
4805 for(int i=0; i<n; i++){
4806 sum += weight[i]*Math.pow(aa[i],m);
4807 }
4808 return Math.pow(sum/sumw, 1.0D/m);
4809 }
4810 }
4811
4812 // weighted generalized mean of a 1D array of floats, aa
4813 public static float generalisedMean(float[] aa, float[] ww, float m){
4814 int n = aa.length;
4815 if(n!=ww.length)throw new IllegalArgumentException("length of variable array, " + n + " and length of weight array, " + ww.length + " are different");
4816
4817 float sum=0.0F;
4818 float sumw=0.0F;
4819 float[] weight = Stat.invertAndSquare(ww);
4820 for(int i=0; i<n; i++){
4821 sumw += weight[i];
4822 }
4823 if(m==0){
4824 for(int i=0; i<n; i++){
4825 sum += (float)Math.log(aa[i]);
4826 }
4827 return (float)Math.exp(sum);
4828 }
4829 else{
4830 for(int i=0; i<n; i++){
4831 sum += Math.pow(aa[i],m);
4832 }
4833 return (float)Math.pow(sum/sumw, 1.0F/m);
4834 }
4835 }
4836
4837
4838 // weighted generalised mean of a 1D array of Complex, aa
4839 public static Complex weightedGeneralisedMean(Complex[] aa, Complex[] ww, double m){
4840 int n = aa.length;
4841 if(n!=ww.length)throw new IllegalArgumentException("length of variable array, " + n + " and length of weight array, " + ww.length + " are different");
4842
4843 return generalisedMean(aa, ww, m);
4844 }
4845
4846 // weighted generalised mean of a 1D array of Complex, aa
4847 public static Complex weightedGeneralisedMean(Complex[] aa, Complex[] ww, Complex m){
4848 int n = aa.length;
4849 if(n!=ww.length)throw new IllegalArgumentException("length of variable array, " + n + " and length of weight array, " + ww.length + " are different");
4850
4851 return generalisedMean(aa, ww, m);
4852 }
4853
4854 // weighted generalised mean of a 1D array of BigDecimal, aa
4855 public static double weightedGeneralisedMean(BigDecimal[] aa, BigDecimal[] ww, double m){
4856 int n = aa.length;
4857 if(n!=ww.length)throw new IllegalArgumentException("length of variable array, " + n + " and length of weight array, " + ww.length + " are different");
4858
4859 return generalisedMean(aa, ww, m);
4860 }
4861
4862 // weighted generalised mean of a 1D array of BigDecimal, aa
4863 public static double weightedGeneralisedMean(BigDecimal[] aa, BigDecimal[] ww, BigDecimal m){
4864 int n = aa.length;
4865 if(n!=ww.length)throw new IllegalArgumentException("length of variable array, " + n + " and length of weight array, " + ww.length + " are different");
4866
4867 return generalisedMean(aa, ww, m);
4868 }
4869
4870 // weighted generalised mean of a 1D array of BigInteger, aa
4871 public static double weightedGeneralisedMean(BigInteger[] aa, BigInteger[] ww, double m){
4872 int n = aa.length;
4873 if(n!=ww.length)throw new IllegalArgumentException("length of variable array, " + n + " and length of weight array, " + ww.length + " are different");
4874
4875 return generalisedMean(aa, ww, m);
4876 }
4877
4878 // weighted generalised mean of a 1D array of BigInteger, aa
4879 public static double weightedGeneralisedMean(BigInteger[] aa, BigInteger[] ww, BigInteger m){
4880 int n = aa.length;
4881 if(n!=ww.length)throw new IllegalArgumentException("length of variable array, " + n + " and length of weight array, " + ww.length + " are different");
4882
4883 return generalisedMean(aa, ww, m);
4884 }
4885
4886 // weighted generalised mean of a 1D array of doubles, aa
4887 public static double weightedGeneralisedMean(double[] aa, double[] ww, double m){
4888 int n = aa.length;
4889 if(n!=ww.length)throw new IllegalArgumentException("length of variable array, " + n + " and length of weight array, " + ww.length + " are different");
4890
4891 return generalisedMean(aa, ww, m);
4892 }
4893
4894 // weighted generalised mean of a 1D array of floats, aa
4895 public static float weightedGeneralisedMean(float[] aa, float[] ww, float m){
4896 int n = aa.length;
4897 if(n!=ww.length)throw new IllegalArgumentException("length of variable array, " + n + " and length of weight array, " + ww.length + " are different");
4898
4899 return generalisedMean(aa, ww, m);
4900 }
4901
4902
4903
4904
4905 // INTERQUARTILE MEANS
4906
4907 // Interquartile mean of a 1D array of BigDecimal, aa
4908 public static BigDecimal interQuartileMean(BigDecimal[] aa){
4909 int n = aa.length;
4910 if(n<4)throw new IllegalArgumentException("At least 4 array elements needed");
4911 ArrayMaths am = new ArrayMaths(aa);
4912 ArrayMaths as = am.sort();
4913 BigDecimal[] bb = as.getArray_as_BigDecimal();
4914 BigDecimal sum = BigDecimal.ZERO;
4915 for(int i=n/4; i<3*n/4; i++)sum = sum.add(bb[i]);
4916 sum = sum.multiply(new BigDecimal(2.0D/(double)n));
4917 bb = null;
4918 return sum;
4919 }
4920
4921 // Interquartile mean of a 1D array of BigInteger, aa
4922 public static BigDecimal interQuartileMean(BigInteger[] aa){
4923 int n = aa.length;
4924 if(n<4)throw new IllegalArgumentException("At least 4 array elements needed");
4925 ArrayMaths am = new ArrayMaths(aa);
4926 ArrayMaths as = am.sort();
4927 BigDecimal[] bb = as.getArray_as_BigDecimal();
4928 BigDecimal sum = BigDecimal.ZERO;
4929 for(int i=n/4; i<3*n/4; i++)sum = sum.add(bb[i]);
4930 sum = sum.multiply(new BigDecimal(2.0D/(double)n));
4931 bb = null;
4932 return sum;
4933 }
4934
4935 // Interquartile mean of a 1D array of doubles, aa
4936 public static double interQuartileMean(double[] aa){
4937 int n = aa.length;
4938 if(n<4)throw new IllegalArgumentException("At least 4 array elements needed");
4939 double[] bb = Fmath.selectionSort(aa);
4940 double sum = 0.0D;
4941 for(int i=n/4; i<3*n/4; i++)sum += bb[i];
4942 return 2.0*sum/(double)(n);
4943 }
4944
4945 // Interquartile mean of a 1D array of floats, aa
4946 public static float interQuartileMean(float[] aa){
4947 int n = aa.length;
4948 if(n<4)throw new IllegalArgumentException("At least 4 array elements needed");
4949 float[] bb = Fmath.selectionSort(aa);
4950 float sum = 0.0F;
4951 for(int i=n/4; i<3*n/4; i++)sum += bb[i];
4952 return 2.0F*sum/(float)(n);
4953 }
4954
4955 // ROOT MEAN SQUARES
4956
4957 // Root mean square (rms) of a 1D array of doubles, aa
4958 public static double rms(double[] aa){
4959 int n = aa.length;
4960 double sum=0.0D;
4961 for(int i=0; i<n; i++){
4962 sum+=aa[i]*aa[i];
4963 }
4964 return Math.sqrt(sum/((double)n));
4965 }
4966
4967 // Root mean square (rms) of a 1D array of floats, aa
4968 public static float rms(float[] aa){
4969 int n = aa.length;
4970 float sum = 0.0F;
4971 for(int i=0; i<n; i++){
4972 sum+=aa[i]*aa[i];
4973 }
4974 sum /= (float)n;
4975
4976 return (float)Math.sqrt(sum);
4977 }
4978
4979 // Root mean square (rms) of a 1D array of BigDecimal, aa
4980 public static double rms(BigDecimal[] aa){
4981 int n = aa.length;
4982 BigDecimal sum = BigDecimal.ZERO;
4983 for(int i=0; i<n; i++){
4984 sum = sum.add(aa[i].multiply(aa[i]));
4985 }
4986 sum = sum.divide((new BigDecimal(n)), BigDecimal.ROUND_HALF_UP);
4987 double ret = Math.sqrt(sum.doubleValue());
4988 sum = null;
4989 return ret;
4990 }
4991
4992 // Root mean square (rms) of a 1D array of BigInteger, aa
4993 public static double rms(BigInteger[] aa){
4994 int n = aa.length;
4995 BigDecimal sum = BigDecimal.ZERO;
4996 BigDecimal bd = BigDecimal.ZERO;
4997 for(int i=0; i<n; i++){
4998 bd = new BigDecimal(aa[i]);
4999 sum = sum.add(bd.multiply(bd));
5000 }
5001 sum = sum.divide((new BigDecimal(n)), BigDecimal.ROUND_HALF_UP);
5002 double ret = Math.sqrt(sum.doubleValue());
5003 bd = null;
5004 sum = null;
5005 return ret;
5006 }
5007
5008 // WEIGHTED ROOT MEAN SQUARES
5009
5010 // Weighted root mean square (rms) of a 1D array of doubles, aa
5011 public static double rms(double[] aa, double[] ww){
5012 int n = aa.length;
5013 if(n!=ww.length)throw new IllegalArgumentException("length of variable array, " + n + " and length of weight array, " + ww.length + " are different");
5014
5015 double sumw =0.0D;
5016 double[] weight = Stat.invertAndSquare(ww);
5017 for(int i=0; i<n; i++){
5018 sumw += weight[i];
5019 }
5020 double sum=0.0D;
5021 for(int i=0; i<n; i++){
5022 sum += weight[i]*aa[i]*aa[i];
5023 }
5024 return Math.sqrt(sum/sumw);
5025 }
5026
5027 // Weighted root mean square (rms) of a 1D array of floats, aa
5028 public static float rms(float[] aa, float[] ww){
5029 int n = aa.length;
5030 if(n!=ww.length)throw new IllegalArgumentException("length of variable array, " + n + " and length of weight array, " + ww.length + " are different");
5031
5032 double sumw =0.0F;
5033 float[] weight = Stat.invertAndSquare(ww);
5034 for(int i=0; i<n; i++){
5035 sumw += weight[i];
5036 }
5037 float sum=0.0F;
5038 for(int i=0; i<n; i++){
5039 sum += weight[i]*aa[i]*aa[i];
5040 }
5041 return (float)Math.sqrt(sum/sumw);
5042 }
5043
5044 // Weighted root mean square (rms) of a 1D array of BigDecimal, aa
5045 public static double rms(BigDecimal[] aa, BigDecimal[] ww){
5046 int n = aa.length;
5047 if(n!=ww.length)throw new IllegalArgumentException("length of variable array, " + n + " and length of weight array, " + ww.length + " are different");
5048
5049 BigDecimal sumw = BigDecimal.ZERO;
5050 BigDecimal[] weight = Stat.invertAndSquare(ww);
5051 for(int i=0; i<n; i++){
5052 sumw = sumw.add(weight[i]);
5053 }
5054
5055 BigDecimal sum = BigDecimal.ZERO;
5056 for(int i=0; i<n; i++){
5057 sum = sum.add((aa[i].multiply(aa[i])).multiply(weight[i]));
5058 }
5059 sum = sum.divide(sumw, BigDecimal.ROUND_HALF_UP);
5060 double ret = Math.sqrt(sum.doubleValue());
5061 sum = null;
5062 weight = null;
5063 return ret;
5064 }
5065
5066 // Weighted root mean square (rms) of a 1D array of BigInteger, aa
5067 public static double rms(BigInteger[] aa, BigInteger[] ww){
5068 int n = aa.length;
5069 if(n!=ww.length)throw new IllegalArgumentException("length of variable array, " + n + " and length of weight array, " + ww.length + " are different");
5070
5071
5072 ArrayMaths amaa = new ArrayMaths(aa);
5073 ArrayMaths amww = new ArrayMaths(ww);
5074 return rms(amaa.array_as_BigDecimal(), amww.array_as_BigDecimal());
5075 }
5076
5077 // MEDIANS
5078
5079 // Median of a 1D array of BigDecimal, aa
5080 public static BigDecimal median(BigDecimal[] aa){
5081 int n = aa.length;
5082 int nOverTwo = n/2;
5083 BigDecimal med = BigDecimal.ZERO;
5084 ArrayMaths bm = new ArrayMaths(aa);
5085 ArrayMaths sm = bm.sort();
5086 BigDecimal[] bb = bm.getArray_as_BigDecimal();
5087 if(Fmath.isOdd(n)){
5088 med = bb[nOverTwo];
5089 }
5090 else{
5091 med = (bb[nOverTwo-1].add(bb[nOverTwo])).divide(new BigDecimal(2.0D), BigDecimal.ROUND_HALF_UP);
5092 }
5093 bb = null;
5094 return med;
5095 }
5096
5097 // Median of a 1D array of BigInteger, aa
5098 public static BigInteger median(BigInteger[] aa){
5099 int n = aa.length;
5100 int nOverTwo = n/2;
5101 BigInteger med = BigInteger.ZERO;
5102 ArrayMaths bm = new ArrayMaths(aa);
5103 ArrayMaths sm = bm.sort();
5104 BigInteger[] bb = bm.getArray_as_BigInteger();
5105 if(Fmath.isOdd(n)){
5106 med = bb[nOverTwo];
5107 }
5108 else{
5109 med = (bb[nOverTwo-1].add(bb[nOverTwo])).divide(new BigInteger("2"));
5110 }
5111 bb = null;
5112 return med;
5113 }
5114
5115 // Median of a 1D array of doubles, aa
5116 public static double median(double[] aa){
5117 int n = aa.length;
5118 int nOverTwo = n/2;
5119 double med = 0.0D;
5120 double[] bb = Fmath.selectionSort(aa);
5121 if(Fmath.isOdd(n)){
5122 med = bb[nOverTwo];
5123 }
5124 else{
5125 med = (bb[nOverTwo-1]+bb[nOverTwo])/2.0D;
5126 }
5127
5128 return med;
5129 }
5130
5131
5132 // Median of a 1D array of floats, aa
5133 public static float median(float[] aa){
5134 int n = aa.length;
5135 int nOverTwo = n/2;
5136 float med = 0.0F;
5137 float[] bb = Fmath.selectionSort(aa);
5138 if(Fmath.isOdd(n)){
5139 med = bb[nOverTwo];
5140 }
5141 else{
5142 med = (bb[nOverTwo-1]+bb[nOverTwo])/2.0F;
5143 }
5144
5145 return med;
5146 }
5147
5148 // Median of a 1D array of int, aa
5149 public static double median(int[] aa){
5150 int n = aa.length;
5151 int nOverTwo = n/2;
5152 double med = 0.0D;
5153 int[] bb = Fmath.selectionSort(aa);
5154 if(Fmath.isOdd(n)){
5155 med = (double)bb[nOverTwo];
5156 }
5157 else{
5158 med = (double)(bb[nOverTwo-1]+bb[nOverTwo])/2.0D;
5159 }
5160
5161 return med;
5162 }
5163
5164 // Median of a 1D array of long, aa
5165 public static double median(long[] aa){
5166 int n = aa.length;
5167 int nOverTwo = n/2;
5168 double med = 0.0D;
5169 long[] bb = Fmath.selectionSort(aa);
5170 if(Fmath.isOdd(n)){
5171 med = (double)bb[nOverTwo];
5172 }
5173 else{
5174 med = (double)(bb[nOverTwo-1]+bb[nOverTwo])/2.0D;
5175 }
5176
5177 return med;
5178 }
5179
5180
5181 // STANDARD DEVIATIONS (STATIC METHODS)
5182
5183 // Standard deviation of a 1D array of BigDecimals, aa
5184 public static double standardDeviation(BigDecimal[] aa){
5185 return Math.sqrt(Stat.variance(aa).doubleValue());
5186 }
5187
5188 // Standard deviation of a 1D array of BigIntegers, aa
5189 public static double standardDeviation(BigInteger[] aa){
5190 return Math.sqrt(Stat.variance(aa).doubleValue());
5191 }
5192
5193 // Standard deviation of a 1D array of Complex, aa
5194 public static Complex standardDeviation(Complex[] aa){
5195 return Complex.sqrt(Stat.variance(aa));
5196 }
5197
5198 // Standard deviation of a 1D array of Complex, aa, conjugate formula
5199 public static double standardDeviationConjugateCalcn(Complex[] aa){
5200 return Math.sqrt(Stat.varianceConjugateCalcn(aa));
5201 }
5202
5203 // Standard deviation of the moduli of a 1D array of Complex aa
5204 public static double standardDeviationModuli(Complex[] aa){
5205 ArrayMaths am = new ArrayMaths(aa);
5206 double[] rl = am.array_as_modulus_of_Complex();
5207 double standardDeviation = Stat.standardDeviation(rl);
5208 return standardDeviation;
5209 }
5210
5211 // Standard deviation of the real parts of a 1D array of Complex aa
5212 public static double standardDeviationRealParts(Complex[] aa){
5213 ArrayMaths am = new ArrayMaths(aa);
5214 double[] rl = am.array_as_real_part_of_Complex();
5215 double standardDeviation = Stat.standardDeviation(rl);
5216 return standardDeviation;
5217 }
5218
5219 // Standard deviation of the imaginary parts of a 1D array of Complex aa
5220 public static double standardDeviationImaginaryParts(Complex[] aa){
5221 ArrayMaths am = new ArrayMaths(aa);
5222 double[] im = am.array_as_imaginary_part_of_Complex();
5223 double standardDeviation = Stat.standardDeviation(im);
5224 return standardDeviation;
5225 }
5226
5227 // Standard deviation of a 1D array of doubles, aa
5228 public static double standardDeviation(double[] aa){
5229 return Math.sqrt(Stat.variance(aa));
5230 }
5231
5232 // Standard deviation of a 1D array of floats, aa
5233 public static float standardDeviation(float[] aa){
5234 return (float)Math.sqrt(Stat.variance(aa));
5235 }
5236
5237 // Standard deviation of a 1D array of int, aa
5238 public static double standardDeviation(int[] aa){
5239 return Math.sqrt(Stat.variance(aa));
5240 }
5241
5242 // Standard deviation of a 1D array of long, aa
5243 public static double standardDeviation(long[] aa){
5244 return Math.sqrt(Stat.variance(aa));
5245 }
5246
5247 // Weighted standard deviation of a 1D array of Complex, aa
5248 public static Complex standardDeviation(Complex[] aa, Complex[] ww){
5249 if(aa.length!=ww.length)throw new IllegalArgumentException("length of variable array, " + aa.length + " and length of weight array, " + ww.length + " are different");
5250 return Complex.sqrt(Stat.variance(aa, ww));
5251 }
5252
5253 // Weighted standard deviation of a 1D array of Complex, aa, using conjugate formula
5254 public static double standardDeviationConjugateCalcn(Complex[] aa, Complex[] ww){
5255 if(aa.length!=ww.length)throw new IllegalArgumentException("length of variable array, " + aa.length + " and length of weight array, " + ww.length + " are different");
5256 return Math.sqrt(Stat.varianceConjugateCalcn(aa, ww));
5257 }
5258
5259 // Weighted standard deviation of the moduli of a 1D array of Complex aa
5260 public static double standardDeviationModuli(Complex[] aa, Complex[] ww){
5261 int n = aa.length;
5262 if(n!=ww.length)throw new IllegalArgumentException("length of variable array, " + n + " and length of weight array, " + ww.length + " are different");
5263 ArrayMaths am = new ArrayMaths(aa);
5264 double[] rl = am.array_as_modulus_of_Complex();
5265 ArrayMaths wm = new ArrayMaths(ww);
5266 double[] wt = wm.array_as_modulus_of_Complex();
5267 double standardDeviation = Stat.standardDeviation(rl, wt);
5268 return standardDeviation;
5269 }
5270
5271 // Weighted standard deviation of the real parts of a 1D array of Complex aa
5272 public static double standardDeviationRealParts(Complex[] aa, Complex[] ww){
5273 int n = aa.length;
5274 if(n!=ww.length)throw new IllegalArgumentException("length of variable array, " + n + " and length of weight array, " + ww.length + " are different");
5275 ArrayMaths am = new ArrayMaths(aa);
5276 double[] rl = am.array_as_real_part_of_Complex();
5277 ArrayMaths wm = new ArrayMaths(ww);
5278 double[] wt = wm.array_as_real_part_of_Complex();
5279 double standardDeviation = Stat.standardDeviation(rl, wt);
5280 return standardDeviation;
5281 }
5282
5283 // Weighted standard deviation of the imaginary parts of a 1D array of Complex aa
5284 public static double standardDeviationImaginaryParts(Complex[] aa, Complex[] ww){
5285 int n = aa.length;
5286 if(n!=ww.length)throw new IllegalArgumentException("length of variable array, " + n + " and length of weight array, " + ww.length + " are different");
5287 ArrayMaths am = new ArrayMaths(aa);
5288 double[] im = am.array_as_imaginary_part_of_Complex();
5289 ArrayMaths wm = new ArrayMaths(ww);
5290 double[] wt = wm.array_as_imaginary_part_of_Complex();
5291 double standardDeviation = Stat.standardDeviation(im, wt);
5292 return standardDeviation;
5293 }
5294
5295
5296 // Weighted standard deviation of a 1D array of BigDecimal, aa
5297 public static double standardDeviation(BigDecimal[] aa, BigDecimal[] ww){
5298 if(aa.length!=ww.length)throw new IllegalArgumentException("length of variable array, " + aa.length + " and length of weight array, " + ww.length + " are different");
5299 return Math.sqrt(Stat.variance(aa, ww).doubleValue());
5300 }
5301
5302 // Weighted standard deviation of a 1D array of BigInteger, aa
5303 public static double standardDeviation(BigInteger[] aa, BigInteger[] ww){
5304 if(aa.length!=ww.length)throw new IllegalArgumentException("length of variable array, " + aa.length + " and length of weight array, " + ww.length + " are different");
5305 return Math.sqrt(Stat.variance(aa, ww).doubleValue());
5306 }
5307
5308 // Weighted standard deviation of a 1D array of doubles, aa
5309 public static double standardDeviation(double[] aa, double[] ww){
5310 if(aa.length!=ww.length)throw new IllegalArgumentException("length of variable array, " + aa.length + " and length of weight array, " + ww.length + " are different");
5311 return Math.sqrt(Stat.variance(aa, ww));
5312 }
5313
5314 // Weighted standard deviation of a 1D array of floats, aa
5315 public static float standardDeviation(float[] aa, float[] ww){
5316 if(aa.length!=ww.length)throw new IllegalArgumentException("length of variable array, " + aa.length + " and length of weight array, " + ww.length + " are different");
5317 return (float)Math.sqrt(Stat.variance(aa, ww));
5318 }
5319
5320
5321 // VOLATILITIES
5322
5323 // volatility log (BigDecimal)
5324 public static double volatilityLogChange(BigDecimal[] array){
5325 int n = array.length-1;
5326 double[] change = new double[n];
5327 for(int i=0; i<n; i++)change[i] = Math.log((array[i+1].divide(array[i], BigDecimal.ROUND_HALF_UP)).doubleValue());
5328 return Stat.standardDeviation(change);
5329 }
5330
5331 // volatility log (BigInteger)
5332 public static double volatilityLogChange(BigInteger[] array){
5333 int n = array.length-1;
5334 double[] change = new double[n];
5335 for(int i=0; i<n; i++)change[i] = Math.log(((new BigDecimal(array[i+1])).divide( new BigDecimal(array[i]), BigDecimal.ROUND_HALF_UP)).doubleValue());
5336 return Stat.standardDeviation(change);
5337 }
5338
5339 // volatility log (doubles)
5340 public static double volatilityLogChange(double[] array){
5341 int n = array.length-1;
5342 double[] change = new double[n];
5343 for(int i=0; i<n; i++)change[i] = Math.log(array[i+1]/array[i]);
5344 return Stat.standardDeviation(change);
5345 }
5346
5347 // volatility log (floats)
5348 public static float volatilityLogChange(float[] array){
5349 int n = array.length-1;
5350 float[] change = new float[n];
5351 for(int i=0; i<n; i++)change[i] = (float)Math.log(array[i+1]/array[i]);
5352 return Stat.standardDeviation(change);
5353 }
5354
5355 // volatility percentage (BigDecimal)
5356 public static double volatilityPerCentChange(BigDecimal[] array){
5357 int n = array.length-1;
5358 double[] change = new double[n];
5359 for(int i=0; i<n; i++)change[i] = ((array[i+1].add(array[i])).multiply((new BigDecimal(100.0D)).divide(array[i], BigDecimal.ROUND_HALF_UP))).doubleValue();
5360 return Stat.standardDeviation(change);
5361 }
5362
5363 // volatility percentage (Biginteger)
5364 public static double volatilityPerCentChange(BigInteger[] array){
5365 int n = array.length-1;
5366 double[] change = new double[n];
5367 ArrayMaths am = new ArrayMaths(array);
5368 BigDecimal[] bd = am.getArray_as_BigDecimal();
5369 for(int i=0; i<n; i++)change[i] = ((bd[i+1].add(bd[i])).multiply((new BigDecimal(100.0D)).divide(bd[i], BigDecimal.ROUND_HALF_UP))).doubleValue();
5370 bd = null;
5371 return Stat.standardDeviation(change);
5372 }
5373
5374 // volatility percentage (double)
5375 public static double volatilityPerCentChange(double[] array){
5376 int n = array.length-1;
5377 double[] change = new double[n];
5378 for(int i=0; i<n; i++)change[i] = (array[i+1] - array[i])*100.0D/array[i];
5379 return Stat.standardDeviation(change);
5380 }
5381
5382 // volatility percentage (float)
5383 public static double volatilityPerCentChange(float[] array){
5384 int n = array.length-1;
5385 float[] change = new float[n];
5386 for(int i=0; i<n; i++)change[i] = (array[i+1] - array[i])*100.0F/array[i];
5387 return Stat.standardDeviation(change);
5388 }
5389
5390
5391 // COEFFICIENT OF VARIATION
5392
5393 // Coefficient of variation of an array of BigInteger
5394 public static double coefficientOfVariation(BigInteger[] array){
5395 return 100.0D*Stat.standardDeviation(array)/Math.abs(Stat.mean(array).doubleValue());
5396 }
5397
5398 // Coefficient of variation of an array of BigDecimals
5399 public static double coefficientOfVariation(BigDecimal[] array){
5400 return 100.0D*Stat.standardDeviation(array)/Math.abs(Stat.mean(array).doubleValue());
5401 }
5402
5403 // Coefficient of variation of an array of doubles
5404 public static double coefficientOfVariation(double[] array){
5405 return 100.0D*Stat.standardDeviation(array)/Math.abs(Stat.mean(array));
5406 }
5407
5408 // Coefficient of variation of an array of float
5409 public static float coefficientOfVariation(float[] array){
5410 return 100.0F*Stat.standardDeviation(array)/Math.abs(Stat.mean(array));
5411 }
5412
5413
5414 // WEIGHTED COEFFICIENT OF VARIATION
5415
5416 // Weighted coefficient of variation of an array of BigInteger
5417 public static double coefficientOfVariation(BigInteger[] array, BigInteger[] weight){
5418 int n = array.length;
5419 if(n!=weight.length)throw new IllegalArgumentException("length of variable array, " + n + " and length of weight array, " + weight.length + " are different");
5420
5421 return 100.0D*Stat.standardDeviation(array, weight)/Math.abs(Stat.mean(array, weight).doubleValue());
5422 }
5423
5424 // Weighted coefficient of variation of an array of BigDecimals
5425 public static double coefficientOfVariation(BigDecimal[] array, BigDecimal[] weight){
5426 int n = array.length;
5427 if(n!=weight.length)throw new IllegalArgumentException("length of variable array, " + n + " and length of weight array, " + weight.length + " are different");
5428
5429 return 100.0D*Stat.standardDeviation(array, weight)/Math.abs(Stat.mean(array, weight).doubleValue());
5430 }
5431
5432 // Weighted coefficient of variation of an array of doubles
5433 public static double coefficientOfVariation(double[] array, double[] weight){
5434 int n = array.length;
5435 if(n!=weight.length)throw new IllegalArgumentException("length of variable array, " + n + " and length of weight array, " + weight.length + " are different");
5436
5437 return 100.0D*Stat.standardDeviation(array, weight)/Math.abs(Stat.mean(array, weight));
5438 }
5439
5440 // Weighted coefficient of variation of an array of float
5441 public static float coefficientOfVariation(float[] array, float[] weight){
5442 int n = array.length;
5443 if(n!=weight.length)throw new IllegalArgumentException("length of variable array, " + n + " and length of weight array, " + weight.length + " are different");
5444
5445 return 100.0F*Stat.standardDeviation(array, weight)/Math.abs(Stat.mean(array, weight));
5446 }
5447
5448
5449 //STANDARDIZATION
5450 // Standardization of an array of doubles to a mean of 0 and a standard deviation of 1
5451 public static double[] standardize(double[] aa){
5452 double mean0 = Stat.mean(aa);
5453 double sd0 = Stat.standardDeviation(aa);
5454 int n = aa.length;
5455 double[] bb = new double[n];
5456 if(sd0==0.0){
5457 for(int i=0; i<n; i++){
5458 bb[i] = 1.0;
5459 }
5460 }
5461 else{
5462 for(int i=0; i<n; i++){
5463 bb[i] = (aa[i] - mean0)/sd0;
5464 }
5465 }
5466 return bb;
5467 }
5468
5469 public static double[] standardise(double[] aa){
5470 return Stat.standardize(aa);
5471 }
5472
5473 // Standardization of an array of floats to a mean of 0 and a standard deviation of 1
5474 public static float[] standardize(float[] aa){
5475 float mean0 = Stat.mean(aa);
5476 float sd0 = Stat.standardDeviation(aa);
5477 int n = aa.length;
5478 float[] bb = new float[n];
5479 if(sd0==0.0){
5480 for(int i=0; i<n; i++){
5481 bb[i] = 1.0F;
5482 }
5483 }
5484 else{
5485 for(int i=0; i<n; i++){
5486 bb[i] = (aa[i] - mean0)/sd0;
5487 }
5488 }
5489 return bb;
5490 }
5491
5492 public static float[] standardise(float[] aa){
5493 return Stat.standardize(aa);
5494 }
5495
5496 // Standardization of an array of longs to a mean of 0 and a standard deviation of 1
5497 // converts to double
5498 public static double[] standardize(long[] aa){
5499 double mean0 = Stat.mean(aa);
5500 double sd0 = Stat.standardDeviation(aa);
5501 int n = aa.length;
5502 double[] bb = new double[n];
5503 if(sd0==0.0){
5504 for(int i=0; i<n; i++){
5505 bb[i] = 1.0;
5506 }
5507 }
5508 else{
5509 for(int i=0; i<n; i++){
5510 bb[i] = ((double)aa[i] - mean0)/sd0;
5511 }
5512 }
5513 return bb;
5514 }
5515
5516 public static double[] standardise(long[] aa){
5517 return Stat.standardize(aa);
5518 }
5519
5520 // Standardization of an array of ints to a mean of 0 and a standard deviation of 1
5521 // converts to double
5522 public static double[] standardize(int[] aa){
5523 double mean0 = Stat.mean(aa);
5524 double sd0 = Stat.standardDeviation(aa);
5525 int n = aa.length;
5526 double[] bb = new double[n];
5527 if(sd0==0.0){
5528 for(int i=0; i<n; i++){
5529 bb[i] = 1.0;
5530 }
5531 }
5532 else{
5533 for(int i=0; i<n; i++){
5534 bb[i] = ((double)aa[i] - mean0)/sd0;
5535 }
5536 }
5537 return bb;
5538 }
5539
5540 public static double[] standardise(int[] aa){
5541 return Stat.standardize(aa);
5542 }
5543
5544 // Standardization of an array of BigDecimals to a mean of 0 and a standard deviation of 1
5545 // converts to double
5546 public static double[] standardize(BigDecimal[] aa){
5547 double mean0 = Stat.mean(aa).doubleValue();
5548 double sd0 = Stat.standardDeviation(aa);
5549 int n = aa.length;
5550 double[] bb = new double[n];
5551 if(sd0==0.0){
5552 for(int i=0; i<n; i++){
5553 bb[i] = 1.0;
5554 }
5555 }
5556 else{
5557 for(int i=0; i<n; i++){
5558 bb[i] = (aa[i].doubleValue() - mean0)/sd0;
5559 }
5560 }
5561 return bb;
5562 }
5563
5564 public static double[] standardise(BigDecimal[] aa){
5565 return Stat.standardize(aa);
5566 }
5567
5568 // Standardization of an array of BigIntegers to a mean of 0 and a standard deviation of 1
5569 // converts to double
5570 public static double[] standardize(BigInteger[] aa){
5571 ArrayMaths am = new ArrayMaths(aa);
5572 BigDecimal[] bd = am.getArray_as_BigDecimal();
5573
5574 return Stat.standardize(bd);
5575 }
5576
5577 public static double[] standardise(BigInteger[] aa){
5578 return Stat.standardize(aa);
5579 }
5580
5581 // SCALING DATA
5582 // Scale an array of doubles to a new mean and new standard deviation
5583 public static double[] scale(double[] aa, double mean, double sd){
5584 double[] bb = Stat.standardize(aa);
5585 int n = aa.length;
5586 for(int i=0; i<n; i++){
5587 bb[i] = bb[i]*sd + mean;
5588 }
5589
5590 return bb;
5591 }
5592
5593 // Scale an array of floats to a new mean and new standard deviation
5594 public static float[] scale(float[] aa, float mean, float sd){
5595 float[] bb = Stat.standardize(aa);
5596 int n = aa.length;
5597 for(int i=0; i<n; i++){
5598 bb[i] = bb[i]*sd + mean;
5599 }
5600
5601 return bb;
5602 }
5603
5604 // Scale an array of longs to a new mean and new standard deviation
5605 public static double[] scale(long[] aa, double mean, double sd){
5606 double[] bb = Stat.standardize(aa);
5607 int n = aa.length;
5608 for(int i=0; i<n; i++){
5609 bb[i] = bb[i]*sd + mean;
5610 }
5611
5612 return bb;
5613 }
5614
5615 // Scale an array of longs to a new mean and new standard deviation
5616 public static double[] scale(int[] aa, double mean, double sd){
5617 double[] bb = Stat.standardize(aa);
5618 int n = aa.length;
5619 for(int i=0; i<n; i++){
5620 bb[i] = bb[i]*sd + mean;
5621 }
5622
5623 return bb;
5624 }
5625
5626 // Scale an array of BigDecimals to a new mean and new standard deviation
5627 public static double[] scale(BigDecimal[] aa, double mean, double sd){
5628 double[] bb = Stat.standardize(aa);
5629 int n = aa.length;
5630 for(int i=0; i<n; i++){
5631 bb[i] = bb[i]*sd + mean;
5632 }
5633
5634 return bb;
5635 }
5636
5637 // Scale an array of BigIntegers to a new mean and new standard deviation
5638 public static double[] scale(BigInteger[] aa, double mean, double sd){
5639 ArrayMaths am = new ArrayMaths(aa);
5640 BigDecimal[] bd = am.getArray_as_BigDecimal();
5641
5642 return Stat.scale(bd, mean, sd);
5643 }
5644
5645
5646 // SKEWNESS
5647 // Static Methods
5648 // Moment skewness of a 1D array of doubles
5649 public static double momentSkewness(double[] aa){
5650 int n = aa.length;
5651 double denom = (double)(n-1);
5652 if(Stat.nFactorOptionS)denom = (double)n;
5653 double sum = 0.0D;
5654 double mean = Stat.mean(aa);
5655 for(int i=0; i<n; i++){
5656 sum+=Math.pow((aa[i]-mean), 3);
5657 }
5658 sum = sum/denom;
5659 return sum/Math.pow(Stat.standardDeviation(aa), 3);
5660 }
5661
5662
5663 // Moment skewness of a 1D array of floats
5664 public static float momentSkewness(float[] aa){
5665 int n = aa.length;
5666 float denom = (float)(n-1);
5667 if(Stat.nFactorOptionS)denom = (float)n;
5668 float sum = 0.0F;
5669 float mean = Stat.mean(aa);
5670 for(int i=0; i<n; i++){
5671 sum+=Math.pow((aa[i]-mean), 3);
5672 }
5673 sum = sum/denom;
5674 return sum/((float)Math.pow(Stat.standardDeviation(aa), 3));
5675 }
5676
5677 // Moment skewness of a 1D array of BigDecimal
5678 public static double momentSkewness(BigDecimal[] aa){
5679 int n = aa.length;
5680 double denom = (double)(n-1);
5681 if(Stat.nFactorOptionS)denom = (double)n;
5682 BigDecimal sum = BigDecimal.ZERO;
5683 BigDecimal mean = Stat.mean(aa);
5684 double sd = Stat.standardDeviation(aa);
5685 for(int i=0; i<n; i++){
5686 BigDecimal hold = aa[i].subtract(mean);
5687 sum = sum.add(hold.multiply(hold.multiply(hold)) );
5688 }
5689 sum = sum.multiply(new BigDecimal(1.0/denom));
5690 return sum.doubleValue()/Math.pow(sd, 3);
5691 }
5692
5693 // Moment skewness of a 1D array of long
5694 public static double momentSkewness(long[] aa){
5695 int n = aa.length;
5696 double denom = (double)(n-1);
5697 if(Stat.nFactorOptionS)denom = (double)n;
5698 double sum = 0.0D;
5699 double mean = Stat.mean(aa);
5700 for(int i=0; i<n; i++){
5701 sum+=Math.pow((aa[i]-mean), 3);
5702 }
5703 sum = sum/denom;
5704 return sum/Math.pow(Stat.standardDeviation(aa), 3);
5705 }
5706
5707 // Moment skewness of a 1D array of int
5708 public static double momentSkewness(int[] aa){
5709 int n = aa.length;
5710 double denom = (double)(n-1);
5711 if(Stat.nFactorOptionS)denom = (double)n;
5712 double sum = 0.0D;
5713 double mean = Stat.mean(aa);
5714 for(int i=0; i<n; i++){
5715 sum+=Math.pow((aa[i]-mean), 3);
5716 }
5717 sum = sum/denom;
5718 return sum/Math.pow(Stat.standardDeviation(aa), 3);
5719 }
5720
5721
5722
5723
5724 // Median skewness of a 1D array of doubles
5725 public static double medianSkewness(double[] aa){
5726 double mean = Stat.mean(aa);
5727 double median = Stat.median(aa);
5728 double sd = Stat.standardDeviation(aa);
5729 return 3.0*(mean - median)/sd;
5730 }
5731
5732 // Median skewness of a 1D array of floats
5733 public static float medianSkewness(float[] aa){
5734 float mean = Stat.mean(aa);
5735 float median = Stat.median(aa);
5736 float sd = Stat.standardDeviation(aa);
5737 return 3.0F*(mean - median)/sd;
5738 }
5739
5740 // Median skewness of a 1D array of BigDecimal
5741 public static double medianSkewness(BigDecimal[] aa){
5742 BigDecimal mean = Stat.mean(aa);
5743 BigDecimal median = Stat.median(aa);
5744 double sd = Stat.standardDeviation(aa);
5745 return 3.0*(mean.subtract(median)).doubleValue()/sd;
5746 }
5747
5748 // Median skewness of a 1D array of long
5749 public static double medianSkewness(long[] aa){
5750 double mean = Stat.mean(aa);
5751 double median = Stat.median(aa);
5752 double sd = Stat.standardDeviation(aa);
5753 return 3.0*(mean - median)/sd;
5754 }
5755
5756 // Median skewness of a 1D array of int
5757 public static double medianSkewness(int[] aa){
5758 double mean = Stat.mean(aa);
5759 double median = Stat.median(aa);
5760 double sd = Stat.standardDeviation(aa);
5761 return 3.0*(mean - median)/sd;
5762 }
5763
5764
5765 // Quartile skewness of a 1D array of double
5766 public static double quartileSkewness(double[] aa){
5767 int n = aa.length;
5768 double median50 = Stat.median(aa);
5769 int start1 = 0;
5770 int start2 = 0;
5771 int end1 = n/2 - 1;
5772 int end2 = n-1;
5773 if(Fmath.isOdd(n)){
5774 start2 = end1 + 2;
5775 }
5776 else{
5777 start2 = end1 + 1;
5778 }
5779 ArrayMaths am = new ArrayMaths(aa);
5780 double[] first = am.subarray_as_double(start1, end1);
5781 double[] last = am.subarray_as_double(start2, end2);
5782 double median25 = Stat.median(first);
5783 double median75 = Stat.median(last);
5784
5785 double ret = (median25 - 2.0*median50 + median75)/(median75 - median25);
5786 if(Fmath.isNaN(ret))ret = 1.0;
5787 return ret;
5788 }
5789
5790 // Quartile skewness of a 1D array of float
5791 public static float quartileSkewness(float[] aa){
5792 int n = aa.length;
5793 float median50 = Stat.median(aa);
5794 int start1 = 0;
5795 int start2 = 0;
5796 int end1 = n/2 - 1;
5797 int end2 = n-1;
5798 if(Fmath.isOdd(n)){
5799 start2 = end1 + 2;
5800 }
5801 else{
5802 start2 = end1 + 1;
5803 }
5804 ArrayMaths am = new ArrayMaths(aa);
5805 float[] first = am.subarray_as_float(start1, end1);
5806 float[] last = am.subarray_as_float(start2, end2);
5807 float median25 = Stat.median(first);
5808 float median75 = Stat.median(last);
5809
5810 float ret = (median25 - 2.0F*median50 + median75)/(median75 - median25);
5811 if(Fmath.isNaN(ret))ret = 1.0F;
5812 return ret;
5813 }
5814
5815
5816 // Quartile skewness of a 1D array of BigDecimal
5817 public static BigDecimal quartileSkewness(BigDecimal[] aa){
5818 int n = aa.length;
5819 BigDecimal median50 = Stat.median(aa);
5820 int start1 = 0;
5821 int start2 = 0;
5822 int end1 = n/2 - 1;
5823 int end2 = n-1;
5824 if(Fmath.isOdd(n)){
5825 start2 = end1 + 2;
5826 }
5827 else{
5828 start2 = end1 + 1;
5829 }
5830 ArrayMaths am = new ArrayMaths(aa);
5831 BigDecimal[] first = am.subarray_as_BigDecimal(start1, end1);
5832 BigDecimal[] last = am.subarray_as_BigDecimal(start2, end2);
5833 BigDecimal median25 = Stat.median(first);
5834 BigDecimal median75 = Stat.median(last);
5835 BigDecimal ret1 = (median25.subtract(median50.multiply(new BigDecimal(2.0)))).add(median75);
5836 BigDecimal ret2 = median75.subtract(median25);
5837 BigDecimal ret = ret1.divide(ret2,BigDecimal.ROUND_HALF_UP);
5838 if(Fmath.isNaN(ret.doubleValue()))ret = new BigDecimal(1.0D);
5839 first = null;
5840 last = null;
5841 median25 = null;
5842 median50 = null;
5843 median75 = null;
5844 ret1 = null;
5845 ret2 = null;
5846 return ret;
5847 }
5848
5849 // Quartile skewness of a 1D array of BigInteger
5850 public static BigDecimal quartileSkewness(BigInteger[] aa){
5851 ArrayMaths am = new ArrayMaths(aa);
5852 BigDecimal[] bd = am.array_as_BigDecimal();
5853 return Stat.quartileSkewness(bd);
5854 }
5855
5856
5857 // Quartile skewness of a 1D array of long
5858 public static double quartileSkewness(long[] aa){
5859 int n = aa.length;
5860 double median50 = Stat.median(aa);
5861 int start1 = 0;
5862 int start2 = 0;
5863 int end1 = n/2 - 1;
5864 int end2 = n-1;
5865 if(Fmath.isOdd(n)){
5866 start2 = end1 + 2;
5867 }
5868 else{
5869 start2 = end1 + 1;
5870 }
5871 ArrayMaths am = new ArrayMaths(aa);
5872 double[] first = am.subarray_as_double(start1, end1);
5873 double[] last = am.subarray_as_double(start2, end2);
5874 double median25 = Stat.median(first);
5875 double median75 = Stat.median(last);
5876
5877 double ret = (median25 - 2.0*median50 + median75)/(median75 - median25);
5878 if(Fmath.isNaN(ret))ret = 1.0;
5879 return ret;
5880 }
5881
5882 // Quartile skewness of a 1D array of int
5883 public static double quartileSkewness(int[] aa){
5884 int n = aa.length;
5885 double median50 = Stat.median(aa);
5886 int start1 = 0;
5887 int start2 = 0;
5888 int end1 = n/2 - 1;
5889 int end2 = n-1;
5890 if(Fmath.isOdd(n)){
5891 start2 = end1 + 2;
5892 }
5893 else{
5894 start2 = end1 + 1;
5895 }
5896 ArrayMaths am = new ArrayMaths(aa);
5897 double[] first = am.subarray_as_double(start1, end1);
5898 double[] last = am.subarray_as_double(start2, end2);
5899 double median25 = Stat.median(first);
5900 double median75 = Stat.median(last);
5901
5902 double ret = (median25 - 2.0*median50 + median75)/(median75 - median25);
5903 if(Fmath.isNaN(ret))ret = 1.0;
5904 return ret;
5905 }
5906
5907
5908
5909 // KURTOSIS
5910 // Static Methods
5911 // Kutosis of a 1D array of doubles
5912 public static double kurtosis(double[] aa){
5913 int n = aa.length;
5914 double denom = (double)(n-1);
5915 if(Stat.nFactorOptionS)denom = (double)n;
5916 double sum=0.0D;
5917 double mean = Stat.mean(aa);
5918 for(int i=0; i<n; i++){
5919 sum+=Math.pow((aa[i]-mean), 4);
5920 }
5921
5922 sum = sum/denom;
5923 double ret = sum/Fmath.square(Stat.variance(aa));
5924 if(Fmath.isNaN(ret))ret = 2.0/denom;
5925 return ret;
5926 }
5927
5928 public static double curtosis(double[] aa){
5929 return Stat.kurtosis(aa);
5930 }
5931
5932 // Kutosis excess of a 1D array of doubles
5933 public static double kurtosisExcess(double[] aa){
5934 return Stat.kurtosis(aa) - 3.0D;
5935 }
5936
5937 public static double curtosisExcess(double[] aa){
5938 return Stat.kurtosisExcess(aa);
5939 }
5940
5941 public static double excessKurtosis(double[] aa){
5942 return Stat.kurtosisExcess(aa);
5943 }
5944
5945 public static double excessCurtosis(double[] aa){
5946 return Stat.kurtosisExcess(aa);
5947 }
5948
5949 // Kutosis of a 1D array of floats
5950 public static float kurtosis(float[] aa){
5951 int n = aa.length;
5952 float denom = (float)(n-1);
5953 if(Stat.nFactorOptionS)denom = (float)n;
5954 float sum=0.0F;
5955 float mean = Stat.mean(aa);
5956 for(int i=0; i<n; i++){
5957 sum+=Math.pow((aa[i]-mean), 4);
5958 }
5959 sum = sum/denom;
5960 float ret = sum/Fmath.square(Stat.variance(aa));
5961 if(Fmath.isNaN(ret))ret = 2.0F/denom;
5962 return ret;
5963 }
5964
5965 public static float curtosis(float[] aa){
5966 return Stat.kurtosis(aa);
5967 }
5968
5969 // Kutosis excess of a 1D array of floats
5970 public static float kurtosisExcess(float[] aa){
5971 return Stat.kurtosis(aa) - 3.0F;
5972 }
5973
5974 public static float curtosisExcess(float[] aa){
5975 return Stat.kurtosisExcess(aa);
5976 }
5977
5978 public static float excessKurtosis(float[] aa){
5979 return Stat.kurtosisExcess(aa);
5980 }
5981
5982 public static float excessCurtosis(float[] aa){
5983 return Stat.kurtosisExcess(aa);
5984 }
5985
5986 // Kutosis of a 1D array of BigInteger
5987 public static BigDecimal kurtosis(BigInteger[] aa){
5988 ArrayMaths am = new ArrayMaths(aa);
5989 BigDecimal[] bd = am.array_as_BigDecimal();
5990 return Stat.kurtosis(bd);
5991 }
5992
5993 public static BigDecimal curtosis(BigInteger[] aa){
5994 return Stat.kurtosis(aa);
5995 }
5996
5997 // Kutosis excess of a 1D array of BigInteger
5998 public static BigDecimal kurtosisExcess(BigInteger[] aa){
5999 return Stat.kurtosis(aa).subtract(new BigDecimal("3.0"));
6000 }
6001
6002 public static BigDecimal curtosisExcess(BigInteger[] aa){
6003 return Stat.kurtosisExcess(aa);
6004 }
6005
6006 public static BigDecimal excessKurtosis(BigInteger[] aa){
6007 return Stat.kurtosisExcess(aa);
6008 }
6009
6010 public static BigDecimal excessCurtosis(BigInteger[] aa){
6011 return Stat.kurtosisExcess(aa);
6012 }
6013
6014
6015 // Kutosis of a 1D array of BigDecimal
6016 public static BigDecimal kurtosis(BigDecimal[] aa){
6017 int n = aa.length;
6018 double denom = (double)(n-1);
6019 if(Stat.nFactorOptionS)denom = (double)n;
6020 BigDecimal sum = BigDecimal.ZERO;
6021 BigDecimal mean = Stat.mean(aa);
6022 for(int i=0; i<n; i++){
6023 BigDecimal hold = aa[i].subtract(mean);
6024 sum = sum.add(hold.multiply(hold.multiply(hold.multiply(hold))));
6025 }
6026 sum = sum.divide(new BigDecimal(denom), BigDecimal.ROUND_HALF_UP);
6027 mean = Stat.variance(aa);
6028 if(mean.doubleValue()==0.0){
6029 sum = new BigDecimal(2.0/denom);
6030 }
6031 else{
6032 sum = sum.divide(mean.multiply(mean), BigDecimal.ROUND_HALF_UP);
6033 }
6034 mean = null;
6035 return sum;
6036 }
6037
6038 public static BigDecimal curtosis(BigDecimal[] aa){
6039 return Stat.kurtosis(aa);
6040 }
6041
6042 // Kutosis excess of a 1D array of BigDecimal
6043 public static BigDecimal kurtosisExcess(BigDecimal[] aa){
6044 return Stat.kurtosis(aa).subtract(new BigDecimal("3.0"));
6045 }
6046
6047 public static BigDecimal curtosisExcess(BigDecimal[] aa){
6048 return Stat.kurtosisExcess(aa);
6049 }
6050
6051 public static BigDecimal excessCurtosis(BigDecimal[] aa){
6052 return Stat.kurtosisExcess(aa);
6053 }
6054
6055 public static BigDecimal excessKurtosis(BigDecimal[] aa){
6056 return Stat.kurtosisExcess(aa);
6057 }
6058
6059
6060 // Kutosis of a 1D array of long
6061 public static double kurtosis(long[] aa){
6062 int n = aa.length;
6063 double denom = (double)(n-1);
6064 if(Stat.nFactorOptionS)denom = (double)n;
6065 double sum=0.0D;
6066 double mean = Stat.mean(aa);
6067 for(int i=0; i<n; i++){
6068 sum+=Math.pow(((double)aa[i]-mean), 4);
6069 }
6070 sum = sum/denom;
6071 double ret = sum/Fmath.square(Stat.variance(aa));
6072 if(Fmath.isNaN(ret))ret = 2.0/denom;
6073 return ret;
6074 }
6075
6076 public static double curtosis(long[] aa){
6077 return Stat.kurtosis(aa);
6078 }
6079
6080 // Kutosis excess of a 1D array of long
6081 public static double kurtosisExcess(long[] aa){
6082 return Stat.kurtosis(aa) - 3.0D;
6083 }
6084
6085 public static double curtosisExcess(long[] aa){
6086 return Stat.kurtosisExcess(aa);
6087 }
6088
6089 public static double excessCurtosis(long[] aa){
6090 return Stat.kurtosisExcess(aa);
6091 }
6092
6093 public static double excessKurtosis(long[] aa){
6094 return Stat.kurtosisExcess(aa);
6095 }
6096
6097
6098 // Kutosis of a 1D array of int
6099 public static double kurtosis(int[] aa){
6100 int n = aa.length;
6101 double denom = (double)(n-1);
6102 if(Stat.nFactorOptionS)denom = (double)n;
6103 double sum=0.0D;
6104 double mean = Stat.mean(aa);
6105 for(int i=0; i<n; i++){
6106 sum+=Math.pow((aa[i]-mean), 4);
6107 }
6108 sum = sum/denom;
6109 double ret = sum/Fmath.square(Stat.variance(aa));
6110 if(Fmath.isNaN(ret))ret = 2.0/denom;
6111 return ret;
6112 }
6113
6114 public static double curtosis(int[] aa){
6115 return Stat.kurtosis(aa);
6116 }
6117
6118 // Kutosis excess of a 1D array of int
6119 public static double kurtosisExcess(int[] aa){
6120 return Stat.kurtosis(aa) - 3.0D;
6121 }
6122
6123 public static double curtosisExcess(int[] aa){
6124 return Stat.kurtosisExcess(aa);
6125 }
6126
6127 public static double excessCurtosis(int[] aa){
6128 return Stat.kurtosisExcess(aa);
6129 }
6130
6131 public static double excessKurtosis(int[] aa){
6132 return Stat.kurtosisExcess(aa);
6133 }
6134
6135 // VARIANCE
6136 // Static methods
6137 // Variance of a 1D array of BigDecimals, aa
6138 public static BigDecimal variance(BigDecimal[] aa){
6139 int n = aa.length;
6140 BigDecimal sum = BigDecimal.ZERO;
6141 BigDecimal mean = Stat.mean(aa);
6142 for(int i=0; i<n; i++){
6143 BigDecimal hold = aa[i].subtract(mean);
6144 sum = sum.add(hold.multiply(hold));
6145 }
6146 BigDecimal ret = sum.divide(new BigDecimal((double)(n-1)), BigDecimal.ROUND_HALF_UP);
6147 if(Stat.nFactorOptionS) ret = sum.divide(new BigDecimal((double)n), BigDecimal.ROUND_HALF_UP);
6148 sum = null;
6149 mean = null;
6150 return ret;
6151 }
6152
6153
6154 // Variance of a 1D array of BigIntegers, aa
6155 public static BigDecimal variance(BigInteger[] aa){
6156 int n = aa.length;
6157 BigDecimal sum = BigDecimal.ZERO;
6158 BigDecimal mean = BigDecimal.ZERO;
6159 for(int i=0; i<n; i++){
6160 sum = sum.add(new BigDecimal(aa[i]));
6161 }
6162 mean = sum.divide(new BigDecimal((double)n), BigDecimal.ROUND_HALF_UP);
6163 sum = BigDecimal.ZERO;
6164 for(int i=0; i<n; i++){
6165 BigDecimal hold = new BigDecimal(aa[i]).subtract(mean);
6166 sum = sum.add(hold.multiply(hold));
6167 }
6168 BigDecimal ret = sum.divide(new BigDecimal((double)(n-1)), BigDecimal.ROUND_HALF_UP);
6169 if(Stat.nFactorOptionS) ret = sum.divide(new BigDecimal((double)n), BigDecimal.ROUND_HALF_UP);
6170 sum = null;
6171 mean = null;
6172 return ret;
6173 }
6174
6175 // Variance of a 1D array of Complex, aa
6176 public static Complex variance(Complex[] aa){
6177 int n = aa.length;
6178 Complex sum = Complex.zero();
6179 Complex mean = Stat.mean(aa);
6180 for(int i=0; i<n; i++){
6181 Complex hold = new Complex(aa[i]).minus(mean);
6182 sum = sum.plus(hold.times(hold));
6183 }
6184 Complex ret = sum.over(new Complex((double)(n-1)));
6185 if(Stat.nFactorOptionS) ret = sum.over(new Complex((double)n));
6186 return ret;
6187 }
6188
6189 // Variance of a 1D array of Complex, aa, using conjugate formula
6190 public static double varianceConjugateCalcn(Complex[] aa){
6191 int n = aa.length;
6192 Complex sum = Complex.zero();
6193 Complex mean = Stat.mean(aa);
6194 for(int i=0; i<n; i++){
6195 Complex hold = new Complex(aa[i]).minus(mean);
6196 sum = sum.plus(hold.times(hold.conjugate()));
6197 }
6198 double ret = sum.getReal()/(double)(n-1);
6199 if(Stat.nFactorOptionS) ret = sum.getReal()/(double)n;
6200 return ret;
6201 }
6202
6203
6204 // Variance of the moduli of a 1D array of Complex aa
6205 public static double varianceModuli(Complex[] aa){
6206 ArrayMaths am = new ArrayMaths(aa);
6207 double[] rl = am.array_as_modulus_of_Complex();
6208 double variance = Stat.variance(rl);
6209 return variance;
6210 }
6211
6212 // Variance of the real parts of a 1D array of Complex aa
6213 public static double varianceRealParts(Complex[] aa){
6214 ArrayMaths am = new ArrayMaths(aa);
6215 double[] rl = am.array_as_real_part_of_Complex();
6216 double variance = Stat.variance(rl);
6217 return variance;
6218 }
6219
6220 // Variance of the imaginary parts of a 1D array of Complex aa
6221 public static double varianceImaginaryParts(Complex[] aa){
6222 ArrayMaths am = new ArrayMaths(aa);
6223 double[] im = am.array_as_imaginary_part_of_Complex();
6224 double variance = Stat.variance(im);
6225 return variance;
6226 }
6227
6228 // Variance of a 1D array of doubles, aa
6229 public static double variance(double[] aa){
6230 int n = aa.length;
6231 double sum=0.0D;
6232 double mean = Stat.mean(aa);
6233 sum=0.0D;
6234 for(int i=0; i<n; i++){
6235 sum+=Fmath.square(aa[i]-mean);
6236 }
6237 double ret = sum/((double)(n-1));
6238 if(Stat.nFactorOptionS) ret = sum/((double)n);
6239 return ret;
6240 }
6241
6242 // Variance of a 1D array of floats, aa
6243 public static float variance(float[] aa){
6244 int n = aa.length;
6245 float sum=0.0F;
6246 float mean = Stat.mean(aa);
6247 for(int i=0; i<n; i++){
6248 sum+=Fmath.square(aa[i]-mean);
6249 }
6250 float ret = sum/((float)(n-1));
6251 if(Stat.nFactorOptionS) ret = sum/((float)n);
6252 return ret;
6253 }
6254
6255 // Variance of a 1D array of int, aa
6256 public static double variance(int[] aa){
6257 int n = aa.length;
6258 double sum=0.0D;
6259 double mean = Stat.mean(aa);
6260 for(int i=0; i<n; i++){
6261 sum+=Fmath.square((double)aa[i]-mean);
6262 }
6263 double ret = sum/((double)(n-1));
6264 if(Stat.nFactorOptionS) ret = sum/((double)n);
6265 return ret;
6266 }
6267
6268 // Variance of a 1D array of long, aa
6269 public static double variance(long[] aa){
6270 int n = aa.length;
6271 double sum=0.0D;
6272 double mean = Stat.mean(aa);
6273 for(int i=0; i<n; i++){
6274 sum+=Fmath.square((double)aa[i]-mean);
6275 }
6276 double ret = sum/((double)(n-1));
6277 if(Stat.nFactorOptionS) ret = sum/((double)n);
6278 return ret;
6279 }
6280
6281 // Weighted variance of a 1D array of doubles, aa
6282 public static double variance(double[] aa, double[] ww){
6283 int n = aa.length;
6284 if(n!=ww.length)throw new IllegalArgumentException("length of variable array, " + n + " and length of weight array, " + ww.length + " are different");
6285 double nn = Stat.effectiveSampleNumber(ww);
6286 double nterm = nn/(nn - 1.0);
6287 if(Stat.nFactorOptionS)nterm = 1.0;
6288
6289 double sumx=0.0D, sumw=0.0D, mean=0.0D;
6290 double[] weight = Stat.invertAndSquare(ww);
6291 for(int i=0; i<n; i++){
6292 sumx+=aa[i]*weight[i];
6293 sumw+=weight[i];
6294 }
6295 mean=sumx/sumw;
6296 sumx=0.0D;
6297 for(int i=0; i<n; i++){
6298 sumx+=weight[i]*Fmath.square(aa[i]-mean);
6299 }
6300 return sumx*nterm/sumw;
6301 }
6302
6303 // Weighted variance of a 1D array of floats, aa
6304 public static float variance(float[] aa, float[] ww){
6305 int n = aa.length;
6306 if(n!=ww.length)throw new IllegalArgumentException("length of variable array, " + n + " and length of weight array, " + ww.length + " are different");
6307 float nn = Stat.effectiveSampleNumber(ww);
6308 float nterm = nn/(nn - 1.0F);
6309 if(Stat.nFactorOptionS)nterm = 1.0F;
6310
6311 float sumx=0.0F, sumw=0.0F, mean=0.0F;
6312 float[] weight = Stat.invertAndSquare(ww);
6313 for(int i=0; i<n; i++){
6314 sumx+=aa[i]*weight[i];
6315 sumw+=weight[i];
6316 }
6317 mean=sumx/sumw;
6318 sumx=0.0F;
6319 for(int i=0; i<n; i++){
6320 sumx+=weight[i]*Fmath.square(aa[i]-mean);
6321 }
6322 return sumx*nterm/sumw;
6323 }
6324
6325 // Weighted variance of a 1D array of Complex aa
6326 public static Complex variance(Complex[] aa, Complex[] ww){
6327 int n = aa.length;
6328 if(n!=ww.length)throw new IllegalArgumentException("length of variable array, " + n + " and length of weight array, " + ww.length + " are different");
6329 Complex nn = Stat.effectiveSampleNumber(ww);
6330 Complex nterm = nn.over(nn.minus(1.0));
6331 if(Stat.nFactorOptionS)nterm = Complex.plusOne();
6332 Complex sumx=Complex.zero();
6333 Complex sumw=Complex.zero();
6334 Complex mean=Complex.zero();
6335 Complex[] weight = Stat.invertAndSquare(ww);
6336 for(int i=0; i<n; i++){
6337 sumx = sumx.plus(aa[i].times(weight[i]));
6338 sumw = sumw.plus(weight[i]);
6339 }
6340 mean=sumx.over(sumw);
6341 sumx=Complex.zero();
6342 for(int i=0; i<n; i++){
6343 Complex hold = aa[i].minus(mean);
6344 sumx = sumx.plus(weight[i].times(hold).times(hold));
6345 }
6346 return (sumx.times(nterm)).over(sumw);
6347 }
6348
6349 // Weighted variance of a 1D array of Complex aa, using conjugate formula
6350 public static double varianceConjugateCalcn(Complex[] aa, Complex[] ww){
6351 int n = aa.length;
6352 if(n!=ww.length)throw new IllegalArgumentException("length of variable array, " + n + " and length of weight array, " + ww.length + " are different");
6353 double nn = Stat.effectiveSampleNumberConjugateCalcn(ww);
6354 double nterm = nn/(nn-1.0);
6355 if(Stat.nFactorOptionS)nterm = 1.0;
6356 Complex sumx=Complex.zero();
6357 Complex sumw=Complex.zero();
6358 Complex sumwc=Complex.zero();
6359 Complex mean=Complex.zero();
6360 Stat st = new Stat(ww);
6361 st = st.invert();
6362 Complex[] weight = st.array_as_Complex();
6363 for(int i=0; i<n; i++){
6364 sumx = sumx.plus(aa[i].times(weight[i].times(weight[i])));
6365 sumw = sumw.plus(weight[i].times(weight[i]));
6366 sumwc = sumwc.plus(weight[i].times(weight[i].conjugate()));
6367 }
6368 mean=sumx.over(sumw);
6369 sumx=Complex.zero();
6370
6371 for(int i=0; i<n; i++){
6372 Complex hold = aa[i].minus(mean);
6373 sumx = sumx.plus((weight[i].times(weight[i].conjugate())).times(hold).times(hold.conjugate()));
6374 }
6375 return nterm*((sumx.times(nterm)).over(sumwc)).getReal();
6376 }
6377
6378 // Weighted variance of the moduli of a 1D array of Complex aa
6379 public static double varianceModuli(Complex[] aa, Complex[] ww){
6380 int n = aa.length;
6381 if(n!=ww.length)throw new IllegalArgumentException("length of variable array, " + n + " and length of weight array, " + ww.length + " are different");
6382 ArrayMaths am = new ArrayMaths(aa);
6383 double[] rl = am.array_as_modulus_of_Complex();
6384 ArrayMaths wm = new ArrayMaths(ww);
6385 double[] wt = wm.array_as_modulus_of_Complex();
6386 double variance = Stat.variance(rl, wt);
6387 return variance;
6388 }
6389
6390 // Weighted variance of the real parts of a 1D array of Complex aa
6391 public static double varianceRealParts(Complex[] aa, Complex[] ww){
6392 int n = aa.length;
6393 if(n!=ww.length)throw new IllegalArgumentException("length of variable array, " + n + " and length of weight array, " + ww.length + " are different");
6394 ArrayMaths am = new ArrayMaths(aa);
6395 double[] rl = am.array_as_real_part_of_Complex();
6396 ArrayMaths wm = new ArrayMaths(ww);
6397 double[] wt = wm.array_as_real_part_of_Complex();
6398 double variance = Stat.variance(rl, wt);
6399 return variance;
6400 }
6401
6402 // Weighted variance of the imaginary parts of a 1D array of Complex aa
6403 public static double varianceImaginaryParts(Complex[] aa, Complex[] ww){
6404 int n = aa.length;
6405 if(n!=ww.length)throw new IllegalArgumentException("length of variable array, " + n + " and length of weight array, " + ww.length + " are different");
6406 ArrayMaths am = new ArrayMaths(aa);
6407 double[] im = am.array_as_imaginary_part_of_Complex();
6408 ArrayMaths wm = new ArrayMaths(ww);
6409 double[] wt = wm.array_as_imaginary_part_of_Complex();
6410 double variance = Stat.variance(im, wt);
6411 return variance;
6412 }
6413
6414
6415 public static BigDecimal variance(BigDecimal[] aa, BigDecimal[] ww){
6416 int n = aa.length;
6417 if(n!=ww.length)throw new IllegalArgumentException("length of variable array, " + n + " and length of weight array, " + ww.length + " are different");
6418 BigDecimal nn = Stat.effectiveSampleNumber(ww);
6419 BigDecimal nterm = nn.divide(nn.subtract(BigDecimal.ONE), BigDecimal.ROUND_HALF_UP);
6420 if(Stat.nFactorOptionS)nterm = BigDecimal.ONE;
6421 BigDecimal sumx=BigDecimal.ZERO;
6422 BigDecimal sumw=BigDecimal.ZERO;
6423 BigDecimal mean=BigDecimal.ZERO;
6424 BigDecimal[] weight = Stat.invertAndSquare(ww);
6425 for(int i=0; i<n; i++){
6426 sumx = sumx.add(aa[i].multiply(weight[i]));
6427 sumw = sumw.add(weight[i]);
6428 }
6429 mean=sumx.divide(sumw, BigDecimal.ROUND_HALF_UP);
6430 sumx=BigDecimal.ZERO;
6431 for(int i=0; i<n; i++){
6432 sumx = sumx.add(weight[i].multiply(aa[i].subtract(mean)).multiply(aa[i].subtract(mean)));
6433 }
6434 sumx = (sumx.multiply(nterm).divide(sumw, BigDecimal.ROUND_HALF_UP));
6435 sumw = null;
6436 mean = null;
6437 weight = null;
6438 nn = null;
6439 nterm = null;
6440 return sumx;
6441 }
6442
6443 public static BigDecimal variance(BigInteger[] aa, BigInteger[] ww){
6444 int n = aa.length;
6445 if(n!=ww.length)throw new IllegalArgumentException("length of variable array, " + n + " and length of weight array, " + ww.length + " are different");
6446 ArrayMaths aab = new ArrayMaths(aa);
6447 ArrayMaths wwb = new ArrayMaths(ww);
6448 return variance(aab.array_as_BigDecimal(), wwb.array_as_BigDecimal());
6449 }
6450
6451
6452 // STANDARD ERROR OF THE MEAN
6453
6454 // Standard error of the mean of a 1D array of BigDecimals, aa
6455 public static double standardError(BigDecimal[] aa){
6456 return Math.sqrt(Stat.variance(aa).doubleValue()/aa.length);
6457 }
6458
6459 // Standard error of the mean of a 1D array of BigIntegers, aa
6460 public static double standardError(BigInteger[] aa){
6461 return Math.sqrt(Stat.variance(aa).doubleValue()/aa.length);
6462 }
6463
6464 // Standard error of the mean of a 1D array of Complex, aa
6465 public static Complex standardError(Complex[] aa){
6466 return Complex.sqrt(Stat.variance(aa).over(aa.length));
6467 }
6468
6469 // Standard error of the mean of a 1D array of Complex, aa, conjugate formula
6470 public static double standardErrorConjugateCalcn(Complex[] aa){
6471 return Math.sqrt(Stat.varianceConjugateCalcn(aa)/aa.length);
6472 }
6473
6474 // Standard error of the moduli of a 1D array of Complex aa
6475 public static double standardErrorModuli(Complex[] aa){
6476 ArrayMaths am = new ArrayMaths(aa);
6477 double[] rl = am.array_as_modulus_of_Complex();
6478 return Stat.standardError(rl);
6479 }
6480
6481 // Standard error of the real parts of a 1D array of Complex aa
6482 public static double standardErrorRealParts(Complex[] aa){
6483 ArrayMaths am = new ArrayMaths(aa);
6484 double[] rl = am.array_as_real_part_of_Complex();
6485 return Stat.standardError(rl);
6486 }
6487
6488 // Standard error of the imaginary parts of a 1D array of Complex aa
6489 public static double standardErrorImaginaryParts(Complex[] aa){
6490 ArrayMaths am = new ArrayMaths(aa);
6491 double[] im = am.array_as_imaginary_part_of_Complex();
6492 return Stat.standardError(im);
6493 }
6494
6495 // Standard error of the mean of a 1D array of doubles, aa
6496 public static double standardError(double[] aa){
6497 return Math.sqrt(Stat.variance(aa)/aa.length);
6498 }
6499
6500 // Standard error of the mean of a 1D array of floats, aa
6501 public static float standardError(float[] aa){
6502 return (float)Math.sqrt(Stat.variance(aa)/aa.length);
6503 }
6504
6505 // Standard error of the mean of a 1D array of int, aa
6506 public static double standardError(int[] aa){
6507 return Math.sqrt(Stat.variance(aa)/aa.length);
6508 }
6509
6510 // Standard error of the mean of a 1D array of long, aa
6511 public static double standardError(long[] aa){
6512 return Math.sqrt(Stat.variance(aa)/aa.length);
6513 }
6514
6515 // Standard error of the weighted mean of a 1D array of Complex, aa
6516 public static Complex standardError(Complex[] aa, Complex[] ww){
6517 if(aa.length!=ww.length)throw new IllegalArgumentException("length of variable array, " + aa.length + " and length of weight array, " + ww.length + " are different");
6518 Complex effectiveNumber = Stat.effectiveSampleNumber(ww);
6519 return Complex.sqrt((Stat.variance(aa, ww)).over(effectiveNumber));
6520 }
6521
6522 // Standard error of the weighted mean of a 1D array of Complex, aa, using conjugate calculation
6523 public static double standardErrorConjugateCalcn(Complex[] aa, Complex[] ww){
6524 if(aa.length!=ww.length)throw new IllegalArgumentException("length of variable array, " + aa.length + " and length of weight array, " + ww.length + " are different");
6525 double effectiveNumber = Stat.effectiveSampleNumberConjugateCalcn(ww);
6526 return Math.sqrt(Stat.varianceConjugateCalcn(aa, ww)/effectiveNumber);
6527 }
6528
6529 // Weighted standard error of the moduli of a 1D array of Complex aa
6530 public static double standardErrorModuli(Complex[] aa, Complex[] ww){
6531 int n = aa.length;
6532 if(n!=ww.length)throw new IllegalArgumentException("length of variable array, " + n + " and length of weight array, " + ww.length + " are different");
6533 ArrayMaths am = new ArrayMaths(aa);
6534 double[] rl = am.array_as_modulus_of_Complex();
6535 ArrayMaths wm = new ArrayMaths(ww);
6536 double[] wt = wm.array_as_modulus_of_Complex();
6537 return Stat.standardError(rl, wt);
6538 }
6539
6540 // Weighted standard error of the real parts of a 1D array of Complex aa
6541 public static double standardErrorRealParts(Complex[] aa, Complex[] ww){
6542 int n = aa.length;
6543 if(n!=ww.length)throw new IllegalArgumentException("length of variable array, " + n + " and length of weight array, " + ww.length + " are different");
6544 ArrayMaths am = new ArrayMaths(aa);
6545 double[] rl = am.array_as_real_part_of_Complex();
6546 ArrayMaths wm = new ArrayMaths(ww);
6547 double[] wt = wm.array_as_real_part_of_Complex();
6548 return Stat.standardError(rl, wt);
6549 }
6550
6551 // Weighted standard error of the imaginary parts of a 1D array of Complex aa
6552 public static double standardErrorImaginaryParts(Complex[] aa, Complex[] ww){
6553 int n = aa.length;
6554 if(n!=ww.length)throw new IllegalArgumentException("length of variable array, " + n + " and length of weight array, " + ww.length + " are different");
6555 ArrayMaths am = new ArrayMaths(aa);
6556 double[] im = am.array_as_imaginary_part_of_Complex();
6557 ArrayMaths wm = new ArrayMaths(ww);
6558 double[] wt = wm.array_as_imaginary_part_of_Complex();
6559 return Stat.standardError(im, wt);
6560 }
6561
6562
6563 // Standard error of the weighted mean of a 1D array of BigDecimal, aa
6564 public static double standardError(BigDecimal[] aa, BigDecimal[] ww){
6565 if(aa.length!=ww.length)throw new IllegalArgumentException("length of variable array, " + aa.length + " and length of weight array, " + ww.length + " are different");
6566 double effectiveNumber = (Stat.effectiveSampleNumber(ww)).doubleValue();
6567 return Math.sqrt(Stat.variance(aa, ww).doubleValue()/effectiveNumber);
6568 }
6569
6570 // Standard error of the weighted mean of a 1D array of BigInteger, aa
6571 public static double standardError(BigInteger[] aa, BigInteger[] ww){
6572 if(aa.length!=ww.length)throw new IllegalArgumentException("length of variable array, " + aa.length + " and length of weight array, " + ww.length + " are different");
6573 double effectiveNumber = (Stat.effectiveSampleNumber(ww)).doubleValue();
6574 return Math.sqrt(Stat.variance(aa, ww).doubleValue()/effectiveNumber);
6575 }
6576
6577 // Standard error of the weighted mean of a 1D array of doubles, aa
6578 public static double standardError(double[] aa, double[] ww){
6579 if(aa.length!=ww.length)throw new IllegalArgumentException("length of variable array, " + aa.length + " and length of weight array, " + ww.length + " are different");
6580 double effectiveNumber = Stat.effectiveSampleNumber(ww);
6581 return Math.sqrt(Stat.variance(aa, ww)/effectiveNumber);
6582 }
6583
6584 // Standard error of the weighted mean of a 1D array of floats, aa
6585 public static float standardError(float[] aa, float[] ww){
6586 float effectiveNumber = Stat.effectiveSampleNumber(ww);
6587 return (float)Math.sqrt(Stat.variance(aa, ww)/effectiveNumber);
6588 }
6589
6590 // COVARIANCE
6591
6592 // Covariance of two 1D arrays of doubles, xx and yy
6593 public static double covariance(double[] xx, double[] yy){
6594 int n = xx.length;
6595 if(n!=yy.length)throw new IllegalArgumentException("length of x variable array, " + n + " and length of y array, " + yy.length + " are different");
6596 double denom = (double)(n-1);
6597 if(Stat.nFactorOptionS)denom = (double)n;
6598
6599 double sumx=0.0D, meanx=0.0D;
6600 double sumy=0.0D, meany=0.0D;
6601 for(int i=0; i<n; i++){
6602 sumx+=xx[i];
6603 sumy+=yy[i];
6604 }
6605 meanx=sumx/((double)n);
6606 meany=sumy/((double)n);
6607 double sum=0.0D;
6608 for(int i=0; i<n; i++){
6609 sum+=(xx[i]-meanx)*(yy[i]-meany);
6610 }
6611 return sum/(denom);
6612 }
6613
6614 // Covariance of two 1D arrays of floats, xx and yy
6615 public static float covariance(float[] xx, float[] yy){
6616 int n = xx.length;
6617 if(n!=yy.length)throw new IllegalArgumentException("length of x variable array, " + n + " and length of y array, " + yy.length + " are different");
6618 float denom = (float)(n-1);
6619 if(Stat.nFactorOptionS)denom = (float)n;
6620
6621 float sumx=0.0F, meanx=0.0F;
6622 float sumy=0.0F, meany=0.0F;
6623 for(int i=0; i<n; i++){
6624 sumx+=xx[i];
6625 sumy+=yy[i];
6626 }
6627 meanx=sumx/((float)n);
6628 meany=sumy/((float)n);
6629 float sum=0.0F;
6630 for(int i=0; i<n; i++){
6631 sum+=(xx[i]-meanx)*(yy[i]-meany);
6632 }
6633 return sum/(denom);
6634 }
6635
6636 // Covariance of two 1D arrays of ints, xx and yy
6637 public static double covariance(int[] xx, int[] yy){
6638 int n = xx.length;
6639 if(n!=yy.length)throw new IllegalArgumentException("length of x variable array, " + n + " and length of y array, " + yy.length + " are different");
6640 double denom = (double)(n-1);
6641 if(Stat.nFactorOptionS)denom = (double)n;
6642
6643 double sumx=0.0D, meanx=0.0D;
6644 double sumy=0.0D, meany=0.0D;
6645 for(int i=0; i<n; i++){
6646 sumx+=(double)xx[i];
6647 sumy+=(double)yy[i];
6648 }
6649 meanx=sumx/((double)n);
6650 meany=sumy/((double)n);
6651 double sum=0.0D;
6652 for(int i=0; i<n; i++){
6653 sum+=((double)xx[i]-meanx)*((double)yy[i]-meany);
6654 }
6655 return sum/(denom);
6656 }
6657
6658 // Covariance of two 1D arrays of ints, xx and yy
6659 public static double covariance(long[] xx, long[] yy){
6660 int n = xx.length;
6661 if(n!=yy.length)throw new IllegalArgumentException("length of x variable array, " + n + " and length of y array, " + yy.length + " are different");
6662 double denom = (double)(n-1);
6663 if(Stat.nFactorOptionS)denom = (double)n;
6664
6665 double sumx=0.0D, meanx=0.0D;
6666 double sumy=0.0D, meany=0.0D;
6667 for(int i=0; i<n; i++){
6668 sumx+=(double)xx[i];
6669 sumy+=(double)yy[i];
6670 }
6671 meanx=sumx/((double)n);
6672 meany=sumy/((double)n);
6673 double sum=0.0D;
6674 for(int i=0; i<n; i++){
6675 sum+=((double)xx[i]-meanx)*((double)yy[i]-meany);
6676 }
6677 return sum/(denom);
6678 }
6679
6680 // Weighted covariance of two 1D arrays of doubles, xx and yy with weights ww
6681 public static double covariance(double[] xx, double[] yy, double[] ww){
6682 int n = xx.length;
6683 if(n!=yy.length)throw new IllegalArgumentException("length of x variable array, " + n + " and length of y array, " + yy.length + " are different");
6684 if(n!=ww.length)throw new IllegalArgumentException("length of x variable array, " + n + " and length of weight array, " + yy.length + " are different");
6685 double nn = Stat.effectiveSampleNumber(ww);
6686 double nterm = nn/(nn - 1.0);
6687 if(Stat.nFactorOptionS)nterm = 1.0;
6688 double sumx=0.0D, sumy=0.0D, sumw=0.0D, meanx=0.0D, meany=0.0D;
6689 double[] weight = Stat.invertAndSquare(ww);
6690 for(int i=0; i<n; i++){
6691 sumx+=xx[i]*weight[i];
6692 sumy+=yy[i]*weight[i];
6693 sumw+=weight[i];
6694 }
6695 meanx=sumx/sumw;
6696 meany=sumy/sumw;
6697
6698 double sum=0.0D;
6699 for(int i=0; i<n; i++){
6700 sum+=weight[i]*(xx[i]-meanx)*(yy[i]-meany);
6701 }
6702 return sum*nterm/sumw;
6703 }
6704
6705
6706 // CORRELATION COEFFICIENT
6707
6708 // Calculate correlation coefficient
6709 // x y data as double
6710 public static double corrCoeff(double[] xx, double[]yy){
6711
6712 double temp0 = 0.0D, temp1 = 0.0D; // working variables
6713 int nData = xx.length;
6714 if(yy.length!=nData)throw new IllegalArgumentException("array lengths must be equal");
6715 int df = nData-1;
6716 // means
6717 double mx = 0.0D;
6718 double my = 0.0D;
6719 for(int i=0; i<nData; i++){
6720 mx += xx[i];
6721 my += yy[i];
6722 }
6723 mx /= nData;
6724 my /= nData;
6725
6726 // calculate sample variances
6727 double s2xx = 0.0D;
6728 double s2yy = 0.0D;
6729 double s2xy = 0.0D;
6730 for(int i=0; i<nData; i++){
6731 s2xx += Fmath.square(xx[i]-mx);
6732 s2yy += Fmath.square(yy[i]-my);
6733 s2xy += (xx[i]-mx)*(yy[i]-my);
6734 }
6735
6736 // calculate corelation coefficient
6737 double sampleR = s2xy/Math.sqrt(s2xx*s2yy);
6738
6739 return sampleR;
6740 }
6741
6742 // Calculate correlation coefficient
6743 // x y data as float
6744 public static float corrCoeff(float[] x, float[] y){
6745 int nData = x.length;
6746 if(y.length!=nData)throw new IllegalArgumentException("array lengths must be equal");
6747 int n = x.length;
6748 double[] xx = new double[n];
6749 double[] yy = new double[n];
6750 for(int i=0; i<n; i++){
6751 xx[i] = (double)x[i];
6752 yy[i] = (double)y[i];
6753 }
6754 return (float)Stat.corrCoeff(xx, yy);
6755 }
6756
6757 // Calculate correlation coefficient
6758 // x y data as int
6759 public static double corrCoeff(int[] x, int[]y){
6760 int n = x.length;
6761 if(y.length!=n)throw new IllegalArgumentException("array lengths must be equal");
6762
6763 double[] xx = new double[n];
6764 double[] yy = new double[n];
6765 for(int i=0; i<n; i++){
6766 xx[i] = (double)x[i];
6767 yy[i] = (double)y[i];
6768 }
6769 return Stat.corrCoeff(xx, yy);
6770 }
6771
6772 // Calculate weighted correlation coefficient
6773 // x y data and weights w as double
6774 public static double corrCoeff(double[] x, double[]y, double[] w){
6775 int n = x.length;
6776 if(y.length!=n)throw new IllegalArgumentException("x and y array lengths must be equal");
6777 if(w.length!=n)throw new IllegalArgumentException("x and weight array lengths must be equal");
6778
6779 double sxy = Stat.covariance(x, y, w);
6780 double sx = Stat.variance(x, w);
6781 double sy = Stat.variance(y, w);
6782 return sxy/Math.sqrt(sx*sy);
6783 }
6784
6785 // Calculate correlation coefficient
6786 // Binary data x and y
6787 // Input is the frequency matrix, F, elements, f(i,j)
6788 // f(0,0) - element00 - frequency of x and y both = 1
6789 // f(0,1) - element01 - frequency of x = 0 and y = 1
6790 // f(1,0) - element10 - frequency of x = 1 and y = 0
6791 // f(1,1) - element11 - frequency of x and y both = 0
6792 public static double corrCoeff(int element00, int element01, int element10, int element11){
6793 return ((double)(element00*element11 - element01*element10))/Math.sqrt((double)((element00+element01)*(element10+element11)*(element00+element10)*(element01+element11)));
6794 }
6795
6796 // Calculate correlation coefficient
6797 // Binary data x and y
6798 // Input is the frequency matrix, F
6799 // F(0,0) - frequency of x and y both = 1
6800 // F(0,1) - frequency of x = 0 and y = 1
6801 // F(1,0) - frequency of x = 1 and y = 0
6802 // F(1,1) - frequency of x and y both = 0
6803 public static double corrCoeff(int[][] freqMatrix){
6804 double element00 = (double)freqMatrix[0][0];
6805 double element01 = (double)freqMatrix[0][1];
6806 double element10 = (double)freqMatrix[1][0];
6807 double element11 = (double)freqMatrix[1][1];
6808 return ((element00*element11 - element01*element10))/Math.sqrt(((element00+element01)*(element10+element11)*(element00+element10)*(element01+element11)));
6809 }
6810
6811 // Linear correlation coefficient cumulative probablity
6812 // old name calls renamed method
6813 public static double linearCorrCoeffProb(double rCoeff, int nu){
6814 return corrCoeffProb(rCoeff, nu);
6815 }
6816
6817 // Linear correlation coefficient cumulative probablity
6818 public static double corrCoeffProb(double rCoeff, int nu){
6819 if(Math.abs(rCoeff)>1.0D)throw new IllegalArgumentException("|Correlation coefficient| > 1 : " + rCoeff);
6820
6821 // Create instances of the classes holding the function evaluation methods
6822 CorrCoeff cc = new CorrCoeff();
6823
6824 // Assign values to constant in the function
6825 cc.a = ((double)nu - 2.0D)/2.0D;
6826
6827
6828 double integral = Integration.gaussQuad(cc, Math.abs(rCoeff), 1.0D, 128);
6829
6830 double preterm = Math.exp(Stat.logGamma((nu+1.0D)/2.0)-Stat.logGamma(nu/2.0D))/Math.sqrt(Math.PI);
6831
6832 return preterm*integral;
6833 }
6834
6835 // Linear correlation coefficient single probablity
6836 // old name calls renamed method
6837 public static double linearCorrCoeff(double rCoeff, int nu){
6838 return Stat.corrCoeffPDF(rCoeff, nu);
6839 }
6840
6841 // Linear correlation coefficient single probablity
6842 public static double corrCoeffPDF(double rCoeff, int nu){
6843 if(Math.abs(rCoeff)>1.0D)throw new IllegalArgumentException("|Correlation coefficient| > 1 : " + rCoeff);
6844
6845 double a = ((double)nu - 2.0D)/2.0D;
6846 double y = Math.pow((1.0D - Fmath.square(rCoeff)),a);
6847
6848 double preterm = Math.exp(Stat.logGamma((nu+1.0D)/2.0)-Stat.logGamma(nu/2.0D))/Math.sqrt(Math.PI);
6849
6850 return preterm*y;
6851 }
6852
6853 // Linear correlation coefficient single probablity
6854 public static double corrCoeffPdf(double rCoeff, int nu){
6855 if(Math.abs(rCoeff)>1.0D)throw new IllegalArgumentException("|Correlation coefficient| > 1 : " + rCoeff);
6856
6857 double a = ((double)nu - 2.0D)/2.0D;
6858 double y = Math.pow((1.0D - Fmath.square(rCoeff)),a);
6859
6860 double preterm = Math.exp(Stat.logGamma((nu+1.0D)/2.0)-Stat.logGamma(nu/2.0D))/Math.sqrt(Math.PI);
6861
6862 return preterm*y;
6863 }
6864
6865 // SHANNON ENTROPY (STATIC METHODS)
6866 // Shannon Entropy returned as bits
6867 public static double shannonEntropy(double[] p){
6868 ArrayMaths am = new ArrayMaths(p);
6869 double max = am.getMaximum_as_double();
6870 if(max>1.0)throw new IllegalArgumentException("All probabilites must be less than or equal to 1; the maximum supplied probabilty is " + max);
6871 double min = am.getMinimum_as_double();
6872 if(min<0.0)throw new IllegalArgumentException("All probabilites must be greater than or equal to 0; the minimum supplied probabilty is " + min);
6873 double total = am.getSum_as_double();
6874 if(!Fmath.isEqualWithinPerCent(total, 1.0D, 0.1D))throw new IllegalArgumentException("the probabilites must add up to 1 within an error of 0.1%; they add up to " + total);
6875
6876 return am.minusxLog2x().getSum_as_double();
6877 }
6878
6879 // Shannon Entropy returned as bits
6880 public static double shannonEntropyBit(double[] p){
6881 return shannonEntropy(p);
6882 }
6883
6884 // Shannon Entropy returned as nats (nits)
6885 public static double shannonEntropyNat(double[] p){
6886 ArrayMaths am = new ArrayMaths(p);
6887 double max = am.getMaximum_as_double();
6888 if(max>1.0)throw new IllegalArgumentException("All probabilites must be less than or equal to 1; the maximum supplied probabilty is " + max);
6889 double min = am.getMinimum_as_double();
6890 if(min<0.0)throw new IllegalArgumentException("All probabilites must be greater than or equal to 0; the minimum supplied probabilty is " + min);
6891 double total = am.getSum_as_double();
6892 if(!Fmath.isEqualWithinPerCent(total, 1.0D, 0.1D))throw new IllegalArgumentException("the probabilites must add up to 1 within an error of 0.1%; they add up to " + total);
6893
6894 return am.minusxLogEx().getSum_as_double();
6895 }
6896
6897 // Shannon Entropy returned as dits
6898 public static double shannonEntropyDit(double[] p){
6899 ArrayMaths am = new ArrayMaths(p);
6900 double max = am.getMaximum_as_double();
6901 if(max>1.0)throw new IllegalArgumentException("All probabilites must be less than or equal to 1; the maximum supplied probabilty is " + max);
6902 double min = am.getMinimum_as_double();
6903 if(min<0.0)throw new IllegalArgumentException("All probabilites must be greater than or equal to 0; the minimum supplied probabilty is " + min);
6904 double total = am.getSum_as_double();
6905 if(!Fmath.isEqualWithinPerCent(total, 1.0D, 0.1D))throw new IllegalArgumentException("the probabilites must add up to 1 within an error of 0.1%; they add up to " + total);
6906
6907 return am.minusxLog10x().getSum_as_double();
6908 }
6909
6910 // Binary Shannon Entropy returned as bits
6911 public static double binaryShannonEntropy(double p){
6912 if(p>1.0)throw new IllegalArgumentException("The probabiliy, " + p + ", must be less than or equal to 1");
6913 if(p<0.0)throw new IllegalArgumentException("The probabiliy, " + p + ", must be greater than or equal to 0");
6914 double entropy = 0.0D;
6915 if(p>0.0D && p<1.0D){
6916 entropy = -p*Fmath.log2(p) - (1-p)*Fmath.log2(1-p);
6917 }
6918 return entropy;
6919 }
6920
6921 // Binary Shannon Entropy returned as bits
6922 public static double binaryShannonEntropyBit(double p){
6923 return binaryShannonEntropy(p);
6924 }
6925
6926 // Binary Shannon Entropy returned as nats (nits)
6927 public static double binaryShannonEntropyNat(double p){
6928 if(p>1.0)throw new IllegalArgumentException("The probabiliy, " + p + ", must be less than or equal to 1");
6929 if(p<0.0)throw new IllegalArgumentException("The probabiliy, " + p + ", must be greater than or equal to 0");
6930 double entropy = 0.0D;
6931 if(p>0.0D && p<1.0D){
6932 entropy = -p*Math.log(p) - (1-p)*Math.log(1-p);
6933 }
6934 return entropy;
6935 }
6936
6937 // Binary Shannon Entropy returned as dits
6938 public static double binaryShannonEntropyDit(double p){
6939 if(p>1.0)throw new IllegalArgumentException("The probabiliy, " + p + ", must be less than or equal to 1");
6940 if(p<0.0)throw new IllegalArgumentException("The probabiliy, " + p + ", must be greater than or equal to 0");
6941 double entropy = 0.0D;
6942 if(p>0.0D && p<1.0D){
6943 entropy = -p*Math.log10(p) - (1-p)*Math.log10(1-p);
6944 }
6945 return entropy;
6946
6947 }
6948
6949 // RENYI ENTROPY
6950 // Renyi Entropy returned as bits
6951 public static double renyiEntropy(double[] p, double alpha){
6952 ArrayMaths am = new ArrayMaths(p);
6953 double max = am.getMaximum_as_double();
6954 if(max>1.0)throw new IllegalArgumentException("All probabilites must be less than or equal to 1; the maximum supplied probabilty is " + max);
6955 double min = am.getMinimum_as_double();
6956 if(min<0.0)throw new IllegalArgumentException("All probabilites must be greater than or equal to 0; the minimum supplied probabilty is " + min);
6957 double total = am.getSum_as_double();
6958 if(!Fmath.isEqualWithinPerCent(total, 1.0D, 0.1D))throw new IllegalArgumentException("the probabilites must add up to 1 within an error of 0.1%; they add up to " + total);
6959 if(alpha<0.0D)throw new IllegalArgumentException("alpha, " + alpha + ", must be greater than or equal to 0");
6960 double entropy = 0.0;
6961 if(alpha==0.0D){
6962 entropy = Fmath.log2(p.length);
6963 }
6964 else{
6965 if(alpha==1.0D){
6966 entropy = Stat.shannonEntropy(p);
6967 }
6968 else{
6969 if(Fmath.isPlusInfinity(alpha)){
6970 entropy = -Fmath.log2(max);
6971 }
6972 else{
6973 if(alpha<=3000){
6974 am = am.pow(alpha);
6975 boolean testUnderFlow = false;
6976 if(am.getMaximum_as_double()==Double.MIN_VALUE)testUnderFlow = true;
6977 entropy = Fmath.log2(am.getSum_as_double())/(1.0D - alpha);
6978 if(Fmath.isPlusInfinity(entropy) || testUnderFlow){
6979 entropy = -Fmath.log2(max);
6980 double entropyMin = entropy;
6981 System.out.println("Stat: renyiEntropy/renyiEntopyBit: underflow or overflow in calculating the entropy");
6982 boolean test1 = true;
6983 boolean test2 = true;
6984 boolean test3 = true;
6985 int iter = 0;
6986 double alpha2 = alpha/2.0;
6987 double entropy2 = 0.0;
6988 while(test3){
6989 while(test1){
6990 ArrayMaths am2 = new ArrayMaths(p);
6991 am2 = am2.pow(alpha2);
6992 entropy2 = Fmath.log2(am2.getSum_as_double())/(1.0D - alpha2);
6993 if(Fmath.isPlusInfinity(entropy2)){
6994 alpha2 /= 2.0D;
6995 iter++;
6996 if(iter==100000){
6997 test1=false;
6998 test2=false;
6999 }
7000 }
7001 else{
7002 test1 = false;
7003 }
7004 }
7005 double alphaTest = alpha2 + 40.0D*alpha/1000.0D;
7006 ArrayMaths am3 = new ArrayMaths(p);
7007 am3 = am3.pow(alphaTest);
7008 double entropy3 = Fmath.log2(am3.getSum_as_double())/(1.0D - alphaTest);
7009 if(!Fmath.isPlusInfinity(entropy3)){
7010 test3=false;
7011 }
7012 else{
7013 alpha2 /= 2.0D;
7014 }
7015 }
7016 double entropyLast = entropy2;
7017 double alphaLast = alpha2;
7018 ArrayList<Double> extrap = new ArrayList<Double>();
7019 if(test2){
7020 double diff = alpha2/1000.0D;
7021 test1 = true;
7022 while(test1){
7023 extrap.add(new Double(alpha2));
7024 extrap.add(new Double(entropy2));
7025 entropyLast = entropy2;
7026 alphaLast = alpha2;
7027 alpha2 += diff;
7028 ArrayMaths am2 = new ArrayMaths(p);
7029 am2 = am2.pow(alpha2);
7030 entropy2 = Fmath.log2(am2.getSum_as_double())/(1.0D - alpha2);
7031 if(Fmath.isPlusInfinity(entropy2)){
7032 test1 = false;
7033 entropy2 = entropyLast;
7034 alpha2 = alphaLast;
7035 }
7036 }
7037 }
7038 int nex = extrap.size()/2 - 20;
7039 double[] alphaex= new double[nex];
7040 double[] entroex= new double[nex];
7041 int ii = -1;
7042 for(int i=0; i<nex; i++){
7043 alphaex[i] = (extrap.get(++ii)).doubleValue();
7044 entroex[i] = Math.log((extrap.get(++ii)).doubleValue() - entropyMin);
7045 }
7046 Regression reg = new Regression(alphaex, entroex);
7047 reg.linear();
7048 double[] param = reg.getCoeff();
7049 entropy = Math.exp(param[0] + param[1]*alpha) + entropyMin;
7050
7051
7052 System.out.println("An interpolated entropy of " + entropy + " returned (see documentation for exponential interpolation)");
7053 System.out.println("Lowest calculable value = " + (Math.exp(entroex[nex-1])+entropyMin) + ", alpha = " + alphaex[nex-1]);
7054 System.out.println("Minimum entropy value = " + entropyMin + ", alpha = infinity");
7055 }
7056 }
7057 else{
7058 entropy = -Fmath.log2(max);
7059 System.out.println("Stat: renyiEntropy/renyiEntropyBit: underflow or overflow in calculating the entropy");
7060 System.out.println("An interpolated entropy of " + entropy + " returned (see documentation for exponential interpolation)");
7061 }
7062 }
7063 }
7064 }
7065 return entropy;
7066 }
7067
7068 // Renyi Entropy returned as nats
7069 public static double renyiEntropyNat(double[] p, double alpha){
7070 ArrayMaths am = new ArrayMaths(p);
7071 double max = am.getMaximum_as_double();
7072 if(max>1.0)throw new IllegalArgumentException("All probabilites must be less than or equal to 1; the maximum supplied probabilty is " + max);
7073 double min = am.getMinimum_as_double();
7074 if(min<0.0)throw new IllegalArgumentException("All probabilites must be greater than or equal to 0; the minimum supplied probabilty is " + min);
7075 double total = am.getSum_as_double();
7076 if(!Fmath.isEqualWithinPerCent(total, 1.0D, 0.1D))throw new IllegalArgumentException("the probabilites must add up to 1 within an error of 0.1%; they add up to " + total);
7077 if(alpha<0.0D)throw new IllegalArgumentException("alpha, " + alpha + ", must be greater than or equal to 0");
7078 double entropy = 0.0;
7079 if(alpha==0.0D){
7080 entropy = Math.log(p.length);
7081 }
7082 else{
7083 if(alpha==1.0D){
7084 entropy = Stat.shannonEntropy(p);
7085 }
7086 else{
7087 if(Fmath.isPlusInfinity(alpha)){
7088 entropy = -Math.log(max);
7089 }
7090 else{
7091 if(alpha<=3000){
7092 am = am.pow(alpha);
7093 boolean testUnderFlow = false;
7094 if(am.getMaximum_as_double()==Double.MIN_VALUE)testUnderFlow = true;
7095 entropy = Math.log(am.getSum_as_double())/(1.0D - alpha);
7096 if(Fmath.isPlusInfinity(entropy) || testUnderFlow){
7097 entropy = -Math.log(max);
7098 double entropyMin = entropy;
7099 System.out.println("Stat: renyiEntropyNat: underflow or overflow in calculating the entropy");
7100 boolean test1 = true;
7101 boolean test2 = true;
7102 boolean test3 = true;
7103 int iter = 0;
7104 double alpha2 = alpha/2.0;
7105 double entropy2 = 0.0;
7106 while(test3){
7107 while(test1){
7108 ArrayMaths am2 = new ArrayMaths(p);
7109 am2 = am2.pow(alpha2);
7110 entropy2 = Math.log(am2.getSum_as_double())/(1.0D - alpha2);
7111 if(Fmath.isPlusInfinity(entropy2)){
7112 alpha2 /= 2.0D;
7113 iter++;
7114 if(iter==100000){
7115 test1=false;
7116 test2=false;
7117 }
7118 }
7119 else{
7120 test1 = false;
7121 }
7122 }
7123 double alphaTest = alpha2 + 40.0D*alpha/1000.0D;
7124 ArrayMaths am3 = new ArrayMaths(p);
7125 am3 = am3.pow(alphaTest);
7126 double entropy3 = Math.log(am3.getSum_as_double())/(1.0D - alphaTest);
7127 if(!Fmath.isPlusInfinity(entropy3)){
7128 test3=false;
7129 }
7130 else{
7131 alpha2 /= 2.0D;
7132 }
7133 }
7134 double entropyLast = entropy2;
7135 double alphaLast = alpha2;
7136 ArrayList<Double> extrap = new ArrayList<Double>();
7137 if(test2){
7138 double diff = alpha2/1000.0D;
7139 test1 = true;
7140 while(test1){
7141 extrap.add(new Double(alpha2));
7142 extrap.add(new Double(entropy2));
7143 entropyLast = entropy2;
7144 alphaLast = alpha2;
7145 alpha2 += diff;
7146 ArrayMaths am2 = new ArrayMaths(p);
7147 am2 = am2.pow(alpha2);
7148 entropy2 = Math.log(am2.getSum_as_double())/(1.0D - alpha2);
7149 if(Fmath.isPlusInfinity(entropy2)){
7150 test1 = false;
7151 entropy2 = entropyLast;
7152 alpha2 = alphaLast;
7153 }
7154 }
7155 }
7156 int nex = extrap.size()/2 - 20;
7157 double[] alphaex= new double[nex];
7158 double[] entroex= new double[nex];
7159 int ii = -1;
7160 for(int i=0; i<nex; i++){
7161 alphaex[i] = (extrap.get(++ii)).doubleValue();
7162 entroex[i] = Math.log((extrap.get(++ii)).doubleValue() - entropyMin);
7163 }
7164 Regression reg = new Regression(alphaex, entroex);
7165 reg.linear();
7166 double[] param = reg.getCoeff();
7167 entropy = Math.exp(param[0] + param[1]*alpha) + entropyMin;
7168
7169
7170 System.out.println("An interpolated entropy of " + entropy + " returned (see documentation for exponential interpolation)");
7171 System.out.println("Lowest calculable value = " + (Math.exp(entroex[nex-1])+entropyMin) + ", alpha = " + alphaex[nex-1]);
7172 System.out.println("Minimum entropy value = " + entropyMin + ", alpha = infinity");
7173 }
7174 }
7175 else{
7176 entropy = -Math.log(max);
7177 System.out.println("Stat: renyiEntropyNat: underflow or overflow in calculating the entropy");
7178 System.out.println("An interpolated entropy of " + entropy + " returned (see documentation for exponential interpolation)");
7179 }
7180 }
7181 }
7182 }
7183 return entropy;
7184 }
7185
7186 // Renyi Entropy returned as dits
7187 public static double renyiEntropyDit(double[] p, double alpha){
7188 ArrayMaths am = new ArrayMaths(p);
7189 double max = am.getMaximum_as_double();
7190 if(max>1.0)throw new IllegalArgumentException("All probabilites must be less than or equal to 1; the maximum supplied probabilty is " + max);
7191 double min = am.getMinimum_as_double();
7192 if(min<0.0)throw new IllegalArgumentException("All probabilites must be greater than or equal to 0; the minimum supplied probabilty is " + min);
7193 double total = am.getSum_as_double();
7194 if(!Fmath.isEqualWithinPerCent(total, 1.0D, 0.1D))throw new IllegalArgumentException("the probabilites must add up to 1 within an error of 0.1%; they add up to " + total);
7195 if(alpha<0.0D)throw new IllegalArgumentException("alpha, " + alpha + ", must be greater than or equal to 0");
7196 double entropy = 0.0;
7197 if(alpha==0.0D){
7198 entropy = Math.log10(p.length);
7199 }
7200 else{
7201 if(alpha==1.0D){
7202 entropy = Stat.shannonEntropy(p);
7203 }
7204 else{
7205 if(Fmath.isPlusInfinity(alpha)){
7206 entropy = -Math.log10(max);
7207 }
7208 else{
7209 if(alpha<=3000){
7210 am = am.pow(alpha);
7211 boolean testUnderFlow = false;
7212 if(am.getMaximum_as_double()==Double.MIN_VALUE)testUnderFlow = true;
7213 entropy = Math.log10(am.getSum_as_double())/(1.0D - alpha);
7214 if(Fmath.isPlusInfinity(entropy) || testUnderFlow){
7215 entropy = -Math.log10(max);
7216 double entropyMin = entropy;
7217 System.out.println("Stat: renyiEntropyDit: underflow or overflow in calculating the entropy");
7218 boolean test1 = true;
7219 boolean test2 = true;
7220 boolean test3 = true;
7221 int iter = 0;
7222 double alpha2 = alpha/2.0;
7223 double entropy2 = 0.0;
7224 while(test3){
7225 while(test1){
7226 ArrayMaths am2 = new ArrayMaths(p);
7227 am2 = am2.pow(alpha2);
7228 entropy2 = Math.log10(am2.getSum_as_double())/(1.0D - alpha2);
7229 if(Fmath.isPlusInfinity(entropy2)){
7230 alpha2 /= 2.0D;
7231 iter++;
7232 if(iter==100000){
7233 test1=false;
7234 test2=false;
7235 }
7236 }
7237 else{
7238 test1 = false;
7239 }
7240 }
7241 double alphaTest = alpha2 + 40.0D*alpha/1000.0D;
7242 ArrayMaths am3 = new ArrayMaths(p);
7243 am3 = am3.pow(alphaTest);
7244 double entropy3 = Math.log10(am3.getSum_as_double())/(1.0D - alphaTest);
7245 if(!Fmath.isPlusInfinity(entropy3)){
7246 test3=false;
7247 }
7248 else{
7249 alpha2 /= 2.0D;
7250 }
7251 }
7252 double entropyLast = entropy2;
7253 double alphaLast = alpha2;
7254 ArrayList<Double> extrap = new ArrayList<Double>();
7255 if(test2){
7256 double diff = alpha2/1000.0D;
7257 test1 = true;
7258 while(test1){
7259 extrap.add(new Double(alpha2));
7260 extrap.add(new Double(entropy2));
7261 entropyLast = entropy2;
7262 alphaLast = alpha2;
7263 alpha2 += diff;
7264 ArrayMaths am2 = new ArrayMaths(p);
7265 am2 = am2.pow(alpha2);
7266 entropy2 = Math.log10(am2.getSum_as_double())/(1.0D - alpha2);
7267 if(Fmath.isPlusInfinity(entropy2)){
7268 test1 = false;
7269 entropy2 = entropyLast;
7270 alpha2 = alphaLast;
7271 }
7272 }
7273 }
7274 int nex = extrap.size()/2 - 20;
7275 double[] alphaex= new double[nex];
7276 double[] entroex= new double[nex];
7277 int ii = -1;
7278 for(int i=0; i<nex; i++){
7279 alphaex[i] = (extrap.get(++ii)).doubleValue();
7280 entroex[i] = Math.log10((extrap.get(++ii)).doubleValue() - entropyMin);
7281 }
7282 Regression reg = new Regression(alphaex, entroex);
7283 reg.linear();
7284 double[] param = reg.getCoeff();
7285 entropy = Math.exp(param[0] + param[1]*alpha) + entropyMin;
7286
7287
7288 System.out.println("An interpolated entropy of " + entropy + " returned (see documentation for exponential interpolation)");
7289 System.out.println("Lowest calculable value = " + (Math.exp(entroex[nex-1])+entropyMin) + ", alpha = " + alphaex[nex-1]);
7290 System.out.println("Minimum entropy value = " + entropyMin + ", alpha = infinity");
7291 }
7292 }
7293 else{
7294 entropy = -Math.log10(max);
7295 System.out.println("Stat: renyiEntropyDit: underflow or overflow in calculating the entropy");
7296 System.out.println("An interpolated entropy of " + entropy + " returned (see documentation for exponential interpolation)");
7297 }
7298 }
7299 }
7300 }
7301 return entropy;
7302 }
7303
7304
7305
7306 // Renyi Entropy returned as bits
7307 public static double renyiEntropyBit(double[] p, double alpha){
7308 return renyiEntropy(p, alpha);
7309 }
7310
7311
7312 // TSALLIS ENTROPY (STATIC METHODS)
7313 // Tsallis Entropy
7314 public static double tsallisEntropyNat(double[] p, double q){
7315 ArrayMaths am = new ArrayMaths(p);
7316 double max = am.getMaximum_as_double();
7317 if(max>1.0D)throw new IllegalArgumentException("All probabilites must be less than or equal to 1; the maximum supplied probabilty is " + max);
7318 double min = am.getMinimum_as_double();
7319 if(min<0.0D)throw new IllegalArgumentException("All probabilites must be greater than or equal to 0; the minimum supplied probabilty is " + min);
7320 double total = am.getSum_as_double();
7321 if(!Fmath.isEqualWithinPerCent(total, 1.0D, 0.1D))throw new IllegalArgumentException("the probabilites must add up to 1 within an error of 0.1%; they add up to " + total);
7322
7323 if(q==1.0D){
7324 return Stat.shannonEntropyNat(p);
7325 }
7326 else{
7327 am = am.pow(q);
7328 return (1.0D - am.getSum_as_double())/(q - 1.0D);
7329
7330 }
7331 }
7332
7333 // GENERALIZED ENTROPY (STATIC METHOD)
7334 public static double generalizedEntropyOneNat(double[] p, double q, double r){
7335 ArrayMaths am = new ArrayMaths(p);
7336 double max = am.getMaximum_as_double();
7337 if(max>1.0D)throw new IllegalArgumentException("All probabilites must be less than or equal to 1; the maximum supplied probabilty is " + max);
7338 double min = am.getMinimum_as_double();
7339 if(min<0.0D)throw new IllegalArgumentException("All probabilites must be greater than or equal to 0; the minimum supplied probabilty is " + min);
7340 double total = am.getSum_as_double();
7341 if(!Fmath.isEqualWithinPerCent(total, 1.0D, 0.1D))throw new IllegalArgumentException("the probabilites must add up to 1 within an error of 0.1%; they add up to " + total);
7342 if(r==0.0D){
7343 return Stat.renyiEntropyNat(p, q);
7344 }
7345 else{
7346 if(r==1.0D){
7347 return Stat.tsallisEntropyNat(p, q);
7348 }
7349 else{
7350 if(q==1.0D){
7351 double[] tsen = new double[10];
7352 double[] tsqq = new double[10];
7353 double qq = 0.995;
7354 for(int i=0; i<5; i++){
7355 ArrayMaths am1 = am.pow(qq);
7356 tsen[i] = (1.0D - Math.pow(am1.getSum_as_double(), r))/(r*(qq - 1.0));
7357 tsqq[i] = qq;
7358 qq += 0.001;
7359 }
7360 qq = 1.001;
7361 for(int i=5; i<10; i++){
7362 ArrayMaths am1 = am.pow(qq);
7363 tsen[i]= (1.0D - Math.pow(am1.getSum_as_double(), r))/(r*(qq - 1.0));
7364 tsqq[i] = qq;
7365 qq += 0.001;
7366 }
7367 Regression reg = new Regression(tsqq, tsen);
7368 reg.polynomial(2);
7369 double[] param = reg.getCoeff();
7370 return param[0] + param[1] + param[2];
7371 }
7372 else{
7373 am = am.pow(q);
7374 return (1.0D - Math.pow(am.getSum_as_double(), r))/(r*(q - 1.0));
7375 }
7376 }
7377 }
7378 }
7379
7380 public static double generalisedEntropyOneNat(double[] p, double q, double r){
7381 return generalizedEntropyOneNat(p, q, r);
7382 }
7383
7384
7385 // HISTOGRAMS
7386
7387 // Distribute data into bins to obtain histogram
7388 // zero bin position and upper limit provided
7389 public static double[][] histogramBins(double[] data, double binWidth, double binZero, double binUpper){
7390 int n = 0; // new array length
7391 int m = data.length; // old array length;
7392 for(int i=0; i<m; i++)if(data[i]<=binUpper)n++;
7393 if(n!=m){
7394 double[] newData = new double[n];
7395 int j = 0;
7396 for(int i=0; i<m; i++){
7397 if(data[i]<=binUpper){
7398 newData[j] = data[i];
7399 j++;
7400 }
7401 }
7402 System.out.println((m-n)+" data points, above histogram upper limit, excluded in Stat.histogramBins");
7403 return histogramBins(newData, binWidth, binZero);
7404 }
7405 else{
7406 return histogramBins(data, binWidth, binZero);
7407
7408 }
7409 }
7410
7411 // Distribute data into bins to obtain histogram
7412 // zero bin position provided
7413 public static double[][] histogramBins(double[] data, double binWidth, double binZero){
7414 double dmax = Fmath.maximum(data);
7415 int nBins = (int) Math.ceil((dmax - binZero)/binWidth);
7416 if(binZero+nBins*binWidth>dmax)nBins++;
7417 int nPoints = data.length;
7418 int[] dataCheck = new int[nPoints];
7419 for(int i=0; i<nPoints; i++)dataCheck[i]=0;
7420 double[]binWall = new double[nBins+1];
7421 binWall[0]=binZero;
7422 for(int i=1; i<=nBins; i++){
7423 binWall[i] = binWall[i-1] + binWidth;
7424 }
7425 double[][] binFreq = new double[2][nBins];
7426 for(int i=0; i<nBins; i++){
7427 binFreq[0][i]= (binWall[i]+binWall[i+1])/2.0D;
7428 binFreq[1][i]= 0.0D;
7429 }
7430 boolean test = true;
7431
7432 for(int i=0; i<nPoints; i++){
7433 test=true;
7434 int j=0;
7435 while(test){
7436 if(j==nBins-1){
7437 if(data[i]>=binWall[j] && data[i]<=binWall[j+1]*(1.0D + Stat.histTol)){
7438 binFreq[1][j]+= 1.0D;
7439 dataCheck[i]=1;
7440 test=false;
7441 }
7442 }
7443 else{
7444 if(data[i]>=binWall[j] && data[i]<binWall[j+1]){
7445 binFreq[1][j]+= 1.0D;
7446 dataCheck[i]=1;
7447 test=false;
7448 }
7449 }
7450 if(test){
7451 if(j==nBins-1){
7452 test=false;
7453 }
7454 else{
7455 j++;
7456 }
7457 }
7458 }
7459 }
7460 int nMissed=0;
7461 for(int i=0; i<nPoints; i++)if(dataCheck[i]==0){
7462 nMissed++;
7463 System.out.println("p " + i + " " + data[i] + " " + binWall[0] + " " + binWall[nBins]);
7464 }
7465 if(nMissed>0)System.out.println(nMissed+" data points, outside histogram limits, excluded in Stat.histogramBins");
7466 return binFreq;
7467 }
7468
7469 // Distribute data into bins to obtain histogram
7470 // zero bin position calculated
7471 public static double[][] histogramBins(double[] data, double binWidth){
7472
7473 double dmin = Fmath.minimum(data);
7474 double dmax = Fmath.maximum(data);
7475 double span = dmax - dmin;
7476 double binZero = dmin;
7477 int nBins = (int) Math.ceil(span/binWidth);
7478 double histoSpan = ((double)nBins)*binWidth;
7479 double rem = histoSpan - span;
7480 if(rem>=0){
7481 binZero -= rem/2.0D;
7482 }
7483 else{
7484 if(Math.abs(rem)/span>histTol){
7485 // readjust binWidth
7486 boolean testBw = true;
7487 double incr = histTol/nBins;
7488 int iTest = 0;
7489 while(testBw){
7490 binWidth += incr;
7491 histoSpan = ((double)nBins)*binWidth;
7492 rem = histoSpan - span;
7493 if(rem<0){
7494 iTest++;
7495 if(iTest>1000){
7496 testBw = false;
7497 System.out.println("histogram method could not encompass all data within histogram\nContact Michael thomas Flanagan");
7498 }
7499 }
7500 else{
7501 testBw = false;
7502 }
7503 }
7504 }
7505 }
7506
7507 return Stat.histogramBins(data, binWidth, binZero);
7508 }
7509
7510 // Distribute data into bins to obtain histogram and plot histogram
7511 // zero bin position and upper limit provided
7512 public static double[][] histogramBinsPlot(double[] data, double binWidth, double binZero, double binUpper){
7513 String xLegend = null;
7514 return histogramBinsPlot(data, binWidth, binZero, binUpper, xLegend);
7515 }
7516
7517 // Distribute data into bins to obtain histogram and plot histogram
7518 // zero bin position, upper limit and x-axis legend provided
7519 public static double[][] histogramBinsPlot(double[] data, double binWidth, double binZero, double binUpper, String xLegend){
7520 int n = 0; // new array length
7521 int m = data.length; // old array length;
7522 for(int i=0; i<m; i++)if(data[i]<=binUpper)n++;
7523 if(n!=m){
7524 double[] newData = new double[n];
7525 int j = 0;
7526 for(int i=0; i<m; i++){
7527 if(data[i]<=binUpper){
7528 newData[j] = data[i];
7529 j++;
7530 }
7531 }
7532 System.out.println((m-n)+" data points, above histogram upper limit, excluded in Stat.histogramBins");
7533 return histogramBinsPlot(newData, binWidth, binZero, xLegend);
7534 }
7535 else{
7536 return histogramBinsPlot(data, binWidth, binZero, xLegend);
7537
7538 }
7539 }
7540
7541 // Distribute data into bins to obtain histogram and plot the histogram
7542 // zero bin position provided
7543 public static double[][] histogramBinsPlot(double[] data, double binWidth, double binZero){
7544 String xLegend = null;
7545 return histogramBinsPlot(data, binWidth, binZero, xLegend);
7546 }
7547
7548 // Distribute data into bins to obtain histogram and plot the histogram
7549 // zero bin position and x-axis legend provided
7550 public static double[][] histogramBinsPlot(double[] data, double binWidth, double binZero, String xLegend){
7551 double[][] results = histogramBins(data, binWidth, binZero);
7552 int nBins = results[0].length;
7553 int nPoints = nBins*3+1;
7554 double[][] cdata = PlotGraph.data(1, nPoints);
7555 cdata[0][0]=binZero;
7556 cdata[1][0]=0.0D;
7557 int k=1;
7558 for(int i=0; i<nBins; i++){
7559 cdata[0][k]=cdata[0][k-1];
7560 cdata[1][k]=results[1][i];
7561 k++;
7562 cdata[0][k]=cdata[0][k-1]+binWidth;
7563 cdata[1][k]=results[1][i];
7564 k++;
7565 cdata[0][k]=cdata[0][k-1];
7566 cdata[1][k]=0.0D;
7567 k++;
7568 }
7569
7570 PlotGraph pg = new PlotGraph(cdata);
7571 pg.setGraphTitle("Histogram: Bin Width = "+binWidth);
7572 pg.setLine(3);
7573 pg.setPoint(0);
7574 pg.setYaxisLegend("Frequency");
7575 if(xLegend!=null)pg.setXaxisLegend(xLegend);
7576 pg.plot();
7577
7578 return results;
7579 }
7580
7581 // Distribute data into bins to obtain histogram and plot the histogram
7582 // zero bin position calculated
7583 public static double[][] histogramBinsPlot(double[] data, double binWidth){
7584 String xLegend = null;
7585 return Stat.histogramBinsPlot(data, binWidth, xLegend);
7586 }
7587
7588 // Distribute data into bins to obtain histogram and plot the histogram
7589 // zero bin position calculated, x-axis legend provided
7590 public static double[][] histogramBinsPlot(double[] data, double binWidth, String xLegend){
7591 double dmin = Fmath.minimum(data);
7592 double dmax = Fmath.maximum(data);
7593 double span = dmax - dmin;
7594 int nBins = (int) Math.ceil(span/binWidth);
7595 double rem = ((double)nBins)*binWidth-span;
7596 double binZero =dmin-rem/2.0D;
7597 return Stat.histogramBinsPlot(data, binWidth, binZero, xLegend);
7598 }
7599
7600
7601
7602 // UNIFORM ORDER STATISTIC MEDIANS
7603 public static double[] uniformOrderStatisticMedians(int n){
7604 double nn = (double)n;
7605 double[] uosm = new double[n];
7606 uosm[n-1] = Math.pow(0.5, 1.0/nn);
7607 uosm[0] = 1.0 - uosm[n-1];
7608 for(int i=1; i<n-1; i++){
7609 uosm[i] = (i + 1 - 0.3175)/(nn + 0.365);
7610 }
7611 return uosm;
7612 }
7613
7614
7615
7616 // GAMMA DISTRIBUTION AND GAMMA FUNCTIONS
7617
7618 // Gamma distribution - three parameter
7619 // Cumulative distribution function
7620 public static double gammaCDF(double mu, double beta, double gamma, double upperLimit){
7621 if(upperLimit<mu)throw new IllegalArgumentException("The upper limit, " + upperLimit + "must be equal to or greater than the location parameter, " + mu);
7622 if(beta<=0.0D)throw new IllegalArgumentException("The scale parameter, " + beta + "must be greater than zero");
7623 if(gamma<=0.0D)throw new IllegalArgumentException("The shape parameter, " + gamma + "must be greater than zero");
7624 double xx = (upperLimit - mu)/beta;
7625 return regularisedGammaFunction(gamma, xx);
7626 }
7627
7628 // Gamma distribution - standard
7629 // Cumulative distribution function
7630 public static double gammaCDF(double gamma, double upperLimit){
7631 if(upperLimit<0.0D)throw new IllegalArgumentException("The upper limit, " + upperLimit + "must be equal to or greater than zero");
7632 if(gamma<=0.0D)throw new IllegalArgumentException("The shape parameter, " + gamma + "must be greater than zero");
7633 return regularisedGammaFunction(gamma, upperLimit);
7634 }
7635
7636 // Gamma distribution - three parameter
7637 // probablity density function
7638 public static double gammaPDF(double mu, double beta, double gamma, double x){
7639 if(x<mu)throw new IllegalArgumentException("The variable, x, " + x + "must be equal to or greater than the location parameter, " + mu);
7640 if(beta<=0.0D)throw new IllegalArgumentException("The scale parameter, " + beta + "must be greater than zero");
7641 if(gamma<=0.0D)throw new IllegalArgumentException("The shape parameter, " + gamma + "must be greater than zero");
7642 double xx = (x - mu)/beta;
7643 return Math.pow(xx, gamma-1)*Math.exp(-xx)/(beta*gammaFunction(gamma));
7644 }
7645
7646 // Gamma distribution - standard
7647 // probablity density function
7648 public static double gammaPDF(double gamma, double x){
7649 if(x<0.0D)throw new IllegalArgumentException("The variable, x, " + x + "must be equal to or greater than zero");
7650 if(gamma<=0.0D)throw new IllegalArgumentException("The shape parameter, " + gamma + "must be greater than zero");
7651 return Math.pow(x, gamma-1)*Math.exp(-x)/gammaFunction(gamma);
7652 }
7653
7654 // Gamma distribution - three parameter
7655 // mean
7656 public static double gammaMean(double mu, double beta, double gamma){
7657 if(beta<=0.0D)throw new IllegalArgumentException("The scale parameter, " + beta + "must be greater than zero");
7658 if(gamma<=0.0D)throw new IllegalArgumentException("The shape parameter, " + gamma + "must be greater than zero");
7659 return gamma*beta - mu;
7660 }
7661
7662 // Gamma distribution - three parameter
7663 // mode
7664 public static double gammaMode(double mu, double beta, double gamma){
7665 if(beta<=0.0D)throw new IllegalArgumentException("The scale parameter, " + beta + "must be greater than zero");
7666 if(gamma<=0.0D)throw new IllegalArgumentException("The shape parameter, " + gamma + "must be greater than zero");
7667 double mode = Double.NaN;
7668 if(gamma>=1.0D)mode = (gamma-1.0D)*beta - mu;
7669 return mode;
7670 }
7671
7672 // Gamma distribution - three parameter
7673 // standard deviation
7674 public static double gammaStandardDeviation(double mu, double beta, double gamma){
7675 return gammaStandDev(mu, beta, gamma);
7676 }
7677
7678
7679 // Gamma distribution - three parameter
7680 // standard deviation
7681 public static double gammaStandDev(double mu, double beta, double gamma){
7682 if(beta<=0.0D)throw new IllegalArgumentException("The scale parameter, " + beta + "must be greater than zero");
7683 if(gamma<=0.0D)throw new IllegalArgumentException("The shape parameter, " + gamma + "must be greater than zero");
7684 return Math.sqrt(gamma)*beta;
7685 }
7686
7687
7688 // Returns an array of Gamma random deviates - clock seed
7689 public static double[] gammaRand(double mu, double beta, double gamma, int n){
7690 if(beta<=0.0D)throw new IllegalArgumentException("The scale parameter, " + beta + "must be greater than zero");
7691 if(gamma<=0.0D)throw new IllegalArgumentException("The shape parameter, " + gamma + "must be greater than zero");
7692 PsRandom psr = new PsRandom();
7693 return psr.gammaArray(mu, beta, gamma, n);
7694 }
7695
7696 // Returns an array of Gamma random deviates - user supplied seed
7697 public static double[] gammaRand(double mu, double beta, double gamma, int n, long seed){
7698 if(beta<=0.0D)throw new IllegalArgumentException("The scale parameter, " + beta + "must be greater than zero");
7699 if(gamma<=0.0D)throw new IllegalArgumentException("The shape parameter, " + gamma + "must be greater than zero");
7700 PsRandom psr = new PsRandom(seed);
7701 return psr.gammaArray(mu, beta, gamma, n);
7702 }
7703
7704 // Gamma function
7705 // Lanczos approximation (6 terms)
7706 public static double gammaFunction(double x){
7707
7708 double xcopy = x;
7709 double first = x + lgfGamma + 0.5;
7710 double second = lgfCoeff[0];
7711 double fg = 0.0D;
7712
7713 if(x>=0.0){
7714 first = Math.pow(first, x + 0.5)*Math.exp(-first);
7715 for(int i=1; i<=lgfN; i++)second += lgfCoeff[i]/++xcopy;
7716 fg = first*Math.sqrt(2.0*Math.PI)*second/x;
7717 }
7718 else{
7719 fg = -Math.PI/(x*Stat.gamma(-x)*Math.sin(Math.PI*x));
7720 }
7721 return fg;
7722 }
7723
7724 // Gamma function
7725 // Lanczos approximation (6 terms)
7726 // retained for backward compatibity
7727 public static double gamma(double x){
7728
7729 double xcopy = x;
7730 double first = x + lgfGamma + 0.5;
7731 double second = lgfCoeff[0];
7732 double fg = 0.0D;
7733
7734 if(x>=0.0){
7735 first = Math.pow(first, x + 0.5)*Math.exp(-first);
7736 for(int i=1; i<=lgfN; i++)second += lgfCoeff[i]/++xcopy;
7737 fg = first*Math.sqrt(2.0*Math.PI)*second/x;
7738 }
7739 else{
7740 fg = -Math.PI/(x*Stat.gamma(-x)*Math.sin(Math.PI*x));
7741 }
7742 return fg;
7743 }
7744
7745 // Return the Lanczos constant gamma
7746 public static double getLanczosGamma(){
7747 return Stat.lgfGamma;
7748 }
7749
7750 // Return the Lanczos constant N (number of coeeficients + 1)
7751 public static int getLanczosN(){
7752 return Stat.lgfN;
7753 }
7754
7755 // Return the Lanczos coeeficients
7756 public static double[] getLanczosCoeff(){
7757 int n = Stat.getLanczosN()+1;
7758 double[] coef = new double[n];
7759 for(int i=0; i<n; i++){
7760 coef[i] = Stat.lgfCoeff[i];
7761 }
7762 return coef;
7763 }
7764
7765 // Return the nearest smallest representable floating point number to zero with mantissa rounded to 1.0
7766 public static double getFpmin(){
7767 return Stat.FPMIN;
7768 }
7769
7770 // log to base e of the Gamma function
7771 // Lanczos approximation (6 terms)
7772 public static double logGammaFunction(double x){
7773 double xcopy = x;
7774 double fg = 0.0D;
7775 double first = x + lgfGamma + 0.5;
7776 double second = lgfCoeff[0];
7777
7778 if(x>=0.0){
7779 first -= (x + 0.5)*Math.log(first);
7780 for(int i=1; i<=lgfN; i++)second += lgfCoeff[i]/++xcopy;
7781 fg = Math.log(Math.sqrt(2.0*Math.PI)*second/x) - first;
7782 }
7783 else{
7784 fg = Math.PI/(Stat.gamma(1.0D-x)*Math.sin(Math.PI*x));
7785
7786 if(fg!=1.0/0.0 && fg!=-1.0/0.0){
7787 if(fg<0){
7788 throw new IllegalArgumentException("\nThe gamma function is negative");
7789 }
7790 else{
7791 fg = Math.log(fg);
7792 }
7793 }
7794 }
7795 return fg;
7796 }
7797
7798 // log to base e of the Gamma function
7799 // Lanczos approximation (6 terms)
7800 // Retained for backward compatibility
7801 public static double logGamma(double x){
7802 double xcopy = x;
7803 double fg = 0.0D;
7804 double first = x + lgfGamma + 0.5;
7805 double second = lgfCoeff[0];
7806
7807 if(x>=0.0){
7808 first -= (x + 0.5)*Math.log(first);
7809 for(int i=1; i<=lgfN; i++)second += lgfCoeff[i]/++xcopy;
7810 fg = Math.log(Math.sqrt(2.0*Math.PI)*second/x) - first;
7811 }
7812 else{
7813 fg = Math.PI/(Stat.gamma(1.0D-x)*Math.sin(Math.PI*x));
7814
7815 if(fg!=1.0/0.0 && fg!=-1.0/0.0){
7816 if(fg<0){
7817 throw new IllegalArgumentException("\nThe gamma function is negative");
7818 }
7819 else{
7820 fg = Math.log(fg);
7821 }
7822 }
7823 }
7824 return fg;
7825 }
7826
7827
7828 // Inverse Gamma Function
7829 public static double[] inverseGammaFunction(double gamma){
7830 double gammaMinimum = 0.8856031944108839;
7831 double iGammaMinimum = 1.4616321399961483;
7832 if(gamma<gammaMinimum)throw new IllegalArgumentException("Entered argument (gamma) value, " + gamma + ", must be equal to or greater than 0.8856031944108839 - this method does not handle the negative domain");
7833
7834 double[] igamma = new double[2];
7835
7836 // required tolerance
7837 double tolerance = 1e-12;
7838
7839
7840 // x value between 0 and 1.4616321399961483
7841 if(gamma==1.0){
7842 igamma[0] = 1.0;
7843 }
7844 else{
7845 if(gamma==gammaMinimum){
7846 igamma[0] = iGammaMinimum;
7847 }
7848 else{
7849 // Create instance of the class holding the gamma inverse function
7850 InverseGammaFunct gif1 = new InverseGammaFunct();
7851
7852 // Set inverse gamma function variable
7853 gif1.gamma = gamma;
7854
7855 // lower bounds
7856 double lowerBound1 = 0.0;
7857
7858 // upper bound
7859 double upperBound1 = iGammaMinimum;
7860
7861 // Create instance of RealRoot
7862 RealRoot realR1 = new RealRoot();
7863
7864 // Set extension limits
7865 realR1.noBoundsExtensions();
7866
7867 // Set tolerance
7868 realR1.setTolerance(tolerance);
7869
7870 // Supress error messages and arrange for NaN to be returned as root if root not found
7871 realR1.resetNaNexceptionToTrue();
7872 realR1.supressLimitReachedMessage();
7873 realR1.supressNaNmessage();
7874
7875 // call root searching method
7876 igamma[0] = realR1.bisect(gif1, lowerBound1, upperBound1);
7877 }
7878 }
7879
7880 // x value above 1.4616321399961483
7881 if(gamma==1.0){
7882 igamma[1] = 2.0;
7883 }
7884 else{
7885 if(gamma==gammaMinimum){
7886 igamma[1] = iGammaMinimum;
7887 }
7888 else{
7889 // Create instance of the class holding the gamma inverse function
7890 InverseGammaFunct gif2 = new InverseGammaFunct();
7891
7892 // Set inverse gamma function variable
7893 gif2.gamma = gamma;
7894
7895 // bounds
7896 double lowerBound2 = iGammaMinimum;
7897 double upperBound2 = 2.0;
7898 double ii = 2.0;
7899 double gii = Stat.gamma(ii);
7900 if(gamma>gii){
7901 boolean test = true;
7902 while(test){
7903 ii += 1.0;
7904 gii = Stat.gamma(ii);
7905 if(gamma<=gii){
7906 upperBound2 = ii;
7907 lowerBound2 = ii - 1.0;
7908 test = false;
7909 }
7910 }
7911 }
7912
7913 // Create instance of RealRoot
7914 RealRoot realR2 = new RealRoot();
7915
7916 // Set extension limits
7917 realR2.noBoundsExtensions();
7918
7919 // Set tolerance
7920 realR2.setTolerance(tolerance);
7921
7922 // Supress error messages and arrange for NaN to be returned as root if root not found
7923 realR2.resetNaNexceptionToTrue();
7924 realR2.supressLimitReachedMessage();
7925 realR2.supressNaNmessage();
7926
7927 // call root searching method
7928 igamma[1] = realR2.bisect(gif2, lowerBound2, upperBound2);
7929 }
7930 }
7931
7932 return igamma;
7933 }
7934
7935 // Return Gamma function minimum
7936 // First element contains the Gamma Function minimum value
7937 // Second element contains the x value at which the minimum occors
7938 public static double[] gammaFunctionMinimum(){
7939 double[] ret = {0.8856031944108839, 1.4616321399961483};
7940 return ret;
7941 }
7942
7943
7944 // Regularised Incomplete Gamma Function P(a,x) = integral from zero to x of (exp(-t)t^(a-1))dt
7945 public static double regularisedGammaFunction(double a, double x){
7946 if(a<0.0D || x<0.0D)throw new IllegalArgumentException("\nFunction defined only for a >= 0 and x>=0");
7947
7948 Stat.igSupress = true;
7949 double igf = 0.0D;
7950
7951 if(x!=0){
7952 if(x < a+1.0D){
7953 // Series representation
7954 igf = incompleteGammaSer(a, x);
7955 }
7956 else{
7957 // Continued fraction representation
7958 igf = incompleteGammaFract(a, x);
7959 }
7960 if(igf!=igf)igf = 1.0 - Stat.crigfGaussQuad(a, x);
7961 }
7962 if(igf<0.0)igf = 0.0;
7963 Stat.igSupress = false;
7964 return igf;
7965 }
7966
7967 // Regularised Incomplete Gamma Function P(a,x) = integral from zero to x of (exp(-t)t^(a-1))dt
7968 public static double regularizedGammaFunction(double a, double x){
7969 return regularisedGammaFunction(a, x);
7970 }
7971
7972 // Regularised Incomplete Gamma Function P(a,x) = integral from zero to x of (exp(-t)t^(a-1))dt
7973 // Retained for backward compatibility
7974 public static double regIncompleteGamma(double a, double x){
7975 return regularisedGammaFunction(a, x);
7976 }
7977
7978 // Regularised Incomplete Gamma Function P(a,x) = integral from zero to x of (exp(-t)t^(a-1))dt
7979 // Retained for backward compatibility
7980 public static double incompleteGamma(double a, double x){
7981 return regularisedGammaFunction(a, x);
7982 }
7983
7984 // Complementary Regularised Incomplete Gamma Function Q(a,x) = 1 - P(a,x) = 1 - integral from zero to x of (exp(-t)t^(a-1))dt
7985 public static double complementaryRegularisedGammaFunction(double a, double x){
7986 if(a<0.0D || x<0.0D)throw new IllegalArgumentException("\nFunction defined only for a >= 0 and x>=0");
7987
7988 Stat.igSupress = true;
7989 double igf = 1.0D;
7990
7991 if(x!=0.0D){
7992 if(x==1.0D/0.0D)
7993 {
7994 igf=0.0D;
7995 }
7996 else{
7997 if(x < a+1.0D){
7998 // Series representation
7999 igf = 1.0 - Stat.incompleteGammaSer(a, x);
8000 }
8001 else{
8002 // Continued fraction representation
8003 igf = 1.0 - Stat.incompleteGammaFract(a, x);
8004 }
8005 }
8006 if(igf!=igf)igf = Stat.crigfGaussQuad(a, x);
8007 }
8008 if(igf>1.0)igf = 1.0;
8009 Stat.igSupress = false;
8010 return igf;
8011 }
8012
8013 // Complementary Regularised Incomplete Gamma Function Q(a,x) = 1 - P(a,x) = 1 - integral from zero to x of (exp(-t)t^(a-1))dt
8014 public static double complementaryRegularizedGammaFunction(double a, double x){
8015 return complementaryRegularisedGammaFunction(a , x);
8016 }
8017
8018 // Complementary Regularised Incomplete Gamma Function Q(a,x) = 1 - P(a,x) = 1 - integral from zero to x of (exp(-t)t^(a-1))dt
8019 // Retained for backward compatibility
8020 public static double incompleteGammaComplementary(double a, double x){
8021 return complementaryRegularisedGammaFunction(a , x);
8022 }
8023
8024 // Complementary Regularised Incomplete Gamma Function Q(a,x) = 1 - P(a,x) = 1 - integral from zero to x of (exp(-t)t^(a-1))dt
8025 // Retained for backward compatibility
8026 public static double regIncompleteGammaComplementary(double a, double x){
8027 return complementaryRegularisedGammaFunction(a , x);
8028 }
8029
8030 // Regularised Incomplete Gamma Function P(a,x) = integral from zero to x of (exp(-t)t^(a-1))dt
8031 // Series representation of the function - valid for x < a + 1
8032 public static double incompleteGammaSer(double a, double x){
8033 if(a<0.0D || x<0.0D)throw new IllegalArgumentException("\nFunction defined only for a >= 0 and x>=0");
8034 if(x>=a+1) throw new IllegalArgumentException("\nx >= a+1 use Continued Fraction Representation");
8035
8036 double igf = 0.0D;
8037
8038 if(x!=0.0D){
8039
8040 int i = 0;
8041 boolean check = true;
8042
8043 double acopy = a;
8044 double sum = 1.0/a;
8045 double incr = sum;
8046 double loggamma = Stat.logGamma(a);
8047
8048 while(check){
8049 ++i;
8050 ++a;
8051 incr *= x/a;
8052 sum += incr;
8053 if(Math.abs(incr) < Math.abs(sum)*Stat.igfeps){
8054 igf = sum*Math.exp(-x+acopy*Math.log(x)- loggamma);
8055 check = false;
8056 }
8057 if(i>=Stat.igfiter){
8058 check=false;
8059 igf = Double.NaN;
8060 if(!Stat.igSupress){
8061 System.out.println("\nMaximum number of iterations were exceeded in Stat.incompleteGammaSer().");
8062 System.out.println("NaN returned.\nIncrement = "+String.valueOf(incr)+".");
8063 System.out.println("Sum = "+String.valueOf(sum)+".\nTolerance = "+String.valueOf(igfeps));
8064 }
8065 }
8066 }
8067 }
8068
8069 return igf;
8070 }
8071
8072 // Regularised Incomplete Gamma Function P(a,x) = integral from zero to x of (exp(-t)t^(a-1))dt
8073 // Continued Fraction representation of the function - valid for x >= a + 1
8074 // This method follows the general procedure used in Numerical Recipes for C,
8075 // The Art of Scientific Computing
8076 // by W H Press, S A Teukolsky, W T Vetterling & B P Flannery
8077 // Cambridge University Press, http://www.nr.com/
8078 public static double incompleteGammaFract(double a, double x){
8079 if(a<0.0D || x<0.0D)throw new IllegalArgumentException("\nFunction defined only for a >= 0 and x>=0");
8080 if(x<a+1) throw new IllegalArgumentException("\nx < a+1 Use Series Representation");
8081
8082 double igf = 0.0D;
8083
8084 if(x!=0.0D){
8085
8086 int i = 0;
8087 double ii = 0;
8088 boolean check = true;
8089
8090 double loggamma = Stat.logGamma(a);
8091 double numer = 0.0D;
8092 double incr = 0.0D;
8093 double denom = x - a + 1.0D;
8094 double first = 1.0D/denom;
8095 double term = 1.0D/FPMIN;
8096 double prod = first;
8097
8098 while(check){
8099 ++i;
8100 ii = (double)i;
8101 numer = -ii*(ii - a);
8102 denom += 2.0D;
8103 first = numer*first + denom;
8104 if(Math.abs(first) < Stat.FPMIN){
8105 first = Stat.FPMIN;
8106 }
8107 term = denom + numer/term;
8108 if(Math.abs(term) < Stat.FPMIN){
8109 term = Stat.FPMIN;
8110 }
8111 first = 1.0D/first;
8112 incr = first*term;
8113 prod *= incr;
8114 if(Math.abs(incr - 1.0D) < igfeps)check = false;
8115 if(i>=Stat.igfiter){
8116 check=false;
8117 igf = Double.NaN;
8118 if(!Stat.igSupress){
8119 System.out.println("\nMaximum number of iterations were exceeded in Stat.incompleteGammaFract().");
8120 System.out.println("NaN returned.\nIncrement - 1 = "+String.valueOf(incr-1)+".");
8121 System.out.println("Tolerance = "+String.valueOf(igfeps));
8122 }
8123 }
8124 }
8125 igf = 1.0D - Math.exp(-x+a*Math.log(x)-loggamma)*prod;
8126 }
8127
8128 return igf;
8129 }
8130
8131 // Guassian quadrature estimation of the complementary regularised incomplete gamma function
8132 private static double crigfGaussQuad(double a, double x){
8133 double sum = 0.0;
8134
8135 // set increment details
8136 double upper = 100.0*a;
8137 double range = upper - x;
8138 double incr = 0;
8139 if(upper>x && range >100){
8140 incr = range/1000;
8141 }
8142 else{
8143 upper = x + 100.0;
8144 range = 100.0;
8145 incr = 0.1;
8146 }
8147 int nIncr = (int)Math.round(range/incr);
8148 incr = range/nIncr;
8149
8150 // Instantiate integration function
8151 CrigFunct f1 = new CrigFunct();
8152 f1.setA(a);
8153 f1.setB(Stat.logGammaFunction(a));
8154
8155 // Instantiate Integration
8156 Integration intgn1 = new Integration(f1);
8157 double xx = x;
8158 double yy = x + incr;
8159 intgn1.setLimits(xx, yy);
8160
8161 // Perform quadrature
8162 sum = intgn1.gaussQuad(64);
8163 boolean test2 = true;
8164 for(int i=1; i<nIncr; i++){
8165 xx = yy;
8166 yy = xx + incr;
8167 intgn1.setLimits(xx, yy);
8168 sum += intgn1.gaussQuad(64);
8169 }
8170 return sum;
8171 }
8172
8173 // Suppress error message in incomplete gamma series and incomplete gamma fraction methods supressed
8174 public static void igSupress(){
8175 Stat.igSupress = true;
8176 }
8177
8178 // Reset the maximum number of iterations allowed in the calculation of the incomplete gamma functions
8179 public static void setIncGammaMaxIter(int igfiter){
8180 Stat.igfiter=igfiter;
8181 }
8182
8183 // Return the maximum number of iterations allowed in the calculation of the incomplete gamma functions
8184 public static int getIncGammaMaxIter(){
8185 return Stat.igfiter;
8186 }
8187
8188 // Reset the tolerance used in the calculation of the incomplete gamma functions
8189 public static void setIncGammaTol(double igfeps){
8190 Stat.igfeps=igfeps;
8191 }
8192
8193 // Return the tolerance used in the calculation of the incomplete gamm functions
8194 public static double getIncGammaTol(){
8195 return Stat.igfeps;
8196 }
8197
8198 // FACTORIALS
8199
8200 // factorial of n
8201 // argument and return are integer, therefore limited to 0<=n<=12
8202 // see below for long and double arguments
8203 public static int factorial(int n){
8204 if(n<0)throw new IllegalArgumentException("n must be a positive integer");
8205 if(n>12)throw new IllegalArgumentException("n must less than 13 to avoid integer overflow\nTry long or double argument");
8206 int f = 1;
8207 for(int i=2; i<=n; i++)f*=i;
8208 return f;
8209 }
8210
8211 // factorial of n
8212 // argument and return are long, therefore limited to 0<=n<=20
8213 // see below for double argument
8214 public static long factorial(long n){
8215 if(n<0)throw new IllegalArgumentException("n must be a positive integer");
8216 if(n>20)throw new IllegalArgumentException("n must less than 21 to avoid long integer overflow\nTry double argument");
8217 long f = 1;
8218 long iCount = 2L;
8219 while(iCount<=n){
8220 f*=iCount;
8221 iCount += 1L;
8222 }
8223 return f;
8224 }
8225
8226 // factorial of n
8227 // Argument is of type BigInteger
8228 public static BigInteger factorial(BigInteger n){
8229 if(n.compareTo(BigInteger.ZERO)==-1)throw new IllegalArgumentException("\nn must be a positive integer\nIs a Gamma funtion [Fmath.gamma(x)] more appropriate?");
8230 BigInteger one = BigInteger.ONE;
8231 BigInteger f = one;
8232 BigInteger iCount = new BigInteger("2");
8233 while(iCount.compareTo(n)!=1){
8234 f = f.multiply(iCount);
8235 iCount = iCount.add(one);
8236 }
8237 one = null;
8238 iCount = null;
8239 return f;
8240 }
8241
8242 // factorial of n
8243 // Argument is of type double but must be, numerically, an integer
8244 // factorial returned as double but is, numerically, should be an integer
8245 // numerical rounding may makes this an approximation after n = 21
8246 public static double factorial(double n){
8247 if(n<0 || (n-Math.floor(n))!=0)throw new IllegalArgumentException("\nn must be a positive integer\nIs a Gamma funtion [Fmath.gamma(x)] more appropriate?");
8248 double f = 1.0D;
8249 double iCount = 2.0D;
8250 while(iCount<=n){
8251 f*=iCount;
8252 iCount += 1.0D;
8253 }
8254 return f;
8255 }
8256
8257 // factorial of n
8258 // Argument is of type BigDecimal but must be, numerically, an integer
8259 public static BigDecimal factorial(BigDecimal n){
8260 if(n.compareTo(BigDecimal.ZERO)==-1 || !Fmath.isInteger(n))throw new IllegalArgumentException("\nn must be a positive integer\nIs a Gamma funtion [Fmath.gamma(x)] more appropriate?");
8261 BigDecimal one = BigDecimal.ONE;
8262 BigDecimal f = one;
8263 BigDecimal iCount = new BigDecimal(2.0D);
8264 while(iCount.compareTo(n)!=1){
8265 f = f.multiply(iCount);
8266 iCount = iCount.add(one);
8267 }
8268 one = null;
8269 iCount = null;
8270 return f;
8271 }
8272
8273
8274 // log to base e of the factorial of n
8275 // log[e](factorial) returned as double
8276 // numerical rounding may makes this an approximation
8277 public static double logFactorial(int n){
8278 if(n<0)throw new IllegalArgumentException("\nn, " + n + ", must be a positive integer\nIs a Gamma funtion [Fmath.gamma(x)] more appropriate?");
8279 double f = 0.0D;
8280 for(int i=2; i<=n; i++)f+=Math.log(i);
8281 return f;
8282 }
8283
8284 // log to base e of the factorial of n
8285 // Argument is of type double but must be, numerically, an integer
8286 // log[e](factorial) returned as double
8287 // numerical rounding may makes this an approximation
8288 public static double logFactorial(long n){
8289 if(n<0)throw new IllegalArgumentException("\nn, " + n + ", must be a positive integer\nIs a Gamma funtion [Fmath.gamma(x)] more appropriate?");
8290 double f = 0.0D;
8291 long iCount = 2L;
8292 while(iCount<=n){
8293 f+=Math.log(iCount);
8294 iCount += 1L;
8295 }
8296 return f;
8297 }
8298
8299 // log to base e of the factorial of n
8300 // Argument is of type double but must be, numerically, an integer
8301 // log[e](factorial) returned as double
8302 // numerical rounding may makes this an approximation
8303 public static double logFactorial(double n){
8304 if(n<0 || (n-Math.floor(n))!=0)throw new IllegalArgumentException("\nn must be a positive integer\nIs a Gamma funtion [Fmath.gamma(x)] more appropriate?");
8305 double f = 0.0D;
8306 double iCount = 2.0D;
8307 while(iCount<=n){
8308 f+=Math.log(iCount);
8309 iCount += 1.0D;
8310 }
8311 return f;
8312 }
8313
8314
8315 // ERLANG DISTRIBUTION AND ERLANG EQUATIONS
8316
8317 // Erlang distribution
8318 // Cumulative distribution function
8319 public static double erlangCDF(double lambda, int kay, double upperLimit){
8320 return gammaCDF(0.0D, 1.0D/lambda, (double)kay, upperLimit);
8321 }
8322
8323 public static double erlangCDF(double lambda, long kay, double upperLimit){
8324 return gammaCDF(0.0D, 1.0D/lambda, (double)kay, upperLimit);
8325 }
8326
8327 public static double erlangCDF(double lambda, double kay, double upperLimit){
8328 if(kay - Math.round(kay)!=0.0D)throw new IllegalArgumentException("kay must, mathematically, be an integer even though it may be entered as a double\nTry the Gamma distribution instead of the Erlang distribution");
8329 return gammaCDF(0.0D, 1.0D/lambda, kay, upperLimit);
8330 }
8331
8332 // Erlang distribution
8333 // probablity density function
8334 public static double erlangPDF(double lambda, int kay, double x){
8335 return gammaPDF(0.0D, 1.0D/lambda, (double)kay, x);
8336 }
8337
8338 public static double erlangPDF(double lambda, long kay, double x){
8339 return gammaPDF(0.0D, 1.0D/lambda, (double)kay, x);
8340 }
8341
8342 public static double erlangPDF(double lambda, double kay, double x){
8343 if(kay - Math.round(kay)!=0.0D)throw new IllegalArgumentException("kay must, mathematically, be an integer even though it may be entered as a double\nTry the Gamma distribution instead of the Erlang distribution");
8344
8345 return gammaPDF(0.0D, 1.0D/lambda, kay, x);
8346 }
8347
8348 // Erlang distribution
8349 // mean
8350 public static double erlangMean(double lambda, int kay){
8351 if(kay<1)throw new IllegalArgumentException("The rate parameter, " + kay + "must be equal to or greater than one");
8352 return (double)kay/lambda;
8353 }
8354
8355 public static double erlangMean(double lambda, long kay){
8356 if(kay<1)throw new IllegalArgumentException("The rate parameter, " + kay + "must be equal to or greater than one");
8357 return (double)kay/lambda;
8358 }
8359
8360 public static double erlangMean(double lambda, double kay){
8361 if(kay - Math.round(kay)!=0.0D)throw new IllegalArgumentException("kay must, mathematically, be an integer even though it may be entered as a double\nTry the Gamma distribution instead of the Erlang distribution");
8362 if(kay<1)throw new IllegalArgumentException("The rate parameter, " + kay + "must be equal to or greater than one");
8363 return kay/lambda;
8364 }
8365
8366 // erlang distribution
8367 // mode
8368 public static double erlangMode(double lambda, int kay){
8369 if(kay<1)throw new IllegalArgumentException("The rate parameter, " + kay + "must be equal to or greater than one");
8370 double mode = Double.NaN;
8371 if(kay>=1)mode = ((double)kay-1.0D)/lambda;
8372 return mode;
8373 }
8374
8375 public static double erlangMode(double lambda, long kay){
8376 if(kay<1)throw new IllegalArgumentException("The rate parameter, " + kay + "must be equal to or greater than one");
8377 double mode = Double.NaN;
8378 if(kay>=1)mode = ((double)kay-1.0D)/lambda;
8379 return mode;
8380 }
8381
8382 public static double erlangMode(double lambda, double kay){
8383 if(kay<1)throw new IllegalArgumentException("The rate parameter, " + kay + "must be equal to or greater than one");
8384 if(kay - Math.round(kay)!=0.0D)throw new IllegalArgumentException("kay must, mathematically, be an integer even though it may be entered as a double\nTry the Gamma distribution instead of the Erlang distribution");
8385 double mode = Double.NaN;
8386 if(kay>=1)mode = (kay-1.0D)/lambda;
8387 return mode;
8388 }
8389
8390
8391 // Erlang distribution
8392 // standard deviation
8393 public static double erlangStandardDeviation(double lambda, int kay){
8394 return erlangStandDev(lambda, kay);
8395 }
8396
8397 // standard deviation
8398 public static double erlangStandardDeviation(double lambda, long kay){
8399 return erlangStandDev(lambda, kay);
8400 }
8401
8402 // standard deviation
8403 public static double erlangStandardDeviation(double lambda, double kay){
8404 return erlangStandDev(lambda, kay);
8405 }
8406
8407 // standard deviation
8408 public static double erlangStandDev(double lambda, int kay){
8409 if(kay<1)throw new IllegalArgumentException("The rate parameter, " + kay + "must be equal to or greater than one");
8410 return Math.sqrt((double)kay)/lambda;
8411 }
8412
8413 public static double erlangStandDev(double lambda, long kay){
8414 if(kay<1)throw new IllegalArgumentException("The rate parameter, " + kay + "must be equal to or greater than one");
8415 return Math.sqrt((double)kay)/lambda;
8416 }
8417
8418 public static double erlangStandDev(double lambda, double kay){
8419 if(kay<1)throw new IllegalArgumentException("The rate parameter, " + kay + "must be equal to or greater than one");
8420 if(kay - Math.round(kay)!=0.0D)throw new IllegalArgumentException("kay must, mathematically, be an integer even though it may be entered as a double\nTry the Gamma distribution instead of the Erlang distribution");
8421 return Math.sqrt(kay)/lambda;
8422 }
8423
8424 // Returns an array of Erlang random deviates - clock seed
8425 public static double[] erlangRand(double lambda, int kay, int n){
8426 if(kay<1)throw new IllegalArgumentException("The rate parameter, " + kay + "must be equal to or greater than one");
8427 return gammaRand(0.0D, 1.0D/lambda, (double) kay, n);
8428 }
8429
8430 public static double[] erlangRand(double lambda, long kay, int n){
8431 if(kay<1)throw new IllegalArgumentException("The rate parameter, " + kay + "must be equal to or greater than one");
8432 return gammaRand(0.0D, 1.0D/lambda, (double) kay, n);
8433 }
8434
8435 public static double[] erlangRand(double lambda, double kay, int n){
8436 if(kay<1)throw new IllegalArgumentException("The rate parameter, " + kay + "must be equal to or greater than one");
8437 if(kay - Math.round(kay)!=0.0D)throw new IllegalArgumentException("kay must, mathematically, be an integer even though it may be entered as a double\nTry the Gamma distribution instead of the Erlang distribution");
8438 return gammaRand(0.0D, 1.0D/lambda, kay, n);
8439 }
8440
8441 // Returns an array of Erlang random deviates - user supplied seed
8442 public static double[] erlangRand(double lambda, int kay, int n, long seed){
8443 if(kay<1)throw new IllegalArgumentException("The rate parameter, " + kay + "must be equal to or greater than one");
8444 return gammaRand(0.0D, 1.0D/lambda, (double) kay, n, seed);
8445 }
8446
8447 public static double[] erlangRand(double lambda, long kay, int n, long seed){
8448 if(kay<1)throw new IllegalArgumentException("The rate parameter, " + kay + "must be equal to or greater than one");
8449 return gammaRand(0.0D, 1.0D/lambda, (double) kay, n, seed);
8450 }
8451
8452 public static double[] erlangRand(double lambda, double kay, int n, long seed){
8453 if(kay<1)throw new IllegalArgumentException("The rate parameter, " + kay + "must be equal to or greater than one");
8454 if(kay - Math.round(kay)!=0.0D)throw new IllegalArgumentException("kay must, mathematically, be an integer even though it may be entered as a double\nTry the Gamma distribution instead of the Erlang distribution");
8455 return gammaRand(0.0D, 1.0D/lambda, kay, n, seed);
8456 }
8457
8458
8459 // ERLANG CONNECTIONS BUSY, B AND C EQUATIONS
8460
8461 // returns the probablility that m resources (connections) are busy
8462 // totalTraffic: total traffic in Erlangs
8463 // totalResouces: total number of resources in the system
8464 public static double erlangMprobability(double totalTraffic, double totalResources, double em){
8465 double prob = 0.0D;
8466 if(totalTraffic>0.0D){
8467
8468 double numer = totalResources*Math.log(em) - Fmath.logFactorial(em);
8469 double denom = 1.0D;
8470 double lastTerm = 1.0D;
8471 for(int i=1; i<=totalResources; i++){
8472 lastTerm = lastTerm*totalTraffic/(double)i;
8473 denom += lastTerm;
8474 }
8475 denom = Math.log(denom);
8476 prob = numer - denom;
8477 prob = Math.exp(prob);
8478 }
8479 return prob;
8480 }
8481
8482 public static double erlangMprobability(double totalTraffic, long totalResources, long em){
8483 return erlangMprobability(totalTraffic, (double)totalResources, (double)em);
8484 }
8485
8486 public static double erlangMprobability(double totalTraffic, int totalResources, int em){
8487 return erlangMprobability(totalTraffic, (double)totalResources, (double)em);
8488 }
8489
8490 // Erlang B equation
8491 // Integer or non-integer number of servers
8492 // returns the probablility that a customer will be rejected due to lack of resources
8493 // totalTraffic: total traffic in Erlangs
8494 // totalResouces: total number of resources in the system
8495 public static double erlangBprobability(double totalTraffic, double totalResources){
8496 if(totalTraffic<0)throw new IllegalArgumentException("Total traffic, " + totalTraffic + ", must be greater than or equal to zero");
8497 if(totalResources<0)throw new IllegalArgumentException("Total resources, " + totalResources + ", must be greater than or equal to zero");
8498
8499 double prob = 0.0D;
8500 if(totalResources==0.0D){
8501 prob = 1.0;
8502 }
8503 else{
8504 if(totalTraffic==0.0D){
8505 prob = 0.0;
8506 }
8507 else{
8508 if(Fmath.isInteger(totalResources)){
8509 double iCount = 1.0D;
8510 prob = 1.0D;
8511 double hold = 0.0D;
8512 while(iCount<=totalResources){
8513 hold = prob*totalTraffic;
8514 prob = hold/(iCount + hold);
8515 iCount += 1.0D;
8516 }
8517 }
8518 else{
8519 prob = Stat.erlangBprobabilityNIR(totalTraffic, totalResources);
8520 }
8521 }
8522 }
8523 return prob;
8524 }
8525
8526 // Erlang B equation
8527 // Integer number of servers
8528 // returns the probablility that a customer will be rejected due to lack of resources
8529 // totalTraffic: total traffic in Erlangs
8530 // totalResouces: total number of resources in the system
8531 public static double erlangBprobability(double totalTraffic, long totalResources){
8532 return erlangBprobability(totalTraffic, (double)totalResources);
8533 }
8534
8535
8536 // Erlang B equation
8537 // Integer number of servers
8538 // returns the probablility that a customer will be rejected due to lack of resources
8539 // totalTraffic: total traffic in Erlangs
8540 // totalResouces: total number of resources in the system
8541 public static double erlangBprobability(double totalTraffic, int totalResources){
8542 return erlangBprobability(totalTraffic, (double)totalResources);
8543 }
8544
8545 // Erlang B equation
8546 // Non-Integer number of servers
8547 // returns the probablility that a customer will be rejected due to lack of resources
8548 // totalTraffic: total traffic in Erlangs
8549 // totalResouces: total number of resources in the system
8550 public static double erlangBprobabilityNIR(double totalTraffic, double totalResources){
8551
8552 double prob = 0.0D; // blocking probability
8553
8554 // numerator
8555 double lognumer = totalResources*Math.log(totalTraffic) - totalTraffic;
8556 // denominator (incomplete Gamma Function)
8557 double oneplustr = 1.0D + totalResources;
8558 double crigf = Stat.complementaryRegularisedGammaFunction(oneplustr, totalTraffic);
8559 if(crigf==0.0){
8560 prob = 1.0;
8561 }
8562 else{
8563 double logdenom = Math.log(crigf) + Stat.logGammaFunction(oneplustr);
8564 prob = Math.exp(lognumer - logdenom);
8565 }
8566 return prob;
8567 }
8568
8569 // Non-Integer number of servers
8570 // returns the probablility that a customer will be rejected due to lack of resources
8571 // totalTraffic: total traffic in Erlangs
8572 // totalResouces: total number of resources in the system
8573 // Retained for compatibility
8574 public static double erlangBprobabilityNonIntRes(double totalTraffic, double totalResources){
8575 return Stat.erlangBprobability(totalTraffic, totalResources);
8576 }
8577
8578 // Erlang B equation
8579 // returns the maximum total traffic in Erlangs
8580 // blockingProbability: probablility that a customer will be rejected due to lack of resources
8581 // totalResouces: total number of resources in the system
8582 public static double erlangBload(double blockingProbability, double totalResources){
8583
8584 // Create instance of the class holding the Erlang B equation
8585 ErlangBfunct eBfunc = new ErlangBfunct();
8586
8587 // Set instance variables
8588 eBfunc.blockingProbability = blockingProbability;
8589 eBfunc.totalResources = totalResources;
8590
8591 // lower bound
8592 double lowerBound = 0.0D;
8593 // upper bound // arbitrary - may be extended by bisects automatic extension
8594 double upperBound = 20.0;
8595 // required tolerance
8596 double tolerance = 1e-6;
8597
8598 // Create instance of RealRoot
8599 RealRoot realR = new RealRoot();
8600
8601 // Set tolerance
8602 realR.setTolerance(tolerance);
8603
8604 // Set bounds limits
8605 realR.noLowerBoundExtension();
8606
8607 // Supress error message if iteration limit reached
8608 realR.supressLimitReachedMessage();
8609
8610 // call root searching method
8611 double root = realR.bisect(eBfunc, lowerBound, upperBound);
8612
8613 return root;
8614 }
8615
8616 public static double erlangBload(double blockingProbability, long totalResources){
8617 return erlangBload(blockingProbability, (double)totalResources);
8618 }
8619
8620 public static double erlangBload(double blockingProbability, int totalResources){
8621 return erlangBload(blockingProbability, (double)totalResources);
8622 }
8623
8624 // Erlang B equation
8625 // returns the resources bracketing a blocking probability for a given total traffic
8626 // blockingProbability: probablility that a customer will be rejected due to lack of resources
8627 // totalResouces: total number of resources in the system
8628 public static double[] erlangBresources(double blockingProbability, double totalTraffic){
8629
8630 double[] ret = new double[8];
8631 long counter = 1;
8632 double lastProb = Double.NaN;
8633 double prob = Double.NaN;
8634 boolean test = true;
8635 while(test){
8636 prob = Stat.erlangBprobability(totalTraffic, counter);
8637 if(prob<=blockingProbability){
8638 ret[0] = (double)counter;
8639 ret[1] = prob;
8640 ret[2] = Stat.erlangBload(blockingProbability, counter);
8641 ret[3] = (double)(counter-1);
8642 ret[4] = lastProb;
8643 ret[5] = Stat.erlangBload( blockingProbability, counter-1);
8644 ret[6] = blockingProbability;
8645 ret[7] = totalTraffic;
8646 test = false;
8647 }
8648 else{
8649 lastProb = prob;
8650 counter++;
8651 if(counter==Integer.MAX_VALUE){
8652 System.out.println("Method erlangBresources: no solution found below " + Long.MAX_VALUE + "resources");
8653 for(int i=0; i<8; i++)ret[i] = Double.NaN;
8654 test = false;
8655 }
8656 }
8657 }
8658 return ret;
8659 }
8660
8661 // Erlang C equation
8662 // returns the probablility that a customer will receive a non-zero delay in obtaining obtaining a resource
8663 // totalTraffic: total traffic in Erlangs
8664 // totalResouces: total number of resources in the system
8665 public static double erlangCprobability(double totalTraffic, double totalResources){
8666
8667 double prob = 0.0D;
8668 if(totalTraffic>0.0D){
8669
8670 double probB = Stat.erlangBprobability(totalTraffic, totalResources);
8671 prob = 1.0 + (1.0/probB - 1.0)*(totalResources - totalTraffic)/totalResources;
8672 prob = 1.0/prob;
8673
8674
8675 }
8676 return prob;
8677 }
8678
8679 public static double erlangCprobability(double totalTraffic, long totalResources){
8680 return erlangCprobability(totalTraffic, (double)totalResources);
8681 }
8682
8683 public static double erlangCprobability(double totalTraffic, int totalResources){
8684 return erlangCprobability(totalTraffic, (double)totalResources);
8685 }
8686
8687 // Erlang C equation
8688 // returns the maximum total traffic in Erlangs
8689 // nonZeroDelayProbability: probablility that a customer will receive a non-zero delay in obtaining obtaining a resource
8690 // totalResouces: total number of resources in the system
8691 public static double erlangCload(double nonZeroDelayProbability, double totalResources){
8692
8693 // Create instance of the class holding the Erlang C equation
8694 ErlangCfunct eCfunc = new ErlangCfunct();
8695
8696 // Set instance variables
8697 eCfunc.nonZeroDelayProbability = nonZeroDelayProbability;
8698 eCfunc.totalResources = totalResources;
8699
8700 // lower bound
8701 double lowerBound = 0.0D;
8702 // upper bound
8703 double upperBound = 10.0D;
8704 // required tolerance
8705 double tolerance = 1e-6;
8706
8707 // Create instance of RealRoot
8708 RealRoot realR = new RealRoot();
8709
8710 // Set tolerance
8711 realR.setTolerance(tolerance);
8712
8713 // Supress error message if iteration limit reached
8714 realR.supressLimitReachedMessage();
8715
8716 // Set bounds limits
8717 realR.noLowerBoundExtension();
8718
8719 // call root searching method
8720 double root = realR.bisect(eCfunc, lowerBound, upperBound);
8721
8722 return root;
8723 }
8724
8725 public static double erlangCload(double nonZeroDelayProbability, long totalResources){
8726 return erlangCload(nonZeroDelayProbability, (double)totalResources);
8727 }
8728
8729 public static double erlangCload(double nonZeroDelayProbability, int totalResources){
8730 return erlangCload(nonZeroDelayProbability, (double)totalResources);
8731 }
8732
8733 // Erlang C equation
8734 // returns the resources bracketing a non-zer delay probability for a given total traffic
8735 // nonZeroDelayProbability: probablility that a customer will receive a non-zero delay in obtaining obtaining a resource
8736 // totalResouces: total number of resources in the system
8737 public static double[] erlangCresources(double nonZeroDelayProbability, double totalTraffic){
8738
8739 double[] ret = new double[8];
8740 long counter = 1;
8741 double lastProb = Double.NaN;
8742 double prob = Double.NaN;
8743 boolean test = true;
8744 while(test){
8745 prob = Stat.erlangCprobability(totalTraffic, counter);
8746 if(prob<=nonZeroDelayProbability){
8747 ret[0] = (double)counter;
8748 ret[1] = prob;
8749 ret[2] = Stat.erlangCload(nonZeroDelayProbability, counter);
8750 ret[3] = (double)(counter-1);
8751 ret[4] = lastProb;
8752 ret[5] = Stat.erlangCload(nonZeroDelayProbability, counter-1);
8753 ret[6] = nonZeroDelayProbability;
8754 ret[7] = totalTraffic;
8755 test = false;
8756 }
8757 else{
8758 lastProb = prob;
8759 counter++;
8760 if(counter==Integer.MAX_VALUE){
8761 System.out.println("Method erlangCresources: no solution found below " + Long.MAX_VALUE + "resources");
8762 for(int i=0; i<8; i++)ret[i] = Double.NaN;
8763 test = false;
8764 }
8765 }
8766 }
8767 return ret;
8768 }
8769
8770
8771
8772
8773 // ENGSET EQUATION
8774
8775 // returns the probablility that a customer will be rejected due to lack of resources
8776 // offeredTraffic: total offeredtraffic in Erlangs
8777 // totalResouces: total number of resources in the system
8778 // numberOfSources: number of sources
8779 public static double engsetProbability(double offeredTraffic, double totalResources, double numberOfSources){
8780 if(totalResources<1)throw new IllegalArgumentException("Total resources, " + totalResources + ", must be an integer greater than or equal to 1");
8781 if(!Fmath.isInteger(totalResources))throw new IllegalArgumentException("Total resources, " + totalResources + ", must be, arithmetically, an integer");
8782 if(numberOfSources<1)throw new IllegalArgumentException("number of sources, " + numberOfSources + ", must be an integer greater than or equal to 1");
8783 if(!Fmath.isInteger(numberOfSources))throw new IllegalArgumentException("number of sources, " + numberOfSources + ", must be, arithmetically, an integer");
8784 if(totalResources>numberOfSources-1)throw new IllegalArgumentException("total resources, " + totalResources + ", must be less than or equal to the number of sources minus one, " + (numberOfSources - 1));
8785 if(offeredTraffic>=numberOfSources)throw new IllegalArgumentException("Number of sources, " + numberOfSources + ", must be greater than the offered traffic, " + offeredTraffic);
8786
8787 double prob = 0.0D;
8788 if(totalResources==0.0D){
8789 prob = 1.0D;
8790 }
8791 else{
8792 if(offeredTraffic==0.0D){
8793 prob = 0.0D;
8794 }
8795 else{
8796 // Set boundaries to the probability
8797 double lowerBound = 0.0D;
8798 double upperBound = 1.0D;
8799
8800 // Create instance of Engset Probability Function
8801 EngsetProb engProb = new EngsetProb();
8802
8803 // Set function variables
8804 engProb.offeredTraffic = offeredTraffic;
8805 engProb.totalResources = totalResources;
8806 engProb.numberOfSources = numberOfSources;
8807
8808 // Perform a root search
8809 RealRoot eprt = new RealRoot();
8810
8811 // Supress error message if iteration limit reached
8812 eprt.supressLimitReachedMessage();
8813
8814 prob = eprt.bisect(engProb, lowerBound, upperBound);
8815 }
8816 }
8817 return prob;
8818 }
8819
8820 public static double engsetProbability(double offeredTraffic, long totalResources, long numberOfSources){
8821 return engsetProbability(offeredTraffic, (double)totalResources, (double)numberOfSources);
8822 }
8823
8824 public static double engsetProbability(double offeredTraffic, int totalResources, int numberOfSources){
8825 return engsetProbability(offeredTraffic, (double)totalResources, (double)numberOfSources);
8826 }
8827
8828 // Engset equation
8829 // returns the maximum total traffic in Erlangs
8830 // blockingProbability: probablility that a customer will be rejected due to lack of resources
8831 // totalResouces: total number of resources in the system
8832 // numberOfSources: number of sources
8833 public static double engsetLoad(double blockingProbability, double totalResources, double numberOfSources){
8834 if(totalResources<1)throw new IllegalArgumentException("Total resources, " + totalResources + ", must be an integer greater than or equal to 1");
8835 if(!Fmath.isInteger(totalResources))throw new IllegalArgumentException("Total resources, " + totalResources + ", must be, arithmetically, an integer");
8836 if(numberOfSources<1)throw new IllegalArgumentException("number of sources, " + numberOfSources + ", must be an integer greater than or equal to 1");
8837 if(!Fmath.isInteger(numberOfSources))throw new IllegalArgumentException("number of sources, " + numberOfSources + ", must be, arithmetically, an integer");
8838 if(totalResources>numberOfSources-1)throw new IllegalArgumentException("total resources, " + totalResources + ", must be less than or equal to the number of sources minus one, " + (numberOfSources - 1));
8839
8840 // Create instance of the class holding the Engset Load equation
8841 EngsetLoad eLfunc = new EngsetLoad();
8842
8843 // Set instance variables
8844 eLfunc.blockingProbability = blockingProbability;
8845 eLfunc.totalResources = totalResources;
8846 eLfunc.numberOfSources = numberOfSources;
8847
8848 // lower bound
8849 double lowerBound = 0.0D;
8850 // upper bound
8851 double upperBound = numberOfSources*0.999999999;
8852 // required tolerance
8853 double tolerance = 1e-6;
8854
8855 // Create instance of RealRoot
8856 RealRoot realR = new RealRoot();
8857
8858 // Set tolerance
8859 realR.setTolerance(tolerance);
8860
8861 // Set bounds limits
8862 realR.noLowerBoundExtension();
8863 realR.noUpperBoundExtension();
8864
8865 // Supress error message if iteration limit reached
8866 realR.supressLimitReachedMessage();
8867
8868 // call root searching method
8869 double root = realR.bisect(eLfunc, lowerBound, upperBound);
8870
8871 return root;
8872 }
8873
8874 public static double engsetLoad(double blockingProbability, long totalResources, long numberOfSources){
8875 return engsetLoad(blockingProbability, (double) totalResources, (double) numberOfSources);
8876 }
8877
8878 public static double engsetLoad(double blockingProbability, int totalResources, int numberOfSources){
8879 return engsetLoad(blockingProbability, (double) totalResources, (double) numberOfSources);
8880 }
8881
8882 // Engset equation
8883 // returns the resources bracketing a blocking probability for a given total traffic and number of sources
8884 // blockingProbability: probablility that a customer will be rejected due to lack of resources
8885 // totalResouces: total number of resources in the system
8886 // numberOfSources: number of sources
8887 public static double[] engsetResources(double blockingProbability, double offeredTraffic, double numberOfSources){
8888 if(numberOfSources<1)throw new IllegalArgumentException("number of sources, " + numberOfSources + ", must be an integer greater than or equal to 1");
8889 if(!Fmath.isInteger(numberOfSources))throw new IllegalArgumentException("number of sources, " + numberOfSources + ", must be, arithmetically, an integer");
8890
8891 double[] ret = new double[9];
8892 long counter = 1;
8893 double lastProb = Double.NaN;
8894 double prob = Double.NaN;
8895 boolean test = true;
8896 while(test){
8897 prob = Stat.engsetProbability(offeredTraffic, counter, numberOfSources);
8898 if(prob<=blockingProbability){
8899
8900 ret[0] = (double)counter;
8901 ret[1] = prob;
8902 ret[2] = Stat.engsetLoad(blockingProbability, (double)counter, numberOfSources);
8903 ret[3] = (double)(counter-1);
8904 ret[4] = lastProb;
8905 ret[5] = Stat.engsetLoad( blockingProbability, (double)(counter-1), numberOfSources);
8906 ret[6] = blockingProbability;
8907 ret[7] = offeredTraffic;
8908 ret[8] = numberOfSources;
8909 test = false;
8910 }
8911 else{
8912 lastProb = prob;
8913 counter++;
8914 if(counter>(long)numberOfSources-1L){
8915 System.out.println("Method engsetResources: no solution found below the (sources-1), " + (numberOfSources-1));
8916 for(int i=0; i<8; i++)ret[i] = Double.NaN;
8917 test = false;
8918 }
8919 }
8920 }
8921 return ret;
8922 }
8923
8924 public static double[] engsetResources(double blockingProbability, double totalTraffic, long numberOfSources){
8925 return Stat.engsetResources(blockingProbability, totalTraffic, (double) numberOfSources);
8926 }
8927
8928 public static double[] engsetResources(double blockingProbability, double totalTraffic, int numberOfSources){
8929 return Stat.engsetResources(blockingProbability, totalTraffic, (double) numberOfSources);
8930 }
8931
8932
8933 // Engset equation
8934 // returns the number of sources bracketing a blocking probability for a given total traffic and given resources
8935 // blockingProbability: probablility that a customer will be rejected due to lack of resources
8936 // totalResouces: total number of resources in the system
8937 // numberOfSources: number of sources
8938 public static double[] engsetSources(double blockingProbability, double offeredTraffic, double resources){
8939 if(resources<1)throw new IllegalArgumentException("resources, " + resources + ", must be an integer greater than or equal to 1");
8940 if(!Fmath.isInteger(resources))throw new IllegalArgumentException("resources, " + resources + ", must be, arithmetically, an integer");
8941
8942 double[] ret = new double[9];
8943 long counter = (long)resources+1L;
8944 double lastProb = Double.NaN;
8945 double prob = Double.NaN;
8946 boolean test = true;
8947 while(test){
8948 prob = Stat.engsetProbability(offeredTraffic, resources, counter);
8949 if(prob>=blockingProbability){
8950
8951 ret[0] = (double)counter;
8952 ret[1] = prob;
8953 ret[2] = Stat.engsetLoad(blockingProbability, resources, (double)counter);
8954 ret[3] = (double)(counter-1L);
8955 ret[4] = lastProb;
8956 if((counter-1L)>=(long)(resources+1L)){
8957 ret[5] = Stat.engsetLoad(blockingProbability, resources, (double)(counter-1L));
8958 }
8959 else{
8960 ret[5] = Double.NaN;
8961 }
8962 ret[6] = blockingProbability;
8963 ret[7] = offeredTraffic;
8964 ret[8] = resources;
8965 test = false;
8966 }
8967 else{
8968 lastProb = prob;
8969 counter++;
8970 if(counter>=Long.MAX_VALUE){
8971 System.out.println("Method engsetResources: no solution found below " + Long.MAX_VALUE + "sources");
8972 for(int i=0; i<8; i++)ret[i] = Double.NaN;
8973 test = false;
8974 }
8975 }
8976 }
8977 return ret;
8978 }
8979
8980 public static double[] engsetSources(double blockingProbability, double totalTraffic, long resources){
8981 return Stat.engsetSources(blockingProbability, totalTraffic, (double) resources);
8982 }
8983
8984 public static double[] engsetSources(double blockingProbability, double totalTraffic, int resources){
8985 return Stat.engsetSources(blockingProbability, totalTraffic, (double) resources);
8986 }
8987
8988
8989
8990
8991 // BETA DISTRIBUTIONS AND BETA FUNCTIONS
8992
8993 // beta distribution cdf
8994 public static double betaCDF(double alpha, double beta, double limit){
8995 return betaCDF(0.0D, 1.0D, alpha, beta, limit);
8996 }
8997
8998 // beta distribution pdf
8999 public static double betaCDF(double min, double max, double alpha, double beta, double limit){
9000 if(alpha<=0.0D)throw new IllegalArgumentException("The shape parameter, alpha, " + alpha + "must be greater than zero");
9001 if(beta<=0.0D)throw new IllegalArgumentException("The shape parameter, beta, " + beta + "must be greater than zero");
9002 if(limit<min)throw new IllegalArgumentException("limit, " + limit + ", must be greater than or equal to the minimum value, " + min);
9003 if(limit>max)throw new IllegalArgumentException("limit, " + limit + ", must be less than or equal to the maximum value, " + max);
9004 return Stat.regularisedBetaFunction(alpha, beta, (limit-min)/(max-min));
9005 }
9006
9007
9008 // beta distribution pdf
9009 public static double betaPDF(double alpha, double beta, double x){
9010 return betaPDF(0.0D, 1.0D, alpha, beta, x);
9011 }
9012
9013 // beta distribution pdf
9014 public static double betaPDF(double min, double max, double alpha, double beta, double x){
9015 if(alpha<=0.0D)throw new IllegalArgumentException("The shape parameter, alpha, " + alpha + "must be greater than zero");
9016 if(beta<=0.0D)throw new IllegalArgumentException("The shape parameter, beta, " + beta + "must be greater than zero");
9017 if(x<min)throw new IllegalArgumentException("x, " + x + ", must be greater than or equal to the minimum value, " + min);
9018 if(x>max)throw new IllegalArgumentException("x, " + x + ", must be less than or equal to the maximum value, " + max);
9019 double pdf = Math.pow(x - min, alpha - 1)*Math.pow(max - x, beta - 1)/Math.pow(max - min, alpha + beta - 1);
9020 return pdf/Stat.betaFunction(alpha, beta);
9021 }
9022
9023 // Returns an array of Beta random deviates - clock seed
9024 public static double[] betaRand(double alpha, double beta, int n){
9025 if(alpha<=0.0D)throw new IllegalArgumentException("The shape parameter, alpha, " + alpha + "must be greater than zero");
9026 if(beta<=0.0D)throw new IllegalArgumentException("The shape parameter, beta, " + beta + "must be greater than zero");
9027 PsRandom psr = new PsRandom();
9028 return psr.betaArray(alpha, beta, n);
9029 }
9030
9031 // Returns an array of Beta random deviates - clock seed
9032 public static double[] betaRand(double min, double max, double alpha, double beta, int n){
9033 if(alpha<=0.0D)throw new IllegalArgumentException("The shape parameter, alpha, " + alpha + "must be greater than zero");
9034 if(beta<=0.0D)throw new IllegalArgumentException("The shape parameter, beta, " + beta + "must be greater than zero");
9035 PsRandom psr = new PsRandom();
9036 return psr.betaArray(min, max, alpha, beta, n);
9037 }
9038
9039
9040 // Returns an array of Beta random deviates - user supplied seed
9041 public static double[] betaRand(double alpha, double beta, int n, long seed){
9042 if(alpha<=0.0D)throw new IllegalArgumentException("The shape parameter, alpha, " + alpha + "must be greater than zero");
9043 if(beta<=0.0D)throw new IllegalArgumentException("The shape parameter, beta, " + beta + "must be greater than zero");
9044 PsRandom psr = new PsRandom(seed);
9045 return psr.betaArray(alpha, beta, n);
9046 }
9047
9048 // Returns an array of Beta random deviates - user supplied seed
9049 public static double[] betaRand(double min, double max, double alpha, double beta, int n, long seed){
9050 if(alpha<=0.0D)throw new IllegalArgumentException("The shape parameter, alpha, " + alpha + "must be greater than zero");
9051 if(beta<=0.0D)throw new IllegalArgumentException("The shape parameter, beta, " + beta + "must be greater than zero");
9052 PsRandom psr = new PsRandom(seed);
9053 return psr.betaArray(min, max, alpha, beta, n);
9054 }
9055
9056 // beta distribution mean
9057 public static double betaMean(double alpha, double beta){
9058 return betaMean(0.0D, 1.0D, alpha, beta);
9059 }
9060
9061 // beta distribution mean
9062 public static double betaMean(double min, double max, double alpha, double beta){
9063 if(alpha<=0.0D)throw new IllegalArgumentException("The shape parameter, alpha, " + alpha + "must be greater than zero");
9064 if(beta<=0.0D)throw new IllegalArgumentException("The shape parameter, beta, " + beta + "must be greater than zero");
9065 return min + alpha*(max - min)/(alpha + beta);
9066 }
9067
9068 // beta distribution mode
9069 public static double betaMode(double alpha, double beta){
9070 return betaMode(0.0D, 1.0D, alpha, beta);
9071 }
9072
9073 // beta distribution mode
9074 public static double betaMode(double min, double max, double alpha, double beta){
9075 if(alpha<=0.0D)throw new IllegalArgumentException("The shape parameter, alpha, " + alpha + "must be greater than zero");
9076 if(beta<=0.0D)throw new IllegalArgumentException("The shape parameter, beta, " + beta + "must be greater than zero");
9077
9078 double mode = Double.NaN;
9079 if(alpha>1){
9080 if(beta>1){
9081 mode = min + (alpha + beta)*(max - min)/(alpha + beta - 2);
9082 }
9083 else{
9084 mode = max;
9085 }
9086 }
9087 else{
9088 if(alpha==1){
9089 if(beta>1){
9090 mode = min;
9091 }
9092 else{
9093 if(beta==1){
9094 mode = Double.NaN;
9095 }
9096 else{
9097 mode = max;
9098 }
9099 }
9100 }
9101 else{
9102 if(beta>=1){
9103 mode = min;
9104 }
9105 else{
9106 System.out.println("Class Stat; method betaMode; distribution is bimodal wirh modes at " + min + " and " + max);
9107 System.out.println("NaN returned");
9108 }
9109 }
9110 }
9111 return mode;
9112 }
9113
9114 // beta distribution standard deviation
9115 public static double betaStandardDeviation(double alpha, double beta){
9116 return betaStandDev(alpha, beta);
9117 }
9118
9119 // beta distribution standard deviation
9120 public static double betaStandDev(double alpha, double beta){
9121 return betaStandDev(0.0D, 1.0D, alpha, beta);
9122 }
9123
9124 // beta distribution standard deviation
9125 public static double betaStandardDeviation(double min, double max, double alpha, double beta){
9126 return betaStandDev(min, max, alpha, beta);
9127 }
9128
9129 // beta distribution standard deviation
9130 public static double betaStandDev(double min, double max, double alpha, double beta){
9131 if(alpha<=0.0D)throw new IllegalArgumentException("The shape parameter, alpha, " + alpha + "must be greater than zero");
9132 if(beta<=0.0D)throw new IllegalArgumentException("The shape parameter, beta, " + beta + "must be greater than zero");
9133 return ((max - min)/(alpha + beta))*Math.sqrt(alpha*beta/(alpha + beta + 1));
9134 }
9135
9136 // Beta function
9137 public static double betaFunction(double z, double w){
9138 return Math.exp(logGamma(z) + logGamma(w) - logGamma(z + w));
9139 }
9140
9141 // Beta function
9142 // retained for compatibility reasons
9143 public static double beta(double z, double w){
9144 return Math.exp(logGamma(z) + logGamma(w) - logGamma(z + w));
9145 }
9146
9147 // Regularised Incomplete Beta function
9148 // Continued Fraction approximation (see Numerical recipies for details of method)
9149 public static double regularisedBetaFunction(double z, double w, double x){
9150 if(x<0.0D || x>1.0D)throw new IllegalArgumentException("Argument x, "+x+", must be lie between 0 and 1 (inclusive)");
9151 double ibeta = 0.0D;
9152 if(x==0.0D){
9153 ibeta=0.0D;
9154 }
9155 else{
9156 if(x==1.0D){
9157 ibeta=1.0D;
9158 }
9159 else{
9160 // Term before continued fraction
9161 ibeta = Math.exp(Stat.logGamma(z+w) - Stat.logGamma(z) - logGamma(w) + z*Math.log(x) + w*Math.log(1.0D-x));
9162 // Continued fraction
9163 if(x < (z+1.0D)/(z+w+2.0D)){
9164 ibeta = ibeta*Stat.contFract(z, w, x)/z;
9165 }
9166 else{
9167 // Use symmetry relationship
9168 ibeta = 1.0D - ibeta*Stat.contFract(w, z, 1.0D-x)/w;
9169 }
9170 }
9171 }
9172 return ibeta;
9173 }
9174
9175
9176 // Regularised Incomplete Beta function
9177 // Continued Fraction approximation (see Numerical recipies for details of method)
9178 public static double regularizedBetaFunction(double z, double w, double x){
9179 return regularisedBetaFunction(z, w, x);
9180 }
9181
9182 // Regularised Incomplete Beta function
9183 // Continued Fraction approximation (see Numerical recipies for details of method)
9184 // retained for compatibility reasons
9185 public static double incompleteBeta(double z, double w, double x){
9186 return regularisedBetaFunction(z, w, x);
9187 }
9188
9189 // Incomplete fraction summation used in the method regularisedBetaFunction
9190 // modified Lentz's method
9191 public static double contFract(double a, double b, double x){
9192
9193 double aplusb = a + b;
9194 double aplus1 = a + 1.0D;
9195 double aminus1 = a - 1.0D;
9196 double c = 1.0D;
9197 double d = 1.0D - aplusb*x/aplus1;
9198 if(Math.abs(d)<Stat.FPMIN)d = FPMIN;
9199 d = 1.0D/d;
9200 double h = d;
9201 double aa = 0.0D;
9202 double del = 0.0D;
9203 int i=1, i2=0;
9204 boolean test=true;
9205 while(test){
9206 i2=2*i;
9207 aa = i*(b-i)*x/((aminus1+i2)*(a+i2));
9208 d = 1.0D + aa*d;
9209 if(Math.abs(d)<Stat.FPMIN)d = FPMIN;
9210 c = 1.0D + aa/c;
9211 if(Math.abs(c)<Stat.FPMIN)c = FPMIN;
9212 d = 1.0D/d;
9213 h *= d*c;
9214 aa = -(a+i)*(aplusb+i)*x/((a+i2)*(aplus1+i2));
9215 d = 1.0D + aa*d;
9216 if(Math.abs(d)<Stat.FPMIN)d = FPMIN;
9217 c = 1.0D + aa/c;
9218 if(Math.abs(c)<Stat.FPMIN)c = FPMIN;
9219 d = 1.0D/d;
9220 del = d*c;
9221 h *= del;
9222 i++;
9223 if(Math.abs(del-1.0D) < Stat.cfTol)test=false;
9224 if(i>Stat.cfMaxIter){
9225 test=false;
9226 System.out.println("Maximum number of iterations ("+Stat.cfMaxIter+") exceeded in Stat.contFract in Stat.incompleteBeta");
9227 }
9228 }
9229 return h;
9230
9231 }
9232
9233 // Reset value of cfMaxIter used in contFract method above
9234 public static void resetCFmaxIter(int cfMaxIter){
9235 Stat.cfMaxIter = cfMaxIter;
9236 }
9237
9238 // Get value of cfMaxIter used in contFract method above
9239 public static int getCFmaxIter(){
9240 return cfMaxIter;
9241 }
9242
9243 // Reset value of cfTol used in contFract method above
9244 public static void resetCFtolerance(double cfTol){
9245 Stat.cfTol = cfTol;
9246 }
9247
9248 // Get value of cfTol used in contFract method above
9249 public static double getCFtolerance(){
9250 return cfTol;
9251 }
9252
9253 // ERROR FUNCTIONS
9254
9255 // Error Function
9256 public static double erf(double x){
9257 double erf = 0.0D;
9258 if(x!=0.0){
9259 if(x==1.0D/0.0D){
9260 erf = 1.0D;
9261 }
9262 else{
9263 if(x>=0){
9264 erf = Stat.incompleteGamma(0.5, x*x);
9265 }
9266 else{
9267 erf = -Stat.incompleteGamma(0.5, x*x);
9268 }
9269 }
9270 }
9271 return erf;
9272 }
9273
9274 // Complementary Error Function
9275 public static double erfc(double x){
9276 double erfc = 1.0D;
9277 if(x!=0.0){
9278 if(x==1.0D/0.0D){
9279 erfc = 0.0D;
9280 }
9281 else{
9282 if(x>=0){
9283 erfc = 1.0D - Stat.incompleteGamma(0.5, x*x);
9284 }
9285 else{
9286 erfc = 1.0D + Stat.incompleteGamma(0.5, x*x);
9287 }
9288 }
9289 }
9290 return erfc;
9291 }
9292
9293
9294 // NORMAL (GAUSSIAN) DISTRIBUTION
9295
9296 // Gaussian (normal) cumulative distribution function
9297 // probability that a variate will assume a value less than the upperlimit
9298 // mean = the mean, sd = standard deviation
9299 public static double normalCDF(double mean, double sd, double upperlimit){
9300 double prob = Double.NaN;
9301 if(upperlimit==Double.POSITIVE_INFINITY){
9302 prob = 1.0;
9303 }
9304 else{
9305 if(upperlimit==Double.NEGATIVE_INFINITY){
9306 prob = 0.0;
9307 }
9308 else{
9309 double arg = (upperlimit - mean)/(sd*Math.sqrt(2.0));
9310 prob = (1.0D + Stat.erf(arg))/2.0D;
9311 }
9312 }
9313 if(Fmath.isNaN(prob)){
9314 if(upperlimit>mean){
9315 prob = 1.0;
9316 }
9317 else{
9318 prob = 0.0;
9319 }
9320 }
9321 return prob;
9322 }
9323
9324 // Gaussian (normal) cumulative distribution function
9325 // probability that a variate will assume a value less than the upperlimit
9326 // mean = the mean, sd = standard deviation
9327 public static double normalProb(double mean, double sd, double upperlimit){
9328 if(upperlimit==Double.POSITIVE_INFINITY){
9329 return 1.0;
9330 }
9331 else{
9332 if(upperlimit==Double.NEGATIVE_INFINITY){
9333 return 0.0;
9334 }
9335 else{
9336 double arg = (upperlimit - mean)/(sd*Math.sqrt(2.0));
9337 return (1.0D + Stat.erf(arg))/2.0D;
9338 }
9339 }
9340 }
9341
9342 // Gaussian (normal) cumulative distribution function
9343 // probability that a variate will assume a value less than the upperlimit
9344 // mean = the mean, sd = standard deviation
9345 public static double gaussianCDF(double mean, double sd, double upperlimit){
9346 return normalCDF(mean, sd, upperlimit);
9347 }
9348
9349 // Gaussian (normal) cumulative distribution function
9350 // probability that a variate will assume a value less than the upperlimit
9351 // mean = the mean, sd = standard deviation
9352 public static double gaussianProb(double mean, double sd, double upperlimit){
9353 return normalCDF(mean, sd, upperlimit);
9354 }
9355
9356 // Gaussian (normal) cumulative distribution function
9357 // probability that a variate will assume a value between the lower and the upper limits
9358 // mean = the mean, sd = standard deviation
9359 public static double normalCDF(double mean, double sd, double lowerlimit, double upperlimit){
9360 return Stat.normalCDF(mean, sd, upperlimit) - Stat.normalCDF(mean, sd, lowerlimit);
9361 }
9362
9363 // Gaussian (normal) cumulative distribution function
9364 // probability that a variate will assume a value between the lower and the upper limits
9365 // mean = the mean, sd = standard deviation
9366 public static double normalProb(double mean, double sd, double lowerlimit, double upperlimit){
9367 return Stat.normalCDF(mean, sd, upperlimit) - Stat.normalCDF(mean, sd, lowerlimit);
9368 }
9369
9370 // Gaussian (normal) cumulative distribution function
9371 // probability that a variate will assume a value between the lower and the upper limits
9372 // mean = the mean, sd = standard deviation
9373 public static double gaussianCDF(double mean, double sd, double lowerlimit, double upperlimit){
9374 return Stat.normalCDF(mean, sd, upperlimit) - Stat.normalCDF(mean, sd, lowerlimit);
9375 }
9376
9377 // Gaussian (normal) cumulative distribution function
9378 // probability that a variate will assume a value between the lower and the upper limits
9379 // mean = the mean, sd = standard deviation
9380 public static double gaussianProb(double mean, double sd, double lowerlimit, double upperlimit){
9381 return Stat.normalCDF(mean, sd, upperlimit) - Stat.normalCDF(mean, sd, lowerlimit);
9382 }
9383
9384 // Gaussian Inverse Cumulative Distribution Function
9385 public static double gaussianInverseCDF(double mean, double sd, double prob){
9386 if(prob<0.0 || prob>1.0)throw new IllegalArgumentException("Entered cdf value, " + prob + ", must lie between 0 and 1 inclusive");
9387
9388 double icdf = 0.0D;
9389
9390 if(prob==0.0){
9391 icdf = Double.NEGATIVE_INFINITY;
9392 }
9393 else{
9394 if(prob==1.0){
9395 icdf = Double.POSITIVE_INFINITY;
9396 }
9397 else{
9398
9399 // Create instance of the class holding the gaussian cfd function
9400 GaussianFunct gauss = new GaussianFunct();
9401
9402 // set function variables
9403 gauss.mean = mean;
9404 gauss.sd = sd;
9405
9406 // required tolerance
9407 double tolerance = 1e-12;
9408
9409 // lower bound
9410 double lowerBound = mean - 10.0*sd;
9411
9412 // upper bound
9413 double upperBound = mean + 10.0*sd;
9414
9415 // Create instance of RealRoot
9416 RealRoot realR = new RealRoot();
9417
9418 // Set extension limits
9419 // none
9420
9421 // Set tolerance
9422 realR.setTolerance(tolerance);
9423
9424 // Supress error messages and arrange for NaN to be returned as root if root not found
9425 realR.resetNaNexceptionToTrue();
9426 realR.supressLimitReachedMessage();
9427 realR.supressNaNmessage();
9428
9429 // set function cfd variable
9430 gauss.cfd = prob;
9431
9432 // call root searching method
9433 icdf = realR.bisect(gauss, lowerBound, upperBound);
9434 }
9435 }
9436
9437 return icdf;
9438 }
9439
9440 // Gaussian Inverse Cumulative Distribution Function
9441 public static double inverseGaussianCDF(double mean, double sd, double prob){
9442 return gaussianInverseCDF(mean, sd, prob);
9443 }
9444
9445 // Gaussian Inverse Cumulative Distribution Function
9446 public static double normalInverseCDF(double mean, double sd, double prob){
9447 return gaussianInverseCDF(mean, sd, prob);
9448 }
9449
9450 // Gaussian Inverse Cumulative Distribution Function
9451 public static double inverseNormalCDF(double mean, double sd, double prob){
9452 return gaussianInverseCDF(mean, sd, prob);
9453 }
9454
9455 // Gaussian Inverse Cumulative Distribution Function
9456 // Standardized
9457 public static double gaussianInverseCDF(double prob){
9458 return gaussianInverseCDF(0.0D, 1.0D, prob);
9459 }
9460
9461 // Gaussian Inverse Cumulative Distribution Function
9462 // Standardized
9463 public static double inverseGaussianCDF(double prob){
9464 return gaussianInverseCDF(0.0D, 1.0D, prob);
9465 }
9466
9467 // Gaussian Inverse Cumulative Distribution Function
9468 // Standardized
9469 public static double normalInverseCDF(double prob){
9470 return gaussianInverseCDF(0.0D, 1.0D, prob);
9471 }
9472
9473 // Gaussian Inverse Cumulative Distribution Function
9474 // Standardized
9475 public static double inverseNormalCDF(double prob){
9476 return gaussianInverseCDF(0.0D, 1.0D, prob);
9477 }
9478
9479
9480 // Gaussian (normal) order statistic medians (n points)
9481 public static double[] gaussianOrderStatisticMedians(double mean, double sigma, int n){
9482 double nn = (double)n;
9483 double[] gosm = new double[n];
9484 double[] uosm = uniformOrderStatisticMedians(n);
9485 for(int i=0; i<n; i++){
9486 gosm[i] =Stat.inverseGaussianCDF(mean, sigma, uosm[i]);
9487 }
9488 gosm = Stat.scale(gosm, mean, sigma);
9489 return gosm;
9490 }
9491
9492 public static double[] normalOrderStatisticMedians(double mean, double sigma, int n){
9493 return Stat.gaussianOrderStatisticMedians(mean, sigma, n);
9494 }
9495
9496 // Gaussian (normal) order statistic medians for a mean of zero and a standard deviation 0f unity (n points)
9497 public static double[] gaussianOrderStatisticMedians(int n){
9498 return Stat.gaussianOrderStatisticMedians(0.0, 1.0, n);
9499 }
9500
9501 public static double[] normalOrderStatisticMedians(int n){
9502 return Stat.gaussianOrderStatisticMedians(0.0, 1.0, n);
9503 }
9504
9505 // Gaussian (normal) probability density function
9506 // mean = the mean, sd = standard deviation
9507 public static double normalPDF(double mean, double sd, double x){
9508 return Math.exp(-Fmath.square((x - mean)/sd)/2.0)/(sd*Math.sqrt(2.0D*Math.PI));
9509 }
9510
9511 // Gaussian (normal) probability density function
9512 // mean = the mean, sd = standard deviation
9513 public static double normal(double mean, double sd, double x){
9514 return Math.exp(-Fmath.square((x - mean)/sd)/2.0)/(sd*Math.sqrt(2.0D*Math.PI));
9515 }
9516
9517 // Gaussian (normal) probability density function
9518 // mean = the mean, sd = standard deviation
9519 public static double gaussianPDF(double mean, double sd, double x){
9520 return Math.exp(-Fmath.square((x - mean)/sd)/2.0)/(sd*Math.sqrt(2.0D*Math.PI));
9521 }
9522 // Gaussian (normal) probability density function
9523 // mean = the mean, sd = standard deviation
9524 public static double gaussian(double mean, double sd, double x){
9525 return Math.exp(-Fmath.square((x - mean)/sd)/2.0)/(sd*Math.sqrt(2.0D*Math.PI));
9526 }
9527
9528 // Returns an array of Gaussian (normal) random deviates - clock seed
9529 // mean = the mean, sd = standard deviation, length of array
9530 public static double[] normalRand(double mean, double sd, int n){
9531 double[] ran = new double[n];
9532 Random rr = new Random();
9533 for(int i=0; i<n; i++){
9534 ran[i]=rr.nextGaussian();
9535 }
9536 ran = Stat.standardize(ran);
9537 for(int i=0; i<n; i++){
9538 ran[i] = ran[i]*sd+mean;
9539 }
9540 return ran;
9541 }
9542
9543 // Returns an array of Gaussian (normal) random deviates - clock seed
9544 // mean = the mean, sd = standard deviation, length of array
9545 public static double[] gaussianRand(double mean, double sd, int n){
9546 return normalRand(mean, sd, n);
9547 }
9548
9549 // Returns an array of Gaussian (normal) random deviates - user provided seed
9550 // mean = the mean, sd = standard deviation, length of array
9551 public static double[] normalRand(double mean, double sd, int n, long seed){
9552 double[] ran = new double[n];
9553 Random rr = new Random(seed);
9554 for(int i=0; i<n; i++){
9555 ran[i]=rr.nextGaussian();
9556 }
9557 ran = Stat.standardize(ran);
9558 for(int i=0; i<n; i++){
9559 ran[i] = ran[i]*sd+mean;
9560 }
9561 return ran;
9562 }
9563
9564 // Returns an array of Gaussian (normal) random deviates - user provided seed
9565 // mean = the mean, sd = standard deviation, length of array
9566 public static double[] gaussianRand(double mean, double sd, int n, long seed){
9567 return normalRand(mean, sd, n, seed);
9568 }
9569
9570
9571
9572 // LOG-NORMAL DISTRIBUTIONS (TWO AND THEE PARAMETER DISTRIBUTIONS)
9573
9574 // TWO PARAMETER LOG-NORMAL DISTRIBUTION
9575
9576 // Two parameter log-normal cumulative distribution function
9577 // probability that a variate will assume a value less than the upperlimit
9578 public static double logNormalCDF(double mu, double sigma, double upperLimit){
9579 if(sigma<0)throw new IllegalArgumentException("The parameter sigma, " + sigma + ", must be greater than or equal to zero");
9580 if(upperLimit<=0){
9581 return 0.0D;
9582 }
9583 else{
9584 return 0.5D*(1.0D + Stat.erf((Math.log(upperLimit)-mu)/(sigma*Math.sqrt(2))));
9585 }
9586 }
9587
9588 public static double logNormalTwoParCDF(double mu, double sigma, double upperLimit){
9589 return logNormalCDF(mu, sigma, upperLimit);
9590 }
9591
9592
9593 // Two parameter log-normal cumulative distribution function
9594 // probability that a variate will assume a value between the lower and the upper limits
9595 public static double logNormalCDF(double mu, double sigma, double lowerLimit, double upperLimit){
9596 if(sigma<0)throw new IllegalArgumentException("The parameter sigma, " + sigma + ", must be greater than or equal to zero");
9597 if(upperLimit<lowerLimit)throw new IllegalArgumentException("The upper limit, " + upperLimit + ", must be greater than the " + lowerLimit);
9598
9599 double arg1 = 0.0D;
9600 double arg2 = 0.0D;
9601 double cdf = 0.0D;
9602
9603 if(lowerLimit!=upperLimit){
9604 if(upperLimit>0.0D)arg1 = 0.5D*(1.0D + Stat.erf((Math.log(upperLimit)-mu)/(sigma*Math.sqrt(2))));
9605 if(lowerLimit>0.0D)arg2 = 0.5D*(1.0D + Stat.erf((Math.log(lowerLimit)-mu)/(sigma*Math.sqrt(2))));
9606 cdf = arg1 - arg2;
9607 }
9608
9609 return cdf;
9610 }
9611
9612 public static double logNormalTwoParCDF(double mu, double sigma, double lowerLimit, double upperLimit){
9613 return logNormalCDF(mu, sigma, lowerLimit, upperLimit);
9614 }
9615
9616 // Log-Normal Inverse Cumulative Distribution Function
9617 // Two parameter
9618 public static double logNormalInverseCDF(double mu, double sigma, double prob){
9619 double alpha = 0.0;
9620 double beta = sigma;
9621 double gamma = Math.exp(mu);
9622
9623 return logNormalInverseCDF(alpha, beta, gamma, prob);
9624 }
9625
9626 // Log-Normal Inverse Cumulative Distribution Function
9627 // Two parameter
9628 public static double logNormaltwoParInverseCDF(double mu, double sigma, double prob){
9629 double alpha = 0.0;
9630 double beta = sigma;
9631 double gamma = Math.exp(mu);
9632
9633 return logNormalInverseCDF(alpha, beta, gamma, prob);
9634 }
9635
9636
9637 // Two parameter log-normal probability density function
9638 public static double logNormalPDF(double mu, double sigma, double x){
9639 if(sigma<0)throw new IllegalArgumentException("The parameter sigma, " + sigma + ", must be greater than or equal to zero");
9640 if(x<0){
9641 return 0.0D;
9642 }
9643 else{
9644 return Math.exp(-0.5D*Fmath.square((Math.log(x)- mu)/sigma))/(x*sigma*Math.sqrt(2.0D*Math.PI));
9645 }
9646 }
9647
9648 public static double logNormalTwoParPDF(double mu, double sigma, double x){
9649 return logNormalPDF(mu, sigma, x);
9650 }
9651
9652
9653 // Two parameter log-normal mean
9654 public static double logNormalMean(double mu, double sigma){
9655 return Math.exp(mu + sigma*sigma/2.0D);
9656 }
9657
9658 public static double logNormalTwoParMean(double mu, double sigma){
9659 return Math.exp(mu + sigma*sigma/2.0D);
9660 }
9661
9662 // Two parameter log-normal standard deviation
9663 public static double logNormalStandardDeviation(double mu, double sigma){
9664 return logNormalStandDev(mu, sigma);
9665 }
9666
9667 // Two parameter log-normal standard deviation
9668 public static double logNormalStandDev(double mu, double sigma){
9669 double sigma2 = sigma*sigma;
9670 return Math.sqrt((Math.exp(sigma2) - 1.0D)*Math.exp(2.0D*mu + sigma2));
9671 }
9672
9673 public static double logNormalTwoParStandardDeviation(double mu, double sigma){
9674 return logNormalTwoParStandDev(mu, sigma);
9675 }
9676
9677 public static double logNormalTwoParStandDev(double mu, double sigma){
9678 double sigma2 = sigma*sigma;
9679 return Math.sqrt((Math.exp(sigma2) - 1.0D)*Math.exp(2.0D*mu + sigma2));
9680 }
9681
9682 // Two parameter log-normal mode
9683 public static double logNormalMode(double mu, double sigma){
9684 return Math.exp(mu - sigma*sigma);
9685 }
9686
9687 public static double logNormalTwoParMode(double mu, double sigma){
9688 return Math.exp(mu - sigma*sigma);
9689 }
9690
9691 // Two parameter log-normal median
9692 public static double logNormalMedian(double mu){
9693 return Math.exp(mu);
9694 }
9695
9696 public static double logNormalTwoParMedian(double mu){
9697 return Math.exp(mu);
9698 }
9699
9700 // Returns an array of two parameter log-normal random deviates - clock seed
9701 public static double[] logNormalRand(double mu, double sigma, int n){
9702 if(n<=0)throw new IllegalArgumentException("The number of random deviates required, " + n + ", must be greater than zero");
9703 if(sigma<0)throw new IllegalArgumentException("The parameter sigma, " + sigma + ", must be greater than or equal to zero");
9704 PsRandom psr = new PsRandom();
9705 return psr.logNormalArray(mu, sigma, n);
9706 }
9707
9708 public static double[] logNormalTwoParRand(double mu, double sigma, int n){
9709 return logNormalRand(mu, sigma, n);
9710 }
9711
9712 // LogNormal order statistic medians (n points)
9713 // Two parametrs
9714 public static double[] logNormalOrderStatisticMedians(double mu, double sigma, int n){
9715 double alpha = 0.0;
9716 double beta = sigma;
9717 double gamma = Math.exp(mu);
9718
9719 return logNormalOrderStatisticMedians(alpha, beta, gamma, n);
9720 }
9721
9722 // LogNormal order statistic medians (n points)
9723 // Two parametrs
9724 public static double[] logNormalTwoParOrderStatisticMedians(double mu, double sigma, int n){
9725 return Stat.logNormalOrderStatisticMedians(mu, sigma, n);
9726 }
9727
9728
9729 // Returns an array of two parameter log-normal random deviates - user supplied seed
9730 public static double[] logNormalRand(double mu, double sigma, int n, long seed){
9731 if(n<=0)throw new IllegalArgumentException("The number of random deviates required, " + n + ", must be greater than zero");
9732 if(sigma<0)throw new IllegalArgumentException("The parameter sigma, " + sigma + ", must be greater than or equal to zero");
9733 PsRandom psr = new PsRandom(seed);
9734 return psr.logNormalArray(mu, sigma, n);
9735 }
9736
9737
9738 public static double[] logNormalTwoParRand(double mu, double sigma, int n, long seed){
9739 return logNormalRand(mu, sigma, n, seed);
9740 }
9741
9742 // THREE PARAMETER LOG-NORMAL DISTRIBUTION
9743
9744 // Three parameter log-normal cumulative distribution function
9745 // probability that a variate will assume a value less than the upperlimit
9746 public static double logNormalThreeParCDF(double alpha, double beta, double gamma, double upperLimit){
9747 if(beta<0)throw new IllegalArgumentException("The parameter beta, " + beta + ", must be greater than or equal to zero");
9748 if(upperLimit<=alpha){
9749 return 0.0D;
9750 }
9751 else{
9752 return 0.5D*(1.0D + Stat.erf(Math.log((upperLimit-alpha)/gamma)/(beta*Math.sqrt(2))));
9753 }
9754 }
9755
9756
9757 // Three parameter log-normal cumulative distribution function
9758 // probability that a variate will assume a value between the lower and the upper limits
9759 public static double logNormalThreeParCDF(double alpha, double beta, double gamma, double lowerLimit, double upperLimit){
9760 if(beta<0)throw new IllegalArgumentException("The parameter beta, " + beta + ", must be greater than or equal to zero");
9761 if(upperLimit<lowerLimit)throw new IllegalArgumentException("The upper limit, " + upperLimit + ", must be greater than the " + lowerLimit);
9762
9763 double arg1 = 0.0D;
9764 double arg2 = 0.0D;
9765 double cdf = 0.0D;
9766
9767 if(lowerLimit!=upperLimit){
9768 if(upperLimit>alpha)arg1 = 0.5D*(1.0D + Stat.erf(Math.log((upperLimit-alpha)/gamma)/(beta*Math.sqrt(2))));
9769 if(lowerLimit>alpha)arg2 = 0.5D*(1.0D + Stat.erf(Math.log((lowerLimit-alpha)/gamma)/(beta*Math.sqrt(2))));
9770 cdf = arg1 - arg2;
9771 }
9772
9773 return cdf;
9774 }
9775
9776
9777 // Log-Normal Inverse Cumulative Distribution Function
9778 // Three parameter
9779 public static double logNormalInverseCDF(double alpha, double beta, double gamma, double prob){
9780 if(prob<0.0 || prob>1.0)throw new IllegalArgumentException("Entered cdf value, " + prob + ", must lie between 0 and 1 inclusive");
9781
9782 double icdf = 0.0D;
9783
9784 if(prob==0.0){
9785 icdf = alpha;
9786 }
9787 else{
9788 if(prob==1.0){
9789 icdf = Double.POSITIVE_INFINITY;
9790 }
9791 else{
9792
9793 // Create instance of the class holding the Log-Normal cfd function
9794 LogNormalThreeParFunct lognorm = new LogNormalThreeParFunct();
9795
9796 // set function variables
9797 lognorm.alpha = alpha;
9798 lognorm.beta = beta;
9799 lognorm.gamma = gamma;
9800
9801 // required tolerance
9802 double tolerance = 1e-12;
9803
9804 // lower bound
9805 double lowerBound = alpha;
9806
9807 // upper bound
9808 double upperBound = Stat.logNormalThreeParMean(alpha, beta, gamma) + 5.0*Stat.logNormalThreeParStandardDeviation(alpha, beta, gamma);
9809
9810 // Create instance of RealRoot
9811 RealRoot realR = new RealRoot();
9812
9813 // Set extension limits
9814 realR.noLowerBoundExtension();
9815
9816 // Set tolerance
9817 realR.setTolerance(tolerance);
9818
9819 // Supress error messages and arrange for NaN to be returned as root if root not found
9820 realR.resetNaNexceptionToTrue();
9821 realR.supressLimitReachedMessage();
9822 realR.supressNaNmessage();
9823
9824 // set function cfd variable
9825 lognorm.cfd = prob;
9826
9827 // call root searching method
9828 icdf = realR.bisect(lognorm, lowerBound, upperBound);
9829 }
9830 }
9831
9832 return icdf;
9833 }
9834
9835 // Log-Normal Inverse Cumulative Distribution Function
9836 // Three parameter
9837 public static double logNormalThreeParInverseCDF(double alpha, double beta, double gamma, double prob){
9838 return logNormalInverseCDF(alpha, beta, gamma, prob);
9839 }
9840
9841
9842 // Three parameter log-normal probability density function
9843 public static double logNormalThreeParPDF(double alpha, double beta, double gamma, double x){
9844 if(beta<0)throw new IllegalArgumentException("The parameter beta, " + beta + ", must be greater than or equal to zero");
9845 if(x<=alpha){
9846 return 0.0D;
9847 }
9848 else{
9849 return Math.exp(-0.5D*Fmath.square(Math.log((x - alpha)/gamma)/beta))/((x - gamma)*beta*Math.sqrt(2.0D*Math.PI));
9850 }
9851 }
9852
9853
9854 // Returns an array of three parameter log-normal random deviates - clock seed
9855 public static double[] logNormalThreeParRand(double alpha, double beta, double gamma, int n){
9856 if(n<=0)throw new IllegalArgumentException("The number of random deviates required, " + n + ", must be greater than zero");
9857 if(beta<0)throw new IllegalArgumentException("The parameter beta, " + beta + ", must be greater than or equal to zero");
9858 PsRandom psr = new PsRandom();
9859 return psr.logNormalThreeParArray(alpha, beta, gamma, n);
9860 }
9861
9862 // Returns an array of three parameter log-normal random deviates - user supplied seed
9863 public static double[] logNormalThreeParRand(double alpha, double beta, double gamma, int n, long seed){
9864 if(n<=0)throw new IllegalArgumentException("The number of random deviates required, " + n + ", must be greater than zero");
9865 if(beta<0)throw new IllegalArgumentException("The parameter beta, " + beta + ", must be greater than or equal to zero");
9866 PsRandom psr = new PsRandom(seed);
9867 return psr.logNormalThreeParArray(alpha, beta, gamma, n);
9868 }
9869
9870
9871 // LogNormal order statistic medians (n points)
9872 // Three parametrs
9873 public static double[] logNormalOrderStatisticMedians(double alpha, double beta, double gamma, int n){
9874 double nn = (double)n;
9875 double[] lnosm = new double[n];
9876 double[] uosm = uniformOrderStatisticMedians(n);
9877 for(int i=0; i<n; i++){
9878 lnosm[i] =Stat.logNormalThreeParInverseCDF(alpha, beta, gamma, uosm[i]);
9879 }
9880 lnosm = Stat.scale(lnosm, Stat.logNormalThreeParMean(alpha, beta, gamma), Stat.logNormalThreeParStandardDeviation(alpha, beta, gamma));
9881 return lnosm;
9882 }
9883
9884 // LogNormal order statistic medians (n points)
9885 // Three parametrs
9886 public static double[] logNormalThreeParOrderStatisticMedians(double alpha, double beta, double gamma, int n){
9887 return Stat.logNormalOrderStatisticMedians(alpha, beta, gamma, n);
9888 }
9889
9890 // Three parameter log-normal mean
9891 public static double logNormalThreeParMean(double alpha, double beta, double gamma){
9892 return gamma*Math.exp(beta*beta/2.0D) + alpha;
9893 }
9894
9895 // Three parameter log-normal standard deviation
9896 public static double logNormalThreeParStandardDeviation(double alpha, double beta, double gamma){
9897 return logNormalThreeParStandDev(alpha, beta, gamma);
9898 }
9899
9900 // Three parameter log-normal standard deviation
9901 public static double logNormalThreeParStandDev(double alpha, double beta, double gamma){
9902 double beta2 = beta*beta;
9903 return Math.sqrt((Math.exp(beta2) - 1.0D)*Math.exp(2.0D*Math.log(gamma) + beta2));
9904 }
9905
9906 // Three parameter log-normal mode
9907 public static double logNormalThreeParMode(double alpha, double beta, double gamma){
9908 return gamma*Math.exp(- beta*beta) + alpha;
9909 }
9910
9911 // Three parameter log-normal median
9912 public static double logNormalThreeParMedian(double alpha, double gamma){
9913 return gamma + alpha;
9914 }
9915
9916
9917 // LOGISTIC DISTRIBUTION
9918 // TWO PARAMETERS (See below for three parameter distribution)
9919
9920 // Logistic cumulative distribution function
9921 // probability that a variate will assume a value less than the upperlimit
9922 // mu = location parameter, beta = scale parameter
9923 public static double logisticCDF(double mu, double beta, double upperlimit){
9924 return 0.5D*(1.0D + Math.tanh((upperlimit - mu)/(2.0D*beta)));
9925 }
9926
9927 // Logistic cumulative distribution function
9928 // probability that a variate will assume a value less than the upperlimit
9929 // mu = location parameter, beta = scale parameter
9930 public static double logisticTwoParCDF(double mu, double beta, double upperlimit){
9931 return 0.5D*(1.0D + Math.tanh((upperlimit - mu)/(2.0D*beta)));
9932 }
9933
9934
9935 // Logistic cumulative distribution function
9936 // probability that a variate will assume a value less than the upperlimit
9937 // mu = location parameter, beta = scale parameter
9938 public static double logisticProb(double mu, double beta, double upperlimit){
9939 return 0.5D*(1.0D + Math.tanh((upperlimit - mu)/(2.0D*beta)));
9940 }
9941
9942
9943 // Logistic cumulative distribution function
9944 // probability that a variate will assume a value between the lower and the upper limits
9945 // mu = location parameter, beta = scale parameter
9946 public static double logisticCDF(double mu, double beta, double lowerlimit, double upperlimit){
9947 double arg1 = 0.5D*(1.0D + Math.tanh((lowerlimit - mu)/(2.0D*beta)));
9948 double arg2 = 0.5D*(1.0D + Math.tanh((upperlimit - mu)/(2.0D*beta)));
9949 return arg2 - arg1;
9950 }
9951
9952 // Logistic cumulative distribution function
9953 // probability that a variate will assume a value between the lower and the upper limits
9954 // mu = location parameter, beta = scale parameter
9955 public static double logisticTwoParCDF(double mu, double beta, double lowerlimit, double upperlimit){
9956 double arg1 = 0.5D*(1.0D + Math.tanh((lowerlimit - mu)/(2.0D*beta)));
9957 double arg2 = 0.5D*(1.0D + Math.tanh((upperlimit - mu)/(2.0D*beta)));
9958 return arg2 - arg1;
9959 }
9960
9961 // Logistic cumulative distribution function
9962 // probability that a variate will assume a value between the lower and the upper limits
9963 // mu = location parameter, beta = scale parameter
9964 public static double logisticProb(double mu, double beta, double lowerlimit, double upperlimit){
9965 double arg1 = 0.5D*(1.0D + Math.tanh((lowerlimit - mu)/(2.0D*beta)));
9966 double arg2 = 0.5D*(1.0D + Math.tanh((upperlimit - mu)/(2.0D*beta)));
9967 return arg2 - arg1;
9968 }
9969
9970
9971 // Logistic Inverse Cumulative Density Function
9972 public static double logisticTwoParInverseCDF(double mu, double beta, double prob){
9973 return logisticInverseCDF(mu, beta, prob);
9974 }
9975
9976 // Logistic Inverse Cumulative Density Function
9977 public static double logisticInverseCDF(double mu, double beta, double prob){
9978 if(prob<0.0 || prob>1.0)throw new IllegalArgumentException("Entered cdf value, " + prob + ", must lie between 0 and 1 inclusive");
9979 double icdf = 0.0D;
9980
9981 if(prob==0.0){
9982 icdf = Double.NEGATIVE_INFINITY;
9983 }
9984 else{
9985 if(prob==1.0){
9986 icdf = Double.POSITIVE_INFINITY;
9987 }
9988 else{
9989 icdf = mu - beta*Math.log(1.0/prob - 1.0);
9990 }
9991 }
9992
9993 return icdf;
9994 }
9995
9996 // Logistic probability density function density function
9997 // mu = location parameter, beta = scale parameter
9998 public static double logisticPDF(double mu, double beta, double x){
9999 return Fmath.square(Fmath.sech((x - mu)/(2.0D*beta)))/(4.0D*beta);
10000 }
10001
10002 // Logistic probability density function density function
10003 // mu = location parameter, beta = scale parameter
10004 public static double logisticTwoParPDF(double mu, double beta, double x){
10005 return Fmath.square(Fmath.sech((x - mu)/(2.0D*beta)))/(4.0D*beta);
10006 }
10007 // Logistic probability density function
10008 // mu = location parameter, beta = scale parameter
10009 public static double logistic(double mu, double beta, double x){
10010 return Fmath.square(Fmath.sech((x - mu)/(2.0D*beta)))/(4.0D*beta);
10011 }
10012
10013 // Returns an array of logistic distribution random deviates - clock seed
10014 // mu = location parameter, beta = scale parameter
10015 public static double[] logisticTwoParRand(double mu, double beta, int n){
10016 return logisticRand(mu, beta, n);
10017 }
10018
10019 // Returns an array of logistic distribution random deviates - clock seed
10020 // mu = location parameter, beta = scale parameter
10021 public static double[] logisticRand(double mu, double beta, int n){
10022 double[] ran = new double[n];
10023 Random rr = new Random();
10024 for(int i=0; i<n; i++){
10025 ran[i] = 2.0D*beta*Fmath.atanh(2.0D*rr.nextDouble() - 1.0D) + mu;
10026 }
10027 return ran;
10028 }
10029
10030 // Returns an array of Logistic random deviates - user provided seed
10031 // mu = location parameter, beta = scale parameter
10032 public static double[] logisticTwoParRand(double mu, double beta, int n, long seed){
10033 return logisticRand(mu, beta, n, seed);
10034 }
10035
10036
10037 // Returns an array of Logistic random deviates - user provided seed
10038 // mu = location parameter, beta = scale parameter
10039 public static double[] logisticRand(double mu, double beta, int n, long seed){
10040 double[] ran = new double[n];
10041 Random rr = new Random(seed);
10042 for(int i=0; i<n; i++){
10043 ran[i] = 2.0D*beta*Fmath.atanh(2.0D*rr.nextDouble() - 1.0D) + mu;
10044 }
10045 return ran;
10046 }
10047
10048 // Logistic order statistic medians (n points)
10049 public static double[] logisticOrderStatisticMedians(double mu, double beta, int n){
10050 double nn = (double)n;
10051 double[] losm = new double[n];
10052 double[] uosm = uniformOrderStatisticMedians(n);
10053 for(int i=0; i<n; i++){
10054 losm[i] = Stat.logisticInverseCDF(mu, beta, uosm[i]);
10055 }
10056 return losm;
10057 }
10058
10059 // Logistic order statistic medians (n points)
10060 public static double[] logisticTwoParOrderStatisticMedians(double mu, double beta, int n){
10061 double nn = (double)n;
10062 double[] losm = new double[n];
10063 double[] uosm = uniformOrderStatisticMedians(n);
10064 for(int i=0; i<n; i++){
10065 losm[i] = Stat.logisticInverseCDF(mu, beta, uosm[i]);
10066 }
10067 return losm;
10068 }
10069
10070 // Logistic distribution mean
10071 public static double logisticMean(double mu){
10072 return mu;
10073 }
10074
10075 // Logistic distribution mean
10076 public static double logisticTwoParMean(double mu){
10077 return mu;
10078 }
10079
10080 // Logistic distribution standard deviation
10081 public static double logisticStandardDeviation(double beta){
10082 return logisticStandDev(beta);
10083 }
10084
10085 // Logistic distribution standard deviation
10086 public static double logisticStandDev(double beta){
10087 return Math.sqrt(Fmath.square(Math.PI*beta)/3.0D);
10088 }
10089
10090 // Logistic distribution standard deviation
10091 public static double logisticTwoParStandardDeviation(double beta){
10092 return Math.sqrt(Fmath.square(Math.PI*beta)/3.0D);
10093 }
10094
10095 // Logistic distribution mode
10096 public static double logisticMode(double mu){
10097 return mu;
10098 }
10099
10100 // Logistic distribution mode
10101 public static double logisticTwoParMode(double mu){
10102 return mu;
10103 }
10104
10105 // Logistic distribution median
10106 public static double logisticMedian(double mu){
10107 return mu;
10108 }
10109
10110 // Logistic distribution median
10111 public static double logisticTwoParMedian(double mu){
10112 return mu;
10113 }
10114
10115
10116 // LORENTZIAN DISTRIBUTION (CAUCHY DISTRIBUTION)
10117
10118 // Lorentzian cumulative distribution function
10119 // probability that a variate will assume a value less than the upperlimit
10120 public static double lorentzianProb(double mu, double gamma, double upperlimit){
10121 double arg = (upperlimit - mu)/(gamma/2.0D);
10122 return (1.0D/Math.PI)*(Math.atan(arg)+Math.PI/2.0);
10123 }
10124 // Lorentzian cumulative distribution function
10125 // probability that a variate will assume a value between the lower and the upper limits
10126 public static double lorentzianCDF(double mu, double gamma, double lowerlimit, double upperlimit){
10127 double arg1 = (upperlimit - mu)/(gamma/2.0D);
10128 double arg2 = (lowerlimit - mu)/(gamma/2.0D);
10129 return (1.0D/Math.PI)*(Math.atan(arg1)-Math.atan(arg2));
10130 }
10131
10132 // Lorentzian cumulative distribution function
10133 // probability that a variate will assume a value between the lower and the upper limits
10134 public static double lorentzianProb(double mu, double gamma, double lowerlimit, double upperlimit){
10135 double arg1 = (upperlimit - mu)/(gamma/2.0D);
10136 double arg2 = (lowerlimit - mu)/(gamma/2.0D);
10137 return (1.0D/Math.PI)*(Math.atan(arg1)-Math.atan(arg2));
10138 }
10139
10140 // Lorentzian Inverse Cumulative Density Function
10141 public static double lorentzianInverseCDF(double mu, double gamma, double prob){
10142 if(prob<0.0 || prob>1.0)throw new IllegalArgumentException("Entered cdf value, " + prob + ", must lie between 0 and 1 inclusive");
10143 double icdf = 0.0D;
10144
10145 if(prob==0.0){
10146 icdf = Double.NEGATIVE_INFINITY;
10147 }
10148 else{
10149 if(prob==1.0){
10150 icdf = Double.POSITIVE_INFINITY;
10151 }
10152 else{
10153 icdf = mu + gamma*Math.tan(Math.PI*(prob - 0.5))/2.0;
10154 }
10155 }
10156
10157 return icdf;
10158 }
10159
10160 // Lorentzian probability density function
10161 public static double lorentzianPDF(double mu, double gamma, double x){
10162 double arg =gamma/2.0D;
10163 return (1.0D/Math.PI)*arg/(Fmath.square(mu-x)+arg*arg);
10164 }
10165
10166 // Lorentzian probability density function
10167 public static double lorentzian(double mu, double gamma, double x){
10168 double arg =gamma/2.0D;
10169 return (1.0D/Math.PI)*arg/(Fmath.square(mu-x)+arg*arg);
10170 }
10171
10172
10173 // Returns an array of Lorentzian random deviates - clock seed
10174 // mu = the mean, gamma = half-height width, length of array
10175 public static double[] lorentzianRand(double mu, double gamma, int n){
10176 double[] ran = new double[n];
10177 Random rr = new Random();
10178 for(int i=0; i<n; i++){
10179 ran[i]=Math.tan((rr.nextDouble()-0.5)*Math.PI);
10180 ran[i] = ran[i]*gamma/2.0 + mu;
10181 }
10182 return ran;
10183 }
10184
10185 // Returns an array of Lorentzian random deviates - user provided seed
10186 // mu = the mean, gamma = half-height width, length of array
10187 public static double[] lorentzianRand(double mu, double gamma, int n, long seed){
10188 double[] ran = new double[n];
10189 Random rr = new Random(seed);
10190 for(int i=0; i<n; i++){
10191 ran[i]=Math.tan((rr.nextDouble()-0.5)*Math.PI);
10192 ran[i] = ran[i]*gamma/2.0 + mu;
10193 }
10194 return ran;
10195 }
10196
10197 // Lorentzian order statistic medians (n points)
10198 public static double[] lorentzianOrderStatisticMedians(double mu, double gamma, int n){
10199 double nn = (double)n;
10200 double[] losm = new double[n];
10201 double[] uosm = uniformOrderStatisticMedians(n);
10202 for(int i=0; i<n; i++){
10203 losm[i] = Stat.lorentzianInverseCDF(mu, gamma, uosm[i]);
10204 }
10205 return losm;
10206 }
10207
10208 // POISSON DISTRIBUTION
10209
10210 // Poisson Cumulative Distribution Function
10211 // probability that a number of Poisson random events will occur between 0 and k (inclusive)
10212 // k is an integer greater than equal to 1
10213 // mean = mean of the Poisson distribution
10214 public static double poissonCDF(int k, double mean){
10215 if(k<1)throw new IllegalArgumentException("k must be an integer greater than or equal to 1");
10216 return Stat.incompleteGammaComplementary((double) k, mean);
10217 }
10218
10219 // Poisson Cumulative Distribution Function
10220 // probability that a number of Poisson random events will occur between 0 and k (inclusive)
10221 // k is an integer greater than equal to 1
10222 // mean = mean of the Poisson distribution
10223 public static double poissonProb(int k, double mean){
10224 if(k<1)throw new IllegalArgumentException("k must be an integer greater than or equal to 1");
10225 return Stat.incompleteGammaComplementary((double) k, mean);
10226 }
10227
10228 // Poisson Probability Density Function
10229 // k is an integer greater than or equal to zero
10230 // mean = mean of the Poisson distribution
10231 public static double poissonPDF(int k, double mean){
10232 if(k<0)throw new IllegalArgumentException("k must be an integer greater than or equal to 0");
10233 return Math.pow(mean, k)*Math.exp(-mean)/Stat.factorial((double)k);
10234 }
10235
10236 // Poisson Probability Density Function
10237 // k is an integer greater than or equal to zero
10238 // mean = mean of the Poisson distribution
10239 public static double poisson(int k, double mean){
10240 if(k<0)throw new IllegalArgumentException("k must be an integer greater than or equal to 0");
10241 return Math.pow(mean, k)*Math.exp(-mean)/Stat.factorial((double)k);
10242 }
10243
10244 // Returns an array of Poisson random deviates - clock seed
10245 // mean = the mean, n = length of array
10246 // follows the ideas of Numerical Recipes
10247 public static double[] poissonRand(double mean, int n){
10248
10249 Random rr = new Random();
10250 double[] ran = poissonRandCalc(rr, mean, n);
10251 return ran;
10252 }
10253
10254 // Returns an array of Poisson random deviates - user provided seed
10255 // mean = the mean, n = length of array
10256 // follows the ideas of Numerical Recipes
10257 public static double[] poissonRand(double mean, int n, long seed){
10258
10259 Random rr = new Random(seed);
10260 double[] ran = poissonRandCalc(rr, mean, n);
10261 return ran;
10262 }
10263
10264 // Calculates and returns an array of Poisson random deviates
10265 private static double[] poissonRandCalc(Random rr, double mean, int n){
10266 double[] ran = new double[n];
10267 double oldm = -1.0D;
10268 double expt = 0.0D;
10269 double em = 0.0D;
10270 double term = 0.0D;
10271 double sq = 0.0D;
10272 double lnMean = 0.0D;
10273 double yDev = 0.0D;
10274
10275 if(mean < 12.0D){
10276 for(int i=0; i<n; i++){
10277 if(mean != oldm){
10278 oldm = mean;
10279 expt = Math.exp(-mean);
10280 }
10281 em = -1.0D;
10282 term = 1.0D;
10283 do{
10284 ++em;
10285 term *= rr.nextDouble();
10286 }while(term>expt);
10287 ran[i] = em;
10288 }
10289 }
10290 else{
10291 for(int i=0; i<n; i++){
10292 if(mean != oldm){
10293 oldm = mean;
10294 sq = Math.sqrt(2.0D*mean);
10295 lnMean = Math.log(mean);
10296 expt = lnMean - Stat.logGamma(mean+1.0D);
10297 }
10298 do{
10299 do{
10300 yDev = Math.tan(Math.PI*rr.nextDouble());
10301 em = sq*yDev+mean;
10302 }while(em<0.0D);
10303 em = Math.floor(em);
10304 term = 0.9D*(1.0D+yDev*yDev)*Math.exp(em*lnMean - Stat.logGamma(em+1.0D)-expt);
10305 }while(rr.nextDouble()>term);
10306 ran[i] = em;
10307 }
10308 }
10309 return ran;
10310 }
10311
10312
10313 // CHI SQUARE DISTRIBUTION AND CHI SQUARE FUNCTIONS
10314
10315 // Chi-Square Cumulative Distribution Function
10316 // probability that an observed chi-square value for a correct model should be less than chiSquare
10317 // nu = the degrees of freedom
10318 public static double chiSquareCDF(double chiSquare, int nu){
10319 if(nu<=0)throw new IllegalArgumentException("The degrees of freedom [nu], " + nu + ", must be greater than zero");
10320 return Stat.incompleteGamma((double)nu/2.0D, chiSquare/2.0D);
10321 }
10322
10323 // retained for compatability
10324 public static double chiSquareProb(double chiSquare, int nu){
10325 if(nu<=0)throw new IllegalArgumentException("The degrees of freedom [nu], " + nu + ", must be greater than zero");
10326 return Stat.incompleteGamma((double)nu/2.0D, chiSquare/2.0D);
10327 }
10328
10329 // Chi-Square Probability Density Function
10330 // nu = the degrees of freedom
10331 public static double chiSquarePDF(double chiSquare, int nu){
10332 if(nu<=0)throw new IllegalArgumentException("The degrees of freedom [nu], " + nu + ", must be greater than zero");
10333 double dnu = (double) nu;
10334 return Math.pow(0.5D, dnu/2.0D)*Math.pow(chiSquare, dnu/2.0D - 1.0D)*Math.exp(-chiSquare/2.0D)/Stat.gammaFunction(dnu/2.0D);
10335 }
10336
10337 // Returns an array of Chi-Square random deviates - clock seed
10338 public static double[] chiSquareRand(int nu, int n){
10339 if(nu<=0)throw new IllegalArgumentException("The degrees of freedom [nu], " + nu + ", must be greater than zero");
10340 PsRandom psr = new PsRandom();
10341 return psr.chiSquareArray(nu, n);
10342 }
10343
10344
10345 // Returns an array of Chi-Square random deviates - user supplied seed
10346 public static double[] chiSquareRand(int nu, int n, long seed){
10347 if(nu<=0)throw new IllegalArgumentException("The degrees of freedom [nu], " + nu + ", must be greater than zero");
10348 PsRandom psr = new PsRandom(seed);
10349 return psr.chiSquareArray(nu, n);
10350 }
10351
10352 // Chi-Square Distribution Mean
10353 // nu = the degrees of freedom
10354 public static double chiSquareMean(int nu){
10355 if(nu<=0)throw new IllegalArgumentException("The degrees of freedom [nu], " + nu + ", must be greater than zero");
10356 return (double)nu;
10357 }
10358
10359 // Chi-Square Distribution Mean
10360 // nu = the degrees of freedom
10361 public static double chiSquareMode(int nu){
10362 if(nu<=0)throw new IllegalArgumentException("The degrees of freedom [nu], " + nu + ", must be greater than zero");
10363 double mode = 0.0D;
10364 if(nu>=2)mode = (double)nu - 2.0D;
10365 return mode;
10366 }
10367
10368 // Chi-Square Distribution Standard Deviation
10369 // nu = the degrees of freedom
10370 public static double chiSquareStandardDeviation(int nu){
10371 return chiSquareStandDev(nu);
10372 }
10373
10374
10375 // Chi-Square Distribution Standard Deviation
10376 // nu = the degrees of freedom
10377 public static double chiSquareStandDev(int nu){
10378 if(nu<=0)throw new IllegalArgumentException("The degrees of freedom [nu], " + nu + ", must be greater than zero");
10379 double dnu = (double) nu;
10380 return Math.sqrt(2.0D*dnu);
10381 }
10382
10383 // Chi-Square Statistic
10384 public static double chiSquare(double[] observed, double[] expected, double[] variance){
10385 int nObs = observed.length;
10386 int nExp = expected.length;
10387 int nVar = variance.length;
10388 if(nObs!=nExp)throw new IllegalArgumentException("observed array length does not equal the expected array length");
10389 if(nObs!=nVar)throw new IllegalArgumentException("observed array length does not equal the variance array length");
10390 double chi = 0.0D;
10391 for(int i=0; i<nObs; i++){
10392 chi += Fmath.square(observed[i]-expected[i])/variance[i];
10393 }
10394 return chi;
10395 }
10396
10397 // Chi-Square Statistic for Poisson distribution for frequency data
10398 // and Poisson distribution for each bin
10399 // double arguments
10400 public static double chiSquareFreq(double[] observedFreq, double[] expectedFreq){
10401 int nObs = observedFreq.length;
10402 int nExp = expectedFreq.length;
10403 if(nObs!=nExp)throw new IllegalArgumentException("observed array length does not equal the expected array length");
10404 double chi = 0.0D;
10405 for(int i=0; i<nObs; i++){
10406 chi += Fmath.square(observedFreq[i]-expectedFreq[i])/expectedFreq[i];
10407 }
10408 return chi;
10409 }
10410
10411 // Chi-Square Statistic for Poisson distribution for frequency data
10412 // and Poisson distribution for each bin
10413 // int arguments
10414 public static double chiSquareFreq(int[] observedFreq, int[] expectedFreq){
10415 int nObs = observedFreq.length;
10416 int nExp = expectedFreq.length;
10417 if(nObs!=nExp)throw new IllegalArgumentException("observed array length does not equal the expected array length");
10418 double[] observ = new double[nObs];
10419 double[] expect = new double[nObs];
10420 for(int i=0; i<nObs; i++){
10421 observ[i] = observedFreq[i];
10422 expect[i] = expectedFreq[i];
10423 }
10424
10425 return chiSquareFreq(observ, expect);
10426 }
10427
10428
10429 // BINOMIAL DISTRIBUTION AND BINOMIAL COEFFICIENTS
10430
10431 // Returns the binomial cumulative distribution function
10432 public static double binomialCDF(double p, int n, int k){
10433 if(p<0.0D || p>1.0D)throw new IllegalArgumentException("\np must lie between 0 and 1");
10434 if(k<0 || n<0)throw new IllegalArgumentException("\nn and k must be greater than or equal to zero");
10435 if(k>n)throw new IllegalArgumentException("\nk is greater than n");
10436 return Stat.regularisedBetaFunction(k, n-k+1, p);
10437 }
10438 // Returns the binomial cumulative distribution function
10439 public static double binomialProb(double p, int n, int k){
10440 if(p<0.0D || p>1.0D)throw new IllegalArgumentException("\np must lie between 0 and 1");
10441 if(k<0 || n<0)throw new IllegalArgumentException("\nn and k must be greater than or equal to zero");
10442 if(k>n)throw new IllegalArgumentException("\nk is greater than n");
10443 return Stat.regularisedBetaFunction(k, n-k+1, p);
10444 }
10445
10446 // Returns a binomial mass probabilty function
10447 public static double binomialPDF(double p, int n, int k){
10448 if(k<0 || n<0)throw new IllegalArgumentException("\nn and k must be greater than or equal to zero");
10449 if(k>n)throw new IllegalArgumentException("\nk is greater than n");
10450 return Math.floor(0.5D + Math.exp(Stat.logFactorial(n) - Stat.logFactorial(k) - Stat.logFactorial(n-k)))*Math.pow(p, k)*Math.pow(1.0D - p, n - k);
10451 }
10452
10453 // Returns a binomial mass probabilty function
10454 public static double binomial(double p, int n, int k){
10455 if(k<0 || n<0)throw new IllegalArgumentException("\nn and k must be greater than or equal to zero");
10456 if(k>n)throw new IllegalArgumentException("\nk is greater than n");
10457 return Math.floor(0.5D + Math.exp(Stat.logFactorial(n) - Stat.logFactorial(k) - Stat.logFactorial(n-k)))*Math.pow(p, k)*Math.pow(1.0D - p, n - k);
10458 }
10459
10460 // Returns a binomial Coefficient as a double
10461 public static double binomialCoeff(int n, int k){
10462 if(k<0 || n<0)throw new IllegalArgumentException("\nn and k must be greater than or equal to zero");
10463 if(k>n)throw new IllegalArgumentException("\nk is greater than n");
10464 return Math.floor(0.5D + Math.exp(Stat.logFactorial(n) - Stat.logFactorial(k) - Stat.logFactorial(n-k)));
10465 }
10466
10467 // Returns an array of n Binomial pseudorandom deviates from a binomial - clock seed
10468 // distribution of nTrial trials each of probablity, prob,
10469 // after bndlev Numerical Recipes in C - W.H. Press et al. (Cambridge)
10470 // 2nd edition 1992 p295.
10471 public double[] binomialRand(double prob, int nTrials, int n){
10472
10473 if(nTrials<n)throw new IllegalArgumentException("Number of deviates requested, " + n + ", must be less than the number of trials, " + nTrials);
10474 if(prob<0.0D || prob>1.0D)throw new IllegalArgumentException("The probablity provided, " + prob + ", must lie between 0 and 1)");
10475
10476 double[] ran = new double[n]; // array of deviates to be returned
10477 Random rr = new Random(); // instance of Random
10478
10479 double binomialDeviate = 0.0D; // the binomial deviate to be returned
10480 double deviateMean = 0.0D; // mean of deviate to be produced
10481 double testDeviate = 0.0D; // test deviate
10482 double workingProb = 0.0; // working value of the probability
10483 double logProb = 0.0; // working value of the probability
10484 double probOld = -1.0D; // previous value of the working probability
10485 double probC = -1.0D; // complementary value of the working probability
10486 double logProbC = -1.0D; // log of the complementary value of the working probability
10487 int nOld= -1; // previous value of trials counter
10488 double enTrials = 0.0D; // (double) trials counter
10489 double oldGamma = 0.0D; // a previous log Gamma function value
10490 double tanW = 0.0D; // a working tangent
10491 double hold0 = 0.0D; // a working holding variable
10492 int jj; // counter
10493
10494 double probOriginalValue = prob;
10495 for(int i=0; i<n; i++){
10496 prob = probOriginalValue;
10497 workingProb=(prob <= 0.5D ? prob : 1.0-prob); // distribution invariant on swapping prob for 1 - prob
10498 deviateMean = nTrials*workingProb;
10499
10500 if(nTrials < 25) {
10501 // if number of trials greater than 25 use direct method
10502 binomialDeviate=0.0D;
10503 for(jj=1;jj<=nTrials;jj++)if (rr.nextDouble() < workingProb) ++binomialDeviate;
10504 }
10505 else if(deviateMean < 1.0D) {
10506 // if fewer than 1 out of 25 events - Poisson approximation is accurate
10507 double expOfMean=Math.exp(-deviateMean);
10508 testDeviate=1.0D;
10509 for (jj=0;jj<=nTrials;jj++) {
10510 testDeviate *= rr.nextDouble();
10511 if (testDeviate < expOfMean) break;
10512 }
10513 binomialDeviate=(jj <= nTrials ? jj : nTrials);
10514
10515 }
10516 else{
10517 // use rejection method
10518 if(nTrials != nOld) {
10519 // if nTrials has changed compute useful quantities
10520 enTrials = (double)nTrials;
10521 oldGamma = Stat.logGamma(enTrials + 1.0D);
10522 nOld = nTrials;
10523 }
10524 if(workingProb != probOld) {
10525 // if workingProb has changed compute useful quantities
10526 probC = 1.0 - workingProb;
10527 logProb = Math.log(workingProb);
10528 logProbC = Math.log(probC);
10529 probOld = workingProb;
10530 }
10531
10532 double sq = Math.sqrt(2.0*deviateMean*probC);
10533 do{
10534 do{
10535 double angle = Math.PI*rr.nextDouble();
10536 tanW = Math.tan(angle);
10537 hold0 = sq*tanW + deviateMean;
10538 }while(hold0 < 0.0D || hold0 >= (enTrials + 1.0D)); //rejection test
10539 hold0 = Math.floor(hold0); // integer value distribution
10540 testDeviate = 1.2D*sq*(1.0D + tanW*tanW)*Math.exp(oldGamma - Stat.logGamma(hold0 + 1.0D) - Stat.logGamma(enTrials - hold0 + 1.0D) + hold0*logProb + (enTrials - hold0)*logProbC);
10541 }while(rr.nextDouble() > testDeviate); // rejection test
10542 binomialDeviate=hold0;
10543 }
10544
10545 if(workingProb != prob) binomialDeviate = nTrials - binomialDeviate; // symmetry transformation
10546
10547 ran[i] = binomialDeviate;
10548 }
10549
10550 return ran;
10551 }
10552
10553 // Returns an array of n Binomial pseudorandom deviates from a binomial - user supplied seed
10554 // distribution of nTrial trials each of probablity, prob,
10555 // after bndlev Numerical Recipes in C - W.H. Press et al. (Cambridge)
10556 // 2nd edition 1992 p295.
10557 public double[] binomialRand(double prob, int nTrials, int n, long seed){
10558
10559 if(nTrials<n)throw new IllegalArgumentException("Number of deviates requested, " + n + ", must be less than the number of trials, " + nTrials);
10560 if(prob<0.0D || prob>1.0D)throw new IllegalArgumentException("The probablity provided, " + prob + ", must lie between 0 and 1)");
10561
10562 double[] ran = new double[n]; // array of deviates to be returned
10563 Random rr = new Random(seed); // instance of Random
10564
10565 double binomialDeviate = 0.0D; // the binomial deviate to be returned
10566 double deviateMean = 0.0D; // mean of deviate to be produced
10567 double testDeviate = 0.0D; // test deviate
10568 double workingProb = 0.0; // working value of the probability
10569 double logProb = 0.0; // working value of the probability
10570 double probOld = -1.0D; // previous value of the working probability
10571 double probC = -1.0D; // complementary value of the working probability
10572 double logProbC = -1.0D; // log of the complementary value of the working probability
10573 int nOld= -1; // previous value of trials counter
10574 double enTrials = 0.0D; // (double) trials counter
10575 double oldGamma = 0.0D; // a previous log Gamma function value
10576 double tanW = 0.0D; // a working tangent
10577 double hold0 = 0.0D; // a working holding variable
10578 int jj; // counter
10579
10580 double probOriginalValue = prob;
10581 for(int i=0; i<n; i++){
10582 prob = probOriginalValue;
10583 workingProb=(prob <= 0.5D ? prob : 1.0-prob); // distribution invariant on swapping prob for 1 - prob
10584 deviateMean = nTrials*workingProb;
10585
10586 if(nTrials < 25) {
10587 // if number of trials greater than 25 use direct method
10588 binomialDeviate=0.0D;
10589 for(jj=1;jj<=nTrials;jj++)if (rr.nextDouble() < workingProb) ++binomialDeviate;
10590 }
10591 else if(deviateMean < 1.0D) {
10592 // if fewer than 1 out of 25 events - Poisson approximation is accurate
10593 double expOfMean=Math.exp(-deviateMean);
10594 testDeviate=1.0D;
10595 for (jj=0;jj<=nTrials;jj++) {
10596 testDeviate *= rr.nextDouble();
10597 if (testDeviate < expOfMean) break;
10598 }
10599 binomialDeviate=(jj <= nTrials ? jj : nTrials);
10600
10601 }
10602 else{
10603 // use rejection method
10604 if(nTrials != nOld) {
10605 // if nTrials has changed compute useful quantities
10606 enTrials = (double)nTrials;
10607 oldGamma = Stat.logGamma(enTrials + 1.0D);
10608 nOld = nTrials;
10609 }
10610 if(workingProb != probOld) {
10611 // if workingProb has changed compute useful quantities
10612 probC = 1.0 - workingProb;
10613 logProb = Math.log(workingProb);
10614 logProbC = Math.log(probC);
10615 probOld = workingProb;
10616 }
10617
10618 double sq = Math.sqrt(2.0*deviateMean*probC);
10619 do{
10620 do{
10621 double angle = Math.PI*rr.nextDouble();
10622 tanW = Math.tan(angle);
10623 hold0 = sq*tanW + deviateMean;
10624 }while(hold0 < 0.0D || hold0 >= (enTrials + 1.0D)); //rejection test
10625 hold0 = Math.floor(hold0); // integer value distribution
10626 testDeviate = 1.2D*sq*(1.0D + tanW*tanW)*Math.exp(oldGamma - Stat.logGamma(hold0 + 1.0D) - Stat.logGamma(enTrials - hold0 + 1.0D) + hold0*logProb + (enTrials - hold0)*logProbC);
10627 }while(rr.nextDouble() > testDeviate); // rejection test
10628 binomialDeviate=hold0;
10629 }
10630
10631 if(workingProb != prob) binomialDeviate = nTrials - binomialDeviate; // symmetry transformation
10632
10633 ran[i] = binomialDeviate;
10634 }
10635
10636 return ran;
10637 }
10638
10639
10640 // F-DISTRIBUTION AND F-TEST
10641
10642 // Returns the F-distribution probabilty for degrees of freedom df1, df2
10643 // F ratio provided
10644 public static double fCompCDF(double fValue, int df1, int df2){
10645 if(df1<=0)throw new IllegalArgumentException("the degrees of freedom, nu1, " + df1 + ", must be greater than zero");
10646 if(df2<=0)throw new IllegalArgumentException("the degrees of freedom, nu2, " + df2 + ", must be greater than zero");
10647 if(fValue<0)throw new IllegalArgumentException("the F-ratio, " + fValue + ", must be greater than or equal to zero");
10648 double ddf1 = (double)df1;
10649 double ddf2 = (double)df2;
10650 double x = ddf2/(ddf2+ddf1*fValue);
10651 return Stat.regularisedBetaFunction(df2/2.0D, df1/2.0D, x);
10652 }
10653
10654 // retained fot compatibility
10655 public static double fTestProb(double fValue, int df1, int df2){
10656 if(df1<=0)throw new IllegalArgumentException("the degrees of freedom, nu1, " + df1 + ", must be greater than zero");
10657 if(df2<=0)throw new IllegalArgumentException("the degrees of freedom, nu2, " + df2 + ", must be greater than zero");
10658 if(fValue<0)throw new IllegalArgumentException("the F-ratio, " + fValue + ", must be greater than or equal to zero");
10659 double ddf1 = (double)df1;
10660 double ddf2 = (double)df2;
10661 double x = ddf2/(ddf2+ddf1*fValue);
10662 return Stat.regularisedBetaFunction(df2/2.0D, df1/2.0D, x);
10663 }
10664
10665 // Returns the F-distribution probabilty for degrees of freedom df1, df2
10666 // numerator and denominator variances provided
10667 public static double fCompCDF(double var1, int df1, double var2, int df2){
10668 if(df1<=0)throw new IllegalArgumentException("the degrees of freedom, nu1, " + df1 + ", must be greater than zero");
10669 if(df2<=0)throw new IllegalArgumentException("the degrees of freedom, nu2, " + df2 + ", must be greater than zero");
10670 if(var1<0)throw new IllegalArgumentException("the variance, var1" + var1 + ", must be greater than or equal to zero");
10671 if(var1<=0)throw new IllegalArgumentException("the variance, var2" + var2 + ", must be greater than zero");
10672 double fValue = var1/var2;
10673 double ddf1 = (double)df1;
10674 double ddf2 = (double)df2;
10675 double x = ddf2/(ddf2+ddf1*fValue);
10676 return Stat.regularisedBetaFunction(df2/2.0D, df1/2.0D, x);
10677 }
10678
10679 // retained fot compatibility
10680 public static double fTestProb(double var1, int df1, double var2, int df2){
10681 if(df1<=0)throw new IllegalArgumentException("the degrees of freedom, nu1, " + df1 + ", must be greater than zero");
10682 if(df2<=0)throw new IllegalArgumentException("the degrees of freedom, nu2, " + df2 + ", must be greater than zero");
10683 if(var1<0)throw new IllegalArgumentException("the variance, var1" + var1 + ", must be greater than or equal to zero");
10684 if(var1<=0)throw new IllegalArgumentException("the variance, var2" + var2 + ", must be greater than zero");
10685 double fValue = var1/var2;
10686 double ddf1 = (double)df1;
10687 double ddf2 = (double)df2;
10688 double x = ddf2/(ddf2+ddf1*fValue);
10689 return Stat.regularisedBetaFunction(df2/2.0D, df1/2.0D, x);
10690 }
10691
10692
10693 // F-distribution Inverse Cumulative Distribution Function
10694 public static double fDistributionInverseCDF(int nu1, int nu2, double prob){
10695 if(prob<0.0 || prob>1.0)throw new IllegalArgumentException("Entered cdf value, " + prob + ", must lie between 0 and 1 inclusive");
10696
10697 double icdf = 0.0D;
10698
10699 if(prob==0.0){
10700 icdf = 0.0;
10701 }
10702 else{
10703 if(prob==1.0){
10704 icdf = Double.POSITIVE_INFINITY;
10705 }
10706 else{
10707
10708 // Create instance of the class holding the F-distribution cfd function
10709 FdistribtionFunct fdistn = new FdistribtionFunct();
10710
10711 // set function variables
10712 fdistn.nu1 = nu1;
10713 fdistn.nu2 = nu2;
10714
10715 // required tolerance
10716 double tolerance = 1e-12;
10717
10718 // lower bound
10719 double lowerBound = 0.0;
10720
10721 // upper bound
10722 double upperBound = 2.0;
10723
10724 // Create instance of RealRoot
10725 RealRoot realR = new RealRoot();
10726
10727 // Set extension limits
10728 realR.noLowerBoundExtension();
10729
10730 // Set tolerance
10731 realR.setTolerance(tolerance);
10732
10733 // Supress error messages and arrange for NaN to be returned as root if root not found
10734 realR.resetNaNexceptionToTrue();
10735 realR.supressLimitReachedMessage();
10736 realR.supressNaNmessage();
10737
10738 // set function cfd variable
10739 fdistn.cfd = prob;
10740
10741 // call root searching method
10742 icdf = realR.bisect(fdistn, lowerBound, upperBound);
10743 }
10744 }
10745 return icdf;
10746 }
10747
10748
10749 // F-distribution order statistic medians (n points)
10750 public static double[] fDistributionOrderStatisticMedians(int nu1, int nu2, int n){
10751 double nn = (double)n;
10752 double[] gosm = new double[n];
10753 double[] uosm = uniformOrderStatisticMedians(n);
10754 for(int i=0; i<n; i++){
10755 gosm[i] =Stat.fDistributionInverseCDF(nu1, nu2, uosm[i]);
10756 }
10757 Stat st = new Stat(gosm);
10758 double mean = st.mean();
10759 double sigma = st.standardDeviation();
10760 gosm = Stat.scale(gosm, mean, sigma);
10761 return gosm;
10762 }
10763
10764
10765 // Returns the F-test value corresponding to a F-distribution probabilty, fProb,
10766 // for degrees of freedom df1, df2
10767 public static double fTestValueGivenFprob(double fProb, int df1, int df2){
10768
10769 // Create an array F-test value array
10770 int fTestsNum = 100; // length of array
10771 double[] fTestValues = new double[fTestsNum];
10772 fTestValues[0]=0.0001D; // lowest array value
10773 fTestValues[fTestsNum-1]=10000.0D; // highest array value
10774 // calculate array increment - log scale
10775 double diff = (Fmath.log10(fTestValues[fTestsNum-1])-Fmath.log10(fTestValues[0]))/(fTestsNum-1);
10776 // Fill array
10777 for(int i=1; i<fTestsNum-1; i++){
10778 fTestValues[i] = Math.pow(10.0D,(Fmath.log10(fTestValues[i-1])+diff));
10779 }
10780
10781 // calculate F test probability array corresponding to F-test value array
10782 double[] fTestProb = new double[fTestsNum];
10783 for(int i=0; i<fTestsNum; i++){
10784 fTestProb[i] = Stat.fTestProb(fTestValues[i], df1, df2);
10785 }
10786
10787 // calculate F-test value for provided probability
10788 // using bisection procedure
10789 double fTest0 = 0.0D;
10790 double fTest1 = 0.0D;
10791 double fTest2 = 0.0D;
10792
10793 // find bracket for the F-test probabilities and calculate F-Test value from above arrays
10794 boolean test0 = true;
10795 boolean test1 = true;
10796 int i=0;
10797 int endTest=0;
10798 while(test0){
10799 if(fProb==fTestProb[i]){
10800 fTest0=fTestValues[i];
10801 test0=false;
10802 test1=false;
10803 }
10804 else{
10805 if(fProb>fTestProb[i]){
10806 test0=false;
10807 if(i>0){
10808 fTest1=fTestValues[i-1];
10809 fTest2=fTestValues[i];
10810 endTest=-1;
10811 }
10812 else{
10813 fTest1=fTestValues[i]/10.0D;
10814 fTest2=fTestValues[i];
10815 }
10816 }
10817 else{
10818 i++;
10819 if(i>fTestsNum-1){
10820 test0=false;
10821 fTest1=fTestValues[i-1];
10822 fTest2=10.0D*fTestValues[i-1];
10823 endTest=1;
10824 }
10825 }
10826 }
10827 }
10828
10829 // call bisection method
10830 if(test1)fTest0=fTestBisect(fProb, fTest1, fTest2, df1, df2, endTest);
10831
10832 return fTest0;
10833 }
10834
10835 // Bisection procedure for calculating and F-test value corresponding
10836 // to a given F-test probability
10837 private static double fTestBisect(double fProb, double fTestLow, double fTestHigh, int df1, int df2, int endTest){
10838
10839 double funcLow = fProb - Stat.fTestProb(fTestLow, df1, df2);
10840 double funcHigh = fProb - Stat.fTestProb(fTestHigh, df1, df2);
10841 double fTestMid = 0.0D;
10842 double funcMid = 0.0;
10843 int nExtensions = 0;
10844 int nIter = 1000; // iterations allowed
10845 double check = fProb*1e-6; // tolerance for bisection
10846 boolean test0 = true; // test for extending bracket
10847 boolean test1 = true; // test for bisection procedure
10848 while(test0){
10849 if(funcLow*funcHigh>0.0D){
10850 if(endTest<0){
10851 nExtensions++;
10852 if(nExtensions>100){
10853 System.out.println("Class: Stats\nMethod: fTestBisect\nProbability higher than range covered\nF-test value is less than "+fTestLow);
10854 System.out.println("This value was returned");
10855 fTestMid=fTestLow;
10856 test0=false;
10857 test1=false;
10858 }
10859 fTestLow /= 10.0D;
10860 funcLow = fProb - Stat.fTestProb(fTestLow, df1, df2);
10861 }
10862 else{
10863 nExtensions++;
10864 if(nExtensions>100){
10865 System.out.println("Class: Stats\nMethod: fTestBisect\nProbability lower than range covered\nF-test value is greater than "+fTestHigh);
10866 System.out.println("This value was returned");
10867 fTestMid=fTestHigh;
10868 test0=false;
10869 test1=false;
10870 }
10871 fTestHigh *= 10.0D;
10872 funcHigh = fProb - Stat.fTestProb(fTestHigh, df1, df2);
10873 }
10874 }
10875 else{
10876 test0=false;
10877 }
10878
10879 int i=0;
10880 while(test1){
10881 fTestMid = (fTestLow+fTestHigh)/2.0D;
10882 funcMid = fProb - Stat.fTestProb(fTestMid, df1, df2);
10883 if(Math.abs(funcMid)<check){
10884 test1=false;
10885 }
10886 else{
10887 i++;
10888 if(i>nIter){
10889 System.out.println("Class: Stats\nMethod: fTestBisect\nmaximum number of iterations exceeded\ncurrent value of F-test value returned");
10890 test1=false;
10891 }
10892 if(funcMid*funcHigh>0){
10893 funcHigh=funcMid;
10894 fTestHigh=fTestMid;
10895 }
10896 else{
10897 funcLow=funcMid;
10898 fTestLow=fTestMid;
10899 }
10900 }
10901 }
10902 }
10903 return fTestMid;
10904 }
10905
10906 // F-distribution pdf
10907 public double fPDF(double fValue, int nu1, int nu2){
10908 double numer = Math.pow(nu1*fValue, nu1)*Math.pow(nu2, nu2);
10909 double dnu1 = (double)nu1;
10910 double dnu2 = (double)nu2;
10911 numer /= Math.pow(dnu1*fValue+dnu2, dnu1+dnu2);
10912 numer = Math.sqrt(numer);
10913 double denom = fValue*Stat.betaFunction(dnu1/2.0D, dnu2/2.0D);
10914 return numer/denom;
10915 }
10916
10917 public double fPDF(double var1, int nu1, double var2, int nu2){
10918 return fPDF(var1/var2, nu1, nu2);
10919 }
10920
10921 // Returns an array of F-distribution random deviates - clock seed
10922 public static double[] fRand(int nu1, int nu2, int n){
10923 if(nu1<=0)throw new IllegalArgumentException("The degrees of freedom [nu1], " + nu1 + ", must be greater than zero");
10924 if(nu2<=0)throw new IllegalArgumentException("The degrees of freedom [nu2], " + nu2 + ", must be greater than zero");
10925 PsRandom psr = new PsRandom();
10926 return psr.fArray(nu1, nu2, n);
10927 }
10928
10929 // Returns an array of F-distribution random deviates - user supplied seed
10930 public static double[] fRand(int nu1, int nu2, int n, long seed){
10931 if(nu1<=0)throw new IllegalArgumentException("The degrees of freedom [nu1], " + nu1 + ", must be greater than zero");
10932 if(nu2<=0)throw new IllegalArgumentException("The degrees of freedom [nu2], " + nu2 + ", must be greater than zero");
10933 PsRandom psr = new PsRandom(seed);
10934 return psr.fArray(nu1, nu2, n);
10935 }
10936
10937 // STUDENT'S T DISTRIBUTION
10938
10939 // Returns the Student's t probability density function
10940 public static double studentst(double tValue, int df){
10941 return studentT(tValue, df);
10942 }
10943
10944 // Returns the Student's t probability density function
10945 public static double studentT(double tValue, int df){
10946 if(tValue!=tValue)throw new IllegalArgumentException("argument tValue is not a number (NaN)");
10947
10948 double ddf = (double)df;
10949 double dfterm = (ddf + 1.0D)/2.0D;
10950 return ((Stat.gamma(dfterm)/Stat.gamma(ddf/2))/Math.sqrt(ddf*Math.PI))*Math.pow(1.0D + tValue*tValue/ddf, -dfterm);
10951 }
10952
10953 // Returns the Student's t probability density function
10954 public static double studentstPDF(double tValue, int df){
10955 return studentTpdf(tValue, df);
10956 }
10957
10958 // Returns the Student's t probability density function
10959 public static double studentTpdf(double tValue, int df){
10960 if(tValue!=tValue)throw new IllegalArgumentException("argument tValue is not a number (NaN)");
10961
10962 double ddf = (double)df;
10963 double dfterm = (ddf + 1.0D)/2.0D;
10964 return ((Stat.gamma(dfterm)/Stat.gamma(ddf/2))/Math.sqrt(ddf*Math.PI))*Math.pow(1.0D + tValue*tValue/ddf, -dfterm);
10965 }
10966
10967 // Returns the Student's t probability density function
10968 public static double studentTPDF(double tValue, int df){
10969 if(tValue!=tValue)throw new IllegalArgumentException("argument tValue is not a number (NaN)");
10970
10971 double ddf = (double)df;
10972 double dfterm = (ddf + 1.0D)/2.0D;
10973 return ((Stat.gamma(dfterm)/Stat.gamma(ddf/2))/Math.sqrt(ddf*Math.PI))*Math.pow(1.0D + tValue*tValue/ddf, -dfterm);
10974 }
10975
10976
10977 // Returns the Student's t cumulative distribution function probability
10978 public static double studentstCDF(double tValue, int df){
10979 return studentTcdf(tValue, df);
10980 }
10981
10982
10983 // Returns the Student's t cumulative distribution function probability
10984 public static double studentTProb(double tValue, int df){
10985 if(tValue!=tValue)throw new IllegalArgumentException("argument tValue is not a number (NaN)");
10986 if(tValue==Double.POSITIVE_INFINITY){
10987 return 1.0;
10988 }
10989 else{
10990 if(tValue==Double.NEGATIVE_INFINITY){
10991 return 0.0;
10992 }
10993 else{
10994 double ddf = (double)df;
10995 double x = ddf/(ddf+tValue*tValue);
10996 return 0.5D*(1.0D + (Stat.regularisedBetaFunction(ddf/2.0D, 0.5D, 1) - Stat.regularisedBetaFunction(ddf/2.0D, 0.5D, x))*Fmath.sign(tValue));
10997 }
10998 }
10999 }
11000
11001 // Returns the Student's t cumulative distribution function probability
11002 public static double studentTcdf(double tValue, int df){
11003 if(tValue!=tValue)throw new IllegalArgumentException("argument tValue is not a number (NaN)");
11004
11005 if(tValue==Double.POSITIVE_INFINITY){
11006 return 1.0;
11007 }
11008 else{
11009 if(tValue==Double.NEGATIVE_INFINITY){
11010 return 0.0;
11011 }
11012 else{
11013 double ddf = (double)df;
11014 double x = ddf/(ddf+tValue*tValue);
11015 return 0.5D*(1.0D + (Stat.regularisedBetaFunction(ddf/2.0D, 0.5D, 1) - Stat.regularisedBetaFunction(ddf/2.0D, 0.5D, x))*Fmath.sign(tValue));
11016 }
11017 }
11018 }
11019
11020 // Returns the Student's t cumulative distribution function probability
11021 public static double studentTCDF(double tValue, int df){
11022 if(tValue!=tValue)throw new IllegalArgumentException("argument tValue is not a number (NaN)");
11023
11024 if(tValue==Double.POSITIVE_INFINITY){
11025 return 1.0;
11026 }
11027 else{
11028 if(tValue==Double.NEGATIVE_INFINITY){
11029 return 0.0;
11030 }
11031 else{
11032 double ddf = (double)df;
11033 double x = ddf/(ddf+tValue*tValue);
11034 return 0.5D*(1.0D + (Stat.regularisedBetaFunction(ddf/2.0D, 0.5D, 1) - Stat.regularisedBetaFunction(ddf/2.0D, 0.5D, x))*Fmath.sign(tValue));
11035 }
11036 }
11037 }
11038
11039 // Returns the Student's t cumulative distribution function probability
11040 public static double studentTcdf(double tValueLower, double tValueUpper, int df){
11041 if(tValueLower!=tValueLower)throw new IllegalArgumentException("argument tLowerValue is not a number (NaN)");
11042 if(tValueUpper!=tValueUpper)throw new IllegalArgumentException("argument tUpperValue is not a number (NaN)");
11043 if(tValueUpper==Double.POSITIVE_INFINITY){
11044 if(tValueLower==Double.NEGATIVE_INFINITY){
11045 return 1.0;
11046 }
11047 else{
11048 if(tValueLower==Double.POSITIVE_INFINITY){
11049 return 0.0;
11050 }
11051 else{
11052 return (1.0 - Stat.studentTcdf(tValueLower, df));
11053 }
11054 }
11055 }
11056 else{
11057 if(tValueLower==Double.NEGATIVE_INFINITY){
11058 if(tValueUpper==Double.NEGATIVE_INFINITY){
11059 return 0.0;
11060 }
11061 else{
11062 return Stat.studentTcdf(tValueUpper, df);
11063 }
11064 }
11065 else{
11066 return Stat.studentTcdf(tValueUpper, df) - Stat.studentTcdf(tValueLower, df);
11067 }
11068 }
11069 }
11070
11071 // Returns the P-value for a given Student's t value and degrees of freedom
11072 public static double pValue(double tValue, int df){
11073 if(tValue!=tValue)throw new IllegalArgumentException("argument tValue is not a number (NaN)");
11074
11075 double abst = Math.abs(tValue);
11076 return 1.0 - Stat.studentTcdf(-abst, abst, df);
11077 }
11078
11079 // Returns the Student's t mean, df = degrees of freedom
11080 public static double studentstMean(int df){
11081 return studentTmean(df);
11082 }
11083
11084 // Returns the Student's t mean, df = degrees of freedom
11085 public static double studentTmean(int df){
11086 double mean = Double.NaN; // mean undefined for df = 1
11087 if(df>1)mean = 0.0D;
11088 return mean;
11089 }
11090
11091 // Returns the Student's t median
11092 public static double studentstMedian(){
11093 return 0.0;
11094 }
11095
11096 // Returns the Student's t median
11097 public static double studentTmedian(){
11098 return 0.0;
11099 }
11100
11101 // Returns the Student's t mode
11102 public static double studentstMode(){
11103 return 0.0;
11104 }
11105
11106 // Returns the Student's t mode
11107 public static double studentTmode(){
11108 return 0.0;
11109 }
11110
11111 // Returns the Student's t standard deviation, df = degrees of freedom
11112 public static double studentstStandardDeviation(int df){
11113 return studentTstandDev(df);
11114 }
11115
11116 // Returns the Student's t standard deviation, df = degrees of freedom
11117 public static double studentTstandDev(int df){
11118 double standDev = Double.POSITIVE_INFINITY;
11119 if(df>2)standDev = Math.sqrt(df/(1 - df));
11120 return standDev;
11121 }
11122
11123 // Returns the A(t|n) distribution probabilty
11124 public static double probAtn(double tValue, int df){
11125 double ddf = (double)df;
11126 double x = ddf/(ddf+tValue*tValue);
11127 return 1.0D - Stat.regularisedBetaFunction(ddf/2.0D, 0.5D, x);
11128 }
11129
11130 // Returns an array of Student's t random deviates - clock seed
11131 // nu = the degrees of freedom
11132 public static double[] studentstRand(int nu, int n){
11133 return studentTRand(nu, n);
11134 }
11135
11136 // Returns an array of Student's t random deviates - clock seed
11137 // nu = the degrees of freedom
11138 public static double[] studentTRand(int nu, int n){
11139 PsRandom psr = new PsRandom();
11140 return psr.studentTarray(nu, n);
11141 }
11142
11143 // Returns an array of Student's t random deviates - clock seed
11144 // nu = the degrees of freedom
11145 public static double[] studentTrand(int nu, int n){
11146 PsRandom psr = new PsRandom();
11147 return psr.studentTarray(nu, n);
11148 }
11149
11150 // Returns an array of a Student's t random deviates - user supplied seed
11151 // nu = the degrees of freedom
11152 public static double[] studentstRand(int nu, int n, long seed){
11153 return studentTrand(nu, n, seed);
11154 }
11155
11156 // Returns an array of a Student's t random deviates - user supplied seed
11157 // nu = the degrees of freedom
11158 public static double[] studentTrand(int nu, int n, long seed){
11159 PsRandom psr = new PsRandom(seed);
11160 return psr.studentTarray(nu, n);
11161 }
11162
11163 // Returns an array of a Student's t random deviates - user supplied seed
11164 // nu = the degrees of freedom
11165 public static double[] studentTRand(int nu, int n, long seed){
11166 PsRandom psr = new PsRandom(seed);
11167 return psr.studentTarray(nu, n);
11168 }
11169
11170
11171 // GUMBEL (TYPE I EXTREME VALUE) DISTRIBUTION
11172
11173 // Minimum Gumbel cumulative distribution function
11174 // probability that a variate will assume a value less than the upperlimit
11175 public static double gumbelMinProbCDF(double mu, double sigma, double upperlimit){
11176 if(sigma<0.0D)throw new IllegalArgumentException("sigma must be positive");
11177 double arg = -(upperlimit - mu)/sigma;
11178 return Math.exp(-Math.exp(arg));
11179 }
11180
11181 // Minimum Gumbel cumulative distribution function
11182 // probability that a variate will assume a value less than the upperlimit
11183 public static double gumbelMinProb(double mu, double sigma, double upperlimit){
11184 if(sigma<0.0D)throw new IllegalArgumentException("sigma must be positive");
11185 double arg = -(upperlimit - mu)/sigma;
11186 return Math.exp(-Math.exp(arg));
11187 }
11188
11189 // Maximum Gumbel cumulative distribution function
11190 // probability that a variate will assume a value less than the upperlimit
11191 public static double gumbelMaxCDF(double mu, double sigma, double upperlimit){
11192 if(sigma<0.0D)throw new IllegalArgumentException("sigma must be positive");
11193 double arg = -(upperlimit - mu)/sigma;
11194 return 1.0D-Math.exp(-Math.exp(arg));
11195 }
11196
11197 // Maximum Gumbel cumulative distribution function
11198 // probability that a variate will assume a value less than the upperlimit
11199 public static double gumbelMaxProb(double mu, double sigma, double upperlimit){
11200 if(sigma<0.0D)throw new IllegalArgumentException("sigma must be positive");
11201 double arg = -(upperlimit - mu)/sigma;
11202 return 1.0D-Math.exp(-Math.exp(arg));
11203 }
11204
11205
11206 // Gumbel (maximum order statistic) Inverse Cumulative Density Function
11207 public static double gumbelMaxInverseCDF(double mu, double sigma, double prob){
11208 if(prob<0.0 || prob>1.0)throw new IllegalArgumentException("Entered cdf value, " + prob + ", must lie between 0 and 1 inclusive");
11209 double icdf = 0.0D;
11210
11211 if(prob==0.0){
11212 icdf = Double.NEGATIVE_INFINITY;
11213 }
11214 else{
11215 if(prob==1.0){
11216 icdf = Double.POSITIVE_INFINITY;
11217 }
11218 else{
11219 icdf = mu - sigma*Math.log(Math.log(1.0/prob));
11220 }
11221 }
11222
11223 return icdf;
11224 }
11225
11226 // Minimum Gumbel cumulative distribution function
11227 // probability that a variate will assume a value between the lower and the upper limits
11228 public static double gumbelMinCDF(double mu, double sigma, double lowerlimit, double upperlimit){
11229 if(sigma<0.0D)throw new IllegalArgumentException("sigma must be positive");
11230 double arg1 = -(lowerlimit - mu)/sigma;
11231 double arg2 = -(upperlimit - mu)/sigma;
11232 double term1 = Math.exp(-Math.exp(arg1));
11233 double term2 = Math.exp(-Math.exp(arg2));
11234 return term2-term1;
11235 }
11236
11237 // Minimum Gumbel cumulative distribution function
11238 // probability that a variate will assume a value between the lower and the upper limits
11239 public static double gumbelMinProb(double mu, double sigma, double lowerlimit, double upperlimit){
11240 if(sigma<0.0D)throw new IllegalArgumentException("sigma must be positive");
11241 double arg1 = -(lowerlimit - mu)/sigma;
11242 double arg2 = -(upperlimit - mu)/sigma;
11243 double term1 = Math.exp(-Math.exp(arg1));
11244 double term2 = Math.exp(-Math.exp(arg2));
11245 return term2-term1;
11246 }
11247
11248 // Maximum Gumbel cumulative distribution function
11249 // probability that a variate will assume a value between the lower and the upper limits
11250 public static double gumbelMaxCDF(double mu, double sigma, double lowerlimit, double upperlimit){
11251 if(sigma<0.0D)throw new IllegalArgumentException("sigma must be positive");
11252 double arg1 = (lowerlimit - mu)/sigma;
11253 double arg2 = (upperlimit - mu)/sigma;
11254 double term1 = -Math.exp(-Math.exp(arg1));
11255 double term2 = -Math.exp(-Math.exp(arg2));
11256 return term2-term1;
11257 }
11258
11259 // Maximum Gumbel cumulative distribution function
11260 // probability that a variate will assume a value between the lower and the upper limits
11261 public static double gumbelMaxProb(double mu, double sigma, double lowerlimit, double upperlimit){
11262 if(sigma<0.0D)throw new IllegalArgumentException("sigma must be positive");
11263 double arg1 = (lowerlimit - mu)/sigma;
11264 double arg2 = (upperlimit - mu)/sigma;
11265 double term1 = -Math.exp(-Math.exp(arg1));
11266 double term2 = -Math.exp(-Math.exp(arg2));
11267 return term2-term1;
11268 }
11269
11270 // Gumbel (minimum order statistic) Inverse Cumulative Density Function
11271 public static double gumbelMinInverseCDF(double mu, double sigma, double prob){
11272 if(prob<0.0 || prob>1.0)throw new IllegalArgumentException("Entered cdf value, " + prob + ", must lie between 0 and 1 inclusive");
11273 double icdf = 0.0D;
11274
11275 if(prob==0.0){
11276 icdf = Double.NEGATIVE_INFINITY;
11277 }
11278 else{
11279 if(prob==1.0){
11280 icdf = Double.POSITIVE_INFINITY;
11281 }
11282 else{
11283 icdf = mu + sigma*Math.log(Math.log(1.0/(1.0 - prob)));
11284 }
11285 }
11286
11287 return icdf;
11288 }
11289
11290 // Minimum Gumbel probability density function
11291 public static double gumbelMinPDF(double mu,double sigma, double x){
11292 if(sigma<0.0D)throw new IllegalArgumentException("sigma must be positive");
11293 double arg =(x-mu)/sigma;
11294 return (1.0D/sigma)*Math.exp(arg)*Math.exp(-Math.exp(arg));
11295 }
11296
11297 // Minimum Gumbel probability density function
11298 public static double gumbelMin(double mu,double sigma, double x){
11299 if(sigma<0.0D)throw new IllegalArgumentException("sigma must be positive");
11300 double arg =(x-mu)/sigma;
11301 return (1.0D/sigma)*Math.exp(arg)*Math.exp(-Math.exp(arg));
11302 }
11303
11304 // Maximum Gumbel probability density function
11305 public static double gumbelMaxPDF(double mu,double sigma, double x){
11306 if(sigma<0.0D)throw new IllegalArgumentException("sigma must be positive");
11307 double arg =-(x-mu)/sigma;
11308 return (1.0D/sigma)*Math.exp(arg)*Math.exp(-Math.exp(arg));
11309 }
11310
11311 // Maximum Gumbel probability density function
11312 public static double gumbelMax(double mu,double sigma, double x){
11313 if(sigma<0.0D)throw new IllegalArgumentException("sigma must be positive");
11314 double arg =-(x-mu)/sigma;
11315 return (1.0D/sigma)*Math.exp(arg)*Math.exp(-Math.exp(arg));
11316 }
11317
11318 // Returns an array of minimal Gumbel (Type I EVD) random deviates - clock seed
11319 // mu = location parameter, sigma = scale parameter, n = length of array
11320 public static double[] gumbelMinRand(double mu, double sigma, int n){
11321 double[] ran = new double[n];
11322 Random rr = new Random();
11323 for(int i=0; i<n; i++){
11324 ran[i] = Math.log(Math.log(1.0D/(1.0D-rr.nextDouble())))*sigma+mu;
11325 }
11326 return ran;
11327 }
11328
11329 // Returns an array of minimal Gumbel (Type I EVD) random deviates - user supplied seed
11330 // mu = location parameter, sigma = scale parameter, n = length of array
11331 public static double[] gumbelMinRand(double mu, double sigma, int n, long seed){
11332 double[] ran = new double[n];
11333 Random rr = new Random(seed);
11334 for(int i=0; i<n; i++){
11335 ran[i] = Math.log(Math.log(1.0D/(1.0D-rr.nextDouble())))*sigma+mu;
11336 }
11337 return ran;
11338 }
11339
11340 // Returns an array of maximal Gumbel (Type I EVD) random deviates - clock seed
11341 // mu = location parameter, sigma = scale parameter, n = length of array
11342 public static double[] gumbelMaxRand(double mu, double sigma, int n){
11343 double[] ran = new double[n];
11344 Random rr = new Random();
11345 for(int i=0; i<n; i++){
11346 ran[i] = mu-Math.log(Math.log(1.0D/(1.0D-rr.nextDouble())))*sigma;
11347 }
11348 return ran;
11349 }
11350
11351 // Returns an array of maximal Gumbel (Type I EVD) random deviates - user supplied seed
11352 // mu = location parameter, sigma = scale parameter, n = length of array
11353 public static double[] gumbelMaxRand(double mu, double sigma, int n, long seed){
11354 double[] ran = new double[n];
11355 Random rr = new Random(seed);
11356 for(int i=0; i<n; i++){
11357 ran[i] = mu-Math.log(Math.log(1.0D/(1.0D-rr.nextDouble())))*sigma;
11358 }
11359 return ran;
11360 }
11361
11362 // Gumbel (minimum order statistic) order statistic medians (n points)
11363 public static double[] gumbelMinOrderStatisticMedians(double mu, double sigma, int n){
11364 double nn = (double)n;
11365 double[] gmosm = new double[n];
11366 double[] uosm = uniformOrderStatisticMedians(n);
11367 for(int i=0; i<n; i++){
11368 gmosm[i] = Stat.gumbelMinInverseCDF(mu, sigma, uosm[i]);
11369 }
11370 return gmosm;
11371 }
11372
11373 // Gumbel (maximum order statistic) order statistic medians (n points)
11374 public static double[] gumbelMaxOrderStatisticMedians(double mu, double sigma, int n){
11375 double nn = (double)n;
11376 double[] gmosm = new double[n];
11377 double[] uosm = uniformOrderStatisticMedians(n);
11378 for(int i=0; i<n; i++){
11379 gmosm[i] = Stat.gumbelMaxInverseCDF(mu, sigma, uosm[i]);
11380 }
11381 return gmosm;
11382 }
11383
11384 // Minimum Gumbel mean
11385 public static double gumbelMinMean(double mu,double sigma){
11386 return mu - sigma*Fmath.EULER_CONSTANT_GAMMA;
11387 }
11388
11389 // Maximum Gumbel mean
11390 public static double gumbelMaxMean(double mu,double sigma){
11391 return mu + sigma*Fmath.EULER_CONSTANT_GAMMA;
11392 }
11393
11394 // Minimum Gumbel standard deviation
11395 public static double gumbelMinStandardDeviation(double sigma){
11396 return sigma*Math.PI/Math.sqrt(6.0D);
11397 }
11398
11399 // Minimum Gumbel standard deviation
11400 public static double gumbelMinStandDev(double sigma){
11401 return sigma*Math.PI/Math.sqrt(6.0D);
11402 }
11403
11404 // Maximum Gumbel standard deviation
11405 public static double gumbelMaxStandardDeviation(double sigma){
11406 return sigma*Math.PI/Math.sqrt(6.0D);
11407 }
11408
11409 // Maximum Gumbel standard deviation
11410 public static double gumbelMaxStandDev(double sigma){
11411 return sigma*Math.PI/Math.sqrt(6.0D);
11412 }
11413
11414 // Minimum Gumbel mode
11415 public static double gumbelMinMode(double mu,double sigma){
11416 return mu;
11417 }
11418
11419 // Maximum Gumbel mode
11420 public static double gumbelMaxMode(double mu,double sigma){
11421 return mu;
11422 }
11423
11424 // Minimum Gumbel median
11425 public static double gumbelMinMedian(double mu,double sigma){
11426 return mu + sigma*Math.log(Math.log(2.0D));
11427 }
11428
11429 // Maximum Gumbel median
11430 public static double gumbelMaxMedian(double mu,double sigma){
11431 return mu - sigma*Math.log(Math.log(2.0D));
11432 }
11433
11434
11435 // FRECHET (TYPE II EXTREME VALUE) DISTRIBUTION
11436
11437 // Frechet cumulative distribution function
11438 // probability that a variate will assume a value less than the upperlimit
11439 public static double frechetProb(double mu, double sigma, double gamma, double upperlimit){
11440 double arg = (upperlimit - mu)/sigma;
11441 double y = 0.0D;
11442 if(arg>0.0D)y = Math.exp(-Math.pow(arg, -gamma));
11443 return y;
11444 }
11445
11446
11447 // Frechet cumulative distribution function
11448 // probability that a variate will assume a value between the lower and the upper limits
11449 public static double frechetCDF(double mu, double sigma, double gamma, double lowerlimit, double upperlimit){
11450 double arg1 = (lowerlimit - mu)/sigma;
11451 double arg2 = (upperlimit - mu)/sigma;
11452 double term1 = 0.0D, term2 = 0.0D;
11453 if(arg1>=0.0D)term1 = Math.exp(-Math.pow(arg1, -gamma));
11454 if(arg2>=0.0D)term2 = Math.exp(-Math.pow(arg2, -gamma));
11455 return term2-term1;
11456 }
11457
11458 // Frechet cumulative distribution function
11459 // probability that a variate will assume a value between the lower and the upper limits
11460 public static double frechetProb(double mu, double sigma, double gamma, double lowerlimit, double upperlimit){
11461 double arg1 = (lowerlimit - mu)/sigma;
11462 double arg2 = (upperlimit - mu)/sigma;
11463 double term1 = 0.0D, term2 = 0.0D;
11464 if(arg1>=0.0D)term1 = Math.exp(-Math.pow(arg1, -gamma));
11465 if(arg2>=0.0D)term2 = Math.exp(-Math.pow(arg2, -gamma));
11466 return term2-term1;
11467 }
11468
11469 // Frechet Inverse Cumulative Density Function
11470 // Three parameter
11471 public static double frechetInverseCDF(double mu, double sigma, double gamma, double prob){
11472 if(prob<0.0 || prob>1.0)throw new IllegalArgumentException("Entered cdf value, " + prob + ", must lie between 0 and 1 inclusive");
11473 double icdf = 0.0D;
11474
11475 if(prob==0.0){
11476 icdf = Double.NEGATIVE_INFINITY;
11477 }
11478 else{
11479 if(prob==1.0){
11480 icdf = Double.POSITIVE_INFINITY;
11481 }
11482 else{
11483 icdf = mu + sigma*Math.pow(Math.log(1.0/prob), -1.0/gamma);
11484 }
11485 }
11486
11487 return icdf;
11488 }
11489
11490 // Frechet Inverse Cumulative Density Function
11491 // Two parameter
11492 public static double frechetInverseCDF(double sigma, double gamma, double prob){
11493 return frechetInverseCDF(0.0D, sigma, gamma, prob);
11494 }
11495
11496 // Frechet Inverse Cumulative Density Function
11497 // Standard
11498 public static double frechetInverseCDF(double gamma, double prob){
11499 return frechetInverseCDF(0.0D, 1.0D, gamma, prob);
11500 }
11501
11502
11503 // Frechet probability density function
11504 public static double frechetPDF(double mu,double sigma, double gamma, double x){
11505 double arg =(x-mu)/sigma;
11506 double y = 0.0D;
11507 if(arg>=0.0D){
11508 y = (gamma/sigma)*Math.pow(arg, -gamma-1.0D)*Math.exp(-Math.pow(arg, -gamma));
11509 }
11510 return y;
11511 }
11512
11513 // Frechet probability density function
11514 public static double frechet(double mu,double sigma, double gamma, double x){
11515 double arg =(x-mu)/sigma;
11516 double y = 0.0D;
11517 if(arg>=0.0D){
11518 y = (gamma/sigma)*Math.pow(arg, -gamma-1.0D)*Math.exp(-Math.pow(arg, -gamma));
11519 }
11520 return y;
11521 }
11522
11523 // Frechet order statistic medians (n points)
11524 // Three parameters
11525 public static double[] frechetOrderStatisticMedians(double mu, double sigma, double gamma, int n){
11526 double nn = (double)n;
11527 double[] fosm = new double[n];
11528 double[] uosm = uniformOrderStatisticMedians(n);
11529 for(int i=0; i<n; i++){
11530 fosm[i] = Stat.frechetInverseCDF(mu, sigma, gamma, uosm[i]);
11531 }
11532 return fosm;
11533 }
11534
11535 // Frechet order statistic medians (n points)
11536 // Two parameters
11537 public static double[] frechetOrderStatisticMedians(double sigma, double gamma, int n){
11538 return frechetOrderStatisticMedians(0.0D, sigma, gamma, n);
11539 }
11540
11541 // Frechet order statistic medians (n points)
11542 // Standard
11543 public static double[] frechetOrderStatisticMedians(double gamma, int n){
11544 return frechetOrderStatisticMedians(0.0D, 1.0D, gamma, n);
11545 }
11546
11547 // Frechet mean
11548 public static double frechetMean(double mu,double sigma, double gamma){
11549 double y = Double.NaN;
11550 if(gamma>1.0D){
11551 y = mu + sigma*Stat.gamma(1.0D-1.0D/gamma);
11552 }
11553 return y;
11554 }
11555
11556 // Frechet standard deviation
11557 public static double frechetStandardDeviation(double sigma, double gamma){
11558 return frechetStandDev(sigma, gamma);
11559 }
11560
11561
11562 // Frechet standard deviation
11563 public static double frechetStandDev(double sigma, double gamma){
11564 double y = Double.NaN;
11565 if(gamma>2.0D){
11566 y = Stat.gamma(1.0D-2.0D/gamma)-Fmath.square(Stat.gamma(1.0D-1.0D/gamma));
11567 y = sigma*Math.sqrt(y);
11568 }
11569 return y;
11570 }
11571
11572 // Frechet mode
11573 public static double frechetMode(double mu,double sigma, double gamma){
11574 return mu + sigma*Math.pow(gamma/(1.0D+gamma), 1.0D/gamma);
11575 }
11576
11577 // Returns an array of Frechet (Type II EVD) random deviates - clock seed
11578 // mu = location parameter, sigma = cale parameter, gamma = shape parametern = length of array
11579 public static double[] frechetRand(double mu, double sigma, double gamma, int n){
11580 double[] ran = new double[n];
11581 Random rr = new Random();
11582 for(int i=0; i<n; i++){
11583 ran[i] = Math.pow((1.0D/(Math.log(1.0D/rr.nextDouble()))),1.0D/gamma)*sigma + mu;
11584 }
11585 return ran;
11586 }
11587
11588 // Returns an array of Frechet (Type II EVD) random deviates - user supplied seed
11589 // mu = location parameter, sigma = cale parameter, gamma = shape parametern = length of array
11590 public static double[] frechetRand(double mu, double sigma, double gamma, int n, long seed){
11591 double[] ran = new double[n];
11592 Random rr = new Random(seed);
11593 for(int i=0; i<n; i++){
11594 ran[i] = Math.pow((1.0D/(Math.log(1.0D/rr.nextDouble()))),1.0D/gamma)*sigma + mu;
11595 }
11596 return ran;
11597 }
11598
11599
11600 // WEIBULL (TYPE III EXTREME VALUE) DISTRIBUTION
11601
11602 // Weibull cumulative distribution function
11603 // probability that a variate will assume a value less than the upperlimit
11604 public static double weibullCDF(double mu, double sigma, double gamma, double upperlimit){
11605 double arg = (upperlimit - mu)/sigma;
11606 double y = 0.0D;
11607 if(arg>0.0D)y = 1.0D - Math.exp(-Math.pow(arg, gamma));
11608 return y;
11609 }
11610
11611 // Weibull cumulative distribution function
11612 // probability that a variate will assume a value less than the upperlimit
11613 public static double weibullProb(double mu, double sigma, double gamma, double upperlimit){
11614 double arg = (upperlimit - mu)/sigma;
11615 double y = 0.0D;
11616 if(arg>0.0D)y = 1.0D - Math.exp(-Math.pow(arg, gamma));
11617 return y;
11618 }
11619
11620
11621 // Weibull cumulative distribution function
11622 // probability that a variate will assume a value between the lower and the upper limits
11623 public static double weibullCDF(double mu, double sigma, double gamma, double lowerlimit, double upperlimit){
11624 double arg1 = (lowerlimit - mu)/sigma;
11625 double arg2 = (upperlimit - mu)/sigma;
11626 double term1 = 0.0D, term2 = 0.0D;
11627 if(arg1>=0.0D)term1 = -Math.exp(-Math.pow(arg1, gamma));
11628 if(arg2>=0.0D)term2 = -Math.exp(-Math.pow(arg2, gamma));
11629 return term2-term1;
11630 }
11631
11632 // Weibull cumulative distribution function
11633 // probability that a variate will assume a value between the lower and the upper limits
11634 public static double weibullProb(double mu, double sigma, double gamma, double lowerlimit, double upperlimit){
11635 double arg1 = (lowerlimit - mu)/sigma;
11636 double arg2 = (upperlimit - mu)/sigma;
11637 double term1 = 0.0D, term2 = 0.0D;
11638 if(arg1>=0.0D)term1 = -Math.exp(-Math.pow(arg1, gamma));
11639 if(arg2>=0.0D)term2 = -Math.exp(-Math.pow(arg2, gamma));
11640 return term2-term1;
11641 }
11642
11643
11644 // Weibull Inverse Cumulative Density Function
11645 // Three parameter
11646 public static double weibullInverseCDF(double mu, double sigma, double gamma, double prob){
11647 if(prob<0.0 || prob>1.0)throw new IllegalArgumentException("Entered cdf value, " + prob + ", must lie between 0 and 1 inclusive");
11648 double icdf = 0.0D;
11649
11650 if(prob==0.0){
11651 icdf = mu;
11652 }
11653 else{
11654 if(prob==1.0){
11655 icdf = Double.POSITIVE_INFINITY;
11656 }
11657 else{
11658 icdf = mu + sigma*(Math.pow(-Math.log(1.0 - prob), 1.0/gamma));
11659 }
11660 }
11661
11662 return icdf;
11663 }
11664
11665 // Weibull Inverse Cumulative Disrtibution Function
11666 public static double inverseWeibullCDF(double mu, double sigma, double gamma, double prob){
11667 return weibullInverseCDF(mu, sigma, gamma, prob);
11668 }
11669
11670 // Weibull Inverse Cumulative Density Function
11671 // Two parameter
11672 public static double weibullInverseCDF(double sigma, double gamma, double prob){
11673 return weibullInverseCDF(0.0D, sigma, gamma, prob);
11674 }
11675
11676 public static double inverseWeibullCDF(double sigma, double gamma, double prob){
11677 return weibullInverseCDF(0.0, sigma, gamma, prob);
11678 }
11679
11680
11681 // Weibull Inverse Cumulative Density Function
11682 // Standard
11683 public static double weibullInverseCDF(double gamma, double prob){
11684 return weibullInverseCDF(0.0D, 1.0D, gamma, prob);
11685 }
11686
11687 public static double inverseWeibullCDF(double gamma, double prob){
11688 return weibullInverseCDF(0.0D, 1.0D, gamma, prob);
11689 }
11690
11691 // Weibull probability density function
11692 public static double weibullPDF(double mu,double sigma, double gamma, double x){
11693 double arg =(x-mu)/sigma;
11694 double y = 0.0D;
11695 if(arg>=0.0D){
11696 y = (gamma/sigma)*Math.pow(arg, gamma-1.0D)*Math.exp(-Math.pow(arg, gamma));
11697 }
11698 return y;
11699 }
11700
11701 // Weibull probability density function
11702 public static double weibull(double mu,double sigma, double gamma, double x){
11703 double arg =(x-mu)/sigma;
11704 double y = 0.0D;
11705 if(arg>=0.0D){
11706 y = (gamma/sigma)*Math.pow(arg, gamma-1.0D)*Math.exp(-Math.pow(arg, gamma));
11707 }
11708 return y;
11709 }
11710
11711 // Weibull mean
11712 public static double weibullMean(double mu,double sigma, double gamma){
11713 return mu + sigma*Stat.gamma(1.0D/gamma+1.0D);
11714 }
11715
11716 // Weibull standard deviation
11717 public static double weibullStandardDeviation(double sigma, double gamma){
11718 return weibullStandDev(sigma, gamma);
11719 }
11720
11721
11722 // Weibull standard deviation
11723 public static double weibullStandDev(double sigma, double gamma){
11724 double y = Stat.gamma(2.0D/gamma+1.0D)-Fmath.square(Stat.gamma(1.0D/gamma+1.0D));
11725 return sigma*Math.sqrt(y);
11726 }
11727
11728 // Weibull mode
11729 public static double weibullMode(double mu,double sigma, double gamma){
11730 double y=mu;
11731 if(gamma>1.0D){
11732 y = mu + sigma*Math.pow((gamma-1.0D)/gamma, 1.0D/gamma);
11733 }
11734 return y;
11735 }
11736
11737 // Weibull median
11738 public static double weibullMedian(double mu,double sigma, double gamma){
11739 return mu + sigma*Math.pow(Math.log(2.0D),1.0D/gamma);
11740 }
11741
11742 // Returns an array of Weibull (Type III EVD) random deviates - clock seed
11743 // mu = location parameter, sigma = cale parameter, gamma = shape parametern = length of array
11744 public static double[] weibullRand(double mu, double sigma, double gamma, int n){
11745 double[] ran = new double[n];
11746 Random rr = new Random();
11747 for(int i=0; i<n; i++){
11748 ran[i] = Math.pow(-Math.log(1.0D-rr.nextDouble()),1.0D/gamma)*sigma + mu;
11749 }
11750 return ran;
11751 }
11752
11753 // Returns an array of Weibull (Type III EVD) random deviates - user supplied seed
11754 // mu = location parameter, sigma = cale parameter, gamma = shape parametern = length of array
11755 public static double[] weibullRand(double mu, double sigma, double gamma, int n, long seed){
11756 double[] ran = new double[n];
11757 Random rr = new Random(seed);
11758 for(int i=0; i<n; i++){
11759 ran[i] = Math.pow(-Math.log(1.0D-rr.nextDouble()),1.0D/gamma)*sigma + mu;
11760 }
11761 return ran;
11762 }
11763
11764 // Weibull order statistic medians (n points)
11765 // Three parameter
11766 public static double[] weibullOrderStatisticMedians(double mu, double sigma, double gamma, int n){
11767 double nn = (double)n;
11768 double[] wosm = new double[n];
11769 double[] uosm = uniformOrderStatisticMedians(n);
11770 for(int i=0; i<n; i++){
11771 wosm[i] = Stat.inverseWeibullCDF(mu, sigma, gamma, uosm[i]);
11772 }
11773 return wosm;
11774 }
11775
11776 // Weibull order statistic medians for a mu of zero (n points)
11777 // Two parameter
11778 public static double[] weibullOrderStatisticMedians(double sigma, double gamma, int n){
11779 return Stat.weibullOrderStatisticMedians(0.0, sigma, gamma, n);
11780 }
11781
11782 // Weibull order statistic medians for a mu of zero and a sigma of unity (n points)
11783 // Standard
11784 public static double[] weibullOrderStatisticMedians(double gamma, int n){
11785 return Stat.weibullOrderStatisticMedians(0.0, 1.0, gamma, n);
11786 }
11787
11788
11789 // EXPONENTIAL DISTRIBUTION
11790
11791 // Exponential cumulative distribution function
11792 // probability that a variate will assume a value less than the upperlimit
11793 public static double exponentialCDF(double mu, double sigma, double upperlimit){
11794 double arg = (upperlimit - mu)/sigma;
11795 double y = 0.0D;
11796 if(arg>0.0D)y = 1.0D - Math.exp(-arg);
11797 return y;
11798 }
11799
11800 // Exponential cumulative distribution function
11801 // probability that a variate will assume a value less than the upperlimit
11802 public static double exponentialProb(double mu, double sigma, double upperlimit){
11803 double arg = (upperlimit - mu)/sigma;
11804 double y = 0.0D;
11805 if(arg>0.0D)y = 1.0D - Math.exp(-arg);
11806 return y;
11807 }
11808
11809 // Exponential cumulative distribution function
11810 // probability that a variate will assume a value between the lower and the upper limits
11811 public static double exponentialCDF(double mu, double sigma, double lowerlimit, double upperlimit){
11812 double arg1 = (lowerlimit - mu)/sigma;
11813 double arg2 = (upperlimit - mu)/sigma;
11814 double term1 = 0.0D, term2 = 0.0D;
11815 if(arg1>=0.0D)term1 = -Math.exp(-arg1);
11816 if(arg2>=0.0D)term2 = -Math.exp(-arg2);
11817 return term2-term1;
11818 }
11819
11820 // Exponential cumulative distribution function
11821 // probability that a variate will assume a value between the lower and the upper limits
11822 public static double exponentialProb(double mu, double sigma, double lowerlimit, double upperlimit){
11823 double arg1 = (lowerlimit - mu)/sigma;
11824 double arg2 = (upperlimit - mu)/sigma;
11825 double term1 = 0.0D, term2 = 0.0D;
11826 if(arg1>=0.0D)term1 = -Math.exp(-arg1);
11827 if(arg2>=0.0D)term2 = -Math.exp(-arg2);
11828 return term2-term1;
11829 }
11830
11831 // Exponential Inverse Cumulative Density Function
11832 public static double exponentialInverseCDF(double mu, double sigma, double prob){
11833 if(prob<0.0 || prob>1.0)throw new IllegalArgumentException("Entered cdf value, " + prob + ", must lie between 0 and 1 inclusive");
11834 double icdf = 0.0D;
11835
11836 if(prob==0.0){
11837 icdf = mu;
11838 }
11839 else{
11840 if(prob==1.0){
11841 icdf = Double.POSITIVE_INFINITY;
11842 }
11843 else{
11844 icdf = mu - sigma*(Math.log(1.0 - prob));
11845 }
11846 }
11847
11848 return icdf;
11849 }
11850
11851 // Exponential Inverse Cumulative Density Function
11852 public static double inverseExponentialCDF(double mu, double sigma, double prob){
11853 return exponentialInverseCDF(mu, sigma, prob);
11854 }
11855
11856 // Exponential probability density function
11857 public static double exponentialPDF(double mu,double sigma, double x){
11858 double arg =(x-mu)/sigma;
11859 double y = 0.0D;
11860 if(arg>=0.0D){
11861 y = Math.exp(-arg)/sigma;
11862 }
11863 return y;
11864 }
11865
11866 // Exponential probability density function
11867 public static double exponential(double mu,double sigma, double x){
11868 double arg =(x-mu)/sigma;
11869 double y = 0.0D;
11870 if(arg>=0.0D){
11871 y = Math.exp(-arg)/sigma;
11872 }
11873 return y;
11874 }
11875
11876 // Exponential mean
11877 public static double exponentialMean(double mu, double sigma){
11878 return mu + sigma;
11879 }
11880
11881 // Exponential standard deviation
11882 public static double exponentialStandardDeviation(double sigma){
11883 return sigma;
11884 }
11885
11886 // Exponential standard deviation
11887 public static double exponentialStandDev(double sigma){
11888 return sigma;
11889 }
11890
11891
11892 // Exponential mode
11893 public static double exponentialMode(double mu){
11894 return mu;
11895 }
11896
11897 // Exponential median
11898 public static double exponentialMedian(double mu,double sigma){
11899 return mu + sigma*Math.log(2.0D);
11900 }
11901
11902 // Returns an array of Exponential random deviates - clock seed
11903 // mu = location parameter, sigma = cale parameter, gamma = shape parametern = length of array
11904 public static double[] exponentialRand(double mu, double sigma, int n){
11905 double[] ran = new double[n];
11906 Random rr = new Random();
11907 for(int i=0; i<n; i++){
11908 ran[i] = mu - Math.log(1.0D-rr.nextDouble())*sigma;
11909 }
11910 return ran;
11911 }
11912
11913 // Returns an array of Exponential random deviates - user supplied seed
11914 // mu = location parameter, sigma = cale parameter, gamma = shape parametern = length of array
11915 public static double[] exponentialRand(double mu, double sigma, int n, long seed){
11916 double[] ran = new double[n];
11917 Random rr = new Random(seed);
11918 for(int i=0; i<n; i++){
11919 ran[i] = mu - Math.log(1.0D-rr.nextDouble())*sigma;
11920 }
11921 return ran;
11922 }
11923
11924 // Exponential order statistic medians (n points)
11925 public static double[] exponentialOrderStatisticMedians(double mu, double sigma, int n){
11926 double nn = (double)n;
11927 double[] eosm = new double[n];
11928 double[] uosm = uniformOrderStatisticMedians(n);
11929 for(int i=0; i<n; i++){
11930 eosm[i] = Stat.inverseExponentialCDF(mu, sigma, uosm[i]);
11931 }
11932 return eosm;
11933 }
11934
11935
11936
11937 // RAYLEIGH DISTRIBUTION
11938
11939 // Rayleigh cumulative distribution function
11940 // probability that a variate will assume a value less than the upperlimit
11941 public static double rayleighCDF(double beta, double upperlimit){
11942 double arg = (upperlimit)/beta;
11943 double y = 0.0D;
11944 if(arg>0.0D)y = 1.0D - Math.exp(-arg*arg/2.0D);
11945 return y;
11946 }
11947
11948 // Rayleigh cumulative distribution function
11949 // probability that a variate will assume a value less than the upperlimit
11950 public static double rayleighProb(double beta, double upperlimit){
11951 double arg = (upperlimit)/beta;
11952 double y = 0.0D;
11953 if(arg>0.0D)y = 1.0D - Math.exp(-arg*arg/2.0D);
11954 return y;
11955 }
11956
11957 // Rayleigh cumulative distribution function
11958 // probability that a variate will assume a value between the lower and the upper limits
11959 public static double rayleighCDF(double beta, double lowerlimit, double upperlimit){
11960 double arg1 = (lowerlimit)/beta;
11961 double arg2 = (upperlimit)/beta;
11962 double term1 = 0.0D, term2 = 0.0D;
11963 if(arg1>=0.0D)term1 = -Math.exp(-arg1*arg1/2.0D);
11964 if(arg2>=0.0D)term2 = -Math.exp(-arg2*arg2/2.0D);
11965 return term2-term1;
11966 }
11967
11968 // Rayleigh cumulative distribution function
11969 // probability that a variate will assume a value between the lower and the upper limits
11970 public static double rayleighProb(double beta, double lowerlimit, double upperlimit){
11971 double arg1 = (lowerlimit)/beta;
11972 double arg2 = (upperlimit)/beta;
11973 double term1 = 0.0D, term2 = 0.0D;
11974 if(arg1>=0.0D)term1 = -Math.exp(-arg1*arg1/2.0D);
11975 if(arg2>=0.0D)term2 = -Math.exp(-arg2*arg2/2.0D);
11976 return term2-term1;
11977 }
11978
11979 // Rayleigh Inverse Cumulative Density Function
11980 public static double rayleighInverseCDF(double beta, double prob){
11981 if(prob<0.0 || prob>1.0)throw new IllegalArgumentException("Entered cdf value, " + prob + ", must lie between 0 and 1 inclusive");
11982 double icdf = 0.0D;
11983
11984 if(prob==0.0){
11985 icdf = 0.0;
11986 }
11987 else{
11988 if(prob==1.0){
11989 icdf = Double.POSITIVE_INFINITY;
11990 }
11991 else{
11992 icdf = beta*(Math.sqrt(-Math.log(1.0 - prob)));
11993 }
11994 }
11995
11996 return icdf;
11997 }
11998
11999 // Rayleigh Inverse Cumulative Density Function
12000 public static double inverseRayleighCDF(double beta, double prob){
12001 return rayleighInverseCDF(beta, prob);
12002 }
12003
12004
12005 // Rayleigh probability density function
12006 public static double rayleighPDF(double beta, double x){
12007 double arg =x/beta;
12008 double y = 0.0D;
12009 if(arg>=0.0D){
12010 y = (arg/beta)*Math.exp(-arg*arg/2.0D)/beta;
12011 }
12012 return y;
12013 }
12014
12015 // Rayleigh probability density function
12016 public static double rayleigh(double beta, double x){
12017 double arg =x/beta;
12018 double y = 0.0D;
12019 if(arg>=0.0D){
12020 y = (arg/beta)*Math.exp(-arg*arg/2.0D)/beta;
12021 }
12022 return y;
12023 }
12024
12025 // Rayleigh mean
12026 public static double rayleighMean(double beta){
12027 return beta*Math.sqrt(Math.PI/2.0D);
12028 }
12029
12030 // Rayleigh standard deviation
12031 public static double rayleighStandardDeviation(double beta){
12032 return beta*Math.sqrt(2.0D-Math.PI/2.0D);
12033 }
12034
12035 // Rayleigh standard deviation
12036 public static double rayleighStandDev(double beta){
12037 return beta*Math.sqrt(2.0D-Math.PI/2.0D);
12038 }
12039
12040 // Rayleigh mode
12041 public static double rayleighMode(double beta){
12042 return beta;
12043 }
12044
12045 // Rayleigh median
12046 public static double rayleighMedian(double beta){
12047 return beta*Math.sqrt(Math.log(2.0D));
12048 }
12049
12050 // Returns an array of Rayleigh random deviates - clock seed
12051 // beta = scale parameter, n = length of array
12052 public static double[] rayleighRand(double beta, int n){
12053 double[] ran = new double[n];
12054 Random rr = new Random();
12055 for(int i=0; i<n; i++){
12056 ran[i] = Math.sqrt(-2.0D*Math.log(1.0D-rr.nextDouble()))*beta;
12057 }
12058 return ran;
12059 }
12060
12061 // Returns an array of Rayleigh random deviates - user supplied seed
12062 // beta = scale parameter, n = length of array
12063 public static double[] rayleighRand(double beta, int n, long seed){
12064 double[] ran = new double[n];
12065 Random rr = new Random(seed);
12066 for(int i=0; i<n; i++){
12067 ran[i] = Math.sqrt(-2.0D*Math.log(1.0D-rr.nextDouble()))*beta;
12068 }
12069 return ran;
12070 }
12071
12072 // Rayleigh order statistic medians (n points)
12073 public static double[] rayleighOrderStatisticMedians(double beta, int n){
12074 double nn = (double)n;
12075 double[] rosm = new double[n];
12076 double[] uosm = uniformOrderStatisticMedians(n);
12077 for(int i=0; i<n; i++){
12078 rosm[i] = Stat.inverseRayleighCDF(beta, uosm[i]);
12079 }
12080 return rosm;
12081 }
12082
12083
12084 // PARETO DISTRIBUTION
12085
12086 // Pareto cumulative distribution function
12087 // probability that a variate will assume a value less than the upperlimit
12088 public static double paretoCDF(double alpha, double beta, double upperlimit){
12089 double y = 0.0D;
12090 if(upperlimit>=beta)y = 1.0D - Math.pow(beta/upperlimit, alpha);
12091 return y;
12092 }
12093
12094 // Pareto cumulative distribution function
12095 // probability that a variate will assume a value less than the upperlimit
12096 public static double paretoProb(double alpha, double beta, double upperlimit){
12097 double y = 0.0D;
12098 if(upperlimit>=beta)y = 1.0D - Math.pow(beta/upperlimit, alpha);
12099 return y;
12100 }
12101
12102 // Pareto cumulative distribution function
12103 // probability that a variate will assume a value between the lower and the upper limits
12104 public static double paretoCDF(double alpha, double beta, double lowerlimit, double upperlimit){
12105 double term1 = 0.0D, term2 = 0.0D;
12106 if(lowerlimit>=beta)term1 = -Math.pow(beta/lowerlimit, alpha);
12107 if(upperlimit>=beta)term2 = -Math.pow(beta/upperlimit, alpha);
12108 return term2-term1;
12109 }
12110
12111 // Pareto cumulative distribution function
12112 // probability that a variate will assume a value between the lower and the upper limits
12113 public static double paretoProb(double alpha, double beta, double lowerlimit, double upperlimit){
12114 double term1 = 0.0D, term2 = 0.0D;
12115 if(lowerlimit>=beta)term1 = -Math.pow(beta/lowerlimit, alpha);
12116 if(upperlimit>=beta)term2 = -Math.pow(beta/upperlimit, alpha);
12117 return term2-term1;
12118 }
12119
12120 // Pareto Inverse Cumulative Density Function
12121 public static double paretoInverseCDF(double alpha, double beta, double prob){
12122 if(prob<0.0 || prob>1.0)throw new IllegalArgumentException("Entered cdf value, " + prob + ", must lie between 0 and 1 inclusive");
12123 double icdf = 0.0D;
12124
12125 if(prob==0.0){
12126 icdf = beta;
12127 }
12128 else{
12129 if(prob==1.0){
12130 icdf = Double.POSITIVE_INFINITY;
12131 }
12132 else{
12133 icdf = beta/Math.pow((1.0 - prob), 1.0/alpha);
12134 }
12135 }
12136
12137 return icdf;
12138 }
12139
12140 // Pareto Inverse Cumulative Density Function
12141 public static double inverseParetoCDF(double alpha, double beta, double prob){
12142 return paretoInverseCDF(alpha, beta, prob);
12143 }
12144
12145
12146 // Pareto probability density function
12147 public static double paretoPDF(double alpha, double beta, double x){
12148 double y = 0.0D;
12149 if(x>=beta){
12150 y = alpha*Math.pow(beta, alpha)/Math.pow(x, alpha+1.0D);
12151 }
12152 return y;
12153 }
12154
12155 // Pareto probability density function
12156 public static double pareto(double alpha, double beta, double x){
12157 double y = 0.0D;
12158 if(x>=beta){
12159 y = alpha*Math.pow(beta, alpha)/Math.pow(x, alpha+1.0D);
12160 }
12161 return y;
12162 }
12163
12164 // Pareto mean
12165 public static double paretoMean(double alpha, double beta){
12166 double y = Double.NaN;
12167 if(alpha>1.0D)y = alpha*beta/(alpha-1);
12168 return y;
12169 }
12170
12171 // Pareto standard deviation
12172 public static double paretoStandardDeviation(double alpha, double beta){
12173 double y = Double.NaN;
12174 if(alpha>1.0D)y = alpha*Fmath.square(beta)/(Fmath.square(alpha-1)*(alpha-2));
12175 return y;
12176 }
12177
12178 // Pareto standard deviation
12179 public static double paretoStandDev(double alpha, double beta){
12180 double y = Double.NaN;
12181 if(alpha>1.0D)y = alpha*Fmath.square(beta)/(Fmath.square(alpha-1)*(alpha-2));
12182 return y;
12183 }
12184
12185 // Pareto mode
12186 public static double paretoMode(double beta){
12187 return beta;
12188 }
12189
12190 // Returns an array of Pareto random deviates - clock seed
12191 public static double[] paretoRand(double alpha, double beta, int n){
12192 double[] ran = new double[n];
12193 Random rr = new Random();
12194 for(int i=0; i<n; i++){
12195 ran[i] = Math.pow(1.0D-rr.nextDouble(), -1.0D/alpha)*beta;
12196 }
12197 return ran;
12198 }
12199
12200 // Returns an array of Pareto random deviates - user supplied seed
12201 public static double[] paretoRand(double alpha, double beta, int n, long seed){
12202 double[] ran = new double[n];
12203 Random rr = new Random(seed);
12204 for(int i=0; i<n; i++){
12205 ran[i] = Math.pow(1.0D-rr.nextDouble(), -1.0D/alpha)*beta;
12206 }
12207 return ran;
12208 }
12209
12210 // Pareto order statistic medians (n points)
12211 public static double[] paretoOrderStatisticMedians(double alpha, double beta, int n){
12212 double nn = (double)n;
12213 double[] posm = new double[n];
12214 double[] uosm = uniformOrderStatisticMedians(n);
12215 for(int i=0; i<n; i++){
12216 posm[i] = Stat.inverseParetoCDF(alpha, beta, uosm[i]);
12217 }
12218 return posm;
12219 }
12220
12221
12222 // FITTING DATA TO ABOVE DISTRIBUTIONS
12223
12224 // Fit a data set to one, several or all of the above distributions (instance)
12225 public void fitOneOrSeveralDistributions(){
12226 double[] dd = this.getArray_as_double();
12227 Regression.fitOneOrSeveralDistributions(dd);
12228 }
12229
12230 // Fit a data set to one, several or all of the above distributions (static)
12231 public static void fitOneOrSeveralDistributions(double[] array){
12232 Regression.fitOneOrSeveralDistributions(array);
12233 }
12234
12235
12236 // OUTLIER TESTING (STATIC)
12237
12238 // Anscombe test for a upper outlier - output as Vector
12239 public static Vector<Object> upperOutliersAnscombeAsVector(double[] values, double constant){
12240 ArrayList<Object> res = Stat.upperOutliersAnscombeAsArrayList(values, constant);
12241 Vector<Object> ret = null;
12242 if(res!=null){
12243 int n = res.size();
12244 ret = new Vector<Object>(n);
12245 for(int i=0; i<n; i++)ret.add(res.get(i));
12246 }
12247 return ret;
12248 }
12249
12250
12251 // Anscombe test for a upper outlier as Vector
12252 public static Vector<Object> upperOutliersAnscombe(double[] values, double constant){
12253 return upperOutliersAnscombeAsVector(values, constant);
12254 }
12255
12256
12257 // Anscombe test for a upper outlier - output as ArrayList
12258 public static ArrayList<Object> upperOutliersAnscombeAsArrayList(double[] values, double constant){
12259
12260 Stat am = new Stat(values);
12261 double[] copy0 = am.getArray_as_double();
12262 double[] copy1 = am.getArray_as_double();
12263 int nValues = values.length;
12264 int nValues0 = nValues;
12265 ArrayList<Object> outers = new ArrayList<Object>();
12266 int nOutliers = 0;
12267 boolean test = true;
12268
12269 while(test){
12270 double mean = am.mean_as_double();
12271 double standDev = am.standardDeviation_as_double();
12272 double max = am.getMaximum_as_double();
12273 int maxIndex = am.getMaximumIndex();
12274 double statistic = (max - mean)/standDev;
12275 if(statistic>constant){
12276 outers.add(new Double(max));
12277 outers.add(new Integer(maxIndex));
12278 nOutliers++;
12279 copy1 = new double[nValues-1];
12280 for(int i=maxIndex; i<nValues-1; i++)copy1[i] = copy0[i+1];
12281
12282 nValues--;
12283 am = new Stat(Conv.copy(copy1));
12284 }
12285 else{
12286 test=false;
12287 }
12288 }
12289
12290 double[] outliers = null;
12291 int[] outIndices = null;
12292
12293 if(nOutliers>0){
12294 outliers = new double[nOutliers];
12295 outIndices = new int[nOutliers];
12296 for(int i=0; i<nOutliers; i++){
12297 outliers[i] = ((Double)outers.get(2*i)).doubleValue();
12298 outIndices[i] = ((Integer)outers.get(2*i+1)).intValue();
12299 }
12300 }
12301
12302 ArrayList<Object> ret = new ArrayList<Object>(4);
12303 ret.add(new Integer(nOutliers));
12304 ret.add(outliers);
12305 ret.add(outIndices);
12306 ret.add(copy1);
12307 return ret;
12308 }
12309
12310 // Anscombe test for a upper outlier - output as Vector
12311 public static Vector<Object> upperOutliersAnscombeAsVector(BigDecimal[] values, BigDecimal constant){
12312 ArrayList<Object> res = Stat.upperOutliersAnscombeAsArrayList(values, constant);
12313 Vector<Object> ret = null;
12314 if(res!=null){
12315 int n = res.size();
12316 ret = new Vector<Object>(n);
12317 for(int i=0; i<n; i++)ret.add(res.get(i));
12318 }
12319 return ret;
12320 }
12321
12322
12323 // Anscombe test for a upper outlier as Vector
12324 public static Vector<Object> upperOutliersAnscombe(BigDecimal[] values, BigDecimal constant){
12325 return upperOutliersAnscombeAsVector(values, constant);
12326 }
12327
12328
12329 // Anscombe test for a upper outlier - output as ArrayList
12330 public static ArrayList<Object> upperOutliersAnscombeAsArrayList(BigDecimal[] values, BigDecimal constant){
12331
12332 Stat am = new Stat(values);
12333 BigDecimal[] copy0 = am.getArray_as_BigDecimal();
12334 BigDecimal[] copy1 = am.getArray_as_BigDecimal();
12335 int nValues = values.length;
12336 int nValues0 = nValues;
12337 ArrayList<Object> outers = new ArrayList<Object>();
12338 int nOutliers = 0;
12339 boolean test = true;
12340 while(test){
12341 BigDecimal mean = am.mean_as_BigDecimal();
12342 BigDecimal variance = am.variance_as_BigDecimal();
12343 BigDecimal max = am.getMaximum_as_BigDecimal();
12344 int maxIndex = am.getMaximumIndex();
12345 BigDecimal statistic = (max.subtract(mean)).divide(variance, BigDecimal.ROUND_HALF_UP);
12346 if(statistic.compareTo(constant.multiply(constant))==1){
12347 outers.add(max);
12348 outers.add(new Integer(maxIndex));
12349 nOutliers++;
12350 copy1 = new BigDecimal[nValues-1];
12351 for(int i=maxIndex; i<nValues-1; i++)copy1[i] = copy0[i+1];
12352
12353 nValues--;
12354 am = new Stat(Conv.copy(copy1));
12355 }
12356 else{
12357 mean = null;
12358 variance = null;
12359 statistic = null;
12360 copy0 = null;
12361 test=false;
12362 }
12363 }
12364
12365 BigDecimal[] outliers = null;
12366 int[] outIndices = null;
12367
12368 if(nOutliers>0){
12369 outliers = new BigDecimal[nOutliers];
12370 outIndices = new int[nOutliers];
12371 for(int i=0; i<nOutliers; i++){
12372 outliers[i] = ((BigDecimal)outers.get(2*i));
12373 outIndices[i] = ((Integer)outers.get(2*i+1)).intValue();
12374 }
12375 }
12376
12377 ArrayList<Object> ret = new ArrayList<Object>(4);
12378 ret.add(new Integer(nOutliers));
12379 ret.add(outliers);
12380 ret.add(outIndices);
12381 ret.add(copy1);
12382 return ret;
12383 }
12384
12385
12386 // Anscombe test for a upper outlier - output as Vector
12387 public static Vector<Object> upperOutliersAnscombeAsVector(BigInteger[] values, BigInteger constant){
12388 ArrayList<Object> res = Stat.upperOutliersAnscombeAsArrayList(values, constant);
12389 Vector<Object> ret = null;
12390 if(res!=null){
12391 int n = res.size();
12392 ret = new Vector<Object>(n);
12393 for(int i=0; i<n; i++)ret.add(res.get(i));
12394 }
12395 return ret;
12396 }
12397
12398
12399 // Anscombe test for a upper outlier as Vector
12400 public static Vector<Object> upperOutliersAnscombe(BigInteger[] values, BigInteger constant){
12401 return upperOutliersAnscombeAsVector(values, constant);
12402 }
12403
12404
12405 // Anscombe test for a upper outlier - output as ArrayList
12406 public static ArrayList<Object> upperOutliersAnscombeAsArrayList(BigInteger[] values, BigInteger constant){
12407 ArrayMaths am = new ArrayMaths(values);
12408 BigDecimal[] bd = am.getArray_as_BigDecimal();
12409 BigDecimal cd = new BigDecimal(constant);
12410 return Stat.upperOutliersAnscombeAsArrayList(bd, cd);
12411 }
12412
12413
12414 // Anscombe test for a lower outlier - output as Vector
12415 public static Vector<Object> lowerOutliersAnscombeAsVector(double[] values, double constant){
12416 ArrayList<Object> res = Stat.lowerOutliersAnscombeAsArrayList(values, constant);
12417 Vector<Object> ret = null;
12418 if(res!=null){
12419 int n = res.size();
12420 ret = new Vector<Object>(n);
12421 for(int i=0; i<n; i++)ret.add(res.get(i));
12422 }
12423 return ret;
12424 }
12425
12426
12427 // Anscombe test for a lower outlier as Vector
12428 public static Vector<Object> lowerOutliersAnscombe(double[] values, double constant){
12429 return upperOutliersAnscombeAsVector(values, constant);
12430 }
12431
12432 // Anscombe test for a lower outlier
12433 public static ArrayList<Object> lowerOutliersAnscombeAsArrayList(double[] values, double constant){
12434
12435 Stat am = new Stat(values);
12436 double[] copy0 = am.getArray_as_double();
12437 double[] copy1 = am.getArray_as_double();
12438 int nValues = values.length;
12439 int nValues0 = nValues;
12440 ArrayList<Object> outers = new ArrayList<Object>();
12441 int nOutliers = 0;
12442 boolean test = true;
12443
12444 while(test){
12445 double mean = am.mean_as_double();
12446 double standDev = am.standardDeviation_as_double();
12447 double min = am.getMinimum_as_double();
12448 int minIndex = am.getMinimumIndex();
12449 double statistic = (mean - min)/standDev;
12450 if(statistic>constant){
12451 outers.add(new Double(min));
12452 outers.add(new Integer(minIndex));
12453 nOutliers++;
12454 copy1 = new double[nValues-1];
12455 for(int i=minIndex; i<nValues-1; i++)copy1[i] = copy0[i+1];
12456
12457 nValues--;
12458 am = new Stat(Conv.copy(copy1));
12459 }
12460 else{
12461 test=false;
12462 }
12463 }
12464
12465 double[] outliers = null;
12466 int[] outIndices = null;
12467
12468 if(nOutliers>0){
12469 outliers = new double[nOutliers];
12470 outIndices = new int[nOutliers];
12471 for(int i=0; i<nOutliers; i++){
12472 outliers[i] = ((Double)outers.get(2*i)).doubleValue();
12473 outIndices[i] = ((Integer)outers.get(2*i+1)).intValue();
12474 }
12475 }
12476
12477 ArrayList<Object> ret = new ArrayList<Object>();
12478 ret.add(new Integer(nOutliers));
12479 ret.add(outliers);
12480 ret.add(outIndices);
12481 ret.add(copy1);
12482 return ret;
12483 }
12484
12485 // Anscombe test for a lower outlier - output as Vector
12486 public static Vector<Object> lowerOutliersAnscombeAsVector(BigDecimal[] values, BigDecimal constant){
12487 ArrayList<Object> res = Stat.lowerOutliersAnscombeAsArrayList(values, constant);
12488 Vector<Object> ret = null;
12489 if(res!=null){
12490 int n = res.size();
12491 ret = new Vector<Object>(n);
12492 for(int i=0; i<n; i++)ret.add(res.get(i));
12493 }
12494 return ret;
12495 }
12496
12497 // Anscombe test for a lower outlier as Vector
12498 public static Vector<Object> lowerOutliersAnscombe(BigDecimal[] values, BigDecimal constant){
12499 return upperOutliersAnscombeAsVector(values, constant);
12500 }
12501
12502 // Anscombe test for a lower outlier
12503 public static ArrayList<Object> lowerOutliersAnscombeAsArrayList(BigDecimal[] values, BigDecimal constant){
12504
12505 Stat am = new Stat(values);
12506 BigDecimal[] copy0 = am.getArray_as_BigDecimal();
12507 BigDecimal[] copy1 = am.getArray_as_BigDecimal();
12508 int nValues = values.length;
12509 int nValues0 = nValues;
12510 ArrayList<Object> outers = new ArrayList<Object>();
12511 int nOutliers = 0;
12512 boolean test = true;
12513 while(test){
12514 BigDecimal mean = am.mean_as_BigDecimal();
12515 BigDecimal variance = am.variance_as_BigDecimal();
12516 BigDecimal min = am.getMinimum_as_BigDecimal();
12517 int minIndex = am.getMinimumIndex();
12518 BigDecimal statistic = (mean.subtract(min)).divide(variance, BigDecimal.ROUND_HALF_UP);
12519 if(statistic.compareTo(constant.multiply(constant))==1){
12520 outers.add(min);
12521 outers.add(new Integer(minIndex));
12522 nOutliers++;
12523 copy1 = new BigDecimal[nValues-1];
12524 for(int i=minIndex; i<nValues-1; i++)copy1[i] = copy0[i+1];
12525
12526 nValues--;
12527 am = new Stat(Conv.copy(copy1));
12528 }
12529 else{
12530 mean = null;
12531 variance = null;
12532 statistic = null;
12533 copy0 = null;
12534 test=false;
12535 }
12536 }
12537
12538 BigDecimal[] outliers = null;
12539 int[] outIndices = null;
12540
12541 if(nOutliers>0){
12542 outliers = new BigDecimal[nOutliers];
12543 outIndices = new int[nOutliers];
12544 for(int i=0; i<nOutliers; i++){
12545 outliers[i] = ((BigDecimal)outers.get(2*i));
12546 outIndices[i] = ((Integer)outers.get(2*i+1)).intValue();
12547 }
12548 }
12549
12550 ArrayList<Object> ret = new ArrayList<Object>();
12551 ret.add(new Integer(nOutliers));
12552 ret.add(outliers);
12553 ret.add(outIndices);
12554 ret.add(copy1);
12555 return ret;
12556 }
12557
12558
12559 // Anscombe test for a lower outlier - output as Vector
12560 public static Vector<Object> lowerOutliersAnscombeAsVector(BigInteger[] values, BigInteger constant){
12561 ArrayList<Object> res = Stat.lowerOutliersAnscombeAsArrayList(values, constant);
12562 Vector<Object> ret = null;
12563 if(res!=null){
12564 int n = res.size();
12565 ret = new Vector<Object>(n);
12566 for(int i=0; i<n; i++)ret.add(res.get(i));
12567 }
12568 return ret;
12569 }
12570
12571 // Anscombe test for a lower outlier as Vector
12572 public static Vector<Object> lowerOutliersAnscombe(BigInteger[] values, BigInteger constant){
12573 return upperOutliersAnscombeAsVector(values, constant);
12574 }
12575
12576 // Anscombe test for a lower outlier
12577 public static ArrayList<Object> lowerOutliersAnscombeAsArrayList(BigInteger[] values, BigInteger constant){
12578 ArrayMaths am = new ArrayMaths(values);
12579 BigDecimal[] bd = am.getArray_as_BigDecimal();
12580 BigDecimal cd = new BigDecimal(constant);
12581 return Stat.lowerOutliersAnscombeAsArrayList(bd, cd);
12582 }
12583
12584
12585
12586
12587
12588 // METHODS OVERRIDING ArrayMaths METHODS
12589 // DEEP COPY
12590 // Copy to a new instance of Stat
12591 public Stat copy(){
12592
12593 Stat am = new Stat();
12594
12595 if(this.amWeights==null){
12596 am.amWeights = null;
12597 }
12598 else{
12599 am.amWeights = this.amWeights;
12600 }
12601 am.weightsSupplied = this.weightsSupplied;
12602 am.upperOutlierDetails = new ArrayList<Object>();
12603 if(this.upperOutlierDetails.size()!=0){
12604 Integer hold0 = (Integer)this.upperOutlierDetails.get(0);
12605 am.upperOutlierDetails.add(hold0);
12606 am.upperOutlierDetails.add(this.upperOutlierDetails.get(1));
12607 int[] hold2 = (int[])this.upperOutlierDetails.get(2);
12608 am.upperOutlierDetails.add(hold2);
12609 am.upperOutlierDetails.add(this.upperOutlierDetails.get(3));
12610 }
12611 am.upperDone = this.upperDone;
12612 am.lowerOutlierDetails = new ArrayList<Object>();
12613 if(this.lowerOutlierDetails.size()!=0){
12614 Integer hold0 = (Integer)this.lowerOutlierDetails.get(0);
12615 am.lowerOutlierDetails.add(hold0);
12616 am.lowerOutlierDetails.add(this.lowerOutlierDetails.get(1));
12617 int[] hold2 = (int[])this.lowerOutlierDetails.get(2);
12618 am.lowerOutlierDetails.add(hold2);
12619 am.lowerOutlierDetails.add(this.lowerOutlierDetails.get(3));
12620 }
12621 am.lowerDone = this.lowerDone;
12622
12623 am.length = this.length;
12624 am.maxIndex = this.maxIndex;
12625 am.minIndex = this.minIndex;
12626 am.sumDone = this.sumDone;
12627 am.productDone = this.productDone;
12628 am.sumlongToDouble = this.sumlongToDouble;
12629 am.productlongToDouble = this.productlongToDouble;
12630 am.type = this.type;
12631 if(this.originalTypes==null){
12632 am.originalTypes = null;
12633 }
12634 else{
12635 am.originalTypes = Conv.copy(this.originalTypes);
12636 }
12637 if(this.sortedIndices==null){
12638 am.sortedIndices = null;
12639 }
12640 else{
12641 am.sortedIndices = Conv.copy(this.sortedIndices);
12642 }
12643 am.suppressMessages = this.suppressMessages;
12644 am.minmax = new ArrayList<Object>();
12645 if(this.minmax.size()!=0){
12646 switch(this.type){
12647 case 0:
12648 case 1: double dd = ((Double)this.minmax.get(0)).doubleValue();
12649 am.minmax.add(new Double(dd));
12650 dd = ((Double)this.minmax.get(1)).doubleValue();
12651 am.minmax.add(new Double(dd));
12652 break;
12653 case 4:
12654 case 5: long ll= ((Long)this.minmax.get(0)).longValue();
12655 am.minmax.add(new Double(ll));
12656 ll = ((Long)this.minmax.get(1)).longValue();
12657 am.minmax.add(new Long(ll));
12658 break;
12659 case 2:
12660 case 3: float ff = ((Float)this.minmax.get(0)).floatValue();
12661 am.minmax.add(new Double(ff));
12662 ff = ((Float)this.minmax.get(1)).floatValue();
12663 am.minmax.add(new Double(ff));
12664 break;
12665 case 6:
12666 case 7: int ii = ((Integer)this.minmax.get(0)).intValue();
12667 am.minmax.add(new Integer(ii));
12668 ii = ((Double)this.minmax.get(1)).intValue();
12669 am.minmax.add(new Integer(ii));
12670 break;
12671 case 8:
12672 case 9: short ss = ((Short)this.minmax.get(0)).shortValue();
12673 am.minmax.add(new Short(ss));
12674 ss = ((Double)this.minmax.get(1)).shortValue();
12675 am.minmax.add(new Short((ss)));
12676 break;
12677 case 10:
12678 case 11: byte bb = ((Byte)this.minmax.get(0)).byteValue();
12679 am.minmax.add(new Byte(bb));
12680 ss = ((Byte)this.minmax.get(1)).byteValue();
12681 am.minmax.add(new Byte((bb)));
12682 break;
12683 case 12: BigDecimal bd = (BigDecimal)this.minmax.get(0);
12684 am.minmax.add(bd);
12685 bd = (BigDecimal)this.minmax.get(1);
12686 am.minmax.add(bd);
12687 bd = null;
12688 break;
12689 case 13: BigInteger bi = (BigInteger)this.minmax.get(0);
12690 am.minmax.add(bi);
12691 bi = (BigInteger)this.minmax.get(1);
12692 am.minmax.add(bi);
12693 bi = null;
12694 break;
12695 case 16:
12696 case 17: int iii = ((Integer)this.minmax.get(0)).intValue();
12697 am.minmax.add(new Integer(iii));
12698 iii = ((Double)this.minmax.get(1)).intValue();
12699 am.minmax.add(new Integer(iii));
12700 break;
12701 }
12702 }
12703
12704 am.summ = new ArrayList<Object>();
12705 if(this.summ.size()!=0){
12706 switch(this.type){
12707 case 0:
12708 case 1:
12709 case 2:
12710 case 3:
12711 case 18: double dd = ((Double)summ.get(0)).doubleValue();
12712 am.summ.add(new Double(dd));
12713 break;
12714 case 4:
12715 case 5:
12716 case 6:
12717 case 7:
12718 case 8:
12719 case 9:
12720 case 10:
12721 case 11:
12722 case 16:
12723 case 17: if(this.sumlongToDouble){
12724 double dd2 = ((Double)summ.get(0)).doubleValue();
12725 am.summ.add(new Double(dd2));
12726 }
12727 else{
12728 long ll = ((Long)summ.get(0)).longValue();
12729 am.summ.add(new Long(ll));
12730 }
12731 break;
12732 case 12: BigDecimal bd = (BigDecimal)summ.get(0);
12733 am.summ.add(bd);
12734 break;
12735 case 13: BigInteger bi = (BigInteger)summ.get(0);
12736 am.summ.add(bi);
12737 break;
12738 case 14: Complex cc = (Complex)summ.get(0);
12739 am.summ.add(cc);
12740 break;
12741 case 15: Phasor pp = (Phasor)summ.get(0);
12742 am.summ.add(pp);
12743 break;
12744 default: throw new IllegalArgumentException("Data type not identified by this method");
12745 }
12746 }
12747
12748 am.productt = new ArrayList<Object>();
12749 if(this.productt.size()!=0){
12750 switch(this.type){
12751 case 0:
12752 case 1:
12753 case 2:
12754 case 3:
12755 case 18: double dd = ((Double)productt.get(0)).doubleValue();
12756 am.productt.add(new Double(dd));
12757 break;
12758 case 4:
12759 case 5:
12760 case 6:
12761 case 7:
12762 case 8:
12763 case 9:
12764 case 10:
12765 case 11:
12766 case 16:
12767 case 17: if(this.sumlongToDouble){
12768 double dd2 = ((Double)productt.get(0)).doubleValue();
12769 am.productt.add(new Double(dd2));
12770 }
12771 else{
12772 long ll = ((Long)productt.get(0)).longValue();
12773 am.productt.add(new Long(ll));
12774 }
12775 break;
12776 case 12: BigDecimal bd = (BigDecimal)productt.get(0);
12777 am.productt.add(bd);
12778 break;
12779 case 13: BigInteger bi = (BigInteger)productt.get(0);
12780 am.productt.add(bi);
12781 break;
12782 case 14: Complex cc = (Complex)productt.get(0);
12783 am.productt.add(cc);
12784 break;
12785 case 15: Phasor pp = (Phasor)productt.get(0);
12786 am.productt.add(pp);
12787 break;
12788 default: throw new IllegalArgumentException("Data type not identified by this method");
12789 }
12790 }
12791
12792
12793 switch(this.type){
12794 case 0:
12795 case 1: double[] dd = Conv.copy(this.getArray_as_double());
12796 for(int i=0; i<this.length; i++)am.array.add(new Double(dd[i]));
12797 break;
12798 case 2:
12799 case 3: float[] ff = Conv.copy(this.getArray_as_float());
12800 for(int i=0; i<this.length; i++)am.array.add(new Float(ff[i]));
12801 break;
12802 case 4:
12803 case 5: long[] ll = Conv.copy(this.getArray_as_long());
12804 for(int i=0; i<this.length; i++)am.array.add(new Long(ll[i]));
12805 break;
12806 case 6:
12807 case 7: int[] ii = Conv.copy(this.getArray_as_int());
12808 for(int i=0; i<this.length; i++)am.array.add(new Integer(ii[i]));
12809 break;
12810 case 8:
12811 case 9: short[] ss = Conv.copy(this.getArray_as_short());
12812 for(int i=0; i<this.length; i++)am.array.add(new Short(ss[i]));
12813 break;
12814 case 10:
12815 case 11: byte[] bb = Conv.copy(this.getArray_as_byte());
12816 for(int i=0; i<this.length; i++)am.array.add(new Byte(bb[i]));
12817 break;
12818 case 12: BigDecimal[] bd = Conv.copy(this.getArray_as_BigDecimal());
12819 for(int i=0; i<this.length; i++)am.array.add(bd[i]);
12820 break;
12821 case 13: BigInteger[] bi = Conv.copy(this.getArray_as_BigInteger());
12822 for(int i=0; i<this.length; i++)am.array.add(bi[i]);
12823 break;
12824 case 14: Complex[] ccc = this.getArray_as_Complex();
12825 for(int i=0; i<this.length; i++)am.array.add(ccc[i].copy());
12826 break;
12827 case 15: Phasor[] ppp = this.getArray_as_Phasor();
12828 for(int i=0; i<this.length; i++)am.array.add(ppp[i].copy());
12829 break;
12830 case 16:
12831 case 17: char[] cc = Conv.copy(this.getArray_as_char());
12832 for(int i=0; i<this.length; i++)am.array.add(new Character(cc[i]));
12833 break;
12834 case 18: String[] sss = Conv.copy(this.getArray_as_String());
12835 for(int i=0; i<this.length; i++)am.array.add(sss[i]);
12836 break;
12837 }
12838
12839 return am;
12840 }
12841
12842 public Stat plus(double constant){
12843 return super.plus(constant).toStat();
12844 }
12845
12846 public Stat plus(float constant){
12847 return super.plus(constant).toStat();
12848 }
12849
12850 public Stat plus(long constant){
12851 return super.plus(constant).toStat();
12852 }
12853
12854 public Stat plus(int constant){
12855 return super.plus(constant).toStat();
12856 }
12857
12858 public Stat plus(short constant){
12859 return super.plus(constant).toStat();
12860 }
12861
12862 public Stat plus(byte constant){
12863 return super.plus(constant).toStat();
12864 }
12865
12866 public Stat plus(char constant){
12867 return super.plus(constant).toStat();
12868 }
12869
12870 public Stat plus(BigDecimal constant){
12871 return super.plus(constant).toStat();
12872 }
12873
12874 public Stat plus(BigInteger constant){
12875 return super.plus(constant).toStat();
12876 }
12877
12878 public Stat plus(Complex constant){
12879 return super.plus(constant).toStat();
12880 }
12881
12882 public Stat plus(Phasor constant){
12883 return super.plus(constant).toStat();
12884 }
12885
12886 public Stat plus(String constant){
12887 return super.plus(constant).toStat();
12888 }
12889
12890
12891 public Stat plus(Double constant){
12892 return super.plus(constant).toStat();
12893 }
12894
12895 public Stat plus(Float constant){
12896 return super.plus(constant).toStat();
12897 }
12898
12899 public Stat plus(Long constant){
12900 return super.plus(constant).toStat();
12901 }
12902
12903 public Stat plus(Integer constant){
12904 return super.plus(constant).toStat();
12905 }
12906
12907 public Stat plus(Short constant){
12908 return super.plus(constant).toStat();
12909 }
12910
12911 public Stat plus(Byte constant){
12912 return super.plus(constant).toStat();
12913 }
12914
12915
12916 public Stat plus(Character constant){
12917 return super.plus(constant).toStat();
12918 }
12919
12920 public Stat plus(Stat arrays){
12921 return super.plus(arrays).toStat();
12922 }
12923
12924 public Stat plus(ArrayMaths arraym){
12925 return super.plus(arraym).toStat();
12926 }
12927
12928 public Stat plus(ArrayList<Object> arrayl){
12929 return super.plus(arrayl).toStat();
12930 }
12931
12932 public Stat plus(Vector<Object> list){
12933 return super.plus(list).toStat();
12934 }
12935
12936 public Stat plus(double[] array){
12937 return super.plus(array).toStat();
12938 }
12939
12940 public Stat plus(float[] array){
12941 return super.plus(array).toStat();
12942 }
12943
12944 public Stat plus(long[] array){
12945 return super.plus(array).toStat();
12946 }
12947
12948 public Stat plus(int[] array){
12949 return super.plus(array).toStat();
12950 }
12951
12952 public Stat plus(short[] array){
12953 return super.plus(array).toStat();
12954 }
12955
12956 public Stat plus(byte[] array){
12957 return super.plus(array).toStat();
12958 }
12959
12960 public Stat plus(char[] array){
12961 return super.plus(array).toStat();
12962 }
12963
12964 public Stat plus(BigDecimal[] array){
12965 return super.plus(array).toStat();
12966 }
12967
12968 public Stat plus(BigInteger[] array){
12969 return super.plus(array).toStat();
12970 }
12971
12972 public Stat plus(Complex[] array){
12973 return super.plus(array).toStat();
12974 }
12975
12976 public Stat plus(Phasor[] array){
12977 return super.plus(array).toStat();
12978 }
12979
12980 public Stat plus(String[] array){
12981 return super.plus(array).toStat();
12982 }
12983
12984 public Stat plus(Double[] array){
12985 return super.plus(array).toStat();
12986 }
12987
12988 public Stat plus(Float[] array){
12989 return super.plus(array).toStat();
12990 }
12991
12992 public Stat plus(Long[] array){
12993 return super.plus(array).toStat();
12994 }
12995
12996 public Stat plus(Integer[] array){
12997 return super.plus(array).toStat();
12998 }
12999
13000 public Stat plus(Short[] array){
13001 return super.plus(array).toStat();
13002 }
13003
13004 public Stat plus(Byte[] array){
13005 return super.plus(array).toStat();
13006 }
13007
13008 public Stat plus(Character[] array){
13009 return super.plus(array).toStat();
13010 }
13011
13012 public Stat minus(double constant){
13013 return super.minus(constant).toStat();
13014 }
13015
13016 public Stat minus(float constant){
13017 return super.minus(constant).toStat();
13018 }
13019
13020 public Stat minus(long constant){
13021 return super.minus(constant).toStat();
13022 }
13023
13024 public Stat minus(int constant){
13025 return super.minus(constant).toStat();
13026 }
13027
13028 public Stat minus(short constant){
13029 return super.minus(constant).toStat();
13030 }
13031
13032 public Stat minus(byte constant){
13033 return super.minus(constant).toStat();
13034 }
13035
13036 public Stat minus(char constant){
13037 return super.minus(constant).toStat();
13038 }
13039
13040 public Stat minus(BigDecimal constant){
13041 return super.minus(constant).toStat();
13042 }
13043
13044 public Stat minus(BigInteger constant){
13045 return super.minus(constant).toStat();
13046 }
13047
13048 public Stat minus(Complex constant){
13049 return super.minus(constant).toStat();
13050 }
13051
13052 public Stat minus(Phasor constant){
13053 return super.minus(constant).toStat();
13054 }
13055
13056 public Stat minus(Double constant){
13057 return super.minus(constant).toStat();
13058 }
13059
13060 public Stat minus(Float constant){
13061 return super.minus(constant).toStat();
13062 }
13063
13064 public Stat minus(Long constant){
13065 return super.minus(constant).toStat();
13066 }
13067
13068 public Stat minus(Integer constant){
13069 return super.minus(constant).toStat();
13070 }
13071
13072 public Stat minus(Short constant){
13073 return super.minus(constant).toStat();
13074 }
13075
13076 public Stat minus(Byte constant){
13077 return super.minus(constant).toStat();
13078 }
13079
13080
13081 public Stat minus(Character constant){
13082 return super.minus(constant).toStat();
13083 }
13084
13085 public Stat minus(Stat arrays){
13086 return super.minus(arrays).toStat();
13087 }
13088
13089 public Stat minus(ArrayMaths arraym){
13090 return super.minus(arraym).toStat();
13091 }
13092
13093 public Stat minus(Vector<Object> vec){
13094 return super.minus(vec).toStat();
13095 }
13096
13097 public Stat minus(ArrayList<Object> list){
13098 return super.minus(list).toStat();
13099 }
13100
13101 public Stat minus(double[] array){
13102 return super.minus(array).toStat();
13103 }
13104
13105 public Stat minus(float[] array){
13106 return super.minus(array).toStat();
13107 }
13108
13109 public Stat minus(long[] array){
13110 return super.minus(array).toStat();
13111 }
13112
13113 public Stat minus(int[] array){
13114 return super.minus(array).toStat();
13115 }
13116
13117 public Stat minus(short[] array){
13118 return super.minus(array).toStat();
13119 }
13120
13121 public Stat minus(byte[] array){
13122 return super.minus(array).toStat();
13123 }
13124
13125 public Stat minus(BigDecimal[] array){
13126 return super.minus(array).toStat();
13127 }
13128
13129 public Stat minus(BigInteger[] array){
13130 return super.minus(array).toStat();
13131 }
13132
13133 public Stat minus(Complex[] array){
13134 return super.minus(array).toStat();
13135 }
13136
13137 public Stat minus(Phasor[] array){
13138 return super.minus(array).toStat();
13139 }
13140
13141 public Stat minus(Double[] array){
13142 return super.minus(array).toStat();
13143 }
13144
13145 public Stat minus(Float[] array){
13146 return super.minus(array).toStat();
13147 }
13148
13149 public Stat minus(Long[] array){
13150 return super.minus(array).toStat();
13151 }
13152
13153 public Stat minus(Integer[] array){
13154 return super.minus(array).toStat();
13155 }
13156
13157 public Stat minus(Short[] array){
13158 return super.minus(array).toStat();
13159 }
13160
13161 public Stat minus(Byte[] array){
13162 return super.minus(array).toStat();
13163 }
13164
13165 public Stat times(double constant){
13166 return super.times(constant).toStat();
13167 }
13168
13169 public Stat times(float constant){
13170 return super.times(constant).toStat();
13171 }
13172
13173 public Stat times(long constant){
13174 return super.times(constant).toStat();
13175 }
13176
13177 public Stat times(int constant){
13178 return super.times(constant).toStat();
13179 }
13180
13181 public Stat times(short constant){
13182 return super.times(constant).toStat();
13183 }
13184
13185 public Stat times(byte constant){
13186 return super.times(constant).toStat();
13187 }
13188
13189 public Stat times(BigDecimal constant){
13190 return super.times(constant).toStat();
13191 }
13192
13193 public Stat times(BigInteger constant){
13194 return super.times(constant).toStat();
13195 }
13196
13197 public Stat times(Complex constant){
13198 return super.times(constant).toStat();
13199 }
13200
13201 public Stat times(Phasor constant){
13202 return super.times(constant).toStat();
13203 }
13204
13205 public Stat times(Double constant){
13206 return super.times(constant).toStat();
13207 }
13208
13209 public Stat times(Float constant){
13210 return super.times(constant).toStat();
13211 }
13212
13213 public Stat times(Long constant){
13214 return super.times(constant).toStat();
13215 }
13216
13217 public Stat times(Integer constant){
13218 return super.times(constant).toStat();
13219 }
13220
13221 public Stat times(Short constant){
13222 return super.times(constant).toStat();
13223 }
13224
13225 public Stat times(Byte constant){
13226 return super.times(constant).toStat();
13227 }
13228
13229 public Stat over(double constant){
13230 return super.over(constant).toStat();
13231 }
13232
13233 public Stat over(float constant){
13234 return super.over(constant).toStat();
13235 }
13236
13237 public Stat over(long constant){
13238 return super.over(constant).toStat();
13239 }
13240
13241 public Stat over(int constant){
13242 return super.over(constant).toStat();
13243 }
13244
13245 public Stat over(short constant){
13246 return super.over(constant).toStat();
13247 }
13248
13249 public Stat over(byte constant){
13250 return super.over(constant).toStat();
13251 }
13252
13253 public Stat over(BigDecimal constant){
13254 return super.over(constant).toStat();
13255 }
13256
13257 public Stat over(BigInteger constant){
13258 return super.over(constant).toStat();
13259 }
13260
13261 public Stat over(Complex constant){
13262 return super.over(constant).toStat();
13263 }
13264
13265 public Stat over(Phasor constant){
13266 return super.over(constant).toStat();
13267 }
13268
13269 public Stat over(Double constant){
13270 return super.over(constant).toStat();
13271 }
13272
13273 public Stat over(Float constant){
13274 return super.over(constant).toStat();
13275 }
13276
13277 public Stat over(Long constant){
13278 return super.over(constant).toStat();
13279 }
13280
13281 public Stat over(Integer constant){
13282 return super.over(constant).toStat();
13283 }
13284
13285 public Stat over(Short constant){
13286 return super.over(constant).toStat();
13287 }
13288
13289 public Stat over(Byte constant){
13290 return super.over(constant).toStat();
13291 }
13292
13293
13294 public Stat pow(double n){
13295 return super.pow(n).toStat();
13296 }
13297
13298 public Stat pow(float n){
13299 return super.pow(n).toStat();
13300 }
13301
13302 public Stat pow(long n){
13303 return super.pow(n).toStat();
13304 }
13305
13306 public Stat pow(int n){
13307 return super.pow(n).toStat();
13308 }
13309
13310 public Stat pow(short n){
13311 return super.pow(n).toStat();
13312 }
13313
13314 public Stat pow(byte n){
13315 return super.pow(n).toStat();
13316 }
13317
13318 public Stat pow(Double n){
13319 return super.pow(n).toStat();
13320 }
13321
13322 public Stat pow(Float n){
13323 return super.pow(n).toStat();
13324 }
13325
13326 public Stat pow(Long n){
13327 return super.pow(n).toStat();
13328 }
13329
13330 public Stat pow(Integer n){
13331 return super.pow(n).toStat();
13332 }
13333
13334 public Stat pow(Short n){
13335 return super.pow(n).toStat();
13336 }
13337
13338 public Stat pow(Byte n){
13339 return super.pow(n).toStat();
13340 }
13341
13342 public Stat pow(BigInteger n){
13343 return super.pow(n).toStat();
13344 }
13345
13346 public Stat pow(BigDecimal n){
13347 return super.pow(n).toStat();
13348 }
13349
13350 public Stat sqrt(){
13351 return super.sqrt().toStat();
13352 }
13353
13354 public Stat oneOverSqrt(){
13355 return super.oneOverSqrt().toStat();
13356 }
13357
13358 public Stat abs(){
13359 return super.abs().toStat();
13360 }
13361
13362 public Stat log(){
13363 return super.log().toStat();
13364 }
13365
13366 public Stat log2(){
13367 return super.log2().toStat();
13368 }
13369
13370 public Stat log10(){
13371 return super.log10().toStat();
13372 }
13373
13374 public Stat antilog10(){
13375 return super.antilog10().toStat();
13376 }
13377
13378 public Stat xLog2x(){
13379 return super.xLog2x().toStat();
13380 }
13381
13382 public Stat xLogEx(){
13383 return super.xLogEx().toStat();
13384 }
13385
13386 public Stat xLog10x(){
13387 return super.xLog10x().toStat();
13388 }
13389
13390 public Stat minusxLog2x(){
13391 return super.minusxLog2x().toStat();
13392 }
13393
13394 public Stat minusxLogEx(){
13395 return super.minusxLogEx().toStat();
13396 }
13397
13398 public Stat minusxLog10x(){
13399 return super.minusxLog10x().toStat();
13400 }
13401
13402 public Stat exp(){
13403 return super.exp().toStat();
13404 }
13405
13406 public Stat invert(){
13407 return super.invert().toStat();
13408 }
13409
13410 public Stat negate(){
13411 return super.negate().toStat();
13412 }
13413
13414 public Stat sort(){
13415 return super.sort().toStat();
13416 }
13417
13418 public Stat sort(int[] indices){
13419 return super.sort(indices).toStat();
13420 }
13421
13422 public Stat reverse(){
13423 return super.reverse().toStat();
13424 }
13425
13426 public Stat concatenate(Stat xx){
13427 return super.concatenate(xx).toStat();
13428 }
13429
13430 public Stat concatenate(ArrayMaths xx){
13431 return super.concatenate(xx).toStat();
13432 }
13433
13434 public Stat concatenate(double[] xx){
13435 return super.concatenate(xx).toStat();
13436 }
13437
13438 public Stat concatenate(float[] xx){
13439 return super.concatenate(xx).toStat();
13440 }
13441
13442 public Stat concatenate(long[] xx){
13443 return super.concatenate(xx).toStat();
13444 }
13445
13446 public Stat concatenate(int[] xx){
13447 return super.concatenate(xx).toStat();
13448 }
13449
13450 public Stat concatenate(short[] xx){
13451 return super.concatenate(xx).toStat();
13452 }
13453
13454 public Stat concatenate(byte[] xx){
13455 return super.concatenate(xx).toStat();
13456 }
13457
13458 public Stat concatenate(char[] xx){
13459 return super.concatenate(xx).toStat();
13460 }
13461
13462 public Stat concatenate(Double[] xx){
13463 return super.concatenate(xx).toStat();
13464 }
13465
13466 public Stat concatenate(Float[] xx){
13467 return super.concatenate(xx).toStat();
13468 }
13469
13470 public Stat concatenate(Long[] xx){
13471 return super.concatenate(xx).toStat();
13472 }
13473
13474 public Stat concatenate(Integer[] xx){
13475 return super.concatenate(xx).toStat();
13476 }
13477
13478 public Stat concatenate(Short[] xx){
13479 return super.concatenate(xx).toStat();
13480 }
13481
13482 public Stat concatenate(Byte[] xx){
13483 return super.concatenate(xx).toStat();
13484 }
13485
13486 public Stat concatenate(Character[] xx){
13487 return super.concatenate(xx).toStat();
13488 }
13489
13490 public Stat concatenate(String[] xx){
13491 return super.concatenate(xx).toStat();
13492 }
13493
13494 public Stat concatenate(BigDecimal[] xx){
13495 return super.concatenate(xx).toStat();
13496 }
13497
13498 public Stat concatenate(BigInteger[] xx){
13499 return super.concatenate(xx).toStat();
13500 }
13501
13502 public Stat concatenate(Complex[] xx){
13503 return super.concatenate(xx).toStat();
13504 }
13505
13506 public Stat concatenate(Phasor[] xx){
13507 return super.concatenate(xx).toStat();
13508 }
13509
13510 public Stat truncate(int n){
13511 return super.truncate(n).toStat();
13512 }
13513
13514 public Stat floor(){
13515 return super.floor().toStat();
13516 }
13517
13518 public Stat ceil(){
13519 return super.ceil().toStat();
13520 }
13521
13522 public Stat rint(){
13523 return super.rint().toStat();
13524 }
13525
13526 public Stat randomize(){
13527 return super.randomize().toStat();
13528 }
13529
13530 public Stat randomise(){
13531 return super.randomize().toStat();
13532 }
13533
13534}
13535
13536// CLASSES NEEDED BY METHODS IN THE ABOVE Stat CLASS
13537
13538// Class to evaluate the linear correlation coefficient probablity function
13539// Needed in calls to Integration.gaussQuad
13540class CorrCoeff implements IntegralFunction{
13541
13542 public double a;
13543
13544 public double function(double x){
13545 double y = Math.pow((1.0D - x*x),a);
13546 return y;
13547 }
13548}
13549
13550// Class to evaluate the normal distribution function
13551class GaussianFunct implements RealRootFunction{
13552
13553 public double cfd = 0.0D;
13554 public double mean = 0.0D;
13555 public double sd = 0.0;
13556
13557 public double function(double x){
13558
13559 double y = cfd - Stat.gaussianCDF(mean, sd, x);
13560
13561 return y;
13562 }
13563}
13564
13565// Class to evaluate the Student's t-function
13566class StudentTfunct implements RealRootFunction{
13567 public int nu = 0;
13568 public double cfd = 0.0D;
13569
13570 public double function(double x){
13571
13572 double y = cfd - Stat.studentTcdf(x, nu);
13573
13574 return y;
13575 }
13576}
13577
13578// Class to evaluate the Gamma distribution function
13579class GammaFunct implements RealRootFunction{
13580 public double mu = 0.0D;
13581 public double beta = 0.0D;
13582 public double gamma = 0.0D;
13583 public double cfd = 0.0D;
13584
13585 public double function(double x){
13586
13587 double y = cfd - Stat.gammaCDF(mu, beta, gamma, x);
13588
13589 return y;
13590 }
13591}
13592
13593// Class to evaluate the Beta distribution function
13594class BetaFunct implements RealRootFunction{
13595 public double alpha = 0.0D;
13596 public double beta = 0.0D;
13597 public double min = 0.0D;
13598 public double max = 0.0D;
13599 public double cfd = 0.0D;
13600
13601 public double function(double x){
13602
13603 double y = cfd - Stat.betaCDF(min, max, alpha, beta, x);
13604
13605 return y;
13606 }
13607}
13608
13609// Class to evaluate the Erlang B equation
13610class ErlangBfunct implements RealRootFunction{
13611
13612 public double blockingProbability = 0.0D;
13613 public double totalResources = 0.0D;
13614
13615 public double function(double x){
13616 return blockingProbability - Stat.erlangBprobability(x, totalResources);
13617 }
13618}
13619
13620// Class to evaluate the Erlang C equation
13621class ErlangCfunct implements RealRootFunction{
13622
13623 public double nonZeroDelayProbability = 0.0D;
13624 public double totalResources = 0.0D;
13625
13626 public double function(double x){
13627 return nonZeroDelayProbability - Stat.erlangCprobability(x, totalResources);
13628 }
13629}
13630
13631// Class to evaluate the Engset probability equation
13632class EngsetProb implements RealRootFunction{
13633
13634 public double offeredTraffic = 0.0D;
13635 public double totalResources = 0.0D;
13636 public double numberOfSources = 0.0D;
13637
13638 public double function(double x){
13639 double mTerm = offeredTraffic/(numberOfSources - offeredTraffic*(1.0D - x));
13640 double pNumer = Stat.logFactorial(numberOfSources-1) - Stat.logFactorial(totalResources) - Stat.logFactorial(numberOfSources-1-totalResources);
13641 double pDenom = 0.0D;
13642 double iDenom = 0.0D;
13643 double iCount = 0.0D;
13644 double pTerm = 0.0D;
13645
13646 while(iCount<=totalResources){
13647 iDenom = Stat.logFactorial(numberOfSources-1) - Stat.logFactorial(iCount) - Stat.logFactorial(numberOfSources-1-iCount);
13648 iDenom += (iCount-totalResources)*Math.log(mTerm);
13649 pDenom += Math.exp(iDenom);
13650 iCount += 1.0D;
13651 }
13652 pTerm = Math.exp(pNumer - Math.log(pDenom));
13653
13654 return x - pTerm;
13655 }
13656}
13657
13658
13659// Class to evaluate the Engset load equation
13660class EngsetLoad implements RealRootFunction{
13661
13662 public double blockingProbability = 0.0D;
13663 public double totalResources = 0.0D;
13664 public double numberOfSources = 0.0D;
13665
13666
13667 public double function(double x){
13668 return blockingProbability - Stat.engsetProbability(x, totalResources, numberOfSources);
13669 }
13670}
13671
13672// Class to evaluate the chi-square distribution function
13673class ChiSquareFunct implements RealRootFunction{
13674
13675 public double cfd = 0.0D;
13676 public int nu = 0;
13677
13678 public double function(double x){
13679
13680 double y = cfd - Stat.chiSquareCDF(x, nu);
13681
13682 return y;
13683 }
13684}
13685
13686// Class to evaluate the F-distribution function
13687class FdistribtionFunct implements RealRootFunction{
13688
13689 public double cfd = 0.0D;
13690 public int nu1 = 0;
13691 public int nu2 = 0;
13692
13693 public double function(double x){
13694
13695 double y = cfd - (1.0 - Stat.fCompCDF(x, nu1, nu2));
13696 // double y = cfd - Stat.fCompCDF(x, nu1, nu2);
13697
13698 return y;
13699 }
13700}
13701
13702// Class to evaluate the two parameter log-normal distribution function
13703class LogNormalTwoParFunct implements RealRootFunction{
13704
13705 public double cfd = 0.0D;
13706 public double mu = 0.0D;
13707 public double sigma = 0.0D;
13708
13709 public double function(double x){
13710
13711 double y = cfd - Stat.logNormalCDF(mu, sigma, x);
13712
13713 return y;
13714 }
13715}
13716
13717// Class to evaluate the three parameter log-normal distribution function
13718class LogNormalThreeParFunct implements RealRootFunction{
13719
13720 public double cfd = 0.0D;
13721 public double alpha = 0.0D;
13722 public double beta = 0.0D;
13723 public double gamma = 0.0D;
13724
13725 public double function(double x){
13726
13727 double y = cfd - Stat.logNormalThreeParCDF(alpha, beta, gamma, x);
13728
13729 return y;
13730 }
13731
13732}
13733
13734// Class to evaluate inverse gamma function
13735class InverseGammaFunct implements RealRootFunction{
13736
13737 public double gamma = 0.0D;
13738
13739 public double function(double x){
13740
13741 double y = gamma - Stat.gamma(x);
13742
13743 return y;
13744 }
13745}
13746
13747// Class to evaluate complementary regularised incomplte gamma function
13748class CrigFunct implements IntegralFunction{
13749
13750 private double a = 0.0D;
13751 private double b = 0.0D;
13752
13753 public double function(double x){
13754 double y = -x + (a-1.0)*Math.log(x) - b;
13755 y = Math.exp(y);
13756 return y;
13757 }
13758
13759 public void setA(double a){
13760 this.a = a;
13761 }
13762
13763 public void setB(double b){
13764 this.b = b;
13765 }
13766}
Note: See TracBrowser for help on using the repository browser.