Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #include "PolynomialRegressionCurveCalculator.hxx"
21 : #include "macros.hxx"
22 : #include "RegressionCalculationHelper.hxx"
23 :
24 : #include <cmath>
25 : #include <rtl/math.hxx>
26 : #include <rtl/ustrbuf.hxx>
27 :
28 : using namespace com::sun::star;
29 :
30 : namespace chart
31 : {
32 :
33 16 : PolynomialRegressionCurveCalculator::PolynomialRegressionCurveCalculator()
34 16 : {}
35 :
36 23 : PolynomialRegressionCurveCalculator::~PolynomialRegressionCurveCalculator()
37 23 : {}
38 :
39 : // ____ XRegressionCurveCalculator ____
40 16 : void SAL_CALL PolynomialRegressionCurveCalculator::recalculateRegression(
41 : const uno::Sequence< double >& aXValues,
42 : const uno::Sequence< double >& aYValues )
43 : throw (uno::RuntimeException, std::exception)
44 : {
45 16 : rtl::math::setNan(&m_fCorrelationCoeffitient);
46 :
47 : RegressionCalculationHelper::tDoubleVectorPair aValues(
48 16 : RegressionCalculationHelper::cleanup( aXValues, aYValues, RegressionCalculationHelper::isValid()));
49 :
50 16 : const sal_Int32 aNoValues = aValues.first.size();
51 :
52 16 : const sal_Int32 aNoPowers = mForceIntercept ? mDegree : mDegree + 1;
53 :
54 16 : mCoefficients.clear();
55 16 : mCoefficients.resize(aNoPowers, 0.0);
56 :
57 16 : double yAverage = 0.0;
58 :
59 32 : std::vector<double> aQRTransposed;
60 16 : aQRTransposed.resize(aNoValues * aNoPowers, 0.0);
61 :
62 32 : std::vector<double> yVector;
63 16 : yVector.resize(aNoValues, 0.0);
64 :
65 122 : for(sal_Int32 i = 0; i < aNoValues; i++)
66 : {
67 106 : double yValue = aValues.second[i];
68 106 : if (mForceIntercept)
69 49 : yValue -= mInterceptValue;
70 106 : yVector[i] = yValue;
71 106 : yAverage += yValue;
72 : }
73 16 : yAverage /= aNoValues;
74 :
75 55 : for(sal_Int32 j = 0; j < aNoPowers; j++)
76 : {
77 39 : sal_Int32 aPower = mForceIntercept ? j+1 : j;
78 39 : sal_Int32 aColumnIndex = j * aNoValues;
79 300 : for(sal_Int32 i = 0; i < aNoValues; i++)
80 : {
81 261 : double xValue = aValues.first[i];
82 261 : aQRTransposed[i + aColumnIndex] = std::pow(xValue, (int) aPower);
83 : }
84 : }
85 :
86 : // QR decomposition - based on org.apache.commons.math.linear.QRDecomposition from apache commons math (ASF)
87 16 : sal_Int32 aMinorSize = std::min(aNoValues, aNoPowers);
88 :
89 32 : std::vector<double> aDiagonal;
90 16 : aDiagonal.resize(aMinorSize, 0.0);
91 :
92 : // Calculate Householder reflectors
93 55 : for (sal_Int32 aMinor = 0; aMinor < aMinorSize; aMinor++)
94 : {
95 39 : double aNormSqr = 0.0;
96 270 : for (sal_Int32 x = aMinor; x < aNoValues; x++)
97 : {
98 231 : double c = aQRTransposed[x + aMinor * aNoValues];
99 231 : aNormSqr += c * c;
100 : }
101 :
102 : double a;
103 :
104 39 : if (aQRTransposed[aMinor + aMinor * aNoValues] > 0.0)
105 16 : a = -std::sqrt(aNormSqr);
106 : else
107 23 : a = std::sqrt(aNormSqr);
108 :
109 39 : aDiagonal[aMinor] = a;
110 :
111 39 : if (a != 0.0)
112 : {
113 39 : aQRTransposed[aMinor + aMinor * aNoValues] -= a;
114 :
115 69 : for (sal_Int32 aColumn = aMinor + 1; aColumn < aNoPowers; aColumn++)
116 : {
117 30 : double alpha = 0.0;
118 227 : for (sal_Int32 aRow = aMinor; aRow < aNoValues; aRow++)
119 : {
120 197 : alpha -= aQRTransposed[aRow + aColumn * aNoValues] * aQRTransposed[aRow + aMinor * aNoValues];
121 : }
122 30 : alpha /= a * aQRTransposed[aMinor + aMinor * aNoValues];
123 :
124 227 : for (sal_Int32 aRow = aMinor; aRow < aNoValues; aRow++)
125 : {
126 197 : aQRTransposed[aRow + aColumn * aNoValues] -= alpha * aQRTransposed[aRow + aMinor * aNoValues];
127 : }
128 : }
129 : }
130 : }
131 :
132 : // Solve the linear equation
133 55 : for (sal_Int32 aMinor = 0; aMinor < aMinorSize; aMinor++)
134 : {
135 39 : double aDotProduct = 0;
136 :
137 270 : for (sal_Int32 aRow = aMinor; aRow < aNoValues; aRow++)
138 : {
139 231 : aDotProduct += yVector[aRow] * aQRTransposed[aRow + aMinor * aNoValues];
140 : }
141 39 : aDotProduct /= aDiagonal[aMinor] * aQRTransposed[aMinor + aMinor * aNoValues];
142 :
143 270 : for (sal_Int32 aRow = aMinor; aRow < aNoValues; aRow++)
144 : {
145 231 : yVector[aRow] += aDotProduct * aQRTransposed[aRow + aMinor * aNoValues];
146 : }
147 :
148 : }
149 :
150 55 : for (sal_Int32 aRow = aDiagonal.size() - 1; aRow >= 0; aRow--)
151 : {
152 39 : yVector[aRow] /= aDiagonal[aRow];
153 39 : double yRow = yVector[aRow];
154 39 : mCoefficients[aRow] = yRow;
155 :
156 69 : for (sal_Int32 i = 0; i < aRow; i++)
157 : {
158 30 : yVector[i] -= yRow * aQRTransposed[i + aRow * aNoValues];
159 : }
160 : }
161 :
162 16 : if(mForceIntercept)
163 : {
164 7 : mCoefficients.insert(mCoefficients.begin(), mInterceptValue);
165 : }
166 :
167 : // Calculate correlation coeffitient
168 16 : double aSumError = 0.0;
169 16 : double aSumTotal = 0.0;
170 16 : double aSumYpred2 = 0.0;
171 :
172 122 : for( sal_Int32 i = 0; i < aNoValues; i++ )
173 : {
174 106 : double xValue = aValues.first[i];
175 106 : double yActual = aValues.second[i];
176 106 : double yPredicted = getCurveValue( xValue );
177 106 : aSumTotal += (yActual - yAverage) * (yActual - yAverage);
178 106 : aSumError += (yActual - yPredicted) * (yActual - yPredicted);
179 106 : if(mForceIntercept)
180 49 : aSumYpred2 += (yPredicted - mInterceptValue) * (yPredicted - mInterceptValue);
181 : }
182 :
183 16 : double aRSquared = 0.0;
184 16 : if(mForceIntercept)
185 : {
186 7 : aRSquared = aSumYpred2 / (aSumError + aSumYpred2);
187 : }
188 : else
189 : {
190 9 : aRSquared = 1.0 - (aSumError / aSumTotal);
191 : }
192 :
193 16 : if (aRSquared > 0.0)
194 16 : m_fCorrelationCoeffitient = std::sqrt(aRSquared);
195 : else
196 16 : m_fCorrelationCoeffitient = 0.0;
197 16 : }
198 :
199 649 : double SAL_CALL PolynomialRegressionCurveCalculator::getCurveValue( double x )
200 : throw (lang::IllegalArgumentException,
201 : uno::RuntimeException, std::exception)
202 : {
203 : double fResult;
204 649 : rtl::math::setNan(&fResult);
205 :
206 649 : if (mCoefficients.empty())
207 : {
208 0 : return fResult;
209 : }
210 :
211 649 : sal_Int32 aNoCoefficients = (sal_Int32) mCoefficients.size();
212 :
213 : // Horner's method
214 649 : fResult = 0.0;
215 3095 : for (sal_Int32 i = aNoCoefficients - 1; i >= 0; i--)
216 : {
217 2446 : fResult = mCoefficients[i] + (x * fResult);
218 : }
219 649 : return fResult;
220 : }
221 :
222 7 : uno::Sequence< geometry::RealPoint2D > SAL_CALL PolynomialRegressionCurveCalculator::getCurveValues(
223 : double min, double max, sal_Int32 nPointCount,
224 : const uno::Reference< chart2::XScaling >& xScalingX,
225 : const uno::Reference< chart2::XScaling >& xScalingY,
226 : sal_Bool bMaySkipPointsInCalculation )
227 : throw (lang::IllegalArgumentException,
228 : uno::RuntimeException, std::exception)
229 : {
230 :
231 7 : return RegressionCurveCalculator::getCurveValues( min, max, nPointCount, xScalingX, xScalingY, bMaySkipPointsInCalculation );
232 : }
233 :
234 15 : OUString PolynomialRegressionCurveCalculator::ImplGetRepresentation(
235 : const uno::Reference< util::XNumberFormatter >& xNumFormatter,
236 : sal_Int32 nNumberFormatKey ) const
237 : {
238 15 : OUStringBuffer aBuf( "f(x) = ");
239 :
240 15 : sal_Int32 aLastIndex = mCoefficients.size() - 1;
241 59 : for (sal_Int32 i = aLastIndex; i >= 0; i--)
242 : {
243 44 : double aValue = mCoefficients[i];
244 44 : if (aValue == 0.0)
245 : {
246 0 : continue;
247 : }
248 44 : else if (aValue < 0.0)
249 : {
250 28 : aBuf.appendAscii( " - " );
251 : }
252 : else
253 : {
254 16 : if (i != aLastIndex)
255 15 : aBuf.appendAscii( " + " );
256 : }
257 :
258 44 : aBuf.append( getFormattedString( xNumFormatter, nNumberFormatKey, std::abs( aValue ) ) );
259 :
260 44 : if(i > 0)
261 : {
262 29 : if (i == 1)
263 : {
264 15 : aBuf.appendAscii( "x" );
265 : }
266 : else
267 : {
268 14 : aBuf.appendAscii( "x^" );
269 14 : aBuf.append(i);
270 : }
271 : }
272 : }
273 :
274 15 : return aBuf.makeStringAndClear();
275 : }
276 :
277 : } // namespace chart
278 :
279 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|