root/src/java/com/omniti/reconnoiter/esper/ExactRegressionBean.java

Revision 5640498f39ed92d4917c04691ad51ab3d17d0674, 2.2 kB (checked in by Theo Schlossnagle <jesus@omniti.com>, 8 years ago)

Regressions that use arbitrary precision math libraries.... step 1, refs #210

  • Property mode set to 100644
Line 
1 /**************************************************************************************
2  * Copyright (C) 2008 EsperTech, Inc. All rights reserved.                            *
3  * Copyright (C) 2009 OmniTI, Inc. All rights reserved.                               *
4  * http://esper.codehaus.org                                                          *
5  * http://www.espertech.com                                                           *
6  * ---------------------------------------------------------------------------------- *
7  * The software in this package is published under the terms of the GPL license       *
8  * a copy of which has been included with this distribution in the license.txt file.  *
9  **************************************************************************************/
10 package com.omniti.reconnoiter.esper;
11
12 import java.math.BigDecimal;
13 import java.math.MathContext;
14
15 public final class ExactRegressionBean extends ExactBaseStatisticsBean
16 {
17     private static final long serialVersionUID = -3558474554218814231L;
18
19     public double getYIntercept()
20     {
21         double sloped = getSlope();
22         if (Double.isNaN(sloped)) return sloped;
23         BigDecimal slope = new BigDecimal(sloped);
24         return getYSum().divide(new BigDecimal(getN()), MathContext.DECIMAL128)
25                         .subtract(slope.multiply(getXSum()).divide(new BigDecimal(getN()), MathContext.DECIMAL128))
26                         .doubleValue();
27     }
28
29     public double getSlope()
30     {
31         try {
32             if (this.getN() == 0) return Double.NaN;
33             BigDecimal ssxi = getXSum().multiply(getXSum()).divide(new BigDecimal(getN()), MathContext.DECIMAL128);
34             BigDecimal ssx = getSumXSq().subtract(ssxi);
35             if (ssx.compareTo(BigDecimal.ZERO) == 0) return Double.NaN;
36             BigDecimal spi = getXSum().multiply(getYSum()).divide(new BigDecimal(getN(), MathContext.DECIMAL128));
37             BigDecimal sp = getSumXY().subtract(spi);
38             return sp.divide(ssx, MathContext.DECIMAL128).doubleValue();
39         }
40         catch(ArithmeticException ae) {
41             ae.printStackTrace();
42             return Double.NaN;
43         }
44     }
45
46 }
Note: See TracBrowser for help on using the browser.