File: LinearFit.h

package info (click to toggle)
audacity 3.7.7%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 134,800 kB
  • sloc: cpp: 366,277; ansic: 198,323; lisp: 7,761; sh: 3,414; python: 1,501; xml: 1,385; perl: 854; makefile: 125
file content (54 lines) | stat: -rw-r--r-- 1,730 bytes parent folder | download | duplicates (2)
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
/*  SPDX-License-Identifier: GPL-2.0-or-later */
/*!********************************************************************

  Audacity: A Digital Audio Editor

  LinearFit.h

  Matthieu Hodgkinson

**********************************************************************/

#pragma once
#include <cassert>
#include <numeric>
#include <utility>
#include <vector>

/*!
 * @brief Linear least-square fit of a set of points `y` located at `x`, with
 * optional weights `w`.
 *
 * @pre x and y have equal size
 * @pre w is empty or has the same size as x and y
 */
template <typename X, typename Y, typename W = double>
std::pair<double, double> LinearFit(
   const std::vector<X>& x, const std::vector<Y>& y, std::vector<W> w = {})
{
   assert(x.size() == y.size() && (w.empty() || w.size() == y.size()));
   if (w.empty())
      w = std::vector<W>(y.size(), 1);

   // Calculate weighted means
   const double xwMean = std::inner_product(x.begin(), x.end(), w.begin(), 0.) /
                         std::accumulate(w.begin(), w.end(), 0.);
   const double ywMean = std::inner_product(y.begin(), y.end(), w.begin(), 0.) /
                         std::accumulate(w.begin(), w.end(), 0.);

   auto n = 0;
   const double numerator = std::inner_product(
      x.begin(), x.end(), y.begin(), 0., std::plus<>(),
      [&](X xi, Y yi) { return w[n++] * (xi - xwMean) * (yi - ywMean); });

   n = 0;
   const double denominator = std::inner_product(
      x.begin(), x.end(), x.begin(), 0., std::plus<>(),
      [&](X xi, X) { return w[n++] * (xi - xwMean) * (xi - xwMean); });

   // Calculate slope (a) and intercept (b)
   const double a = numerator / denominator;
   const double b = ywMean - a * xwMean;

   return std::make_pair(a, b);
}