Skip to content

Commit

Permalink
Merge pull request #67 from aimalz/issue/65/conversions
Browse files Browse the repository at this point in the history
Issue/65/conversions, closes #65
  • Loading branch information
aimalz authored Mar 31, 2017
2 parents eb6665d + 759c4e7 commit edcedfe
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
1 change: 1 addition & 0 deletions docs/notebooks/demo.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@
"gridded = P.evaluate(grid, using='truth', vb=False)\n",
"\n",
"G = qp.PDF(gridded=gridded)\n",
"G.sample(100, vb=False)\n",
"G.plot()"
]
},
Expand Down
15 changes: 13 additions & 2 deletions qp/pdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,17 @@ def sample(self, N=100, infty=100., using=None, vb=True):
elif using == 'mix_mod':
samples = self.mix_mod.rvs(size=N)

elif using == 'gridded':
interpolator = self.interpolate(using = 'gridded')
(xmin, xmax) = (min(self.gridded[0]), max(self.gridded[0]))
(ymin, ymax) = (min(self.gridded[1]), max(self.gridded[1]))
(xran, yran) = (xmax - xmin, ymax - ymin)
samples = []
while len(samples) < N:
(x, y) = (xmin + xran * np.random.uniform(), ymin + yran * np.random.uniform())
if y < interpolator(x):
samples.append(x)

else:
if using == 'quantiles':
# First find the quantiles if none exist:
Expand Down Expand Up @@ -353,7 +364,7 @@ def sample(self, N=100, infty=100., using=None, vb=True):
samples.append(np.random.uniform(low=endpoints[c], high=endpoints[c+1]))

if vb: print 'Sampled values: ', samples
self.samples = samples
self.samples = np.array(samples)
self.last = 'samples'
return self.samples

Expand Down Expand Up @@ -508,7 +519,7 @@ def plot(self, vb=True):
x = np.linspace(min_x, max_x, 100)
extrema = [min(extrema[0], min_x), max(extrema[1], max_x)]
y = self.mix_mod.pdf(x)
plt.plot(x, y, color='k', linestyle=':', lw=1.0, alpha=0.5, label='Mixture Model PDF')
plt.plot(x, y, color='k', linestyle=':', lw=1.0, alpha=1.0, label='Mixture Model PDF')
if vb:
print 'Plotted mixture model.'

Expand Down

0 comments on commit edcedfe

Please sign in to comment.