-
Notifications
You must be signed in to change notification settings - Fork 10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Issue/35/quantilespacing #38
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,6 @@ | |
|
||
import qp | ||
|
||
<<<<<<< HEAD | ||
class PDF(object): | ||
|
||
def __init__(self, truth=None, quantiles=None, histogram=None, | ||
|
@@ -37,8 +36,7 @@ def __init__(self, truth=None, quantiles=None, histogram=None, | |
self.initialized = 'histogram' | ||
self.last = self.initialized | ||
|
||
if vb and self.truth is None and self.quantiles is None | ||
and self.histogram is None: | ||
if vb and self.truth is None and self.quantiles is None and self.histogram is None: | ||
print 'Warning: initializing a PDF object without inputs' | ||
self.difs = None | ||
self.mids = None | ||
|
@@ -96,12 +94,14 @@ def integrate(self, limits): | |
""" | ||
return None | ||
|
||
def quantize(self, percent=1., number=None, vb=True): | ||
def quantize(self, quants=None, percent=1., number=None, vb=True): | ||
""" | ||
Computes an array of evenly-spaced quantiles from the truth. | ||
|
||
Parameters | ||
---------- | ||
quants: ndarray, float | ||
array of quantile locations as decimals | ||
percent: float | ||
the separation of the requested quantiles, in percent | ||
number: int | ||
|
@@ -123,25 +123,34 @@ def quantize(self, percent=1., number=None, vb=True): | |
object stored in `self.truth`. This calculates the inverse CDF. | ||
See `the Scipy docs <https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.rv_continuous.ppf.html#scipy.stats.rv_continuous.ppf>`_ for details. | ||
""" | ||
if number is not None: | ||
# Compute the spacing of the quantiles: | ||
quantum = 1.0 / float(number+1) | ||
if quants is not None: | ||
full_quants = np.append(np.array([0.]),quants) | ||
full_quants = np.append(full_quants,np.array([1.])) | ||
quantdifs = full_quants[1:]-full_quants[:-1] | ||
assert np.sum(quantdifs)==1. | ||
self.quantpoints = quants | ||
else: | ||
quantum = percent/100.0 | ||
# Over-write the number of quantiles: | ||
number = np.ceil(100.0 / percent) - 1 | ||
assert number > 0 | ||
|
||
points = np.linspace(0.0+quantum, 1.0-quantum, number) | ||
if vb: print("Calculating quantiles: ", points) | ||
if number is not None: | ||
# Compute the spacing of the quantiles: | ||
quantum = 1.0 / float(number+1) | ||
else: | ||
quantum = percent/100.0 | ||
# Over-write the number of quantiles: | ||
number = np.ceil(100.0 / percent) - 1 | ||
assert number > 0 | ||
|
||
self.quantpoints = np.linspace(0.0+quantum, 1.0-quantum, number) | ||
if vb: print("Calculating quantiles: ", self.quantpoints) | ||
if self.truth is not None: | ||
self.quantiles = self.truth.ppf(points) | ||
self.quantiles = self.truth.ppf(self.quantpoints) | ||
else: | ||
print('New quantiles can only be computed from a truth distribution in this version.') | ||
return | ||
|
||
if vb: print("Result: ", self.quantiles) | ||
self.quantvals = self.evaluate(self.quantiles) | ||
self.last = 'quantiles' | ||
self.quantiles = (self.quantiles, self.quantvals) | ||
return self.quantiles | ||
|
||
def histogramize(self, binends=None, nbins=10, binrange=[0., 1.], vb=True): | ||
|
@@ -222,9 +231,10 @@ def interpolate(self, using=None, vb=True): | |
if self.quantiles is None: | ||
self.quantiles = self.quantize() | ||
|
||
self.difs = self.quantiles[1:]-self.quantiles[:-1] | ||
self.mids = (self.quantiles[1:]+self.quantiles[:-1])/2. | ||
self.vals = (1.0/(len(self.quantiles)+1))/self.difs | ||
#self.difs = self.quantiles[0][1:]-self.quantiles[0][:-1] | ||
#self.mids = (self.quantiles[0][1:]+self.quantiles[0][:-1])/2. | ||
self.mids = self.quantiles[0] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Much neater! Feel free to remove the commented out parts, they're in the history. |
||
self.vals = self.quantiles[1]#(1.0/(len(self.quantiles)+1))/self.difs | ||
|
||
if using == 'histogram': | ||
# First find the histogram if none exists: | ||
|
@@ -300,7 +310,7 @@ def plot(self, limits, points=None): | |
plt.plot(x, self.truth.pdf(x), color='k', linestyle='-', lw=1.0, alpha=1.0, label='True PDF') | ||
|
||
if self.quantiles is not None: | ||
plt.vlines(self.quantiles, 0., 1., color='b', linestyle='--', lw=1.0, alpha=1., label='Quantiles') | ||
plt.vlines(self.quantiles[0], np.zeros(len(self.quantiles[1])), self.quantiles[1], color='b', linestyle='--', lw=1.0, alpha=1., label='Quantiles') | ||
if points is not None: | ||
(grid, qinterpolated) = self.approximate(points, using='quantiles') | ||
plt.plot(grid, qinterpolated, color='b', linestyle=':', lw=2.0, alpha=1.0, label='Quantile Interpolated PDF') | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see - you are punting the choice of quantpoints to the user. Fine! It would be good to come up with a good algorithm for setting the quantpoints, though.