71 template<
typename dtype,
73 std::enable_if_t<std::is_arithmetic_v<dtype>,
int> = 0,
75 std::enable_if_t<
all_same_v<dtype, Params...>,
int> = 0>
76 std::pair<NdArray<double>,
double>
82 sizeof...(Params)>& derivatives,
83 Params... initialGuess)
87 const auto coordinatesShape = coordinates.
shape();
89 if (coordinatesShape.rows != measurements.
size())
98 const auto colSlice = coordinates.
cSlice();
99 for (
uint32 iteration = 1; iteration <= numIterations; ++iteration)
101 for (
uint32 measIdx = 0; measIdx < coordinatesShape.rows; ++measIdx)
103 const auto coordinate = coordinates(measIdx, colSlice);
106 static_cast<double>(measurements[measIdx]) -
static_cast<double>(function(coordinate,
beta));
108 for (
uint32 paramIdx = 0; paramIdx <
sizeof...(Params); ++paramIdx)
110 const auto& derivative = derivatives[paramIdx];
111 jacobian(measIdx, paramIdx) =
static_cast<double>(derivative(coordinate,
beta));
116 const auto jacobianT = jacobian.
transpose();
117 const auto jacobianPsuedoInverse =
linalg::inv(jacobianT.dot(jacobian));
118 const auto intermediate = jacobianPsuedoInverse.dot(jacobianT);
119 const auto deltaBeta = intermediate.dot(residuals);
124 for (
uint32 measIdx = 0; measIdx < coordinatesShape.rows; ++measIdx)
126 const auto coordinate = coordinates(measIdx, colSlice);
129 static_cast<double>(measurements[measIdx]) -
static_cast<double>(function(coordinate,
beta));
132 return std::make_pair(
beta.flatten(),
rms(residuals).item());
#define THROW_INVALID_ARGUMENT_ERROR(msg)
Definition: Error.hpp:37
#define STATIC_ASSERT_ARITHMETIC(dtype)
Definition: StaticAsserts.hpp:39
Holds 1D and 2D arrays, the main work horse of the NumCpp library.
Definition: NdArrayCore.hpp:139
size_type size() const noexcept
Definition: NdArrayCore.hpp:4524
self_type transpose() const
Definition: NdArrayCore.hpp:4882
const Shape & shape() const noexcept
Definition: NdArrayCore.hpp:4511
Slice cSlice(index_type inStartIdx=0, size_type inStepSize=1) const
Definition: NdArrayCore.hpp:1008
Definition: cholesky.hpp:41
NdArray< double > inv(const NdArray< dtype > &inArray)
Definition: inv.hpp:54
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:77
dtype beta(GeneratorType &generator, dtype inAlpha, dtype inBeta)
Definition: Random/beta.hpp:61
constexpr bool all_arithmetic_v
Definition: TypeTraits.hpp:67
constexpr bool all_same_v
Definition: TypeTraits.hpp:101
NdArray< double > rms(const NdArray< dtype > &inArray, Axis inAxis=Axis::NONE)
Definition: rms.hpp:51
std::uint32_t uint32
Definition: Types.hpp:40