def __init__( self, Dist=Normal, Score=LogScore, Base=default_tree_learner, natural_gradient=True, n_estimators=500, learning_rate=0.01, minibatch_frac=1.0, col_sample=1.0, verbose=True, verbose_eval=100, tol=1e-4, random_state=None, ): self.Dist = Dist self.Score = Score self.Base = Base self.Manifold = manifold(Score, Dist) self.natural_gradient = natural_gradient self.n_estimators = n_estimators self.learning_rate = learning_rate self.minibatch_frac = minibatch_frac self.col_sample = col_sample self.verbose = verbose self.verbose_eval = verbose_eval self.init_params = None self.base_models = [] self.scalings = [] self.col_idxs = [] self.tol = tol self.random_state = check_random_state(random_state) self.best_val_loss_itr = None
def __setstate__(self, state_dict): # Recreate the object which could not be pickled state_dict["Dist"] = SurvivalDistnClass(state_dict["_basedist"]) del state_dict["_basedist"] state_dict["Manifold"] = manifold(state_dict["Score"], state_dict["Dist"]) self.__dict__ = state_dict
def __setstate__(self, state_dict): # Recreate the object which could not be pickled name_to_dist_dict = {"Categorical": k_categorical, "MVN": MultivariateNormal} if "K" in state_dict.keys(): state_dict["Dist"] = name_to_dist_dict[state_dict["Dist_name"]]( state_dict["K"] ) state_dict["Manifold"] = manifold(state_dict["Score"], state_dict["Dist"]) self.__dict__ = state_dict
def test_dists_grad(dist_score_pair: DistScore): # Set seed as this test involves randomness # All errors around 1e-5 mark np.random.seed(9) dist, score = dist_score_pair params = np.random.rand(dist.n_params, 1) manifold_test = manifold(score, dist) grad_err = estimate_grad_err(params, manifold_test) assert grad_err < 1e-3
def test_dists_metric(dist_score_pair: DistScore, seed: int): # Set seed as this test involves randomness # Note if this test fails on a new distribution # There is a chance it is due to randomness np.random.seed(seed) dist, score = dist_score_pair params = np.random.rand(dist.n_params, 1) manifold_test = manifold(LogScore, dist) FI_err = estimate_metric_err(params, manifold_test) assert FI_err < 1e-1
def __init__( self, Dist=Normal, Score=LogScore, Base=default_tree_learner, natural_gradient=True, n_estimators=500, learning_rate=0.01, minibatch_frac=1.0, col_sample=1.0, verbose=True, verbose_eval=100, tol=1e-4, random_state=None, validation_fraction=0.1, early_stopping_rounds=None, ): self.Dist = Dist self.Score = Score self.Base = Base self.Manifold = manifold(Score, Dist) self.natural_gradient = natural_gradient self.n_estimators = n_estimators self.learning_rate = learning_rate self.minibatch_frac = minibatch_frac self.col_sample = col_sample self.verbose = verbose self.verbose_eval = verbose_eval self.init_params = None self.n_features = None self.base_models = [] self.scalings = [] self.col_idxs = [] self.tol = tol self.random_state = check_random_state(random_state) self.best_val_loss_itr = None self.validation_fraction = validation_fraction self.early_stopping_rounds = early_stopping_rounds if hasattr(self.Dist, "multi_output"): self.multi_output = self.Dist.multi_output else: self.multi_output = False
import itertools import matplotlib as mpl import numpy as np from matplotlib import pyplot as plt from tqdm import tqdm from ngboost.distns import Normal from ngboost.manifold import manifold from ngboost.scores import LogScore if __name__ == "__main__": rvs = np.random.randn(500) nll_fn = (lambda p: manifold(LogScore, Normal) (np.array(p)[:, np.newaxis]).score(rvs).mean()) fisher_fn = lambda p: manifold(LogScore, Normal)(np.array(p)[:, np.newaxis] ).metric() grad_fn = (lambda p: manifold(LogScore, Normal) (np.array(p)[:, np.newaxis]).d_score(rvs).mean(axis=0)) loc = np.linspace(-3, 3, 20) scale = np.linspace(-0.5, 2, 20) loc, scale = np.meshgrid(loc, scale) grads_fisher_x = np.zeros((20, 20)) grads_fisher_y = np.zeros((20, 20)) grads_x = np.zeros((20, 20)) grads_y = np.zeros((20, 20)) nlls = np.zeros((20, 20))
import matplotlib as mpl import numpy as np import scipy as sp import scipy.stats from matplotlib import pyplot as plt from tqdm import tqdm from ngboost.distns import Normal from ngboost.manifold import manifold from ngboost.scores import CRPScore if __name__ == "__main__": rvs = np.random.randn(500) crps_fn = (lambda p: manifold(CRPScore, Normal) (np.array(p)[:, np.newaxis]).score(rvs).mean()) metric_fn = lambda p: manifold(CRPScore, Normal)(np.array(p)[:, np.newaxis] ).metric() grad_fn = (lambda p: manifold(CRPScore, Normal) (np.array(p)[:, np.newaxis]).d_score(rvs).mean(axis=0)) loc = np.linspace(-3, 3, 20) scale = np.linspace(-0.5, 2, 20) loc, scale = np.meshgrid(loc, scale) grads_metric_x = np.zeros((20, 20)) grads_metric_y = np.zeros((20, 20)) grads_x = np.zeros((20, 20)) grads_y = np.zeros((20, 20)) crps = np.zeros((20, 20))
def __setstate__(self, state_dict): if "K" in state_dict.keys(): state_dict["Dist"] = k_categorical(state_dict["K"]) state_dict["Manifold"] = manifold(state_dict["Score"], state_dict["Dist"]) self.__dict__ = state_dict