Skip to content

Commit 7ceb215

Browse files
committed
Implement sampling for v2 prior distributions
1 parent 5429412 commit 7ceb215

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

petab/v1/distributions.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,9 @@ def _cdf_untransformed_untruncated(self, x) -> np.ndarray | float:
508508
def _ppf_untransformed_untruncated(self, q) -> np.ndarray | float:
509509
return cauchy.ppf(q, loc=self._loc, scale=self._scale)
510510

511+
def _sample(self, shape=None) -> np.ndarray | float:
512+
return cauchy.rvs(loc=self._loc, scale=self._scale, size=shape)
513+
511514
@property
512515
def loc(self) -> float:
513516
"""The location parameter of the underlying distribution."""
@@ -565,6 +568,9 @@ def _cdf_untransformed_untruncated(self, x) -> np.ndarray | float:
565568
def _ppf_untransformed_untruncated(self, q) -> np.ndarray | float:
566569
return chi2.ppf(q, df=self._dof)
567570

571+
def _sample(self, shape=None) -> np.ndarray | float:
572+
return chi2.rvs(df=self._dof, size=shape)
573+
568574
@property
569575
def dof(self) -> int:
570576
"""The degrees of freedom parameter."""
@@ -602,6 +608,9 @@ def _cdf_untransformed_untruncated(self, x) -> np.ndarray | float:
602608
def _ppf_untransformed_untruncated(self, q) -> np.ndarray | float:
603609
return expon.ppf(q, scale=self._scale)
604610

611+
def _sample(self, shape=None) -> np.ndarray | float:
612+
return expon.rvs(scale=self._scale, size=shape)
613+
605614
@property
606615
def scale(self) -> float:
607616
"""The scale parameter of the underlying distribution."""
@@ -650,6 +659,9 @@ def _cdf_untransformed_untruncated(self, x) -> np.ndarray | float:
650659
def _ppf_untransformed_untruncated(self, q) -> np.ndarray | float:
651660
return gamma.ppf(q, a=self._shape, scale=self._scale)
652661

662+
def _sample(self, shape=None) -> np.ndarray | float:
663+
return gamma.rvs(a=self._shape, scale=self._scale, size=shape)
664+
653665
@property
654666
def shape(self) -> float:
655667
"""The shape parameter of the underlying distribution."""
@@ -700,6 +712,9 @@ def _cdf_untransformed_untruncated(self, x) -> np.ndarray | float:
700712
def _ppf_untransformed_untruncated(self, q) -> np.ndarray | float:
701713
return rayleigh.ppf(q, scale=self._scale)
702714

715+
def _sample(self, shape=None) -> np.ndarray | float:
716+
return rayleigh.rvs(scale=self._scale, size=shape)
717+
703718
@property
704719
def scale(self) -> float:
705720
"""The scale parameter of the underlying distribution."""

tests/v1/test_distributions.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@
3434
Normal(2, 1, log=10),
3535
Laplace(1, 2, trunc=(1, 2)),
3636
Laplace(1, 0.5, log=True, trunc=(0.5, 8)),
37+
Cauchy(2, 1),
38+
ChiSquare(4),
39+
Exponential(1),
40+
Gamma(3, 5),
41+
Rayleigh(3),
3742
],
3843
)
3944
def test_sample_matches_pdf(distribution):

0 commit comments

Comments
 (0)