5#ifndef LINE_API_PFQN_PFQN_AGHQ_H
6#define LINE_API_PFQN_PFQN_AGHQ_H
60 const std::vector<T>& Z, std::size_t q) {
62 "pfqn_aghq requires transcendental arithmetic (quadrature of an integral)");
65 const std::size_t M = L.
rows(), R = L.
cols();
67 if (q == 0)
throw InputError(
"pfqn_aghq: q must be at least 1");
69 T Ntot = zero, Lsum = zero, Zsum = zero;
70 for (
const T& x : N) Ntot += x;
71 for (
const T& x : Z) Zsum += x;
72 for (std::size_t i = 0; i < M; ++i)
73 for (std::size_t r = 0; r < R; ++r) Lsum += L(i, r);
78 const std::size_t d = M - 1;
87 const T ld = (M < 2) ? zero : detail::pfqn_logdet(A);
88 T S = zero, sum_lu = zero;
89 for (std::size_t r = 0; r < R; ++r) {
91 for (std::size_t i = 0; i < M; ++i) uL += umax[i] * L(i, r);
94 for (std::size_t i = 0; i < M; ++i) sum_lu += log(umax[i]);
95 const T h0 = T(S + sum_lu);
97 for (std::size_t i = 0; i < d; ++i) w0[i] = log(T(umax[i] / umax[M - 1]));
99 const std::vector<T>* Np = &N;
100 auto h = [Lp, Np, M, R, zero](
const std::vector<T>& w) {
104 for (std::size_t r = 0; r < R; ++r) {
106 for (std::size_t i = 0; i < M; ++i) c += x[i] * (*Lp)(i, r);
107 acc += (*Np)[r] * log(c);
109 for (std::size_t i = 0; i < M; ++i) acc += log(x[i]);
114 for (std::size_t r = 0; r < R; ++r) lG -= detail::num_factln<T>(N[r]);
115 res.
lG = T(lG + h0 + lacc - half * ld);
118 std::vector<T> w0(d);
119 for (std::size_t i = 0; i < d; ++i) w0[i] = log(T(mode.
x[i] / mode.
x[M - 1]));
121 const std::vector<T>* Np = &N;
122 const std::vector<T>* Zp = &Z;
123 auto h = [Lp, Np, Zp, M, R, zero](
const std::vector<T>& w) {
126 std::vector<T> c(R, zero);
127 for (std::size_t r = 0; r < R; ++r)
128 for (std::size_t i = 0; i < M; ++i) c[r] += x[i] * (*Lp)(i, r);
130 for (std::size_t i = 0; i < M; ++i) acc += log(x[i]);
135 for (std::size_t r = 0; r < R; ++r) lG -= detail::num_factln<T>(N[r]);
136 res.
lG = T(lG + mode.
h0 + lacc - half * mode.
ld);
144 const std::vector<T>& Z) {
145 return pfqn_aghq(L, N, Z,
static_cast<std::size_t
>(3));
150 return pfqn_aghq(L, N, std::vector<T>(),
static_cast<std::size_t
>(3));
The exception types the port throws.
Enumerations and the minimal distribution descriptor shared by the model layer of the C++ port.
Dense matrix and non-owning view.
std::vector< T > softmax_gauge(const std::vector< T > &w)
softmax of [w; 0], the logistic parametrisation of the simplex with gauge w_M = 0.
T aghq_rule(const F &h, const std::vector< T > &w0, const T &h0, const Matrix< T > &A, std::size_t q, std::size_t d)
Log of the tensor Gauss-Hermite sum, accumulated with a running maximum; the det(A)^(-1/2) of the rul...
Mode< T > simplex_mode(const Matrix< T > &L, const std::vector< T > &N, const std::vector< T > &Z)
Mode and curvature of h(w) = log J(L'x(w)) + sum_i log x_i with J the exact radial integral.
Radial< T > radial(const std::vector< T > &c, const std::vector< T > &N, const std::vector< T > &Z, std::size_t M)
log J(c) = log int_0^inf exp(-v) v^(M-1) prod_r (Z_r + v c_r)^N_r dv, plus the moments of the tilted ...
AghqResult< T > pfqn_aghq(const Matrix< T > &L, const std::vector< T > &N, const std::vector< T > &Z, std::size_t q)
std::vector< T > pfqn_le_fpi(const Matrix< T > &L, const std::vector< T > &N)
Mode of the logistic-transformed integrand, Z = 0 case (pfqn_le_fpi).
LeResult< T > pfqn_le(const Matrix< T > &L, const std::vector< T > &N, const std::vector< T > &Z)
Logistic expansion estimate of the normalizing constant.
Matrix< T > pfqn_le_hessian(const Matrix< T > &L, const std::vector< T > &N, const std::vector< T > &u0)
Hessian of the Z = 0 logistic integrand at the mode ((M-1) x (M-1)).
Number-type abstraction for the templated API port.
Shared scalar machinery for the integration / asymptotic members of the pfqn family (pfqn_le,...
Logistic expansion (LE) asymptotic approximation of the normalizing constant of a closed product-form...
Shared machinery for closures of the simplex factor of the McKenna-Mitra integral,...
static constexpr double Zero
Return value of pfqn_le, mirroring [Gn, lGn].
Mode, curvature and log-integrand at the mode of the simplex factor.