-
Notifications
You must be signed in to change notification settings - Fork 241
Add beta distribution #391
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
Open
BaerVervergaert
wants to merge
3
commits into
stanfordmlgroup:master
Choose a base branch
from
BaerVervergaert:add-beta-distribution
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 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 hidden or 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 hidden or 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 |
---|---|---|
@@ -0,0 +1,82 @@ | ||
"""The NGBoost Beta distribution and scores""" | ||
import numpy as np | ||
from scipy.special import digamma, polygamma | ||
from scipy.stats import beta as dist | ||
|
||
from ngboost.distns.distn import RegressionDistn | ||
from ngboost.scores import LogScore | ||
|
||
|
||
class BetaLogScore(LogScore): | ||
"""Log score for the Beta distribution.""" | ||
|
||
def score(self, Y): | ||
"""Calculate the log score for the Beta distribution.""" | ||
return -self.dist.logpdf(Y) | ||
|
||
def d_score(self, Y): | ||
"""Calculate the derivative of the log score with respect to the parameters.""" | ||
D = np.zeros( | ||
(len(Y), 2) | ||
) # first col is dS/d(log(a)), second col is dS/d(log(b)) | ||
D[:, 0] = -self.a * (digamma(self.a + self.b) - digamma(self.a) + np.log(Y)) | ||
D[:, 1] = -self.b * (digamma(self.a + self.b) - digamma(self.b) + np.log(1 - Y)) | ||
return D | ||
|
||
def metric(self): | ||
"""Return the Fisher Information matrix for the Beta distribution.""" | ||
FI = np.zeros((self.a.shape[0], 2, 2)) | ||
trigamma_a_b = polygamma(1, self.a + self.b) | ||
FI[:, 0, 0] = self.a**2 * (polygamma(1, self.a) - trigamma_a_b) | ||
FI[:, 0, 1] = -self.a * self.b * trigamma_a_b | ||
FI[:, 1, 0] = -self.a * self.b * trigamma_a_b | ||
FI[:, 1, 1] = self.b**2 * (polygamma(1, self.b) - trigamma_a_b) | ||
return FI | ||
|
||
|
||
class Beta(RegressionDistn): | ||
""" | ||
Implements the Beta distribution for NGBoost. | ||
|
||
The Beta distribution has two parameters, a and b. | ||
The scipy loc and scale parameters are held constant for this implementation. | ||
LogScore is supported for the Beta distribution. | ||
""" | ||
|
||
n_params = 2 | ||
scores = [BetaLogScore] # will implement this later | ||
|
||
# pylint: disable=super-init-not-called | ||
def __init__(self, params): | ||
self._params = params | ||
|
||
# create other objects that will be useful later | ||
self.log_a = params[0] | ||
self.log_b = params[1] | ||
self.a = np.exp(params[0]) # since params[0] is log(a) | ||
self.b = np.exp(params[1]) # since params[1] is log(b) | ||
self.dist = dist(a=self.a, b=self.b) | ||
|
||
@staticmethod | ||
def fit(Y): | ||
"""Fit the distribution to the data.""" | ||
# Use scipy's beta distribution to fit the parameters | ||
# pylint: disable=unused-variable | ||
a, b, loc, scale = dist.fit(Y, floc=0, fscale=1) | ||
return np.array([np.log(a), np.log(b)]) | ||
|
||
def sample(self, m): | ||
"""Sample from the distribution.""" | ||
return np.array([self.dist.rvs() for i in range(m)]) | ||
|
||
def __getattr__( | ||
self, name | ||
): # gives us access to Beta.mean() required for RegressionDist.predict() | ||
if name in dir(self.dist): | ||
return getattr(self.dist, name) | ||
return None | ||
|
||
@property | ||
def params(self): | ||
"""Return the parameters of the Beta distribution.""" | ||
return {"a": self.a, "b": self.b} |
This file contains hidden or 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
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
Might need to introduce clipping here because sometimes the algorithm overflows and sets value a or b to 0.