Add beta distribution#391
Open
BaerVervergaert wants to merge 3 commits into
Open
Conversation
BaerVervergaert
commented
Jul 6, 2025
| 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) |
Author
There was a problem hiding this comment.
Might need to introduce clipping here because sometimes the algorithm overflows and sets value a or b to 0.
alejandroschuler
approved these changes
Jul 7, 2025
Collaborator
|
@BaerVervergaert can you merge master into your PR when you have time? That way we can test 3.13 as well |
ryan-wolbeck
requested changes
Sep 6, 2025
Comment on lines
+39
to
+43
| 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. |
Collaborator
There was a problem hiding this comment.
"""
Implements the Beta distribution for NGBoost.
The Beta distribution is defined on the interval [0, 1] and is parameterized
by two shape parameters a > 0 and b > 0. The distribution is useful for
modeling bounded continuous data, such as proportions, probabilities, or
normalized measurements.
Parameters
----------
params : array-like, shape (n_samples, 2)
Array containing the distribution parameters in log space:
- params[:, 0]: log(a) - first shape parameter
- params[:, 1]: log(b) - second shape parameter
Attributes
----------
a : array-like, shape (n_samples,)
First shape parameter (a > 0), obtained by exponentiating log_a
b : array-like, shape (n_samples,)
Second shape parameter (b > 0), obtained by exponentiating log_b
dist : scipy.stats.beta
Scipy beta distribution object for sampling and PDF calculations
log_a : array-like, shape (n_samples,)
Log of the first shape parameter
log_b : array-like, shape (n_samples,)
Log of the second shape parameter
| """ | ||
|
|
||
| n_params = 2 | ||
| scores = [BetaLogScore] # will implement this later |
Collaborator
There was a problem hiding this comment.
Can we add CRPSScore to be consistent?
Comment on lines
+50
to
+58
| 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) |
Collaborator
There was a problem hiding this comment.
Something like this might help here
"""
Initialize Beta distribution with parameters.
Parameters
----------
params : array-like, shape (n_samples, 2)
Array containing log(a) and log(b) parameters
Raises
------
ValueError
If params has wrong shape, contains NaN/Inf values, or results in
non-positive shape parameters
"""
# Validate input shape
if len(params) != 2:
raise ValueError(
f"Beta distribution requires exactly 2 parameters, got {len(params)}"
)
# Validate parameter values
if np.any(np.isnan(params)) or np.any(np.isinf(params)):
raise ValueError(
"Invalid parameters: NaN or Inf values detected. "
"Parameters must be finite numbers."
)
# Store parameters
self._params = params
self.log_a = params[0]
self.log_b = params[1]
# Convert to shape parameters
self.a = np.exp(params[0])
self.b = np.exp(params[1])
# Validate resulting shape parameters
if np.any(self.a <= 0) or np.any(self.b <= 0):
raise ValueError(
"Beta distribution requires positive shape parameters. "
f"Got a={self.a}, b={self.b}"
)
# Create scipy distribution object
self.dist = dist(a=self.a, b=self.b)```
Author
There was a problem hiding this comment.
Thanks for the feedback :)
It's much appreciated.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Added the Beta distribution (scipy.stats.beta) with loc fixed at zero and scale fixed at one.