@@ -61,46 +61,6 @@ real normal_copula_vector_lpdf(vector u, vector v, real rho) {
6161 return a1 * x / a2 - N * a3;
6262}
6363
64- /**
65- * Multi-Normal Cholesky copula log density
66- *
67- * \f{aligned}{
68- * c(\mathbf{u}) &= \frac{\partial^d C}{\partial \mathbf{\Phi_1}\cdots \partial \mathbf{\Phi_d}} \\
69- * &= \frac{1}{\sqrt{\det \Sigma}} \exp \Bigg(-\frac{1}{2}
70- * \begin{pmatrix} \mathbf{\Phi}^{-1}(u_1) \\ \vdots \\ \mathbf{\Phi}^{-1}(u_d) \end{pmatrix}^T
71- * (\Sigma^{-1} - I)
72- * \begin{pmatrix} \mathbf{\Phi}^{-1}(u_1) \\ \vdots \\ \mathbf{\Phi}^{-1}(u_d) \end{pmatrix}
73- * \Bigg) \\
74- * &= \frac{1}{\sqrt{\det \Sigma}} \exp \bigg(-\frac{1}{2}(A^T\Sigma^{-1}A - A^TA) \bigg)\f}
75- * where
76- * \f[
77- * A = \begin{pmatrix} \mathbf{\Phi}^{-1}(u_1) \\ \vdots \\ \mathbf{\Phi}^{-1}(u_d) \end{pmatrix}.
78- * \f]
79- * Note that \f$\det \Sigma = |LL^T| = |L |^2 = \big(\prod_{i=1}^d L_{i,i}\big)^2\f$ so \f$\sqrt{\det \Sigma} = \prod_{i=1}^d L_{i,i}\f$
80- * and
81- *
82- *\f{aligned}{
83- * c(\mathbf{u}) &= \Bigg(\prod_{i=1}^d L_{i,i}\Bigg)^{-1} \exp \bigg(-\frac{1}{2}(A^T(LL^T)^{-1}A - A^TA)\bigg) \\
84- * &= \Bigg(\prod_{i=1}^d L_{i,i}\Bigg)^{-1} \exp \bigg(-\frac{1}{2}\big((L^{-1}A)^TL^{-1}A - A^TA\big)\bigg)
85- \f}
86- * where \f$X = L^{-1}A\f$ can be solved with \code{.cpp} X = mdivide_left_tri_low(L, A)\endcode.
87- *
88- * @copyright Sean Pinkney, 2021
89- *
90- * @param u Matrix
91- * @param L Cholesky factor matrix
92- * @return log density
93- */
94-
95- real multi_normal_copula_lpdf(matrix u, matrix L) {
96- int K = rows(u);
97- int N = cols(u);
98- real inv_sqrt_det_log = sum(log(diagonal(L)));
99- matrix[K, N] x = mdivide_left_tri_low(L, u);
100-
101- return -N * inv_sqrt_det_log - 0.5 * sum(columns_dot_self(x) - columns_dot_self(u));
102- }
103-
10464/**
10565 * Bivariate Normal Copula cdf
10666 *
@@ -133,41 +93,25 @@ real bivariate_normal_copula_cdf(vector u, real rho) {
13393 * Multivariate Normal Copula log density (Cholesky parameterisation)
13494 *
13595 * \f{aligned}{
136- * c(\mathbf{u}) &= \frac{\partial^d C}{\partial \mathbf{\Phi_1}\cdots \partial \mathbf{\Phi_d}} \\
137- * &= \prod_{i=1}^{n} \lvert \Sigma \rvert^{-1/2} \exp \left\{-\frac{1}{2}
96+ * c(\mathbf{u}) &= \frac{\partial^d C}{\partial \mathbf{\Phi_1}\cdots \partial \mathbf{\Phi_d}} \\
97+ * &= \prod_{i=1}^{n} \lvert \Sigma \rvert^{-1/2} \exp \left\{-\frac{1}{2}
13898 * \begin{pmatrix} \mathbf{\Phi}^{-1}(u_1) \\ \vdots \\ \mathbf{\Phi}^{-1}(u_d) \end{pmatrix}^T
13999 * (\Sigma^{-1} - I)
140100 * \begin{pmatrix} \mathbf{\Phi}^{-1}(u_1) \\ \vdots \\ \mathbf{\Phi}^{-1}(u_d) \end{pmatrix}
141101 * \right\} \\
142102 * &= \lvert \Sigma \rvert^{-n/2} \exp\left\{ -\frac{1}{2} \text{tr}\left( \left[ \Sigma^{-1} - I \right] \sum_{i=1}^n \Phi^{-1}(u_i) \Phi^{-1}(u_i)' \right) \right\} \hspace{0.5cm} \\
143- * &= \lvert L \rvert^{-n} \exp\left\{ -\frac{1}{2} \text{tr}\left( \left[ L^{-T}L^{-1}- I \right] Q'Q \right) \\
144- * &= \lvert L \rvert^{-n} \exp\left\{ -\frac{1}{2} \text{tr}\left( L^{-T}L^{-1} Q'Q - Q'Q \right) \right\} \\
145- * &= \lvert L \rvert^{-n} \exp\left\{ -\frac{1}{2} \text{tr}\left( QL^{-T}L^{-1}Q' \right) + \text{tr} \left( Q'Q \right) \right\} \\
146- * &= \lvert L \rvert^{-n} \exp\left\{ -\frac{1}{2} \left[ \text{tr}\left( [L^{-1}Q'][L^{-1}Q']' \right) + \text{tr} \left( Q'Q \right) \right]\right\}
103+ * &= \lvert L \rvert^{-n} \exp\left\{ -\frac{1}{2} \text{tr}\left( \left[ L^{-T}L^{-1}- I \right] Q'Q \right) \right\} \\
104+ * &= \lvert L \rvert^{-n} \exp\left\{ -\frac{1}{2} \sum_{j=1}^{d}\sum_{i=1}^{n} \left( \left[ L^{-T}L^{-1}- I \right] \odot Q'Q \right) \right\} \\
147105 * \f}
148106 * where
149107 * \f[
150- * Q_{n \times d} = \begin{pmatrix} \mathbf{\Phi}^{-1}(u_1) \\ \vdots \\ \mathbf{\Phi}^{-1}(u_d) \end{pmatrix}.
151- * \f]
152- * Note that \f$\det \Sigma = |LL^T| = |L |^2 = \big(\prod_{i=1}^d L_{i,i}\big)^2\f$ so \f$\sqrt{\det \Sigma} = \prod_{i=1}^d L_{i,i}\f$
153- * and
154- *
155- *\f{aligned}{
156- * c(\mathbf{u}) &= \Bigg(\prod_{i=1}^d L_{i,i}\Bigg)^{-1} \exp \bigg(-\frac{1}{2}(A^T(LL^T)^{-1}A - A^TA)\bigg) \\
157- * &= \Bigg(\prod_{i=1}^d L_{i,i}\Bigg)^{-1} \exp \bigg(-\frac{1}{2}\big((L^{-1}A)^TL^{-1}A - A^TA\big)\bigg)
158- \f}
159- * where \f$X = L^{-1}A\f$ can be solved with \code{.cpp} X = mdivide_left_tri_low(L, A)\endcode.
108+ * Q_{n \times d} = \begin{pmatrix} \mathbf{\Phi}^{-1}(u_1) \\ \vdots \\ \mathbf{\Phi}^{-1}(u_d) \end{pmatrix}
109+ * \f]
110+ * and \f$ \odot \f$ is the elementwise multiplication operator.
111+ * Note that \f$\det \Sigma = |LL^T| = |L |^2 = \big(\prod_{i=1}^d L_{i,i}\big)^2\f$ so \f$\sqrt{\det \Sigma} = \prod_{i=1}^d L_{i,i}\f$.
160112 *
161113 * @copyright Ethan Alt, Sean Pinkney 2021
162114 *
163- * @param U Matrix where the number of rows equal observations and the columns are equal to the number of outcomes
164- * @param L Cholesky factor of the correlation matrix
165- * @return log density of the distribution
166- */
167-
168- /**
169- * Multivariate Normal Copula lpdf (Cholesky parameterisation)
170- *
171115 * @param U Matrix of outcomes from marginal calculations
172116 * @param L Cholesky factor of the correlation/covariance matrix
173117 * @return log density of distribution
0 commit comments