<commit_before>import scipy as sp
import numpy as np

from scipy.stats import lognorm as dist

eps = 1e-6

class LogNormal(object):
    n_params = 2

    def __init__(self, params, temp_scale = 1.0):
        self.loc = params[0]
        self.scale = np.exp(params[1])
        self.dist = dist(s=self.scale, scale=np.exp(self.loc))

    def __getattr__(self, name):
        if name in dir(self.dist):
            return getattr(self.dist, name)
        return None

    def nll(self, Y):
        E = Y['Event']
        T = Y['Time']
        cens = (1-E) * np.log(1 - self.dist.cdf(T) + eps)
        uncens = E * self.dist.logpdf(T)
        return -(cens + uncens)

    def D_nll(self, Y):
        E = Y['Event'][:,np.newaxis]
        T = Y['Time']
        lT = np.log(T)
        Z = (lT - self.loc) / self.scale

        D_uncens = np.zeros((self.loc.shape[0], 2))
        D_uncens[:, 0] = (self.loc - lT) / (self.scale ** 2)
        D_uncens[:, 1] = 1 - ((self.loc - lT) ** 2) / (self.scale ** 2)

        D_cens = np.zeros((self.loc.shape[0], 2))
        D_cens[:, 0] = -sp.stats.norm.pdf(lT, loc=self.loc, scale=self.scale)/(1 - self.dist.cdf(T) + eps)
        D_cens[:, 0] = -Z * sp.stats.norm.pdf(lT, loc=self.loc, scale=self.scale)/(1 - self.dist.cdf(T) + eps)

        return (1-E) * D_cens + E * D_uncens

    def crps(self, Y):
        E = Y["Event"]
        T = Y["Time"]
        lT = np.log(T)
        Z = (lT - self.loc) / self.scale

        crps_uncens = (self.scale * (Z * (2 * sp.stats.norm.cdf(Z) - 1) + \
                      2 * sp.stats.norm.pdf(Z) - 1 / np.sqrt(np.pi)))
        crps_cens = self.scale * (Z * sp.stats.norm.cdf(Z) ** 2 + \
                    2 * sp.stats.norm.cdf(Z) * sp.stats.norm.pdf(Z) - \
                    sp.stats.norm.cdf(np.sqrt(2) * Z) / np.sqrt(np.pi))
        return (1-E) * crps_cens + E * crps_uncens

    def D_crps(self, Y):
        E = Y["Event"]
        T = Y["Time"]<commit_msg>fix lognormal bug<commit_after>import scipy as sp
import numpy as np

from scipy.stats import lognorm as dist

eps = 1e-6

class LogNormal(object):
    n_params = 2

    def __init__(self, params, temp_scale = 1.0):
        self.loc = params[0]
        self.scale = np.exp(params[1])
        self.dist = dist(s=self.scale, scale=np.exp(self.loc))

    def __getattr__(self, name):
        if name in dir(self.dist):
            return getattr(self.dist, name)
        return None

    def nll(self, Y):
        E = Y['Event']
        T = Y['Time']
        cens = (1-E) * np.log(1 - self.dist.cdf(T) + eps)
        uncens = E * self.dist.logpdf(T)
        return -(cens + uncens)

    def D_nll(self, Y):
        E = Y['Event'][:,np.newaxis]
        T = Y['Time']
        lT = np.log(T)
        Z = (lT - self.loc) / self.scale

        D_uncens = np.zeros((self.loc.shape[0], 2))
        D_uncens[:, 0] = (self.loc - lT) / (self.scale ** 2)
        D_uncens[:, 1] = 1 - ((self.loc - lT) ** 2) / (self.scale ** 2)

        D_cens = np.zeros((self.loc.shape[0], 2))
        D_cens[:, 0] = -sp.stats.norm.pdf(lT, loc=self.loc, scale=self.scale)/(1 - self.dist.cdf(T) + eps)
        D_cens[:, 1] = -Z * sp.stats.norm.pdf(lT, loc=self.loc, scale=self.scale)/(1 - self.dist.cdf(T) + eps)

        return (1-E) * D_cens + E * D_uncens

    def crps(self, Y):
        E = Y["Event"]
        T = Y["Time"]
        lT = np.log(T)
        Z = (lT - self.loc) / self.scale

        crps_uncens = (self.scale * (Z * (2 * sp.stats.norm.cdf(Z) - 1) + \
                      2 * sp.stats.norm.pdf(Z) - 1 / np.sqrt(np.pi)))
        crps_cens = self.scale * (Z * sp.stats.norm.cdf(Z) ** 2 + \
                    2 * sp.stats.norm.cdf(Z) * sp.stats.norm.pdf(Z) - \
                    sp.stats.norm.cdf(np.sqrt(2) * Z) / np.sqrt(np.pi))
        return (1-E) * crps_cens + E * crps_uncens

    def D_crps(self, Y):
        E = Y["Event"]
        T = Y["Time"]
{'fraction_non_alphanumeric': 0.18308406395048996, 'fraction_numerical': 0.016503352243424446, 'mean_word_length': 1.7966834895457822, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6930829', 'n_tokens_mistral': 1612, 'n_tokens_neox': 1546, 'n_words': 323}