Skip to content

Commit

Permalink
fix fdd signature, move test in a dedicated file
Browse files Browse the repository at this point in the history
  • Loading branch information
MainRo committed Feb 5, 2022
1 parent 79b0281 commit efa2d10
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 14 deletions.
4 changes: 2 additions & 2 deletions distogram/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ def histogram(
return u


def frequency_density_distribution(h: Distogram) -> List[Tuple[float, float]]:
def frequency_density_distribution(h: Distogram) -> Tuple[List[float], List[float]]:
""" Returns a histogram of the distribution
Args:
Expand All @@ -379,7 +379,7 @@ def frequency_density_distribution(h: Distogram) -> List[Tuple[float, float]]:
densities = [
(new - last) / delta
for new, last, delta in zip(counts[1:], counts[:-1], bin_widths)]
return tuple([densities, bin_bounds])
return (densities, bin_bounds)


def quantile(h: Distogram, value: float) -> Optional[float]:
Expand Down
26 changes: 26 additions & 0 deletions tests/test_frequency_density_distribution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import random
import numpy as np
from pytest import approx
import distogram
import pytest


def test_frequency_density_distribution():
normal = [random.normalvariate(0.0, 1.0) for _ in range(10000)]
h = distogram.Distogram(bin_count=64)

for i in normal:
h = distogram.update(h, i)

h = distogram.Distogram(bin_count=3)
h = distogram.update(h, 23)
h = distogram.update(h, 28)
h = distogram.update(h, 16)

hist = distogram.frequency_density_distribution(h)
integral = 0
for density, new, old in zip(hist[0], hist[1][1:], hist[1][:-1]):
integral += density * (new-old)

assert(hist == (approx([0.21428571428571427, 0.3]), [16.0, 23.0, 28.0]))
assert(integral == approx(3.0))
12 changes: 0 additions & 12 deletions tests/test_histogram.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,6 @@ def test_histogram():
[16.0, 20.0, 24.0, 28]))
assert(sum(distogram.histogram(h, bin_count=3)[0]) == approx(3.0))

hist = distogram.frequency_density_distribution(h)
integral = 0
for density, new, old in zip(hist[0], hist[1][1:], hist[1][:-1]):
integral += density * (new-old)

assert(hist == approx(([0.21428571428571427, 0.3], [16.0, 23.0, 28.0])))
assert(integral == approx(3.0))

# how to compare histograms?
#assert np_values == approx(d_values, abs=0.2)
#assert np_edges == approx(d_edges, abs=0.2)


def test_histogram_on_too_small_distribution():
h = distogram.Distogram(bin_count=64)
Expand Down

0 comments on commit efa2d10

Please sign in to comment.