def __init__(self, model, params, normalized_cov_params, scale):
     super(RLMResults, self).__init__(model, params,
             normalized_cov_params, scale)
     self.model = model
     self.df_model = model.df_model
     self.df_resid = model.df_resid
     self.nobs = model.nobs
     self._cache = resettable_cache()
 def __init__(self, model, params, normalized_cov_params, scale):
     super(RLMResults, self).__init__(model, params, normalized_cov_params,
                                      scale)
     self.model = model
     self.df_model = model.df_model
     self.df_resid = model.df_resid
     self.nobs = model.nobs
     self._cache = resettable_cache()
Esempio n. 3
0
    def __init__(self, model, mlefit):
#        super(DiscreteResults, self).__init__(model, params,
#                np.linalg.inv(-hessian), scale=1.)
        self.model = model
        self.df_model = model.df_model
        self.df_resid = model.df_resid
        self._cache = resettable_cache()
        self.nobs = model.exog.shape[0]
        self.__dict__.update(mlefit.__dict__)
Esempio n. 4
0
 def __init__(self, model, mlefit):
     #        super(DiscreteResults, self).__init__(model, params,
     #                np.linalg.inv(-hessian), scale=1.)
     self.model = model
     self.df_model = model.df_model
     self.df_resid = model.df_resid
     self._cache = resettable_cache()
     self.nobs = model.exog.shape[0]
     self.__dict__.update(mlefit.__dict__)
 def __init__(self, model, params, normalized_cov_params, scale):
     super(GLMResults, self).__init__(model, params,
             normalized_cov_params=normalized_cov_params, scale=scale)
     self.family = model.family
     self._endog = model.endog
     self.nobs = model.endog.shape[0]
     self.mu = model.mu
     self._data_weights = model.data_weights
     self.df_resid = model.df_resid
     self.df_model = model.df_model
     self.pinv_wexog = model.pinv_wexog
     self._cache = resettable_cache()
Esempio n. 6
0
 def __init__(self, model, params, normalized_cov_params, scale):
     super(GLMResults,
           self).__init__(model,
                          params,
                          normalized_cov_params=normalized_cov_params,
                          scale=scale)
     self.family = model.family
     self._endog = model.endog
     self.nobs = model.endog.shape[0]
     self.mu = model.mu
     self._data_weights = model.data_weights
     self.df_resid = model.df_resid
     self.df_model = model.df_model
     self.pinv_wexog = model.pinv_wexog
     self._cache = resettable_cache()
 def __init__(self, model, params, normalized_cov_params, scale):
     super(GLMResults, self).__init__(model, params,
             normalized_cov_params=normalized_cov_params, scale=scale)
     self.model._results = self.model.results = self # TODO: get rid of this
                                                   # since results isn't a
                                                   # property for GLM
     # above is needed for model.predict
     self.family = model.family
     self._endog = model.endog
     self.nobs = model.endog.shape[0]
     self.mu = model.mu
     self._data_weights = model.data_weights
     self.df_resid = model.df_resid
     self.df_model = model.df_model
     self.pinv_wexog = model.pinv_wexog
     self._cache = resettable_cache()
 def __init__(self, model, params, normalized_cov_params, scale):
     super(GLMResults, self).__init__(model, params,
             normalized_cov_params=normalized_cov_params, scale=scale)
     self.model._results = self.model.results = self # TODO: get rid of this
                                                   # since results isn't a
                                                   # property for GLM
     # above is needed for model.predict
     self.family = model.family
     self._endog = model.endog
     self.nobs = model.endog.shape[0]
     self.mu = model.mu
     self._data_weights = model.data_weights
     self.df_resid = model.df_resid
     self.df_model = model.df_model
     self.pinv_wexog = model.pinv_wexog
     self._cache = resettable_cache()
Esempio n. 9
0
 def __init__(self, model, params, normalized_cov_params=None, scale=1.):
     super(ARResults, self).__init__(model, params, normalized_cov_params,
                                     scale)
     self._cache = resettable_cache()
     self.nobs = model.nobs
     n_totobs = len(model.endog)
     self.n_totobs = n_totobs
     self.X = model.X  # copy?
     self.Y = model.Y
     k_ar = model.k_ar
     self.k_ar = k_ar
     k_trend = model.k_trend
     self.k_trend = k_trend
     trendorder = None
     if k_trend > 0:
         trendorder = k_trend - 1
     self.trendorder = 1
     self.df_resid = n_totobs - k_ar - k_trend  #TODO: cmle vs mle?
Esempio n. 10
0
 def __init__(self, model, params, normalized_cov_params=None, scale=1.0):
     super(ARMAResults, self).__init__(model, params, normalized_cov_params, scale)
     self.sigma2 = model.sigma2
     nobs = model.nobs
     self.nobs = nobs
     k_exog = model.k_exog
     self.k_exog = k_exog
     k_trend = model.k_trend
     self.k_trend = k_trend
     k_ar = model.k_ar
     self.k_ar = k_ar
     self.n_totobs = len(model.endog)
     k_ma = model.k_ma
     self.k_ma = k_ma
     df_model = k_exog + k_trend + k_ar + k_ma
     self.df_model = df_model
     self.df_resid = self.nobs - df_model
     self._cache = resettable_cache()
Esempio n. 11
0
 def __init__(self, model, params, normalized_cov_params=None, scale=1.):
     super(ARResults, self).__init__(model, params, normalized_cov_params,
             scale)
     self._cache = resettable_cache()
     self.nobs = model.nobs
     n_totobs = len(model.endog)
     self.n_totobs = n_totobs
     self.X = model.X # copy?
     self.Y = model.Y
     k_ar = model.k_ar
     self.k_ar = k_ar
     k_trend = model.k_trend
     self.k_trend = k_trend
     trendorder = None
     if k_trend > 0:
         trendorder = k_trend - 1
     self.trendorder = 1
     self.df_resid = n_totobs - k_ar - k_trend #TODO: cmle vs mle?
Esempio n. 12
0
 def __init__(self, model, params, normalized_cov_params=None, scale=1.):
     super(ARMAResults, self).__init__(model, params, normalized_cov_params,
                                       scale)
     self.sigma2 = model.sigma2
     nobs = model.nobs
     self.nobs = nobs
     k_exog = model.k_exog
     self.k_exog = k_exog
     k_trend = model.k_trend
     self.k_trend = k_trend
     k_ar = model.k_ar
     self.k_ar = k_ar
     self.n_totobs = len(model.endog)
     k_ma = model.k_ma
     self.k_ma = k_ma
     df_model = k_exog + k_trend + k_ar + k_ma
     self.df_model = df_model
     self.df_resid = self.nobs - df_model
     self._cache = resettable_cache()
 def __init__(self, model, params, normalized_cov_params=None, scale=1.):
     super(RegressionResults, self).__init__(model, params,
                                              normalized_cov_params,
                                              scale)
     self._cache = resettable_cache()
Esempio n. 14
0
 def __init__(self, model, params, normalized_cov_params=None, scale=1.):
     super(RegressionResults, self).__init__(model, params,
                                              normalized_cov_params,
                                              scale)
     self._cache = resettable_cache()
Esempio n. 15
0
 def __init__(self, endog, exog=None, **kwds):
     self._orig_endog = endog
     self._orig_exog = exog
     self.endog, self.exog = self._convert_endog_exog(endog, exog)
     self._check_integrity()
     self._cache = resettable_cache()
Esempio n. 16
0
 def __init__(self, endog, exog=None, **kwds):
     self._orig_endog = endog
     self._orig_exog = exog
     self.endog, self.exog = self._convert_endog_exog(endog, exog)
     self._check_integrity()
     self._cache = resettable_cache()
Esempio n. 17
0
class KDE(object):
    """
    Kernel Density Estimator

    Parameters
    ----------
    endog : array-like
        The variable for which the density estimate is desired.

    Notes
    -----
    If cdf, sf, cumhazard, or entropy are computed, they are computed based on
    the definition of the kernel rather than the FFT approximation, even if
    the density is fit with FFT = True.
    """
    _cache = resettable_cache()

    def __init__(self, endog):
        self.endog = np.asarray(endog)

    def fit(self,
            kernel="gau",
            bw="scott",
            fft=True,
            weights=None,
            gridsize=None,
            adjust=1,
            cut=3,
            clip=(-np.inf, np.inf)):
        """
        Attach the density estimate to the KDE class.

        Parameters
        ----------
        kernel : str
            The Kernel to be used. Choices are
            - "biw" for biweight
            - "cos" for cosine
            - "epa" for Epanechnikov
            - "gauss" for Gaussian.
            - "tri" for triangular
            - "triw" for triweight
            - "uni" for uniform
        bw : str, float
            "scott" - 1.059 * A * nobs ** (-1/5.), where A is
                    min(std(X),IQR/1.34)
            "silverman" - .9 * A * nobs ** (-1/5.), where A is
                    min(std(X),IQR/1.34)
            If a float is given, it is the bandwidth.
        fft : bool
            Whether or not to use FFT. FFT implementation is more
            computationally efficient. However, only the Gaussian kernel
            is implemented. If FFT is False, then a 'nobs' x 'gridsize'
            intermediate array is created.
        gridsize : int
            If gridsize is None, max(len(X), 50) is used.
        cut : float
            Defines the length of the grid past the lowest and highest values
            of X so that the kernel goes to zero. The end points are
            -/+ cut*bw*{min(X) or max(X)}
        adjust : float
            An adjustment factor for the bw. Bandwidth becomes bw * adjust.
        """
        try:
            bw = float(bw)
            self.bw_method = "user-given"
        except:
            self.bw_method = bw
        endog = self.endog

        if fft:
            if kernel != "gau":
                msg = "Only gaussian kernel is available for fft"
                raise NotImplementedError(msg)
            if weights is not None:
                msg = "Weights are not implemented for fft"
                raise NotImplementedError(msg)
            density, grid, bw = kdensityfft(endog,
                                            kernel=kernel,
                                            bw=bw,
                                            adjust=adjust,
                                            weights=weights,
                                            gridsize=gridsize,
                                            clip=clip,
                                            cut=cut)
        else:
            density, grid, bw = kdensity(endog,
                                         kernel=kernel,
                                         bw=bw,
                                         adjust=adjust,
                                         weights=weights,
                                         gridsize=gridsize,
                                         clip=clip,
                                         cut=cut)
        self.density = density
        self.support = grid
        self.bw = bw
        self.kernel = kernel_switch[kernel](h=bw)  # we instantiate twice,
        # should this passed to funcs?

    @cache_readonly
    def cdf(self):
        """
        Returns the cumulative distribution function evaluated at the support.

        Notes
        -----
        Will not work if fit has not been called.
        """
        _checkisfit(self)
        density = self.density
        kern = self.kernel
        if kern.domain is None:  # TODO: test for grid point at domain bound
            a, b = -np.inf, np.inf
        else:
            a, b = kern.domain
        func = lambda x, s: kern.density(s, x)

        support = self.support
        support = np.r_[a, support]
        gridsize = len(support)
        endog = self.endog
        probs = [
            integrate.quad(func, support[i - 1], support[i], args=endog)[0]
            for i in xrange(1, gridsize)
        ]
        return np.cumsum(probs)

    @cache_readonly
    def cumhazard(self):
        """
        Returns the hazard function evaluated at the support.

        Notes
        -----
        Will not work if fit has not been called.

        """
        _checkisfit(self)
        return -np.log(self.sf)

    @cache_readonly
    def sf(self):
        """
        Returns the survival function evaluated at the support.

        Notes
        -----
        Will not work if fit has not been called.
        """
        _checkisfit(self)
        return 1 - self.cdf

    @cache_readonly
    def entropy(self):
        """
        Returns the differential entropy evaluated at the support

        Notes
        -----
        Will not work if fit has not been called. 1e-12 is added to each
        probability to ensure that log(0) is not called.
        """
        _checkisfit(self)

        def entr(x, s):
            pdf = kern.density(s, x)
            return pdf * np.log(pdf + 1e-12)

        pdf = self.density
        kern = self.kernel

        if kern.domain is not None:
            a, b = self.domain
        else:
            a, b = -np.inf, np.inf
        endog = self.endog
        #TODO: below could run into integr problems, cf. stats.dist._entropy
        return -integrate.quad(entr, a, b, args=(endog, ))[0]

    @cache_readonly
    def icdf(self):
        """
        Inverse Cumulative Distribution (Quantile) Function

        Notes
        -----
        Will not work if fit has not been called. Uses
        `scipy.stats.mstats.mquantiles`.
        """
        _checkisfit(self)
        gridsize = len(self.density)
        return stats.mstats.mquantiles(self.endog, np.linspace(0, 1, gridsize))

    def evaluate(self, point):
        """
        Evaluate density at a single point.

        Paramters
        ---------
        point : float
            Point at which to evaluate the density.
        """
        _checkisfit(self)
        return self.kernel.density(self.endog, point)