[change] introducing a whole new concept of handling pdfs; evaluating function on demand rather than holding a sampled array

This commit is contained in:
Sebastian Wehling-Benatelli 2016-07-11 10:42:04 +02:00
parent 950696053c
commit 08fc5d554b

View File

@ -128,8 +128,18 @@ class ProbabilityDensityFunction(object):
assert isinstance(other, ProbabilityDensityFunction), \ assert isinstance(other, ProbabilityDensityFunction), \
'both operands must be of type ProbabilityDensityFunction' 'both operands must be of type ProbabilityDensityFunction'
x0, incr, npts, pdf_self, pdf_other = self.rearrange(other) if not self.incr == other.incr:
pdf = np.convolve(pdf_self, pdf_other, 'full') * incr raise NotImplementedError('Upsampling of the lower sampled PDF not implemented yet!')
else:
incr = self.incr
x0, npts = self.commonlimits(incr, other)
axis = create_axis(x0, incr, npts)
pdf_self = np.array([self.data(x) for x in axis])
pdf_other = np.array([other.data(x) for x in axis])
pdf = lambda x: np.convolve(pdf_self, pdf_other, 'full') * incr
# shift axis values for correct plotting # shift axis values for correct plotting
npts = pdf.size npts = pdf.size
@ -169,7 +179,10 @@ class ProbabilityDensityFunction(object):
return prec if prec >= 0 else 0 return prec if prec >= 0 else 0
def data(self, value): def data(self, value):
return self._pdf(value, self.mu, *self.params) try:
return [self._pdf(k, self.mu, *self.params) for k in iter(value)]
except TypeError:
return self._pdf(value, self.mu, *self.params)
@property @property
def mu(self): def mu(self):
@ -296,7 +309,7 @@ class ProbabilityDensityFunction(object):
def quantile(self, prob_value, eps=0.01): def quantile(self, prob_value, eps=0.01):
''' '''
:param prob_value: :param prob_value:
:param eps: :param eps:
:return: :return:
@ -324,7 +337,9 @@ class ProbabilityDensityFunction(object):
def plot(self, label=None): def plot(self, label=None):
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
plt.plot(self.axis, self.data) axis = self.axis
plt.plot(axis, self.data(axis))
plt.xlabel('x') plt.xlabel('x')
plt.ylabel('f(x)') plt.ylabel('f(x)')
plt.autoscale(axis='x', tight=True) plt.autoscale(axis='x', tight=True)
@ -388,15 +403,10 @@ class ProbabilityDensityFunction(object):
:return: :return:
''' '''
assert isinstance(other, ProbabilityDensityFunction), \ x0 = self.x0
'both operands must be of type ProbabilityDensityFunction' incr = self.incr
npts = self.npts
if not self.incr == other.incr:
raise NotImplementedError('Upsampling of the lower sampled PDF not implemented yet!')
else:
incr = self.incr
x0, npts = self.commonlimits(incr, other)
pdf_self = np.zeros(npts) pdf_self = np.zeros(npts)
pdf_other = np.zeros(npts) pdf_other = np.zeros(npts)