1 /* Copyright 2016, Ableton AG, Berlin. All rights reserved. 2 * 3 * This program is free software: you can redistribute it and/or modify 4 * it under the terms of the GNU General Public License as published by 5 * the Free Software Foundation, either version 2 of the License, or 6 * (at your option) any later version. 7 * 8 * This program is distributed in the hope that it will be useful, 9 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * GNU General Public License for more details. 12 * 13 * You should have received a copy of the GNU General Public License 14 * along with this program. If not, see <http://www.gnu.org/licenses/>. 15 * 16 * If you would like to incorporate Link into a proprietary software application, 17 * please contact <link-devs@ableton.com>. 18 */ 19 20 #pragma once 21 22 #include <cfloat> 23 #include <cmath> 24 #include <numeric> 25 #include <utility> 26 27 namespace ableton 28 { 29 namespace link 30 { 31 32 template <typename It> 33 std::pair<double, double> linearRegression(It begin, It end) 34 { 35 using namespace std; 36 using Point = pair<double, double>; 37 38 const double numPoints = static_cast<double>(distance(begin, end)); 39 40 const double meanX = accumulate(begin, end, 0.0, [](double a, Point b) { 41 return a + b.first; 42 }) / numPoints; 43 44 const double productXX = accumulate(begin, end, 0.0, 45 [&meanX](double a, Point b) { return a + pow(b.first - meanX, 2.0); }); 46 47 const double meanY = accumulate(begin, end, 0.0, [](double a, Point b) { 48 return a + b.second; 49 }) / numPoints; 50 51 const double productXY = 52 inner_product(begin, end, begin, 0.0, [](double a, double b) { return a + b; }, 53 [&meanX, &meanY]( 54 Point a, Point b) { return ((a.first - meanX) * (b.second - meanY)); }); 55 56 const double slope = productXX == 0.0 ? 0.0 : productXY / productXX; 57 58 const double intercept = meanY - (slope * meanX); 59 60 return make_pair(slope, intercept); 61 } 62 63 } // namespace link 64 } // namespace ableton 65