-
Notifications
You must be signed in to change notification settings - Fork 0
/
posnorm.py
32 lines (23 loc) · 955 Bytes
/
posnorm.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import scipy.stats as stats
import numpy as np
class posnorm_gen(stats.rv_continuous):
"""Positive normal distribution"""
def _pdf(self, x, mu, s):
a = -mu / s
return stats.truncnorm.pdf(x, a, np.inf, scale=s, loc=mu)
def _cdf(self, x, mu, s):
a = -mu / s
return stats.truncnorm.cdf(x, a, np.inf, scale=s, loc=mu)
def _fitstart(self, data, args=None):
return np.mean(data), np.std(data), 0.0, 1.0
def _get_support(self, *args, **kwargs):
return 0.0, np.inf
def _rvs(self, mu, s, size=None, random_state=None):
a = -mu / s
return stats.truncnorm.rvs(a, np.inf, scale=s, loc=mu, size=size, random_state=random_state)
def _argcheck(self, *args):
return args[1] > 0
def _ppf(self, q, mu, s):
a = -mu / s
A = (1 - stats.norm.cdf(a)) * q + stats.norm.cdf(a)
return s * stats.norm.ppf(A) + mu