Skip to content
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

Adding NormalFixedMean #333

Merged
merged 5 commits into from
Feb 20, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ngboost/distns/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
from .laplace import Laplace # NOQA
from .lognormal import LogNormal # NOQA
from .multivariate_normal import MultivariateNormal # NOQA
from .normal import Normal, NormalFixedVar # NOQA
from .normal import Normal, NormalFixedMean, NormalFixedVar # NOQA
from .poisson import Poisson # NOQA
from .t import T, TFixedDf, TFixedDfFixedVar # NOQA
62 changes: 62 additions & 0 deletions ngboost/distns/normal.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,3 +150,65 @@ def __init__(self, params):
def fit(Y):
m, _ = sp.stats.norm.fit(Y)
return m


# ### Fixed Mean Normal ###
class NormalFixedMeanLogScore(LogScore):
def score(self, Y):
return -self.dist.logpdf(Y)

def d_score(self, Y):
D = np.zeros((len(Y), 1))
D[:, 0] = 1 - ((self.loc - Y) ** 2) / self.var
return D

def metric(self):
FI = np.zeros((self.var.shape[0], 1, 1))
FI[:, 0, 0] = 2
return FI


class NormalFixedMeanCRPScore(CRPScore):
def score(self, Y):
Z = (Y - self.loc) / self.scale
return self.scale * (
Z * (2 * sp.stats.norm.cdf(Z) - 1)
+ 2 * sp.stats.norm.pdf(Z)
- 1 / np.sqrt(np.pi)
)

def d_score(self, Y):
Z = (Y - self.loc) / self.scale
D = np.zeros((len(Y), 1))
D[:, 0] = self.score(Y) + (Y - self.loc) * -1 * (2 * sp.stats.norm.cdf(Z) - 1)
return D

def metric(self):
I = np.c_[self.var]
I = I.reshape((self.var.shape[0], 1, 1))
I = 1 / (2 * np.sqrt(np.pi)) * I
return I


class NormalFixedMean(Normal):
"""
Implements the normal distribution with mean=0 for NGBoost.

The fixed-mean normal distribution has one parameter, scale which is the standard deviation.
This distribution has both LogScore and CRPScore implemented for it.
"""

n_params = 1
scores = [NormalFixedMeanLogScore, NormalFixedMeanCRPScore]

# pylint: disable=super-init-not-called
def __init__(self, params):
self.loc = np.zeros_like(params[0])
self.var = params[0] ** 2
self.scale = params[0]
self.shape = self.loc.shape
self.dist = dist(loc=self.loc, scale=self.scale)

def fit(Y):
_, s = sp.stats.norm.fit(Y)
return s
15 changes: 14 additions & 1 deletion tests/test_distns.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
LogNormal,
MultivariateNormal,
Normal,
NormalFixedMean,
NormalFixedVar,
T,
TFixedDf,
TFixedDfFixedVar,
Expand All @@ -33,7 +35,18 @@
@pytest.mark.slow
@pytest.mark.parametrize(
"dist",
[Normal, LogNormal, Exponential, Gamma, T, TFixedDf, TFixedDfFixedVar, Cauchy],
[
Normal,
NormalFixedVar,
NormalFixedMean,
LogNormal,
Exponential,
Gamma,
T,
TFixedDf,
TFixedDfFixedVar,
Cauchy,
],
)
@pytest.mark.parametrize(
"learner",
Expand Down