Skip to content

Commit 0e24cfd

Browse files
committed
add docs, add limits for adding distributions
1 parent 57ab650 commit 0e24cfd

File tree

4 files changed

+92
-0
lines changed

4 files changed

+92
-0
lines changed

ciw/dists/distributions.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,18 @@ def variance(self):
150150
return float('nan')
151151
return (v1 / (m2 ** 2)) + ((m1 ** 2) * v2) / (m2 ** 4)
152152

153+
@property
154+
def upper_limit(self):
155+
if self.operator == add:
156+
return self.d1.upper_limit + self.d2.upper_limit
157+
return float('nan')
158+
159+
@property
160+
def lower_limit(self):
161+
if self.operator == add:
162+
return self.d1.lower_limit + self.d2.lower_limit
163+
return float('nan')
164+
153165

154166
class Uniform(Distribution):
155167
"""

docs/Guides/Distributions/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ Contents:
99
set_distributions.rst
1010
phasetype.rst
1111
combining_distributions.rst
12+
summary_stats.rst
1213
time_dependent.rst
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
.. _set-dists:
2+
3+
===============================================
4+
How to Access Distributions' Summary Statistics
5+
===============================================
6+
7+
Every distribution object in Ciw has attributes giving summary statisics for the distributions. These are:
8+
9+
+ Mean
10+
+ Median
11+
+ Variance
12+
+ Standard deviation
13+
+ Upper limit
14+
+ Lower limit
15+
16+
They can be accessed as follows::
17+
18+
>>> import ciw
19+
>>> D = ciw.dists.Exponential(rate=5)
20+
>>> D.mean
21+
0.2
22+
>>> D.median
23+
0.1386294361...
24+
>>> D.variance
25+
0.04
26+
>>> D.sd
27+
0.2
28+
>>> D.upper_limit
29+
inf
30+
>>> D.lower_limit
31+
0
32+
33+
34+
In some cases where a closed from expression does not exist, the attribute will return :code:`nan`. For example::
35+
36+
>>> G = ciw.dists.Gamma(shape=0.6, scale=1.2)
37+
>>> G.median
38+
nan
39+
40+
41+
And where possible, summary statistics are calculated for :ref:`combined distributions <combine-dists>`. For example::
42+
43+
44+
>>> D1 = ciw.dists.Uniform(lower=2, upper=6)
45+
>>> D2 = ciw.dists.Uniform(lower=3, upper=9)
46+
>>> D = D1 + D2
47+
>>> D1.mean, D2.mean, D.mean
48+
(4.0, 6.0, 10.0)
49+
>>> D1.sd, D2.sd, D.sd
50+
(1.154..., 1.732..., 2.081...)
51+
>>> D1.lower_limit, D2.lower_limit, D.lower_limit
52+
(2, 3, 5)
53+
>>> D1.upper_limit, D2.upper_limit, D.upper_limit
54+
(6, 9, 15)
55+
56+
and :ref:`mixture distributions <combine-dists>`::
57+
58+
>>> D1 = ciw.dists.Uniform(lower=2, upper=6)
59+
>>> D2 = ciw.dists.Uniform(lower=3, upper=9)
60+
>>> D = ciw.dists.MixtureDistribution(
61+
... dists=[D1, D2],
62+
... probs=[0.5, 0.5]
63+
... )
64+
>>> D.mean
65+
5.0
66+
>>> D.sd
67+
1.7795...
68+
>>> D.upper_limit
69+
9
70+
>>> D.lower_limit
71+
2
72+
73+
74+
75+

tests/test_sampling.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1205,6 +1205,8 @@ def test_combined_add_summary_stats(self):
12051205
expected_var = A.variance + B.variance # 1 + 0.25 = 1.25
12061206
self.assertEqual(C.mean, expected_mean)
12071207
self.assertEqual(C.variance, expected_var)
1208+
self.assertEqual(C.upper_limit, float('inf'))
1209+
self.assertEqual(C.lower_limit, 0)
12081210

12091211
def test_combined_sub_summary_stats(self):
12101212
A = ciw.dists.Normal(5.0, 1.0)
@@ -1215,6 +1217,8 @@ def test_combined_sub_summary_stats(self):
12151217
expected_var = A.variance + B.variance # 1 + 0.25 = 1.25
12161218
self.assertEqual(C.mean, expected_mean)
12171219
self.assertEqual(C.variance, expected_var)
1220+
self.assertTrue(math.isnan(C.upper_limit))
1221+
self.assertTrue(math.isnan(C.lower_limit))
12181222

12191223
def test_combined_mul_summary_stats(self):
12201224
# Product moments (independent):

0 commit comments

Comments
 (0)