5#ifndef LINE_API_PFQN_PFQN_NRL_H
6#define LINE_API_PFQN_PFQN_NRL_H
56T pfqn_nr_impl(
const Matrix<T>& L0,
const std::vector<T>& N,
const std::vector<T>& Z,
57 const Matrix<T>& alpha0,
bool probit) {
59 const std::size_t M = L0.rows(), R = L0.cols();
60 if (N.size() != R)
throw InputError(
"pfqn_nr: L and N disagree on the class count");
61 const T zero = num_traits<T>::from_int(0), one = num_traits<T>::from_int(1);
62 T Nt = zero, Zsum = zero;
63 for (
const T& v : N) Nt += v;
64 for (
const T& v : Z) Zsum += v;
65 if (Nt < zero)
throw InputError(
"pfqn_nr: negative population");
66 if (Nt == zero)
return zero;
67 const std::size_t Ntot =
static_cast<std::size_t
>(num_traits<T>::to_double(Nt));
70 const std::size_t Mx = M + (Zsum > zero ? 1 : 0);
72 for (std::size_t i = 0; i < M; ++i)
73 for (std::size_t r = 0; r < R; ++r) L(i, r) = L0(i, r);
74 Matrix<T> alpha(Mx, Ntot);
75 for (std::size_t i = 0; i < M && i < alpha0.rows(); ++i)
76 for (std::size_t k = 0; k < Ntot; ++k)
77 alpha(i, k) = k < alpha0.cols() ? alpha0(i, k) : one;
79 if (Z.size() != R)
throw InputError(
"pfqn_nr: Z has the wrong length");
80 for (std::size_t r = 0; r < R; ++r) L(M, r) = Z[r];
81 for (std::size_t k = 0; k < Ntot; ++k)
82 alpha(M, k) = num_traits<T>::from_int(
static_cast<long>(k) + 1);
86 if (M == 1 && Zsum == zero) {
87 T lG = detail::num_factln<T>(Nt);
88 for (std::size_t r = 0; r < R; ++r) {
89 lG -= detail::num_factln<T>(N[r]);
90 if (L(0, r) > zero) lG += N[r] * log(L(0, r));
92 for (std::size_t k = 0; k < Ntot; ++k) lG -= log(alpha(0, k));
97 std::vector<T> Lmax(R, zero);
98 for (std::size_t r = 0; r < R; ++r) {
100 for (std::size_t i = 1; i < Mx; ++i)
101 if (L(i, r) > m) m = L(i, r);
102 if (m <= zero)
throw InputError(
"pfqn_nr: a class has no positive demand");
106 for (std::size_t i = 0; i < Mx; ++i)
107 for (std::size_t r = 0; r < R; ++r)
Ls(i, r) = T(L(i, r) / Lmax[r]);
109 const std::function<T(
const std::vector<T>&)> h = [&](
const std::vector<T>& x) {
112 const std::vector<T> x0(R, zero);
115 for (std::size_t r = 0; r < R; ++r) lG += N[r] * log(Lmax[r]);
133 "pfqn_nrl requires transcendental arithmetic (Laplace approximation of a contour "
135 return detail::pfqn_nr_impl(L, N, Z, alpha,
false);
143 "pfqn_nrp requires transcendental arithmetic (Laplace approximation of a contour "
145 return detail::pfqn_nr_impl(L, N, Z, alpha,
true);
The exception types the port throws.
Integrands of the Norlund-Rice inversion of the normalizing constant, in their two changes of variabl...
Laplace approximation of a multidimensional integral around a given point.
Dense matrix and non-owning view.
LaplaceResult< T > laplaceapprox(const std::function< T(const std::vector< T > &)> &h, const std::vector< T > &x0)
Laplace approximation of a multidimensional integral around a given point.
T pfqn_nrp(const Matrix< T > &L, const std::vector< T > &N, const std::vector< T > &Z, const Matrix< T > &alpha)
Norlund-Rice probit approximation of log G.
T pfqn_nrl(const Matrix< T > &L, const std::vector< T > &N, const std::vector< T > &Z, const Matrix< T > &alpha)
Norlund-Rice logit approximation of log G.
T infradius_h(const std::vector< T > &x, const Matrix< T > &L, const std::vector< T > &N, const Matrix< T > &alpha)
Logistic-substitution integrand (matlab/src/api/pfqn/infradius_h.m).
T infradius_hnorm(const std::vector< T > &x, const Matrix< T > &L, const std::vector< T > &N, const Matrix< T > &alpha)
Normal-CDF substitution integrand (matlab/src/api/pfqn/infradius_hnorm.m).
Number-type abstraction for the templated API port.
Shared scalar machinery for the integration / asymptotic members of the pfqn family (pfqn_le,...