NumCpp  2.9.0
A Templatized Header Only C++ Implementation of the Python NumPy Library
gaussNewtonNlls.hpp
Go to the documentation of this file.
1 #pragma once
31 
32 #include <array>
33 #include <functional>
34 #include <type_traits>
35 #include <utility>
36 
40 #include "NumCpp/Core/Shape.hpp"
41 #include "NumCpp/Core/Types.hpp"
42 #include "NumCpp/Functions/rms.hpp"
43 #include "NumCpp/Linalg/inv.hpp"
44 #include "NumCpp/NdArray.hpp"
45 
46 namespace nc
47 {
48  namespace linalg
49  {
50  //============================================================================
51  // Method Description:
73  template<typename dtype,
74  typename... Params,
76  nc::enable_if_t<all_arithmetic_v<Params...>, int> = 0,
77  nc::enable_if_t<all_same_v<dtype, Params...>, int> = 0>
78  std::pair<NdArray<double>, double>
79  gaussNewtonNlls(const uint32 numIterations,
80  const NdArray<dtype>& coordinates,
81  const NdArray<dtype>& measurements,
82  const std::function<dtype(const NdArray<dtype>&, const NdArray<dtype>&)>& function,
83  const std::array<std::function<dtype(const NdArray<dtype>&, const NdArray<dtype>&)>,
84  sizeof...(Params)>& derivatives,
85  Params... initialGuess)
86  {
88 
89  const auto coordinatesShape = coordinates.shape();
90 
91  if (coordinatesShape.rows != measurements.size())
92  {
93  THROW_INVALID_ARGUMENT_ERROR("coordinates number of rows, and measurements size must be the same.");
94  }
95 
96  NdArray<double> beta = NdArray<dtype>({ initialGuess... }).template astype<double>().transpose();
97  NdArray<double> residuals(coordinatesShape.rows, 1);
98  NdArray<double> jacobian(coordinatesShape.rows, sizeof...(Params));
99 
100  const auto colSlice = coordinates.cSlice();
101  for (uint32 iteration = 1; iteration <= numIterations; ++iteration)
102  {
103  for (uint32 measIdx = 0; measIdx < coordinatesShape.rows; ++measIdx)
104  {
105  const auto coordinate = coordinates(measIdx, colSlice);
106 
107  residuals[measIdx] =
108  static_cast<double>(measurements[measIdx]) - static_cast<double>(function(coordinate, beta));
109 
110  for (uint32 paramIdx = 0; paramIdx < sizeof...(Params); ++paramIdx)
111  {
112  const auto& derivative = derivatives[paramIdx];
113  jacobian(measIdx, paramIdx) = static_cast<double>(derivative(coordinate, beta));
114  }
115  }
116 
117  // perform the gauss-newton linear algebra
118  const auto jacobianT = jacobian.transpose();
119  const auto jacobianPsuedoInverse = linalg::inv(jacobianT.dot(jacobian));
120  const auto intermediate = jacobianPsuedoInverse.dot(jacobianT);
121  const auto deltaBeta = intermediate.dot(residuals);
122  beta += deltaBeta;
123  }
124 
125  // calculate the final rms of the residuals
126  for (uint32 measIdx = 0; measIdx < coordinatesShape.rows; ++measIdx)
127  {
128  const auto coordinate = coordinates(measIdx, colSlice);
129 
130  residuals[measIdx] =
131  static_cast<double>(measurements[measIdx]) - static_cast<double>(function(coordinate, beta));
132  }
133 
134  return std::make_pair(beta.flatten(), rms(residuals).item());
135  }
136  } // namespace linalg
137 } // namespace nc
#define THROW_INVALID_ARGUMENT_ERROR(msg)
Definition: Error.hpp:36
#define STATIC_ASSERT_ARITHMETIC(dtype)
Definition: StaticAsserts.hpp:37
Holds 1D and 2D arrays, the main work horse of the NumCpp library.
Definition: NdArrayCore.hpp:72
size_type size() const noexcept
Definition: NdArrayCore.hpp:4105
Slice cSlice(int32 inStartIdx=0, uint32 inStepSize=1) const noexcept
Definition: NdArrayCore.hpp:969
Shape shape() const noexcept
Definition: NdArrayCore.hpp:4092
NdArray< dtype > transpose() const
Definition: NdArrayCore.hpp:4457
NdArray< double > inv(const NdArray< dtype > &inArray)
Definition: inv.hpp:56
std::pair< NdArray< double >, double > gaussNewtonNlls(const uint32 numIterations, const NdArray< dtype > &coordinates, const NdArray< dtype > &measurements, const std::function< dtype(const NdArray< dtype > &, const NdArray< dtype > &)> &function, const std::array< std::function< dtype(const NdArray< dtype > &, const NdArray< dtype > &)>, sizeof...(Params)> &derivatives, Params... initialGuess)
Definition: gaussNewtonNlls.hpp:79
dtype beta(GeneratorType &generator, dtype inAlpha, dtype inBeta)
Definition: Random/beta.hpp:63
Definition: Coordinate.hpp:45
constexpr bool all_arithmetic_v
Definition: TypeTraits.hpp:109
NdArray< double > rms(const NdArray< dtype > &inArray, Axis inAxis=Axis::NONE)
Definition: rms.hpp:51
constexpr bool all_same_v
Definition: TypeTraits.hpp:143
typename std::enable_if< B, T >::type enable_if_t
Definition: TypeTraits.hpp:40
std::uint32_t uint32
Definition: Types.hpp:40