Skip to content

Commit

Permalink
add variance to distribution
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew McCluskey committed May 2, 2020
1 parent 7862dc8 commit 0e979cb
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
23 changes: 21 additions & 2 deletions uravu/distribution.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,15 +90,34 @@ def n(self):
return np.percentile(self.samples, [50])[0]

@property
def s(self):
def s(self, ddof=1):
"""
Get the standard deviation of the distribution. For a non-normal distribution, this will return :py:attr:`None`.
Args:
ddof (:py:attr:`int`): Degrees of freedom to be included in calculation.
Returns:
:py:attr:`float` or :py:attr:`None`: Standard deviation of the distribution.
"""
if self.normal:
return np.std(self.samples, ddof=ddof)
else:
return None

@property
def v(self, ddof=1):
"""
Get the variance of the distribution. For a non-normal distribution, this will return :py:attr:`None`.
Args:
ddof (:py:attr:`int`): Degrees of freedom to be included in calculation.
Returns:
:py:attr:`float` or :py:attr:`None`: Standard deviation of the distribution.
"""
if self.normal:
return np.std(self.samples)
return np.var(self.samples, ddof=ddof)
else:
return None

Expand Down
8 changes: 8 additions & 0 deletions uravu/tests/test_distribution.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,14 @@ def test_check_normality_less_than_3(self):
distro = Distribution(np.random.randn(2))
assert_equal(distro.normal, False)

def test_s(self):
"""
Test the standard deviation and variance
"""
distro = Distribution(np.random.randn(10000) * 10)
assert_almost_equal(distro.s, 10., decimal=1)
assert_almost_equal(distro.v, 100., decimal=0)

def test_add_samples_single(self):
"""
Test add_samples with a single value.
Expand Down

0 comments on commit 0e979cb

Please sign in to comment.