Normand Briere
2017-05-07 314b34423070cf127464da79a53cddf6b1c38587
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
/*
 * This software is a cooperative product of The MathWorks and the National
 * Institute of Standards and Technology (NIST) which has been released to the
 * public domain. Neither The MathWorks nor NIST assumes any responsibility
 * whatsoever for its use by other parties, and makes no guarantees, expressed
 * or implied, about its quality, reliability, or any other characteristic.
 */
 
/*
 * Maths.java
 * Copyright (C) 1999 The Mathworks and NIST
 *
 */
 
package //weka.core.
        matrix;
 
import weka.core.RevisionHandler;
import weka.core.RevisionUtils;
import weka.core.Statistics;
 
import java.util.Random;
 
/**
 * Utility class.
 * <p/>
 * Adapted from the <a href="http://math.nist.gov/javanumerics/jama/" target="_blank">JAMA</a> package.
 *
 * @author The Mathworks and NIST 
 * @author Fracpete (fracpete at waikato dot ac dot nz)
 * @version $Revision: 1.3 $
 */
public class Maths
  implements RevisionHandler {
  
  /** The constant 1 / sqrt(2 pi) */
  public static final double PSI = 0.3989422804014327028632;
 
  /** The constant - log( sqrt(2 pi) ) */
  public static final double logPSI = -0.9189385332046726695410;
 
  /** Distribution type: undefined */
  public static final int undefinedDistribution = 0;
 
  /** Distribution type: noraml */
  public static final int normalDistribution = 1;
 
  /** Distribution type: chi-squared */
  public static final int chisqDistribution = 2;
 
  /** 
   * sqrt(a^2 + b^2) without under/overflow. 
   */
  public static double hypot(double a, double b) {
    double r;
    if (Math.abs(a) > Math.abs(b)) {
      r = b/a;
      r = Math.abs(a)*Math.sqrt(1+r*r);
    } else if (b != 0) {
      r = a/b;
      r = Math.abs(b)*Math.sqrt(1+r*r);
    } else {
      r = 0.0;
    }
    return r;
  }
 
  /**
   *  Returns the square of a value
   *  @param x 
   *  @return the square
   */
  public static double  square( double x ) 
  {
    return x * x;
  }
 
  /* methods for normal distribution */
 
  /**
   *  Returns the cumulative probability of the standard normal.
   *  @param x the quantile
   */
  public static double  pnorm( double x ) 
  {
    return Statistics.normalProbability( x );
  }
    
  /** 
   *  Returns the cumulative probability of a normal distribution.
   *  @param x the quantile
   *  @param mean the mean of the normal distribution
   *  @param sd the standard deviation of the normal distribution.
   */
  public static double  pnorm( double x, double mean, double sd ) 
  {
    if( sd <= 0.0 )
      throw new IllegalArgumentException("standard deviation <= 0.0");
    return pnorm( (x - mean) / sd );
  }
    
  /** 
   *  Returns the cumulative probability of a set of normal distributions
   *  with different means.
   *  @param x the vector of quantiles
   *  @param mean the means of the normal distributions
   *  @param sd the standard deviation of the normal distribution.
   *  @return the cumulative probability */
  public static DoubleVector  pnorm( double x, DoubleVector mean, 
                                     double sd ) 
  {
    DoubleVector p = new DoubleVector( mean.size() );
        
    for( int i = 0; i < mean.size(); i++ ) {
      p.set( i, pnorm(x, mean.get(i), sd) );
    }
    return p;
  }
    
  /** Returns the density of the standard normal.
   *  @param x the quantile
   *  @return the density
   */
  public static double  dnorm( double x ) 
  {
    return Math.exp( - x * x / 2. ) * PSI;
  }
    
  /** Returns the density value of a standard normal.
   *  @param x the quantile
   *  @param mean the mean of the normal distribution
   *  @param sd the standard deviation of the normal distribution.
   *  @return the density */
  public static double  dnorm( double x, double mean, double sd ) 
  {
    if( sd <= 0.0 )
      throw new IllegalArgumentException("standard deviation <= 0.0");
    return dnorm( (x - mean) / sd );
  }
    
  /** Returns the density values of a set of normal distributions with
   *  different means.
   *  @param x the quantile
   *  @param mean the means of the normal distributions
   *  @param sd the standard deviation of the normal distribution.
   * @return the density */
  public static DoubleVector  dnorm( double x, DoubleVector mean, 
                                     double sd ) 
  {
    DoubleVector den = new DoubleVector( mean.size() );
        
    for( int i = 0; i < mean.size(); i++ ) {
      den.set( i, dnorm(x, mean.get(i), sd) );
    }
    return den;
  }
    
  /** Returns the log-density of the standard normal.
   *  @param x the quantile
   *  @return the density
   */
  public static double  dnormLog( double x ) 
  {
    return logPSI - x * x / 2.;
  }
    
  /** Returns the log-density value of a standard normal.
   *  @param x the quantile
   *  @param mean the mean of the normal distribution
   *  @param sd the standard deviation of the normal distribution.
   *  @return the density */
  public static double  dnormLog( double x, double mean, double sd ) {
    if( sd <= 0.0 )
      throw new IllegalArgumentException("standard deviation <= 0.0");
    return - Math.log(sd) + dnormLog( (x - mean) / sd );
  }
    
  /** Returns the log-density values of a set of normal distributions with
   *  different means.
   *  @param x the quantile
   *  @param mean the means of the normal distributions
   *  @param sd the standard deviation of the normal distribution.
   * @return the density */
  public static DoubleVector  dnormLog( double x, DoubleVector mean, 
                                        double sd ) 
  {
    DoubleVector denLog = new DoubleVector( mean.size() );
        
    for( int i = 0; i < mean.size(); i++ ) {
      denLog.set( i, dnormLog(x, mean.get(i), sd) );
    }
    return denLog;
  }
    
  /** 
   *  Generates a sample of a normal distribution.
   *  @param n the size of the sample
   *  @param mean the mean of the normal distribution
   *  @param sd the standard deviation of the normal distribution.
   *  @param random the random stream
   *  @return the sample
   */
  public static DoubleVector rnorm( int n, double mean, double sd, 
                                    Random random ) 
  {
    if( sd < 0.0)
      throw new IllegalArgumentException("standard deviation < 0.0");
        
    if( sd == 0.0 ) return new DoubleVector( n, mean );
    DoubleVector v = new DoubleVector( n );
    for( int i = 0; i < n; i++ ) 
      v.set( i, (random.nextGaussian() + mean) / sd );
    return v;
  }
    
  /* methods for Chi-square distribution */
 
  /** Returns the cumulative probability of the Chi-squared distribution
   *  @param x the quantile
   */
  public static double  pchisq( double x ) 
  {
    double xh = Math.sqrt( x );
    return pnorm( xh ) - pnorm( -xh );
  }
    
  /** Returns the cumulative probability of the noncentral Chi-squared
   *  distribution.
   *  @param x the quantile
   *  @param ncp the noncentral parameter */
  public static double  pchisq( double x, double ncp ) 
  {
    double mean = Math.sqrt( ncp );
    double xh = Math.sqrt( x );
    return pnorm( xh - mean ) - pnorm( -xh - mean );
  }
    
  /** Returns the cumulative probability of a set of noncentral Chi-squared
   *  distributions.
   *  @param x the quantile
   *  @param ncp the noncentral parameters */
  public static DoubleVector  pchisq( double x, DoubleVector ncp )
  {
    int n = ncp.size();
    DoubleVector p = new DoubleVector( n );
    double mean;
    double xh = Math.sqrt( x );
        
    for( int i = 0; i < n; i++ ) {
      mean = Math.sqrt( ncp.get(i) );
      p.set( i, pnorm( xh - mean ) - pnorm( -xh - mean ) );
    }
    return p;
  }
    
  /** Returns the density of the Chi-squared distribution.
   *  @param x the quantile
   *  @return the density
   */
  public static double  dchisq( double x ) 
  {
    if( x == 0.0 ) return Double.POSITIVE_INFINITY;
    double xh = Math.sqrt( x );
    return dnorm( xh ) / xh;
  }
    
  /** Returns the density of the noncentral Chi-squared distribution.
   *  @param x the quantile
   *  @param ncp the noncentral parameter
   */
  public static double  dchisq( double x, double ncp ) 
  {
    if( ncp == 0.0 ) return dchisq( x );
    double xh = Math.sqrt( x );
    double mean = Math.sqrt( ncp );
    return (dnorm( xh - mean ) + dnorm( -xh - mean)) / (2 * xh);
  }
    
  /** Returns the density of the noncentral Chi-squared distribution.
   *  @param x the quantile
   *  @param ncp the noncentral parameters 
   */
  public static DoubleVector  dchisq( double x, DoubleVector ncp )
  {
    int n = ncp.size();
    DoubleVector d = new DoubleVector( n );
    double xh = Math.sqrt( x );
    double mean;
    for( int i = 0; i < n; i++ ) {
      mean = Math.sqrt( ncp.get(i) );
      if( ncp.get(i) == 0.0 ) d.set( i, dchisq( x ) );
      else d.set( i, (dnorm( xh - mean ) + dnorm( -xh - mean)) / 
                  (2 * xh) );
    }
    return d;
  }
    
  /** Returns the log-density of the noncentral Chi-square distribution.
   *  @param x the quantile
   *  @return the density
   */
  public static double  dchisqLog( double x ) 
  {
    if( x == 0.0) return Double.POSITIVE_INFINITY;
    double xh = Math.sqrt( x );
    return dnormLog( xh ) - Math.log( xh );
  }
    
  /** Returns the log-density value of a noncentral Chi-square distribution.
   *  @param x the quantile
   *  @param ncp the noncentral parameter
   *  @return the density */
  public static double  dchisqLog( double x, double ncp ) {
    if( ncp == 0.0 ) return dchisqLog( x );
    double xh = Math.sqrt( x );
    double mean = Math.sqrt( ncp );
    return Math.log( dnorm( xh - mean ) + dnorm( -xh - mean) ) - 
    Math.log(2 * xh);
  }
    
  /** Returns the log-density of a set of noncentral Chi-squared
   *  distributions.
   *  @param x the quantile
   *  @param ncp the noncentral parameters */
  public static DoubleVector  dchisqLog( double x, DoubleVector ncp )
  {
    DoubleVector dLog = new DoubleVector( ncp.size() );
    double xh = Math.sqrt( x );
    double mean;
        
    for( int i = 0; i < ncp.size(); i++ ) {
      mean = Math.sqrt( ncp.get(i) );
      if( ncp.get(i) == 0.0 ) dLog.set( i, dchisqLog( x ) );
      else dLog.set( i, Math.log( dnorm( xh - mean ) + dnorm( -xh - mean) ) - 
                     Math.log(2 * xh) );
    }
    return dLog;
  }
    
  /** 
   *  Generates a sample of a Chi-square distribution.
   *  @param n the size of the sample
   *  @param ncp the noncentral parameter
   *  @param random the random stream
   *  @return the sample
   */
  public static DoubleVector  rchisq( int n, double ncp, Random random ) 
  {
    DoubleVector v = new DoubleVector( n );
    double mean = Math.sqrt( ncp );
    double x;
    for( int i = 0; i < n; i++ ) {
      x = random.nextGaussian() + mean;
      v.set( i, x * x );
    }
    return v;
  }
  
  /**
   * Returns the revision string.
   * 
   * @return        the revision
   */
  public String getRevision() {
    return RevisionUtils.extract("$Revision: 1.3 $");
  }
}