Skip to content

Commit 2698724

Browse files
committed
tidy up some lines, tests, merge some tests
1 parent 237c2d8 commit 2698724

File tree

2 files changed

+245
-383
lines changed

2 files changed

+245
-383
lines changed

ciw/dists/distributions.py

Lines changed: 28 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,28 @@
11
'''Distributions available in Ciw.'''
22

3-
import copy
4-
import math
5-
import random
6-
from math import sqrt, exp, pi, erf
7-
from itertools import cycle
8-
from operator import add, mul, sub, truediv
9-
from random import (
10-
expovariate,
11-
uniform,
12-
triangular,
13-
gammavariate,
14-
lognormvariate,
15-
weibullvariate,
3+
import copy
4+
import math
5+
import random
6+
from math import sqrt, exp, pi, erf
7+
from itertools import cycle
8+
from operator import add, mul, sub, truediv
9+
from random import (
10+
expovariate,
11+
uniform,
12+
triangular,
13+
gammavariate,
14+
lognormvariate,
15+
weibullvariate,
1616
)
17-
from typing import List, NoReturn
17+
from typing import List, NoReturn
1818

19-
import numpy as np
20-
from scipy.stats import norm
19+
import numpy as np
20+
from scipy.stats import norm
2121

22-
from ciw.auxiliary import *
23-
from ciw.individual import Individual
22+
from ciw.auxiliary import *
23+
from ciw.individual import Individual
2424

25-
class Distribution(object):
25+
class Distribution(object):
2626
"""
2727
A general distribution from which all other distirbutions will inherit.
2828
"""
@@ -126,8 +126,9 @@ def mean(self):
126126
elif self.operator == mul:
127127
return m1 * m2
128128
elif self.operator == truediv:
129-
return float('nan') if m2 == 0 else m1 / m2 # delta-method mean
130-
return float('nan')
129+
if m2 == 0:
130+
return float('nan')
131+
return m1 / m2 # delta-method mean
131132

132133
@property
133134
def variance(self):
@@ -148,7 +149,6 @@ def variance(self):
148149
if m2 == 0:
149150
return float('nan')
150151
return (v1 / (m2 ** 2)) + ((m1 ** 2) * v2) / (m2 ** 4)
151-
return float('nan')
152152

153153

154154
class Uniform(Distribution):
@@ -759,7 +759,7 @@ def variance(self):
759759

760760
# Var(T) = E[T^2] - (E[T])^2 (with tinynegative clamp)
761761
v = second_moment - (mean ** 2)
762-
return 0.0 if v < 0 and abs(v) <= 1e-12 else v
762+
return v
763763

764764
@property
765765
def upper_limit(self):
@@ -892,8 +892,7 @@ def variance(self):
892892
for p, r, k in zip(self.probs, self.rates, self.phase_lengths)
893893
)
894894
v = second_moment - (mean ** 2)
895-
# tiny numerical guard
896-
return 0.0 if v < 0 and abs(v) <= 1e-12 else v
895+
return v
897896

898897

899898
class Coxian(PhaseType):
@@ -1007,7 +1006,7 @@ def overall_rate(self):
10071006
]
10081007
P = self.endpoints[-1]
10091008
LambdaP = sum(r * d for r, d in zip(self.rates, deltas))
1010-
return 0. if P == 0 else (LambdaP / P)
1009+
return LambdaP / P
10111010

10121011
@property
10131012
def mean(self):
@@ -1016,8 +1015,7 @@ def mean(self):
10161015

10171016
@property
10181017
def variance(self):
1019-
O_R = self.overall_rate
1020-
return 1 / (O_R ** 2) if O_R > 0 else float('inf')
1018+
return float('nan')
10211019

10221020
@property
10231021
def median(self):
@@ -1062,7 +1060,7 @@ def variance(self):
10621060
@property
10631061
def median(self):
10641062
"""this is a well known approximation of the median of a Poisson distribution"""
1065-
return float('nan') if self.rate == 0 else math.floor(self.rate + (1 / 3) - (0.02 / self.rate))
1063+
return math.floor(self.rate + (1 / 3) - (0.02 / self.rate))
10661064

10671065
@property
10681066
def upper_limit(self):
@@ -1103,7 +1101,7 @@ def variance(self):
11031101

11041102
@property
11051103
def median(self):
1106-
return math.ceil(-math.log(0.5) / math.log(1 - self.prob))
1104+
return math.ceil(-1 / math.log(1 - self.prob, 2))
11071105

11081106
@property
11091107
def upper_limit(self):

0 commit comments

Comments
 (0)