gaussian_copula_helpers
Sampling and likelihood helpers for the Gaussian copula
The copula loglikelihood of an entry is the joint normal log-density minus the independent one,
.. math:: \ell(z) = \log \phi_\Sigma(z) - \sum_j \log \phi(z_j),
For each entry, we use :func:batch_log_likelihood to compute the difference
directly, then form the group total (:func:group_log_likelihood) using a
trace formula documented in that function's docstring. Also includes helpers for
computing a Cholesky factorization, which can be shared for both likelihood
calculation and sampling.
batch_log_likelihood(z, cov_struct)
Per-entry copula log-likelihood for a batch of gaussianized scores.
Unlike :func:group_log_likelihood, this keeps the cells separate, so it
needs the scores themselves.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
z
|
ndarray
|
Normal scores, shape |
required |
cov_struct
|
CovarianceStructure
|
Fitted structure for the group these cells belong to. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
Log-likelihood per cell, shape |
Source code in scdesigner/src/scdesigner/copulas/gaussian_copula_helpers.py
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 | |
cholesky_factor(corr, group)
Cholesky factor of a correlation matrix
Source code in scdesigner/src/scdesigner/copulas/gaussian_copula_helpers.py
42 43 44 45 46 47 48 49 50 51 52 53 | |
covariance_to_correlation(cov)
Rescale a covariance matrix to unit diagonal.
Source code in scdesigner/src/scdesigner/copulas/gaussian_copula_helpers.py
31 32 33 34 35 36 37 38 39 | |
factorize_correlation(corr, group, jitter=False)
Factorize a correlation matrix
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
corr
|
ndarray
|
Correlation matrix, shape |
required |
group
|
str or int
|
Group label, used in messages. |
required |
jitter
|
bool
|
Add jitter to the diagonal of the original input isn't PSD. |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
corr |
ndarray
|
The matrix used after applying potential ridge |
factor |
ndarray
|
Its lower Cholesky factor. |
Source code in scdesigner/src/scdesigner/copulas/gaussian_copula_helpers.py
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 | |
group_log_likelihood(factor, second_moment, n_samples)
Copula log-likelihood for a group
Adding :math:\ell(z_i) over the group gives
.. math::
\sum_i \ell(z_i) = -\tfrac{1}{2}\left( n \log|\Sigma|
+ \operatorname{tr}(\Sigma^{-1} S) - \operatorname{tr}(S) \right),
where :math:S = \sum_i z_i z_i^\top. Note that we assume the mean is zero,
matching the copula assumptions. So, we only ever need S in our inputs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
factor
|
ndarray
|
Lower Cholesky factor of the group's correlation matrix. |
required |
second_moment
|
ndarray
|
Uncentered second moment :math: |
required |
n_samples
|
int
|
Cells in the group. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Total log-likelihood. |
Source code in scdesigner/src/scdesigner/copulas/gaussian_copula_helpers.py
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 | |
sample_pseudo_obs(n_samples, cov_struct, rng=None)
Draw dependent uniform pseudoobservations from a CovarianceStructure
Genes outside the CovarianceStructure's top_k block are drawn independently.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_samples
|
int
|
Entries to generate. |
required |
cov_struct
|
CovarianceStructure
|
Jointly modeled block plus remaining independent genes. |
required |
rng
|
Generator
|
Optional random number generator. |
None
|
Returns:
| Type | Description |
|---|---|
ndarray
|
Uniform values, shape |
Source code in scdesigner/src/scdesigner/copulas/gaussian_copula_helpers.py
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 | |