<commit_before>"""
Empirical Likelihood Linear Regression Inference

Included in this script are functions to conduct hypothesis test of linear
regression parameters as well as restrictions.

Start Date: 22 June
Last Updated: 28 June

General References
-----------------

<NAME>.(2001). Empirical Likelihood. Chapman and Hall

"""
import numpy as np
from scipy.stats import chi2
from statsmodels.regression.linear_model import OLS
from scipy import optimize
from descriptive import OptFuncts
from statsmodels.tools.tools import add_constant
from statsmodels.regression.linear_model import RegressionResults


class _ElRegSetup(OptFuncts):
    """

    Empirical Likelihood is a method of inference, not estimation.  Therefore,
    all of the estimates for EL regression are the same as OLS since both
    methods set the estimating equation X(Y-XB) = 0.  to obtain parameter
    estimates.

    This class fits the data using OLS and initializes the OLS results that are
    the same when using Empirical Likelihood.  Note that estimates are
    different when the regression is forced through the origin.

    Attributes
    ----------
    exog
    nobs
    nvar
    endog
    params
    fittedvalues
    mse_model
    mse_resid
    mse_total
    resid
    rsquared
    rsquared_adj

    See Also
    --------
    OLS documentation
    El_Origin_Regress

    """
    def __init__(self, endog, exog):
        self.exog = exog
        self.nobs = float(self.exog.shape[0])
        self.nvar = float(self.exog.shape[1])
        self.endog = endog.reshape(self.nobs, 1)
        self.ols_fit = OLS(self.endog, self.exog).fit()
        self.params = self.ols_fit.params
        self.fittedvalues = self.ols_fit.fittedvalues
        self.mse_model = self.ols_fit.mse_model
        self.mse_resid = self.ols_fit.mse_resid
        self.mse_total = self.ols_fit.mse_total
        self.resid = self.ols_fit.resid
        self.rsquared = self.ols_fit.rsquared
        self.rsquared_adj = self.ols_fit.rsquared_adj
        #All of the above are the same when using EL or OLS


class _ElRegOpts(_ElRegSetup):
    """

    A class that holds functions to be optimized over when conducting
    hypothesis tests and calculating confidence intervals.

    """
    def __init__(self, endog, exog):
            super(_ElRegOpts, self).__init__(endog, exog)

    def _opt_nuis_regress(self, nuisance_params):
        """
        A function that is optimized over nuisance parameters to conduct a
        hypothesis test for the parameters of interest

        Parameters
        ----------

        params: 1d array
            The regression coefficients of the model.  This includes the
            nuisance and parameters of interests.

        Returns
        -------

        llr: float
            -2 times the log likelihood of the nuisance parameters and the
            hypothesized value of the parameter(s) of interest.

        """
        params = np.copy(self.params)
        params[self.param_nums] = self.b0_vals
        nuis_param_index = np.int_(np.delete(np.arange(self.nvar),
                                             self.param_nums))
        params[nuis_param_index] = nuisance_params
        self.new_params = params.reshape(self.nvar, 1)
        self.est_vect = self.exog * (self.endog - np.dot(self.exog,
                                                         self.new_params))
        if not self._stochastic_exog:
            exog_means = np.mean(self.exog, axis=0)[1:]
            exog_mom2 = (np.sum(self.exog * self.exog, axis=0))[1:]\
                          / self.nobs
            mean_est_vect = self.exog[:, 1:] - exog_means
            mom2_est_vect = (self.exog * self.exog)[:, 1:] - exog_mom2
            regressor_est_vect = np.concatenate((mean_est_vect, mom2_est_vect),
                                                axis=1)
            self.est_vect = np.concatenate((self.est_vect, regressor_est_vect),
                                           axis=1)

        eta_star = self._modif_newton(self.start_lbda)
        self.eta_star = eta_star
        denom = 1. + np.dot(eta_star, self.est_vect.T)
        self.new_weights = 1. / self.nobs * 1. / denom
        llr = np.sum(np.log(self.nobs * self.new_weights))
        return -2 * llr

    def _ci_limits_beta(self, beta):
        self.b0_vals = beta
        return self.hy_test_beta([beta], [self.param_nums],<commit_msg>Added option for regression to have weights.<commit_after>"""
Empirical Likelihood Linear Regression Inference

Included in this script are functions to conduct hypothesis test of linear
regression parameters as well as restrictions.

Start Date: 22 June
Last Updated: 28 June

General References
-----------------

<NAME>.(2001). Empirical Likelihood. Chapman and Hall

"""
import numpy as np
from scipy.stats import chi2
from statsmodels.regression.linear_model import OLS
from scipy import optimize
from descriptive import OptFuncts
from statsmodels.tools.tools import add_constant
from statsmodels.regression.linear_model import RegressionResults


class _ElRegSetup(OptFuncts):
    """

    Empirical Likelihood is a method of inference, not estimation.  Therefore,
    all of the estimates for EL regression are the same as OLS since both
    methods set the estimating equation X(Y-XB) = 0.  to obtain parameter
    estimates.

    This class fits the data using OLS and initializes the OLS results that are
    the same when using Empirical Likelihood.  Note that estimates are
    different when the regression is forced through the origin.

    Attributes
    ----------
    exog
    nobs
    nvar
    endog
    params
    fittedvalues
    mse_model
    mse_resid
    mse_total
    resid
    rsquared
    rsquared_adj

    See Also
    --------
    OLS documentation
    El_Origin_Regress

    """
    def __init__(self, endog, exog):
        self.exog = exog
        self.nobs = float(self.exog.shape[0])
        self.nvar = float(self.exog.shape[1])
        self.endog = endog.reshape(self.nobs, 1)
        self.ols_fit = OLS(self.endog, self.exog).fit()
        self.params = self.ols_fit.params
        self.fittedvalues = self.ols_fit.fittedvalues
        self.mse_model = self.ols_fit.mse_model
        self.mse_resid = self.ols_fit.mse_resid
        self.mse_total = self.ols_fit.mse_total
        self.resid = self.ols_fit.resid
        self.rsquared = self.ols_fit.rsquared
        self.rsquared_adj = self.ols_fit.rsquared_adj
        #All of the above are the same when using EL or OLS


class _ElRegOpts(_ElRegSetup):
    """

    A class that holds functions to be optimized over when conducting
    hypothesis tests and calculating confidence intervals.

    """
    def __init__(self, endog, exog):
            super(_ElRegOpts, self).__init__(endog, exog)

    def _opt_nuis_regress(self, nuisance_params, weights=None):
        """
        A function that is optimized over nuisance parameters to conduct a
        hypothesis test for the parameters of interest

        Parameters
        ----------

        params: 1d array
            The regression coefficients of the model.  This includes the
            nuisance and parameters of interests.

        Returns
        -------

        llr: float
            -2 times the log likelihood of the nuisance parameters and the
            hypothesized value of the parameter(s) of interest.

        """
        if weights is None
            weights = np.ones(nobs).reshape(nobs,1)
        params = np.copy(self.params)
        params[self.param_nums] = self.b0_vals
        nuis_param_index = np.int_(np.delete(np.arange(self.nvar),
                                             self.param_nums))
        params[nuis_param_index] = nuisance_params
        self.new_params = params.reshape(self.nvar, 1)
        self.est_vect = self.exog * (self.endog - np.dot(self.exog,
                                                         self.new_params))
        if not self._stochastic_exog:
            exog_means = np.mean(self.exog, axis=0)[1:]
            exog_mom2 = (np.sum(self.exog * self.exog, axis=0))[1:]\
                          / self.nobs
            mean_est_vect = self.exog[:, 1:] - exog_means
            mom2_est_vect = (self.exog * self.exog)[:, 1:] - exog_mom2
            regressor_est_vect = np.concatenate((mean_est_vect, mom2_est_vect),
                                                axis=1)
            self.est_vect = np.concatenate((self.est_vect, regressor_est_vect),
                                           axis=1)
        self.est_vect = weights * self.est_vect
        eta_star = self._modif_newton(self.start_lbda)
        self.eta_star = eta_star
        denom = 1. + np.dot(eta_star, self.est_vect.T)
        self.new_weights = 1. / self.nobs * 1. / denom
        llr = np.sum(np.log(self.nobs * self.new_weights))
        return -2 * llr

    def _ci_limits_beta(self, beta):
        self.b0_vals = beta
        return self.hy_test_beta([beta], [self.param_nums],
{'fraction_non_alphanumeric': 0.08584790112459637, 'fraction_numerical': 0.0076828860928627105, 'mean_word_length': 2.1144244105409156, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3930582', 'n_tokens_mistral': 2797, 'n_tokens_neox': 2668, 'n_words': 731}