Skip to content

Commit

Permalink
improve Lognormal docstring + add unit tests (#1319)
Browse files Browse the repository at this point in the history
thanks @crocicc for feedback
  • Loading branch information
slayoo authored Apr 24, 2024
1 parent 4463ed7 commit 12d03e9
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
3 changes: 3 additions & 0 deletions PySDM/initialisation/spectra/lognormal.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@

class Lognormal(Spectrum):
def __init__(self, norm_factor: float, m_mode: float, s_geom: float):
"""`norm_factor=1` corresponds to standard normalised probability density,
other settings allow to express, e.g., size or mass distributions;
`m_mode` is the median value, `s_geom` is the geometric standard deviation"""
super().__init__(lognorm, (math.log(s_geom), 0, m_mode), norm_factor)

@property
Expand Down
52 changes: 52 additions & 0 deletions tests/unit_tests/initialisation/test_spectra_lognormal.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
""" tests for lognormal probability distribution """

import numpy as np
import pytest
from PySDM.initialisation.spectra import Lognormal


class TestSpectraLognormal:
"""checks ctor args against computed values"""

@staticmethod
@pytest.mark.parametrize(
"m_mode, s_geom",
(
(0.01, 1.5),
(0.1, 1.1),
(1, 1.01),
),
)
def test_median(m_mode, s_geom):
# arrange
sut = Lognormal(m_mode=m_mode, norm_factor=1, s_geom=s_geom)

# act
median = sut.percentiles(0.5)

# assert
assert median == m_mode

@staticmethod
@pytest.mark.parametrize(
"m_mode, s_geom",
(
(0.01, 3.5),
(0.1, 2.1),
(1, 1.5),
),
)
def test_mean(m_mode, s_geom):
# arrange
sut = Lognormal(m_mode=m_mode, norm_factor=1, s_geom=s_geom)
x = np.linspace(m_mode / 1000, m_mode * 1000, num=10000)

# act
mean = np.sum(sut.pdf(x) * x) / np.sum(sut.pdf(x))

# assert
np.testing.assert_approx_equal(
actual=np.log(m_mode) + 0.5 * np.log(s_geom) ** 2,
desired=np.log(mean),
significant=3,
)

0 comments on commit 12d03e9

Please sign in to comment.