chart2/source/tools/PotentialRegressionCurveCalculator.cxx |   15 ++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)

New commits:
commit e0e285574244e855fd148ab7320b1aeb5914655a
Author: Philippe Jung <phil.j...@free.fr>
Date:   Sun Jun 21 19:08:34 2015 +0200

    tdf#92231 Potential regression curve calculation is wrong
    
    Fixes potential regression curve class.
    
    For potential regression curve algorithm, we try to get y = C * D^x
    Switching to neperian logs:
       ln(y) = ln(C) + x ln(D)
    So we make a linear regression and get
       slope = ln(D) => D = exp(slope)
       intercept = ln(C) => C = exp(intercept)
    
    The current code computes the linear regression between log(y) and
    log(x)
    It should be between ln(y) and x.
    Moreover, the slope is ln(D) so exp(slope) should be returned.
    Finally, in getCurveValue, the return value is y = C x^D which is wrong
    
    Change-Id: If8c952001229d3436be48abfef87c8302cf0544f
    Reviewed-on: https://gerrit.libreoffice.org/16400
    Reviewed-by: Philippe Jung <phil.j...@free.fr>
    Tested-by: Philippe Jung <phil.j...@free.fr>

diff --git a/chart2/source/tools/PotentialRegressionCurveCalculator.cxx 
b/chart2/source/tools/PotentialRegressionCurveCalculator.cxx
index 7495c91..6f3d438 100644
--- a/chart2/source/tools/PotentialRegressionCurveCalculator.cxx
+++ b/chart2/source/tools/PotentialRegressionCurveCalculator.cxx
@@ -51,6 +51,14 @@ void SAL_CALL 
PotentialRegressionCurveCalculator::recalculateRegression(
             aXValues, aYValues,
             RegressionCalculationHelper::isValidAndBothPositive()));
 
+    // We try to get y =C * D^x
+    // switching to neperian logs:
+    // ln(y) = ln(C) + x ln(D)
+    // So we make a linear regression and get
+    // slope = ln(D) => D = exp(slope)
+    // intercept = ln(C) => C = exp(intercept)
+    // Warning: the linear regression is between
+    // ln(y) and x. Not between ln(y) and ln(x)
     const size_t nMax = aValues.first.size();
     if( nMax == 0 )
     {
@@ -64,7 +72,7 @@ void SAL_CALL 
PotentialRegressionCurveCalculator::recalculateRegression(
     size_t i = 0;
     for( i = 0; i < nMax; ++i )
     {
-        fAverageX += log( aValues.first[i] );
+        fAverageX += aValues.first[i] ;
         fAverageY += log( aValues.second[i] );
     }
 
@@ -75,7 +83,7 @@ void SAL_CALL 
PotentialRegressionCurveCalculator::recalculateRegression(
     double fQx = 0.0, fQy = 0.0, fQxy = 0.0;
     for( i = 0; i < nMax; ++i )
     {
-        double fDeltaX = log( aValues.first[i] ) - fAverageX;
+        double fDeltaX = aValues.first[i] - fAverageX;
         double fDeltaY = log( aValues.second[i] ) - fAverageY;
 
         fQx  += fDeltaX * fDeltaX;
@@ -87,6 +95,7 @@ void SAL_CALL 
PotentialRegressionCurveCalculator::recalculateRegression(
     m_fIntercept = fAverageY - m_fSlope * fAverageX;
     m_fCorrelationCoeffitient = fQxy / sqrt( fQx * fQy );
 
+    m_fSlope = exp( m_fSlope );
     m_fIntercept = exp( m_fIntercept );
 }
 
@@ -100,7 +109,7 @@ double SAL_CALL 
PotentialRegressionCurveCalculator::getCurveValue( double x )
     if( ! ( ::rtl::math::isNan( m_fSlope ) ||
             ::rtl::math::isNan( m_fIntercept )))
     {
-        fResult = m_fIntercept * pow( x, m_fSlope );
+        fResult = m_fIntercept * pow( m_fSlope, x );
     }
 
     return fResult;
_______________________________________________
Libreoffice-commits mailing list
libreoffice-comm...@lists.freedesktop.org
http://lists.freedesktop.org/mailman/listinfo/libreoffice-commits

Reply via email to