<commit_before>"""
This module implements some standard regression models:

Generalized Least Squares (GLS),
Ordinary Least Squares (OLS),
and Weighted Least Squares (WLS),
as well as an GLS model with autoregressive error terms GLSAR(p)

Models are specified with an endogenous response variable and an
exogenous design matrix and are fit using their `fit` method.

Subclasses that have more complicated covariance matrices
should write over the 'whiten' method as the fit method
prewhitens the response by calling 'whiten'.

General reference for regression models:

<NAME> and <NAME>. "Introduction to Linear Regression
    Analysis." 2nd. Ed., Wiley, 1992.

Econometrics references for regression models:

<NAME> and <NAME>.  "Econometric Theory and Methods," Oxford,
    2004.

<NAME>.  "Econometric Analysis," 5th ed., Pearson, 2003.
"""

__docformat__ = 'restructuredtext en'

__all__ = ['GLS', 'WLS', 'OLS', 'GLSAR']

import numpy as np
from scipy.linalg import toeplitz
from scipy import stats
from scipy.stats.stats import ss
from statsmodels.tools.tools import (add_constant, rank,
                                             recipr, chain_dot)
from statsmodels.tools.decorators import (resettable_cache,
        cache_readonly, cache_writable)
import statsmodels.base.model as base
import statsmodels.base.wrapper as wrap
from statsmodels.emplike.elregress import _ELRegOpts
from scipy import optimize
from scipy.stats import chi2

def _get_sigma(sigma, nobs):
    """
    Returns sigma for GLS and the inverse of its Cholesky decomposition.
    Handles dimensions and checks integrity. If sigma is None, returns
    None, None. Otherwise returns sigma, cholsigmainv.
    """
    if sigma is None:
        return None, None
    sigma = np.asarray(sigma).squeeze()
    if sigma.ndim == 1:
        sigma = np.diag(sigma)
    elif sigma.ndim == 0:
        sigma = np.diag([sigma] * nobs)

    if sigma.shape != (nobs, nobs):
        raise ValueError("Sigma must be a scalar, 1d of length %s or a 2d "
                        "array of shape %s x %s" % (nobs, nobs))

    cholsigmainv = np.linalg.cholesky(np.linalg.pinv(sigma)).T
    return sigma, cholsigmainv
<commit_msg>ENH: Avoid taking cholesky decomposition of diagonal matrix<commit_after>"""
This module implements some standard regression models:

Generalized Least Squares (GLS),
Ordinary Least Squares (OLS),
and Weighted Least Squares (WLS),
as well as an GLS model with autoregressive error terms GLSAR(p)

Models are specified with an endogenous response variable and an
exogenous design matrix and are fit using their `fit` method.

Subclasses that have more complicated covariance matrices
should write over the 'whiten' method as the fit method
prewhitens the response by calling 'whiten'.

General reference for regression models:

<NAME> and <NAME>. "Introduction to Linear Regression
    Analysis." 2nd. Ed., Wiley, 1992.

Econometrics references for regression models:

<NAME> and <NAME>.  "Econometric Theory and Methods," Oxford,
    2004.

<NAME>.  "Econometric Analysis," 5th ed., Pearson, 2003.
"""

__docformat__ = 'restructuredtext en'

__all__ = ['GLS', 'WLS', 'OLS', 'GLSAR']

import numpy as np
from scipy.linalg import toeplitz
from scipy import stats
from scipy.stats.stats import ss
from statsmodels.tools.tools import (add_constant, rank,
                                             recipr, chain_dot)
from statsmodels.tools.decorators import (resettable_cache,
        cache_readonly, cache_writable)
import statsmodels.base.model as base
import statsmodels.base.wrapper as wrap
from statsmodels.emplike.elregress import _ELRegOpts
from scipy import optimize
from scipy.stats import chi2

def _get_sigma(sigma, nobs):
    """
    Returns sigma for GLS and the inverse of its Cholesky decomposition.
    Handles dimensions and checks integrity. If sigma is None, returns
    None, None. Otherwise returns sigma, cholsigmainv.
    """
    if sigma is None:
        return None, None
    sigma = np.asarray(sigma).squeeze()
    if sigma.ndim == 0:
        sigma = np.repeat(sigma, nobs)
    if sigma.ndim == 1:
        cholsigmainv = np.diag(1/sigma**.5)
        sigma = np.diag(sigma)
    else:
        cholsigmainv = np.linalg.cholesky(np.linalg.pinv(sigma)).T

    if sigma.shape != (nobs, nobs):
        raise ValueError("Sigma must be a scalar, 1d of length %s or a 2d "
                        "array of shape %s x %s" % (nobs, nobs))

    return sigma, cholsigmainv

{'fraction_non_alphanumeric': 0.08282963957913589, 'fraction_numerical': 0.008954555630176853, 'mean_word_length': 3.441351888667992, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6424419', 'n_tokens_mistral': 1419, 'n_tokens_neox': 1322, 'n_words': 510}