Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: improve the numerical stability of lfc_shrink #370

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions pydeseq2/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,10 @@ def load_example_data(
pandas.DataFrame
Requested data modality.
"""
assert modality in ["raw_counts", "metadata"], (
"The modality argument must be one of the following: " "raw_counts, metadata"
)
assert modality in [
"raw_counts",
"metadata",
], "The modality argument must be one of the following: raw_counts, metadata"

assert dataset in [
"synthetic"
Expand Down Expand Up @@ -1078,13 +1079,14 @@ def f(beta: np.ndarray, cnst: float = scale_cnst) -> float:
def df(beta: np.ndarray, cnst: float = scale_cnst) -> np.ndarray:
# Gradient of the function to optimize
xbeta = design_matrix @ beta
exp_xbeta_off = np.exp(xbeta + offset)
d_neg_prior = (
beta * no_shrink_mask / prior_no_shrink_scale**2
+ 2 * beta * shrink_mask / (prior_scale**2 + beta[shrink_index] ** 2)
)

d_nll = (
counts - (counts + size) / (1 + size * np.exp(-xbeta - offset))
size * (counts - exp_xbeta_off) / (size + exp_xbeta_off)
) @ design_matrix

return (d_neg_prior - d_nll) / cnst
Expand Down