1 | /*
|
---|
2 | * Class BoxCox
|
---|
3 | *
|
---|
4 | * USAGE: Box-Cox Transformation
|
---|
5 | *
|
---|
6 | * WRITTEN BY: Dr Michael Thomas Flanagan
|
---|
7 | *
|
---|
8 | * DATE: July - August 2008
|
---|
9 | * AMENDED: 2-12 September 2008, 15-19 December 2010, 2-5 January 2011, 20 January 2011
|
---|
10 | *
|
---|
11 | * DOCUMENTATION:
|
---|
12 | * See Michael Thomas Flanagan's Java library on-line web page:
|
---|
13 | * http://www.ee.ucl.ac.uk/~mflanaga/java/BoxCox.html
|
---|
14 | * http://www.ee.ucl.ac.uk/~mflanaga/java/
|
---|
15 | *
|
---|
16 | * Copyright (c) 2008 - 2011 Michael Thomas Flanagan
|
---|
17 | *
|
---|
18 | * PERMISSION TO COPY:
|
---|
19 | *
|
---|
20 | * Permission to use, copy and modify this software and its documentation for NON-COMMERCIAL purposes is granted, without fee,
|
---|
21 | * provided that an acknowledgement to the author, Dr Michael Thomas Flanagan at www.ee.ucl.ac.uk/~mflanaga, appears in all copies
|
---|
22 | * and associated documentation or publications.
|
---|
23 | *
|
---|
24 | * Redistributions of the source code of this source code, or parts of the source codes, must retain the above copyright notice, this list of conditions
|
---|
25 | * and the following disclaimer and requires written permission from the Michael Thomas Flanagan:
|
---|
26 | *
|
---|
27 | * Redistribution in binary form of all or parts of this class must reproduce the above copyright notice, this list of conditions and
|
---|
28 | * the following disclaimer in the documentation and/or other materials provided with the distribution and requires written permission from the Michael Thomas Flanagan:
|
---|
29 | *
|
---|
30 | * Dr Michael Thomas Flanagan makes no representations about the suitability or fitness of the software for any or for a particular purpose.
|
---|
31 | * Dr Michael Thomas Flanagan shall not be liable for any damages suffered as a result of using, modifying or distributing this software
|
---|
32 | * or its derivatives.
|
---|
33 | *
|
---|
34 | ***************************************************************************************/
|
---|
35 |
|
---|
36 | package agents.anac.y2015.agentBuyogV2.flanagan.analysis;
|
---|
37 |
|
---|
38 | import java.math.BigDecimal;
|
---|
39 | import java.math.BigInteger;
|
---|
40 | import java.util.*;
|
---|
41 |
|
---|
42 | import agents.anac.y2015.agentBuyogV2.flanagan.analysis.Regression;
|
---|
43 | import agents.anac.y2015.agentBuyogV2.flanagan.analysis.Stat;
|
---|
44 | import agents.anac.y2015.agentBuyogV2.flanagan.io.FileOutput;
|
---|
45 | import agents.anac.y2015.agentBuyogV2.flanagan.math.ArrayMaths;
|
---|
46 | import agents.anac.y2015.agentBuyogV2.flanagan.math.Conv;
|
---|
47 | import agents.anac.y2015.agentBuyogV2.flanagan.math.Fmath;
|
---|
48 | import agents.anac.y2015.agentBuyogV2.flanagan.math.Maximization;
|
---|
49 | import agents.anac.y2015.agentBuyogV2.flanagan.math.MaximizationFunction;
|
---|
50 | import agents.anac.y2015.agentBuyogV2.flanagan.plot.*;
|
---|
51 |
|
---|
52 | import java.text.*;
|
---|
53 |
|
---|
54 |
|
---|
55 | public class BoxCox{
|
---|
56 |
|
---|
57 | private double[] originalData = null; // original data to be analysed as Gaussian
|
---|
58 | private double[] sortedOriginalData = null; // ordered original data
|
---|
59 | private double[] standardizedOriginalData = null; // standardized original data
|
---|
60 | private Stat sod = null; // Stat instance of the standardizedOriginalData
|
---|
61 | private double[] shiftedStandardizedOriginalData = null; // data shifted to ensure all points are positive
|
---|
62 | private int[] originalIndices = null; // original indices before data are ordered into ascending order
|
---|
63 | private int nData = 0; // number of data points
|
---|
64 |
|
---|
65 | private double originalRange = 0.0; // original data range
|
---|
66 | private double originalMinimum = 0.0; // original data minimum
|
---|
67 | private double originalMaximum = 0.0; // original data maximum
|
---|
68 | private double originalMean = 0.0; // original data mean
|
---|
69 | private double originalMedian = 0.0; // original original data median
|
---|
70 | private double originalStandardDeviation = 0.0; // original data standard deviation
|
---|
71 | private double originalVariance = 0.0; // original data variance
|
---|
72 | private double originalMomentSkewness = 0.0; // original data moment skewness
|
---|
73 | private double originalMedianSkewness = 0.0; // original data median skewness
|
---|
74 | private double originalQuartileSkewness = 0.0; // original data quartile skewness
|
---|
75 | private double originalExcessKurtosis = 0.0; // original data excess kurtosis
|
---|
76 |
|
---|
77 | private double standardizedOriginalRange = 0.0; // standardized original data range
|
---|
78 | private double standardizedOriginalMinimum = 0.0; // standardized original data minimum
|
---|
79 | private double standardizedOriginalMaximum = 0.0; // standardized original data maximum
|
---|
80 | private double standardizedOriginalMean = 0.0; // standardized original original data mean
|
---|
81 | private double standardizedOriginalMedian = 0.0; // standardized original original data median
|
---|
82 | private double standardizedOriginalStandardDeviation = 1.0; // standardized original data standard deviation
|
---|
83 | private double standardizedOriginalVariance = 1.0; // standardized original data variance
|
---|
84 | private double standardizedOriginalMomentSkewness = 0.0; // standardized original data moment skewness
|
---|
85 | private double standardizedOriginalMedianSkewness = 0.0; // standardized original data median skewness
|
---|
86 | private double standardizedOriginalQuartileSkewness = 0.0; // standardized original data quartile skewness
|
---|
87 | private double standardizedOriginalExcessKurtosis = 0.0; // standardized original data excess kurtosis
|
---|
88 |
|
---|
89 | private double originalSampleR = 0.0; // Probabilty plot correlation coefficient for the original data
|
---|
90 | private double originalIntercept = 0.0; // Probabilty plot intercept for the original data
|
---|
91 | private double originalGradient = 0.0; // Probabilty plot gradient for the original data
|
---|
92 | private double originalInterceptError = 0.0; // Estimated error of the probabilty plot intercept for the original data
|
---|
93 | private double originalGradientError = 0.0; // Estimated error of the probabilty plot gradient for the original data
|
---|
94 |
|
---|
95 | private boolean initializationDone = false; // = true when initialization of`the data is complete
|
---|
96 |
|
---|
97 | private double[] transformedData = null; // Box-Cox transformed data
|
---|
98 | private double[] standardizedTransformedData = null; // standardized Box-Cox transformed data
|
---|
99 | private double[] scaledTransformedData = null; // Box-Cox transformed data scaled to original mean and standard deviation
|
---|
100 | private double[] sortedScaledTransformedData = null; // Box-Cox transformed data scaled to original mean and standard deviation sorted into ascending order
|
---|
101 |
|
---|
102 | private double transformedRange = 0.0; // scaled transformed data range
|
---|
103 | private double transformedMinimum = 0.0; // scaled transformed data minimum
|
---|
104 | private double transformedMaximum = 0.0; // scaled transformed data maximum
|
---|
105 | private double transformedMean = 0.0; // scaled transformed data mean
|
---|
106 | private double transformedStandardDeviation = 0.0; // scaled transformed data standard deviation
|
---|
107 | private double transformedMedian = 0.0; // scaled transformed data median
|
---|
108 | private double transformedVariance = 0.0; // scaled transformed data variance
|
---|
109 | private double transformedMomentSkewness = 0.0; // scaled transformed data moment skewness
|
---|
110 | private double transformedMedianSkewness = 0.0; // scaled transformed data median skewness
|
---|
111 | private double transformedQuartileSkewness = 0.0; // scaled transformed data quartile skewness
|
---|
112 | private double transformedExcessKurtosis = 0.0; // scaled transformed data excess kurtosis
|
---|
113 |
|
---|
114 | private double standardizedTransformedRange = 0.0; // standardized transformed data range
|
---|
115 | private double standardizedTransformedMinimum = 0.0; // standardized transformed data minimum
|
---|
116 | private double standardizedTransformedMaximum = 0.0; // standardized transformed data maximum
|
---|
117 | private double standardizedTransformedMean = 0.0; // standardized transformed data mean
|
---|
118 | private double standardizedTransformedMedian = 0.0; // standardized transformed data median
|
---|
119 | private double standardizedTransformedStandardDeviation = 1.0; // standardized transformed data standard deviation
|
---|
120 | private double standardizedTransformedVariance = 1.0; // standardized transformed data variance
|
---|
121 | private double standardizedTransformedMomentSkewness = 0.0; // standardized transformed data moment skewness
|
---|
122 | private double standardizedTransformedMedianSkewness = 0.0; // standardized transformed data median skewness
|
---|
123 | private double standardizedTransformedQuartileSkewness = 0.0; // standardized transformed data quartile skewness
|
---|
124 | private double standardizedTransformedExcessKurtosis = 0.0; // standardized transformed data excess kurtosis
|
---|
125 |
|
---|
126 | private double[] inverseData = null; // inverse transformed data
|
---|
127 | private double lambdaThree = 0.0; // shift, if needed, to allow inverse transform
|
---|
128 |
|
---|
129 | private double lambdaOne = 0.0; // Box-Cox lambdaOne
|
---|
130 | private double lambdaTwo = 0.0; // Box-Cox lambdaTwo
|
---|
131 |
|
---|
132 | private double transformedSampleR = 0.0; // Probabilty plot correlation coefficient for the transformed data
|
---|
133 | private double transformedIntercept = 0.0; // Probabilty plot intercept for the transformed data
|
---|
134 | private double transformedGradient = 0.0; // Probabilty plot gradient for the transformed data
|
---|
135 | private double transformedInterceptError = 0.0; // Estimated error of the probabilty plot intercept for the transformed data
|
---|
136 | private double transformedGradientError = 0.0; // Estimated error of the probabilty plot gradient for the transformed data
|
---|
137 |
|
---|
138 | private double[] uniformOrderMedians = null; // uniform order statistic medians
|
---|
139 | private double[] gaussianOrderMedians = null; // Gaussian order statistic medians
|
---|
140 |
|
---|
141 | private boolean transformDone = false; // = true when Box-Cox transform performed
|
---|
142 | private boolean inverseDone = false; // = true when Box-Cox inverse transform performed
|
---|
143 |
|
---|
144 |
|
---|
145 |
|
---|
146 | // CONSTRUCTORS
|
---|
147 | public BoxCox(double[] originalData){
|
---|
148 | this.sod = new Stat(originalData);
|
---|
149 | }
|
---|
150 |
|
---|
151 | public BoxCox(float[] originalData){
|
---|
152 | this.sod = new Stat(originalData);
|
---|
153 | }
|
---|
154 |
|
---|
155 | public BoxCox(int[] originalData){
|
---|
156 | this.sod = new Stat(originalData);
|
---|
157 | }
|
---|
158 |
|
---|
159 | public BoxCox(long[] originalData){
|
---|
160 | this.sod = new Stat(originalData);
|
---|
161 | }
|
---|
162 |
|
---|
163 | public BoxCox(short[] originalData){
|
---|
164 | this.sod = new Stat(originalData);
|
---|
165 | }
|
---|
166 |
|
---|
167 | public BoxCox(byte[] originalData){
|
---|
168 | this.sod = new Stat(originalData);
|
---|
169 | }
|
---|
170 |
|
---|
171 | public BoxCox(BigDecimal[] originalData){
|
---|
172 | this.sod = new Stat(originalData);
|
---|
173 | }
|
---|
174 |
|
---|
175 | public BoxCox(BigInteger[] originalData){
|
---|
176 | this.sod = new Stat(originalData);
|
---|
177 | }
|
---|
178 |
|
---|
179 | public BoxCox(Stat originalData){
|
---|
180 | this.sod = originalData;
|
---|
181 | }
|
---|
182 |
|
---|
183 | public BoxCox(ArrayMaths amoriginalData){
|
---|
184 | this.sod = amoriginalData.toStat();
|
---|
185 | }
|
---|
186 |
|
---|
187 | public BoxCox(ArrayList<Object> originalData){
|
---|
188 | this.sod = new Stat(originalData);
|
---|
189 | }
|
---|
190 |
|
---|
191 | public BoxCox(Vector<Object> originalData){
|
---|
192 | this.sod = new Stat(originalData);
|
---|
193 | }
|
---|
194 |
|
---|
195 | // set denominator of variances to n
|
---|
196 | public void setDenominatorToN(){
|
---|
197 | Stat.setStaticDenominatorToN();
|
---|
198 | }
|
---|
199 |
|
---|
200 | // Initialise original data arrays
|
---|
201 | private void initialize(){
|
---|
202 |
|
---|
203 | // store entered data as instance variable
|
---|
204 | this.originalData = this.sod.array_as_double();
|
---|
205 |
|
---|
206 | // Calculate original data statistics
|
---|
207 | this.originalMinimum = this.sod.minimum();
|
---|
208 | this.originalMaximum = this.sod.maximum();
|
---|
209 | this.originalMedian = this.sod.median();
|
---|
210 | if(this.originalMinimum==this.originalMaximum)throw new IllegalArgumentException("A Box-Cox transformation cannot be performed on a data array of identical values");
|
---|
211 | this.originalRange = this.originalMaximum - this.originalMinimum;
|
---|
212 | this.originalMean = this.sod.mean();
|
---|
213 | this.originalStandardDeviation = this.sod.standardDeviation();
|
---|
214 | this.originalVariance = this.sod.variance();
|
---|
215 | this.originalMomentSkewness = this.sod.momentSkewness();
|
---|
216 | this.originalMedianSkewness = this.sod.medianSkewness();
|
---|
217 | this.originalQuartileSkewness = this.sod.quartileSkewness();
|
---|
218 | this.originalExcessKurtosis = this.sod.excessKurtosis();
|
---|
219 |
|
---|
220 | // Store original data sorted into ascending order
|
---|
221 | Stat sorted = this.sod.sort();
|
---|
222 | this.sortedOriginalData = sorted.array();
|
---|
223 | this.originalIndices = sorted.originalIndices();
|
---|
224 |
|
---|
225 | // Standardize and store standardized data
|
---|
226 | this.standardizedOriginalData = this.sod.standardize();
|
---|
227 |
|
---|
228 | // Calculate standardized original data statistics
|
---|
229 | this.standardizedOriginalMinimum = this.sod.minimum();
|
---|
230 | this.standardizedOriginalMaximum = this.sod.maximum();
|
---|
231 | this.standardizedOriginalMedian = this.sod.median();
|
---|
232 | this.standardizedOriginalRange = this.standardizedOriginalMaximum - this.standardizedOriginalMinimum;
|
---|
233 | this.standardizedOriginalMean = 0.0;
|
---|
234 | this.standardizedOriginalStandardDeviation = 1.0;
|
---|
235 | this.standardizedOriginalVariance = 1.0;
|
---|
236 | this.standardizedOriginalMomentSkewness = this.sod.momentSkewness();
|
---|
237 | this.standardizedOriginalMedianSkewness = this.sod.medianSkewness();
|
---|
238 | this.standardizedOriginalQuartileSkewness = this.sod.quartileSkewness();
|
---|
239 | this.standardizedOriginalExcessKurtosis = this.sod.excessKurtosis();
|
---|
240 |
|
---|
241 | // Numbet of data points
|
---|
242 | this.nData = this.originalData.length;
|
---|
243 |
|
---|
244 | // Calculate uniform order statistic medians
|
---|
245 | this.uniformOrderMedians = Stat.uniformOrderStatisticMedians(this.nData);
|
---|
246 |
|
---|
247 | // Calculate Gaussian N[0,1] order statistic medians
|
---|
248 | this.gaussianOrderMedians = Stat.gaussianOrderStatisticMedians(this.nData);
|
---|
249 |
|
---|
250 | // calculate the correlation coefficient of the probability plot for the untransformed data
|
---|
251 | Regression reg = new Regression(this.gaussianOrderMedians, ((new ArrayMaths(this.standardizedOriginalData)).sort().array()));
|
---|
252 | reg.linear();
|
---|
253 | this.originalSampleR = reg.getSampleR();
|
---|
254 | double[] coeff = reg.getBestEstimates();
|
---|
255 | this.originalIntercept = coeff[0];
|
---|
256 | this.originalGradient = coeff[1];
|
---|
257 | coeff = reg.getBestEstimatesErrors();
|
---|
258 | this.originalInterceptError = coeff[0];
|
---|
259 | this.originalGradientError = coeff[1];
|
---|
260 |
|
---|
261 | this.initializationDone = true;
|
---|
262 | }
|
---|
263 |
|
---|
264 | // BOX-COX TRANSFORMATION
|
---|
265 | private double[] transform(){
|
---|
266 |
|
---|
267 | // initialize arrays and analyse original data
|
---|
268 | if(!this.initializationDone)this.initialize();
|
---|
269 |
|
---|
270 | // Shift data to ensure all values are greater than zero
|
---|
271 | this.lambdaTwo = 0.1*this.standardizedOriginalRange - this.standardizedOriginalMinimum;
|
---|
272 | Stat st1 = this.sod.plus(this.lambdaTwo);
|
---|
273 | this.shiftedStandardizedOriginalData = st1.getArray_as_double();
|
---|
274 |
|
---|
275 | // Create an instance of the BoxCoxFunction for Maximization
|
---|
276 | BoxCoxFunction bcf = new BoxCoxFunction();
|
---|
277 | bcf.shiftedData = this.shiftedStandardizedOriginalData;
|
---|
278 | bcf.nData = this.nData;
|
---|
279 | bcf.yTransform = new double[this.nData];
|
---|
280 | bcf.gaussianOrderMedians = this.gaussianOrderMedians;
|
---|
281 |
|
---|
282 | // Create an instance of Maximization
|
---|
283 | Maximization max = new Maximization();
|
---|
284 |
|
---|
285 | // Initial estimate of lambdaOne
|
---|
286 | double[] start = {1.0};
|
---|
287 |
|
---|
288 | // Initial step size in maximization search
|
---|
289 | double[] step = {0.3};
|
---|
290 |
|
---|
291 | // Tolerance for maximization search termination
|
---|
292 | double maxzTol = 1e-9;
|
---|
293 |
|
---|
294 | // Maximiztaion of the Gaussian probabilty plot correlation coefficient varying lambdaOne
|
---|
295 | max.nelderMead(bcf, start, step, maxzTol);
|
---|
296 |
|
---|
297 | // coeff[0] = value of lambdaOne for a maximum Gaussian probabilty plot correlation coefficient
|
---|
298 | double[] coeff = max.getParamValues();
|
---|
299 | double lambda1 = coeff[0];
|
---|
300 |
|
---|
301 | //maximum Gaussian probabilty plot correlation coefficient
|
---|
302 | double sampleR1 = max.getMaximum();
|
---|
303 |
|
---|
304 | // Repeat maximization starting equidistant from the final value of lambdaOne on the opposite side from the starting estimate
|
---|
305 | start[0] = lambda1 - (start[0] - lambda1);
|
---|
306 | max.nelderMead(bcf, start, step, maxzTol);
|
---|
307 | coeff = max.getParamValues();
|
---|
308 | this.lambdaOne = coeff[0];
|
---|
309 | this.transformedSampleR = max.getMaximum();
|
---|
310 |
|
---|
311 | // Choose solution with the largest Gaussian probabilty plot correlation coefficient
|
---|
312 | if(sampleR1>this.transformedSampleR){
|
---|
313 | this.transformedSampleR = sampleR1;
|
---|
314 | this.lambdaOne = lambda1;
|
---|
315 | }
|
---|
316 |
|
---|
317 | // Store transformed data
|
---|
318 | this.transformedData = new double[this.nData];
|
---|
319 | if(this.lambdaOne==0.0){
|
---|
320 | for(int i=0; i<this.nData; i++){
|
---|
321 | this.transformedData[i] = Math.exp(this.shiftedStandardizedOriginalData[i]);
|
---|
322 | }
|
---|
323 | }
|
---|
324 | else{
|
---|
325 | for(int i=0; i<this.nData; i++){
|
---|
326 | this.transformedData[i] = (Math.pow(this.shiftedStandardizedOriginalData[i], this.lambdaOne) - 1.0)/this.lambdaOne;
|
---|
327 | }
|
---|
328 | }
|
---|
329 |
|
---|
330 | // Standardize transformed data
|
---|
331 | this.standardizedTransformedData = (new Stat(this.transformedData)).standardize();
|
---|
332 |
|
---|
333 | // Calculate standardized transformed data statistics
|
---|
334 | this.standardizedTransformedDataStatistics(this.standardizedTransformedData);
|
---|
335 |
|
---|
336 | // Obtain the intercept and gradient of the Gaussian probabilty plot
|
---|
337 | Stat st4 = new Stat(this.standardizedTransformedData);
|
---|
338 | st4 = st4.sort();
|
---|
339 | double[] ordered = st4.array();
|
---|
340 | Regression reg = new Regression(this.gaussianOrderMedians, ordered);
|
---|
341 | reg.linear();
|
---|
342 | coeff = reg.getBestEstimates();
|
---|
343 | this.transformedIntercept = coeff[0];
|
---|
344 | this.transformedGradient = coeff[1];
|
---|
345 | coeff = reg.getBestEstimatesErrors();
|
---|
346 | this.transformedInterceptError = coeff[0];
|
---|
347 | this.transformedGradientError = coeff[1];
|
---|
348 |
|
---|
349 | // Adust mean and standard deviation of the transformed data to match those of the entered data
|
---|
350 | this.scaledTransformedData = Stat.scale(this.standardizedTransformedData, this.originalMean, this.originalStandardDeviation);
|
---|
351 |
|
---|
352 | // Calculate transformed data statistics
|
---|
353 | transformedDataStatistics(this.scaledTransformedData);
|
---|
354 |
|
---|
355 | // end of method and return transformed data
|
---|
356 | this.transformDone = true;
|
---|
357 | return this.transformedData;
|
---|
358 |
|
---|
359 | }
|
---|
360 |
|
---|
361 | // Calculate transformed data statistics
|
---|
362 | private void transformedDataStatistics(double[] data){
|
---|
363 | // Calculate transformed data statistics
|
---|
364 | Stat st2 = new Stat(data);
|
---|
365 | this.transformedMinimum = st2.minimum();
|
---|
366 | this.transformedMaximum = st2.maximum();
|
---|
367 | this.transformedMedian = st2.median();
|
---|
368 | this.transformedRange = this.transformedMaximum - this.transformedMinimum;
|
---|
369 | this.transformedMean = st2.mean();
|
---|
370 | this.transformedStandardDeviation = st2.standardDeviation();
|
---|
371 | this.transformedVariance = st2.variance();
|
---|
372 | this.transformedMomentSkewness = st2.momentSkewness();
|
---|
373 | this.transformedMedianSkewness = st2.medianSkewness();
|
---|
374 | this.transformedQuartileSkewness = st2.quartileSkewness();
|
---|
375 | this.transformedExcessKurtosis = st2.excessKurtosis();
|
---|
376 |
|
---|
377 | // Arrange scaled transformed data into ascending order
|
---|
378 | Stat st5 = new Stat(data);
|
---|
379 | this.sortedScaledTransformedData = (st5.sort()).array();
|
---|
380 | }
|
---|
381 |
|
---|
382 | // Calculate standardized transformed data statistics
|
---|
383 | private void standardizedTransformedDataStatistics(double[] data){
|
---|
384 | // Calculate standardized transformed data statistics
|
---|
385 | Stat st3 = new Stat(data);
|
---|
386 | this.standardizedTransformedMinimum = st3.minimum();
|
---|
387 | this.standardizedTransformedMaximum = st3.maximum();
|
---|
388 | this.standardizedTransformedMedian = st3.median();
|
---|
389 | this.standardizedTransformedRange = this.standardizedTransformedMaximum - this.standardizedTransformedMinimum;
|
---|
390 | this.standardizedTransformedMean = 0.0;
|
---|
391 | this.standardizedTransformedStandardDeviation = 1.0;
|
---|
392 | this.standardizedTransformedVariance = 1.0;
|
---|
393 | this.standardizedTransformedMomentSkewness = st3.momentSkewness();
|
---|
394 | this.standardizedTransformedMedianSkewness = st3.medianSkewness();
|
---|
395 | this.standardizedTransformedQuartileSkewness = st3.quartileSkewness();
|
---|
396 | this.standardizedTransformedExcessKurtosis = st3.excessKurtosis();
|
---|
397 | }
|
---|
398 |
|
---|
399 |
|
---|
400 | // INVERSE BOX-COX TRANSFORMATION
|
---|
401 | // Inverse transform, lambdaOne and lambdaTwo provided
|
---|
402 | public double[] inverseTransform(double lambdaOne, double lambdaTwo){
|
---|
403 | this.lambdaOne = lambdaOne;
|
---|
404 | this.lambdaTwo = lambdaTwo;
|
---|
405 |
|
---|
406 | // initialize arrays and analyse original data
|
---|
407 | if(!this.initializationDone)this.initialize();
|
---|
408 | if(this.originalData==null)throw new IllegalArgumentException("No data has been entered (via a constructor)");
|
---|
409 | this.inverseData = new double[this.nData];
|
---|
410 |
|
---|
411 | // Check for negative values tat will not inverse transform
|
---|
412 | double[] shiftedData = Conv.copy(this.originalData);
|
---|
413 | if(this.originalMinimum<0.0){
|
---|
414 | if(Fmath.isNaN(Math.pow((this.originalMinimum*this.lambdaOne + 1.0), 1.0/this.lambdaOne))){
|
---|
415 | this.lambdaThree = -0.999/this.lambdaOne - this.originalMinimum;
|
---|
416 | for(int i=0; i<this.nData; i++)shiftedData[i] += this.lambdaThree;
|
---|
417 | }
|
---|
418 | }
|
---|
419 |
|
---|
420 | // inverse transform
|
---|
421 | if(this.lambdaOne==0.0){
|
---|
422 | for(int i=0; i<this.nData; i++){
|
---|
423 | this.inverseData[i] = Math.exp(shiftedData[i]) - this.lambdaTwo;
|
---|
424 | }
|
---|
425 | }
|
---|
426 | else{
|
---|
427 | for(int i=0; i<this.nData; i++){
|
---|
428 | this.inverseData[i] = Math.pow((shiftedData[i]*this.lambdaOne + 1.0), 1.0/this.lambdaOne) - this.lambdaTwo;
|
---|
429 | }
|
---|
430 | }
|
---|
431 |
|
---|
432 | // Calculate inverse transformed data statistics
|
---|
433 | this.transformedData = Conv.copy(this.inverseData);
|
---|
434 | transformedDataStatistics(inverseData);
|
---|
435 |
|
---|
436 | // Standardize transformed data
|
---|
437 | this.standardizedTransformedData = (new Stat(this.transformedData)).standardize();
|
---|
438 |
|
---|
439 | // Calculate standardized transformed data statistics
|
---|
440 | this.standardizedTransformedDataStatistics(this.standardizedTransformedData);
|
---|
441 |
|
---|
442 | this.inverseDone = true;
|
---|
443 |
|
---|
444 | return this.inverseData;
|
---|
445 | }
|
---|
446 |
|
---|
447 | // Inverse transform, lambdaOne provided, lambdaTwo = 0
|
---|
448 | public double[] inverseTransform(double lambdaOne){
|
---|
449 | return this.inverseTransform(lambdaOne, 0.0);
|
---|
450 | }
|
---|
451 |
|
---|
452 |
|
---|
453 |
|
---|
454 | // FIXED VALUE BOX-COX TRANSFORMATION
|
---|
455 | // Fixed value transform, lambdaOne and lambdaTwo provided
|
---|
456 | public double[] fixedValueTransform(double lambdaOne, double lambdaTwo){
|
---|
457 | this.lambdaOne = lambdaOne;
|
---|
458 | this.lambdaTwo = lambdaTwo;
|
---|
459 |
|
---|
460 | // initialize arrays and analyse original data
|
---|
461 | if(!this.initializationDone)this.initialize();
|
---|
462 | if(this.originalData==null)throw new IllegalArgumentException("No data has been entered (via a constructor)");
|
---|
463 |
|
---|
464 | // check for negative values
|
---|
465 | if((this.originalMinimum + this.lambdaTwo)<0.0)throw new IllegalArgumentException("Negative (data plus lambdaTwo) value, " + (this.originalMinimum + this.lambdaTwo));
|
---|
466 |
|
---|
467 | // transform
|
---|
468 | this.transformedData = new double[this.nData];
|
---|
469 | if(this.lambdaOne==0.0){
|
---|
470 | for(int i=0; i<this.nData; i++){
|
---|
471 | this.transformedData[i] = Math.exp(this.shiftedStandardizedOriginalData[i]);
|
---|
472 | }
|
---|
473 | }
|
---|
474 | else{
|
---|
475 | for(int i=0; i<this.nData; i++){
|
---|
476 | this.transformedData[i] = (Math.pow(this.shiftedStandardizedOriginalData[i], this.lambdaOne) - 1.0)/this.lambdaOne;
|
---|
477 | }
|
---|
478 | }
|
---|
479 |
|
---|
480 | // Standardize transformed data
|
---|
481 | this.standardizedTransformedData = (new Stat(this.transformedData)).standardize();
|
---|
482 |
|
---|
483 | // Calculate standardized transformed data statistics
|
---|
484 | this.standardizedTransformedDataStatistics(this.standardizedTransformedData);
|
---|
485 |
|
---|
486 | // Obtain the intercept and gradient of the Gaussian probabilty plot
|
---|
487 | Stat st4 = new Stat(this.standardizedTransformedData);
|
---|
488 | st4 = st4.sort();
|
---|
489 | double[] ordered = st4.array();
|
---|
490 | Regression reg = new Regression(this.gaussianOrderMedians, ordered);
|
---|
491 | reg.linear();
|
---|
492 | double[] coeff = reg.getBestEstimates();
|
---|
493 | this.transformedIntercept = coeff[0];
|
---|
494 | this.transformedGradient = coeff[1];
|
---|
495 |
|
---|
496 | // Adust mean and standard deviation of the transformed data to match those of the entered data
|
---|
497 | this.scaledTransformedData = Stat.scale(this.standardizedTransformedData, this.originalMean, this.originalStandardDeviation);
|
---|
498 |
|
---|
499 | // Calculate transformed data statistics
|
---|
500 | this.transformedDataStatistics(this.scaledTransformedData);
|
---|
501 |
|
---|
502 | // end of method and return transformed data
|
---|
503 | this.transformDone = true;
|
---|
504 | return this.transformedData;
|
---|
505 |
|
---|
506 | }
|
---|
507 |
|
---|
508 | // Fixed value transform, lambdaOne and lambdaTwo provided
|
---|
509 | public double[] fixedValueTransform(double lambdaOne){
|
---|
510 | return this.fixedValueTransform(lambdaOne, 0.0);
|
---|
511 | }
|
---|
512 |
|
---|
513 |
|
---|
514 |
|
---|
515 | // RETURN RESULTS
|
---|
516 | // Return lambdaOne
|
---|
517 | public double lambdaOne(){
|
---|
518 | if(!this.transformDone)this.transform();
|
---|
519 | return this.lambdaOne;
|
---|
520 | }
|
---|
521 |
|
---|
522 | // Return lambdaTwo
|
---|
523 | public double lambdaTwo(){
|
---|
524 | if(!this.transformDone)this.transform();
|
---|
525 | return this.lambdaTwo;
|
---|
526 | }
|
---|
527 |
|
---|
528 | // Return lambdaThree
|
---|
529 | public double lambdaThree(){
|
---|
530 | if(!this.inverseDone){
|
---|
531 | System.out.println("BoxCox: method lambdaThree: no inverse transform has been performed, zero returned");
|
---|
532 | }
|
---|
533 | return this.lambdaThree;
|
---|
534 | }
|
---|
535 |
|
---|
536 | // Return Gaussian probabilty plot correlation coefficient of the transformed data
|
---|
537 | public double transformedCorrelationCoefficient(){
|
---|
538 | if(!this.transformDone)this.transform();
|
---|
539 | return this.transformedSampleR;
|
---|
540 | }
|
---|
541 |
|
---|
542 | // Return Gaussian probabilty plot gradient of the transformed data
|
---|
543 | public double transformedGradient(){
|
---|
544 | if(!this.transformDone)this.transform();
|
---|
545 | return this.transformedGradient;
|
---|
546 | }
|
---|
547 |
|
---|
548 | // Return estimated error of the Gaussian probabilty plot gradient of the transformed data
|
---|
549 | public double transformedGradientError(){
|
---|
550 | if(!this.transformDone)this.transform();
|
---|
551 | return this.transformedGradientError;
|
---|
552 | }
|
---|
553 |
|
---|
554 | // Return Gaussian probabilty plot intercept of the transformed data
|
---|
555 | public double transformedIntercept(){
|
---|
556 | if(!this.transformDone)this.transform();
|
---|
557 | return this.transformedIntercept;
|
---|
558 | }
|
---|
559 |
|
---|
560 | // Return estimated error of the Gaussian probabilty plot intercept of the transformed data
|
---|
561 | public double transformedInterceptError(){
|
---|
562 | if(!this.transformDone)this.transform();
|
---|
563 | return this.transformedInterceptError;
|
---|
564 | }
|
---|
565 |
|
---|
566 | // Return Gaussian probabilty plot correlation coefficient of the original data
|
---|
567 | public double originalCorrelationCoefficient(){
|
---|
568 | if(!this.initializationDone)this.initialize();
|
---|
569 | return this.originalSampleR;
|
---|
570 | }
|
---|
571 |
|
---|
572 | // Return Gaussian probabilty plot gradient of the orioginal data
|
---|
573 | public double originalGradient(){
|
---|
574 | if(!this.initializationDone)this.initialize();
|
---|
575 | return this.originalGradient;
|
---|
576 | }
|
---|
577 |
|
---|
578 | // Return estimated error of the Gaussian probabilty plot gradient of the orioginal data
|
---|
579 | public double originalGradientError(){
|
---|
580 | if(!this.initializationDone)this.initialize();
|
---|
581 | return this.originalGradientError;
|
---|
582 | }
|
---|
583 |
|
---|
584 | // Return Gaussian probabilty plot intercept of the original data
|
---|
585 | public double originalIntercept(){
|
---|
586 | if(!this.initializationDone)this.initialize();
|
---|
587 | return this.originalIntercept;
|
---|
588 | }
|
---|
589 |
|
---|
590 | // Return estimated error of the Gaussian probabilty plot intercept of the orioginal data
|
---|
591 | public double originalInterceptError(){
|
---|
592 | if(!this.initializationDone)this.initialize();
|
---|
593 | return this.originalInterceptError;
|
---|
594 | }
|
---|
595 |
|
---|
596 | // Return original data
|
---|
597 | public double[] originalData(){
|
---|
598 | if(!this.initializationDone)this.initialize();
|
---|
599 | return this.originalData;
|
---|
600 | }
|
---|
601 |
|
---|
602 | // Return standardized original data
|
---|
603 | public double[] standardizedOriginalData(){
|
---|
604 | if(!this.initializationDone)this.initialize();
|
---|
605 | return this.standardizedOriginalData;
|
---|
606 | }
|
---|
607 |
|
---|
608 | // Return ordered original data
|
---|
609 | public double[] sortedOriginalData(){
|
---|
610 | if(!this.initializationDone)this.initialize();
|
---|
611 | return this.sortedOriginalData;
|
---|
612 | }
|
---|
613 |
|
---|
614 | // Return shifted standardized original data
|
---|
615 | public double[] shiftedStandardizedOriginalata(){
|
---|
616 | if(!this.initializationDone)this.initialize();
|
---|
617 | return this.shiftedStandardizedOriginalData;
|
---|
618 | }
|
---|
619 |
|
---|
620 | // Return transformed data
|
---|
621 | public double[] transformedData(){
|
---|
622 | if(!this.transformDone)this.transform();
|
---|
623 | return this.transformedData;
|
---|
624 | }
|
---|
625 |
|
---|
626 | // Return scaled transformed data
|
---|
627 | public double[] scaledTransformedData(){
|
---|
628 | if(!this.transformDone)this.transform();
|
---|
629 | return this.scaledTransformedData;
|
---|
630 | }
|
---|
631 |
|
---|
632 | // Return standardized transformed data
|
---|
633 | public double[] standardizedTransformedData(){
|
---|
634 | if(!this.transformDone)this.transform();
|
---|
635 | return this.standardizedTransformedData;
|
---|
636 | }
|
---|
637 |
|
---|
638 | // Return ordered transformed data
|
---|
639 | public double[] orderedTransformedData(){
|
---|
640 | if(!this.transformDone)this.transform();
|
---|
641 | ArrayMaths am = new ArrayMaths(this.transformedData);
|
---|
642 | double[] ordered = (am.sort()).array();
|
---|
643 | return ordered;
|
---|
644 | }
|
---|
645 |
|
---|
646 | // Return ordered scaled transformed data
|
---|
647 | public double[] orderedScaledTransformedData(){
|
---|
648 | if(!this.transformDone)this.transform();
|
---|
649 | return this.sortedScaledTransformedData;
|
---|
650 |
|
---|
651 | }
|
---|
652 |
|
---|
653 | // Return original data mean
|
---|
654 | public double originalMean(){
|
---|
655 | if(!this.initializationDone)this.initialize();
|
---|
656 | return this.originalMean;
|
---|
657 | }
|
---|
658 |
|
---|
659 | // Return original data median
|
---|
660 | public double originalMedian(){
|
---|
661 | if(!this.initializationDone)this.initialize();
|
---|
662 | return this.originalMedian;
|
---|
663 | }
|
---|
664 |
|
---|
665 | // Return original data standard deviation
|
---|
666 | public double originalStandardDeviation(){
|
---|
667 | if(!this.initializationDone)this.initialize();
|
---|
668 | return this.originalStandardDeviation;
|
---|
669 | }
|
---|
670 |
|
---|
671 | // Return original data standard error of the mean
|
---|
672 | public double originalStandardError(){
|
---|
673 | if(!this.initializationDone)this.initialize();
|
---|
674 | return this.originalStandardDeviation/Math.sqrt(this.nData);
|
---|
675 | }
|
---|
676 |
|
---|
677 | // Return original data variance
|
---|
678 | public double originalVariance(){
|
---|
679 | if(!this.initializationDone)this.initialize();
|
---|
680 | return this.originalVariance;
|
---|
681 | }
|
---|
682 |
|
---|
683 | // Return original data moment skewness
|
---|
684 | public double originalMomentSkewness(){
|
---|
685 | if(!this.initializationDone)this.initialize();
|
---|
686 | return this.originalMomentSkewness;
|
---|
687 | }
|
---|
688 |
|
---|
689 | // Return original data median skewness
|
---|
690 | public double originalMedianSkewness(){
|
---|
691 | if(!this.initializationDone)this.initialize();
|
---|
692 | return this.originalMedianSkewness;
|
---|
693 | }
|
---|
694 |
|
---|
695 | // Return original data quartile skewness
|
---|
696 | public double originalQuartiletSkewness(){
|
---|
697 | if(!this.initializationDone)this.initialize();
|
---|
698 | return this.originalQuartileSkewness;
|
---|
699 | }
|
---|
700 |
|
---|
701 | // Return original data excess kurtosis
|
---|
702 | public double originalExcessKurtosis(){
|
---|
703 | if(!this.initializationDone)this.initialize();
|
---|
704 | return this.originalExcessKurtosis;
|
---|
705 | }
|
---|
706 |
|
---|
707 | // Return original data maximum
|
---|
708 | public double originalMaximum(){
|
---|
709 | if(!this.initializationDone)this.initialize();
|
---|
710 | return this.originalMaximum;
|
---|
711 | }
|
---|
712 |
|
---|
713 | // Return original data minimum
|
---|
714 | public double originalMinimum(){
|
---|
715 | if(!this.initializationDone)this.initialize();
|
---|
716 | return this.originalMinimum;
|
---|
717 | }
|
---|
718 |
|
---|
719 | // Return original data range
|
---|
720 | public double originalRange(){
|
---|
721 | if(!this.initializationDone)this.initialize();
|
---|
722 | return this.originalRange;
|
---|
723 | }
|
---|
724 |
|
---|
725 |
|
---|
726 | // Return transformed data mean
|
---|
727 | public double transformedMean(){
|
---|
728 | if(!this.transformDone)this.transform();
|
---|
729 | return this.transformedMean;
|
---|
730 | }
|
---|
731 |
|
---|
732 | // Return transformed data median
|
---|
733 | public double transformedMedian(){
|
---|
734 | if(!this.transformDone)this.transform();
|
---|
735 | return this.transformedMedian;
|
---|
736 | }
|
---|
737 |
|
---|
738 |
|
---|
739 | // Return transformed data standard deviation
|
---|
740 | public double transformedStandardDeviation(){
|
---|
741 | if(!this.transformDone)this.transform();
|
---|
742 | return this.transformedStandardDeviation;
|
---|
743 | }
|
---|
744 |
|
---|
745 | // Return transformed data standard error of the mean
|
---|
746 | public double transformedStandardError(){
|
---|
747 | if(!this.transformDone)this.transform();
|
---|
748 | return this.transformedStandardDeviation/Math.sqrt(this.nData);
|
---|
749 | }
|
---|
750 |
|
---|
751 | // Return transformed data variance
|
---|
752 | public double transformedVariance(){
|
---|
753 | if(!this.transformDone)this.transform();
|
---|
754 | return this.transformedVariance;
|
---|
755 | }
|
---|
756 |
|
---|
757 | // Return transformed data moment skewness
|
---|
758 | public double transformedMomentSkewness(){
|
---|
759 | if(!this.transformDone)this.transform();
|
---|
760 | return this.transformedMomentSkewness;
|
---|
761 | }
|
---|
762 |
|
---|
763 | // Return transformed data median skewness
|
---|
764 | public double transformedMedianSkewness(){
|
---|
765 | if(!this.transformDone)this.transform();
|
---|
766 | return this.transformedMedianSkewness;
|
---|
767 | }
|
---|
768 |
|
---|
769 | // Return transformed data quartile skewness
|
---|
770 | public double transformedQuartileSkewness(){
|
---|
771 | if(!this.transformDone)this.transform();
|
---|
772 | return this.transformedQuartileSkewness;
|
---|
773 | }
|
---|
774 |
|
---|
775 | // Return transformed data excess kurtosis
|
---|
776 | public double transformedExcessKurtosis(){
|
---|
777 | if(!this.transformDone)this.transform();
|
---|
778 | return this.transformedExcessKurtosis;
|
---|
779 | }
|
---|
780 |
|
---|
781 | // Return transformed data maximum
|
---|
782 | public double transformedMaximum(){
|
---|
783 | if(!this.transformDone)this.transform();
|
---|
784 | return this.transformedMaximum;
|
---|
785 | }
|
---|
786 |
|
---|
787 | // Return transformed data minimum
|
---|
788 | public double transformedMinimum(){
|
---|
789 | if(!this.transformDone)this.transform();
|
---|
790 | return this.transformedMinimum;
|
---|
791 | }
|
---|
792 |
|
---|
793 | // Return transformed data range
|
---|
794 | public double transformedRange(){
|
---|
795 | if(!this.transformDone)this.transform();
|
---|
796 | return this.transformedRange;
|
---|
797 | }
|
---|
798 |
|
---|
799 | // PLOTS
|
---|
800 | // Gaussian probabilty plot of the standardized transformed data
|
---|
801 | public void transformedProbabilityPlot(){
|
---|
802 | if(!this.transformDone)this.transform();
|
---|
803 |
|
---|
804 | double[][] data = PlotGraph.data(2,this.nData);
|
---|
805 | data[0] = this.gaussianOrderMedians;
|
---|
806 | data[1] = ((new ArrayMaths(this.standardizedTransformedData)).sort()).array();
|
---|
807 |
|
---|
808 | data[2] = this.gaussianOrderMedians;
|
---|
809 | for(int i=0; i<this.nData; i++){
|
---|
810 | data[3][i] = this.transformedIntercept + this.transformedGradient*this.gaussianOrderMedians[i];
|
---|
811 | }
|
---|
812 | PlotGraph pg = new PlotGraph(data);
|
---|
813 | int[] points = {4, 0};
|
---|
814 | pg.setPoint(points);
|
---|
815 | int[] lines = {0, 3};
|
---|
816 | pg.setLine(lines);
|
---|
817 | pg.setXaxisLegend("Gaussian [0,1] Order Statistic Medians");
|
---|
818 | pg.setYaxisLegend("Ordered Response Values");
|
---|
819 | String legend1 = "Gausssian probability plot: Box-Cox transformed data";
|
---|
820 | String legend2 = "lambdaOne = " + Fmath.truncate(this.lambdaOne, 4) + ", lambdaTwo = " + Fmath.truncate(this.lambdaTwo, 4) + ", gradient = " + Fmath.truncate(this.transformedGradient, 4) + ", intercept = " + Fmath.truncate(this.transformedIntercept, 4) + ", R = " + Fmath.truncate(this.transformedSampleR, 4);
|
---|
821 | pg.setGraphTitle(legend1);
|
---|
822 | pg.setGraphTitle2(legend2);
|
---|
823 | pg.plot();
|
---|
824 | }
|
---|
825 |
|
---|
826 | // Gaussian probabilty plot of the standardized original data
|
---|
827 | public void originalProbabilityPlot(){
|
---|
828 | if(!this.initializationDone)this.initialize();
|
---|
829 |
|
---|
830 | double[][] data = PlotGraph.data(2,this.nData);
|
---|
831 | data[0] = this.gaussianOrderMedians;
|
---|
832 | data[1] = ((new ArrayMaths(this.standardizedOriginalData)).sort()).array();
|
---|
833 | data[2] = this.gaussianOrderMedians;
|
---|
834 | for(int i=0; i<this.nData; i++){
|
---|
835 | data[3][i] = this.originalIntercept + this.originalGradient*this.gaussianOrderMedians[i];
|
---|
836 | }
|
---|
837 | PlotGraph pg = new PlotGraph(data);
|
---|
838 | int[] points = {4, 0};
|
---|
839 | pg.setPoint(points);
|
---|
840 | int[] lines = {0, 3};
|
---|
841 | pg.setLine(lines);
|
---|
842 | pg.setXaxisLegend("Gaussian [0,1] Order Statistic Medians");
|
---|
843 | pg.setYaxisLegend("Ordered Response Values");
|
---|
844 | String legend1 = "Gausssian probability plot: original data for a Box-Cox transformation";
|
---|
845 | String legend2 = "gradient = " + Fmath.truncate(this.originalGradient, 4) + ", intercept = " + Fmath.truncate(this.originalIntercept, 4) + ", R = " + Fmath.truncate(this.originalSampleR, 4);
|
---|
846 | pg.setGraphTitle(legend1);
|
---|
847 | pg.setGraphTitle2(legend2);
|
---|
848 | pg.plot();
|
---|
849 | }
|
---|
850 |
|
---|
851 | // Output analysis to text
|
---|
852 | public void analysis(){
|
---|
853 | this.analysis("BoxCoxAnalysis.txt");
|
---|
854 | }
|
---|
855 |
|
---|
856 | public void analysis(String title){
|
---|
857 | if(!this.transformDone)this.transform();
|
---|
858 |
|
---|
859 | this.originalProbabilityPlot();
|
---|
860 | this.transformedProbabilityPlot();
|
---|
861 |
|
---|
862 | int posdot = title.indexOf(".");
|
---|
863 | if(posdot==-1)title = title + ".txt";
|
---|
864 |
|
---|
865 | FileOutput fout = new FileOutput(title);
|
---|
866 | fout.println("Box-Cox Analysis");
|
---|
867 | fout.println("File name: " + title);
|
---|
868 | Date d = new Date();
|
---|
869 | String day = DateFormat.getDateInstance().format(d);
|
---|
870 | String tim = DateFormat.getTimeInstance().format(d);
|
---|
871 | fout.println("Program executed at " + tim + " on " + day);
|
---|
872 | fout.println();
|
---|
873 |
|
---|
874 | int field1 = 30;
|
---|
875 | int field2 = 15;
|
---|
876 | fout.print("Box-Cox lambda one", field1);
|
---|
877 | fout.println(Fmath.truncate(this.lambdaOne, 4));
|
---|
878 | fout.print("Box-Cox lambda two", field1);
|
---|
879 | fout.println(Fmath.truncate(this.lambdaTwo, 4));
|
---|
880 | fout.println();
|
---|
881 |
|
---|
882 | fout.print(" ", field1);
|
---|
883 | fout.print("Transformed", field2);
|
---|
884 | fout.print(" ", field2);
|
---|
885 | fout.println("Original ");
|
---|
886 | fout.print(" ", field1);
|
---|
887 | fout.print("scaled data", field2);
|
---|
888 | fout.print(" ", field2);
|
---|
889 | fout.println("data ");
|
---|
890 | fout.println();
|
---|
891 |
|
---|
892 | fout.print(" ", field1);
|
---|
893 | fout.print("Value", field2);
|
---|
894 | fout.print("Error", field2);
|
---|
895 | fout.print("Value", field2);
|
---|
896 | fout.println("Error");
|
---|
897 | fout.println();
|
---|
898 |
|
---|
899 | fout.println("Gaussian Probability plot ");
|
---|
900 | fout.print(" Correlation coefficient", field1);
|
---|
901 | fout.print(Fmath.truncate(this.transformedSampleR, 4), field2);
|
---|
902 | fout.print(" ", field2);
|
---|
903 | fout.println(Fmath.truncate(this.originalSampleR, 4));
|
---|
904 |
|
---|
905 | fout.print(" Gradient", field1);
|
---|
906 | fout.print(Fmath.truncate(this.transformedGradient, 4), field2);
|
---|
907 | fout.print(Fmath.truncate(this.transformedGradientError, 4), field2);
|
---|
908 | fout.print(Fmath.truncate(this.originalGradient, 4), field2);
|
---|
909 | fout.println(Fmath.truncate(this.originalGradientError, 4));
|
---|
910 |
|
---|
911 | fout.print(" Intercept", field1);
|
---|
912 | fout.print(Fmath.truncate(this.transformedIntercept, 4), field2);
|
---|
913 | fout.print(Fmath.truncate(this.transformedInterceptError, 4), field2);
|
---|
914 | fout.print(Fmath.truncate(this.originalIntercept, 4), field2);
|
---|
915 | fout.println(Fmath.truncate(this.originalInterceptError, 4));
|
---|
916 | fout.println();
|
---|
917 |
|
---|
918 | fout.print("Data ");
|
---|
919 | fout.println();
|
---|
920 | fout.print(" Mean", field1);
|
---|
921 | fout.print(Fmath.truncate(this.transformedMean, 4), field2);
|
---|
922 | fout.print(" ", field2);
|
---|
923 | fout.println(Fmath.truncate(this.originalMean, 4));
|
---|
924 |
|
---|
925 | fout.print(" Median", field1);
|
---|
926 | fout.print(Fmath.truncate(this.transformedMedian, 4), field2);
|
---|
927 | fout.print(" ", field2);
|
---|
928 | fout.println(Fmath.truncate(this.originalMedian, 4));
|
---|
929 |
|
---|
930 | fout.print(" Standard deviation", field1);
|
---|
931 | fout.print(Fmath.truncate(this.transformedStandardDeviation, 4), field2);
|
---|
932 | fout.print(" ", field2);
|
---|
933 | fout.println(Fmath.truncate(this.originalStandardDeviation, 4));
|
---|
934 |
|
---|
935 | fout.print(" Standard error", field1);
|
---|
936 | fout.print(Fmath.truncate(this.transformedStandardDeviation/Math.sqrt(this.nData), 4), field2);
|
---|
937 | fout.print(" ", field2);
|
---|
938 | fout.println(Fmath.truncate(this.originalStandardDeviation/Math.sqrt(this.nData), 4));
|
---|
939 |
|
---|
940 | fout.print(" Moment skewness", field1);
|
---|
941 | fout.print(Fmath.truncate(this.transformedMomentSkewness, 4), field2);
|
---|
942 | fout.print(" ", field2);
|
---|
943 | fout.println(Fmath.truncate(this.originalMomentSkewness, 4));
|
---|
944 |
|
---|
945 | fout.print(" Median skewness", field1);
|
---|
946 | fout.print(Fmath.truncate(this.transformedMedianSkewness, 4), field2);
|
---|
947 | fout.print(" ", field2);
|
---|
948 | fout.println(Fmath.truncate(this.originalMedianSkewness, 4));
|
---|
949 |
|
---|
950 | fout.print(" Quartile skewness", field1);
|
---|
951 | fout.print(Fmath.truncate(this.transformedQuartileSkewness, 4), field2);
|
---|
952 | fout.print(" ", field2);
|
---|
953 | fout.println(Fmath.truncate(this.originalQuartileSkewness, 4));
|
---|
954 |
|
---|
955 | fout.print(" Excess kurtosis", field1);
|
---|
956 | fout.print(Fmath.truncate(this.transformedExcessKurtosis, 4), field2);
|
---|
957 | fout.print(" ", field2);
|
---|
958 | fout.println(Fmath.truncate(this.originalExcessKurtosis, 4));
|
---|
959 |
|
---|
960 | fout.print(" Minimum", field1);
|
---|
961 | fout.print(Fmath.truncate(this.transformedMinimum, 4), field2);
|
---|
962 | fout.print(" ", field2);
|
---|
963 | fout.println(Fmath.truncate(this.originalMinimum, 4));
|
---|
964 |
|
---|
965 | fout.print(" Maximum", field1);
|
---|
966 | fout.print(Fmath.truncate(this.transformedMaximum, 4), field2);
|
---|
967 | fout.print(" ", field2);
|
---|
968 | fout.println(Fmath.truncate(this.originalMaximum, 4));
|
---|
969 |
|
---|
970 | fout.print(" Range", field1);
|
---|
971 | fout.print(Fmath.truncate(this.transformedRange, 4), field2);
|
---|
972 | fout.print(" ", field2);
|
---|
973 | fout.println(Fmath.truncate(this.originalRange, 4));
|
---|
974 |
|
---|
975 | fout.close();
|
---|
976 | }
|
---|
977 |
|
---|
978 | }
|
---|
979 |
|
---|
980 | // Function for Box-Cox transform maximization
|
---|
981 | class BoxCoxFunction implements MaximizationFunction{
|
---|
982 |
|
---|
983 | public double[] shiftedData = null;
|
---|
984 | public int nData = 0;
|
---|
985 | public double[] yTransform = null;
|
---|
986 | public double[] gaussianOrderMedians = null;
|
---|
987 | public Regression reg = null;
|
---|
988 | public Stat am = null;
|
---|
989 |
|
---|
990 | public double function( double[] x){
|
---|
991 |
|
---|
992 | // Box-Cox transform
|
---|
993 | if(x[0]==0){
|
---|
994 | for(int i=0; i<nData; i++)yTransform[i] = Math.log(shiftedData[i]);
|
---|
995 | }
|
---|
996 | else{
|
---|
997 | for(int i=0; i<nData; i++)yTransform[i] = (Math.pow(shiftedData[i], x[0]) - 1.0)/x[0];
|
---|
998 | }
|
---|
999 |
|
---|
1000 | // Sort transformed array into ascending order
|
---|
1001 | am = new Stat(yTransform);
|
---|
1002 | am = am.sort();
|
---|
1003 |
|
---|
1004 | yTransform = am.standardize();
|
---|
1005 |
|
---|
1006 | // Calculate and return probability plot correlation coefficient
|
---|
1007 | reg = new Regression(gaussianOrderMedians, yTransform);
|
---|
1008 | reg.linear();
|
---|
1009 | return reg.getSampleR();
|
---|
1010 |
|
---|
1011 | }
|
---|
1012 |
|
---|
1013 | }
|
---|