-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdest.py
42 lines (33 loc) · 1.26 KB
/
dest.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
33
34
35
36
37
38
39
40
41
42
from scipy import size,linspace
from matplotlib.pyplot import plot,show
from scipy.stats.kde import gaussian_kde
class dest(object):
"""Density estimator"""
def __init__(self, rvs):
self.rvs = rvs.copy()
self.rvs.sort()
self.size = size(self.rvs)
self.kde = gaussian_kde(self.rvs)
def pdf(self,x):
if size(x)==1:
return self.kde(x)[0]
else:
return self.kde(x)
def ppf(self,x):
if x<0 or x>1:
raise ValueError("Not between 0 and 1")
return self.rvs[int(x*(self.size-1))]
def cdf(self,x):
return size(self.rvs[self.rvs<x])/float(self.size)
def graph(self,inc=1000):
lowb = self.rvs[0] - (self.ppf(.1)-self.ppf(0.))
upb = self.rvs[self.size-1] + (self.ppf(.1)-self.ppf(0.))
x = linspace(lowb,upb,inc)
plot(x,self.kde(x))
show()
def Confident(self, confLevel):
lowerbound = int(round(((1.- confLevel)/2.)*self.size))
upperbound = int(round((((1.- confLevel)/2.)+confLevel)*self.size))
LowBound = self.rvs[lowerbound]
UpBound = self.rvs[upperbound]
return LowBound, UpBound