Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions ci/azure_template_posix.yml
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ jobs:
black --check linearmodels
isort --check linearmodels
ruff check linearmodels
flake8 linearmodels
displayName: 'Check style and formatting'

- script: |
Expand Down
44 changes: 22 additions & 22 deletions linearmodels/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
from __future__ import annotations

import os
import sys

from ._version import version as __version__, version_tuple
from .asset_pricing.model import (
Expand All @@ -55,34 +56,34 @@
from .system import IV3SLS, SUR, IVSystemGMM

OLS = _OLS
WARN_ON_MISSING = os.environ.get("LINEARMODELS_WARN_ON_MISSING", True)
WARN_ON_MISSING = False if WARN_ON_MISSING in ("0", "False") else True
DROP_MISSING = os.environ.get("LINEARMODELS_DROP_MISSING", True)
DROP_MISSING = False if DROP_MISSING in ("0", "False") else True
WARN_ON_MISSING = os.environ.get("LINEARMODELS_WARN_ON_MISSING", "1")
WARN_ON_MISSING = False if WARN_ON_MISSING in ("", "0", "false", "False") else True
DROP_MISSING = os.environ.get("LINEARMODELS_DROP_MISSING", "1")
DROP_MISSING = False if DROP_MISSING in ("", "0", "false", "False") else True

__all__ = [
"AbsorbingLS",
"PooledOLS",
"PanelOLS",
"FirstDifferenceOLS",
"BetweenOLS",
"RandomEffects",
"FamaMacBeth",
"IVLIML",
"DROP_MISSING",
"IV2SLS",
"IV3SLS",
"IVGMM",
"IVGMMCUE",
"IV2SLS",
"IVLIML",
"OLS",
"SUR",
"IV3SLS",
"WARN_ON_MISSING",
"AbsorbingLS",
"BetweenOLS",
"FamaMacBeth",
"FirstDifferenceOLS",
"IVSystemGMM",
"LinearFactorModel",
"LinearFactorModelGMM",
"PanelOLS",
"PooledOLS",
"RandomEffects",
"TradedFactorModel",
"WARN_ON_MISSING",
"DROP_MISSING",
"version_tuple",
"__version__",
"version_tuple",
]


Expand All @@ -92,12 +93,11 @@ def test(
append: bool = True,
location: str = "",
) -> int:
import sys

try:
import pytest
except ImportError: # pragma: no cover
raise ImportError("Need pytest to run tests")
import pytest # noqa: PLC0415
except ImportError as exc: # pragma: no cover
raise ImportError("Need pytest to run tests") from exc

cmd = ["--tb=auto"]
if extra_args:
Expand All @@ -117,7 +117,7 @@ def test(
print(pkg)
if not os.path.exists(pkg):
raise RuntimeError(f"{pkg} was not found. Unable to run tests")
cmd = [pkg] + cmd
cmd = [pkg, *cmd]
print("running: pytest {}".format(" ".join(cmd)))
status = pytest.main(cmd)
if exit: # pragma: no cover
Expand Down
2 changes: 1 addition & 1 deletion linearmodels/asset_pricing/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from .model import LinearFactorModel, LinearFactorModelGMM, TradedFactorModel

__all__ = ["TradedFactorModel", "LinearFactorModelGMM", "LinearFactorModel"]
__all__ = ["LinearFactorModel", "LinearFactorModelGMM", "TradedFactorModel"]
17 changes: 8 additions & 9 deletions linearmodels/asset_pricing/covariance.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@

from __future__ import annotations

import numpy
from numpy import empty
from numpy import empty, ndarray
from numpy.linalg import inv

from linearmodels.iv.covariance import (
Expand All @@ -20,7 +19,7 @@ class _HACMixin:
def __init__(self, kernel: str, bandwidth: float | None) -> None:
self._kernel: str | None = None
self._bandwidth: float | None = None # pragma: no cover
self._moments: numpy.ndarray = empty((0,)) # pragma: no cover
self._moments: ndarray = empty((0,)) # pragma: no cover
self._check_kernel(kernel)
self._check_bandwidth(bandwidth)

Expand Down Expand Up @@ -56,8 +55,8 @@ def _check_bandwidth(self, bandwidth: float | None) -> None:
try:
assert bandwidth is not None
bandwidth = float(bandwidth)
except (TypeError, ValueError):
raise TypeError("bandwidth must be either None or a float")
except (TypeError, ValueError) as exc:
raise TypeError("bandwidth must be either None or a float") from exc
if bandwidth < 0:
raise ValueError("bandwidth must be non-negative.")

Expand Down Expand Up @@ -101,8 +100,8 @@ def __init__(
self,
xe: linearmodels.typing.data.Float64Array,
*,
jacobian: numpy.ndarray | None = None,
inv_jacobian: numpy.ndarray | None = None,
jacobian: ndarray | None = None,
inv_jacobian: ndarray | None = None,
center: bool = True,
debiased: bool = False,
df: int = 0,
Expand Down Expand Up @@ -234,8 +233,8 @@ def __init__(
self,
xe: linearmodels.typing.data.Float64Array,
*,
jacobian: numpy.ndarray | None = None,
inv_jacobian: numpy.ndarray | None = None,
jacobian: ndarray | None = None,
inv_jacobian: ndarray | None = None,
kernel: str | None = None,
bandwidth: float | None = None,
center: bool = True,
Expand Down
48 changes: 24 additions & 24 deletions linearmodels/asset_pricing/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
from formulaic.materializers.types import NAAction
import numpy as np
from numpy.linalg import lstsq
import pandas
from pandas import DataFrame
from scipy.optimize import minimize

Expand Down Expand Up @@ -99,7 +98,7 @@ def __repr__(self) -> str:
def _drop_missing(self) -> linearmodels.typing.data.BoolArray:
data = (self.portfolios, self.factors)
missing = cast(
linearmodels.typing.data.BoolArray,
"linearmodels.typing.data.BoolArray",
np.any(np.c_[[dh.isnull for dh in data]], 0),
)
if any(missing):
Expand All @@ -123,11 +122,11 @@ def _validate_data(self) -> None:
)
self._drop_missing()

p = cast(linearmodels.typing.data.Float64Array, self.portfolios.ndarray)
f = cast(linearmodels.typing.data.Float64Array, self.factors.ndarray)
p = cast("linearmodels.typing.data.Float64Array", self.portfolios.ndarray)
f = cast("linearmodels.typing.data.Float64Array", self.factors.ndarray)
if has_constant(p)[0]:
raise ValueError(
"portfolios must not contains a constant or "
"portfolios must not contain a constant or "
"equivalent and must not have rank\n"
"less than the dimension of the smaller shape."
)
Expand All @@ -153,7 +152,7 @@ def formula(self, value: str | None) -> None:

@staticmethod
def _prepare_data_from_formula(
formula: str, data: pandas.DataFrame, portfolios: pandas.DataFrame | None
formula: str, data: DataFrame, portfolios: DataFrame | None
) -> tuple[DataFrame, DataFrame, str]:
orig_formula = formula
na_action = NAAction("raise")
Expand Down Expand Up @@ -224,9 +223,9 @@ def __init__(self, portfolios: IVDataLike, factors: IVDataLike):
def from_formula(
cls,
formula: str,
data: pandas.DataFrame,
data: DataFrame,
*,
portfolios: pandas.DataFrame | None = None,
portfolios: DataFrame | None = None,
) -> TradedFactorModel:
"""
Parameters
Expand Down Expand Up @@ -349,7 +348,7 @@ def fit(
kernel=kernel,
)
bw = cov_est.bandwidth
_cov_config = {k: v for k, v in cov_config.items()}
_cov_config = dict(cov_config.items())
_cov_config["bandwidth"] = bw
rp_cov_est = KernelCovariance(
fe,
Expand Down Expand Up @@ -388,10 +387,10 @@ def fit(
param_names = []
for portfolio in self.portfolios.cols:
param_names.append(f"alpha-{portfolio}")
for factor in self.factors.cols:
param_names.append(f"beta-{portfolio}-{factor}")
for factor in self.factors.cols:
param_names.append(f"lambda-{factor}")
param_names.extend(
[f"beta-{portfolio}-{factor}" for factor in self.factors.cols]
)
param_names.extend([f"lambda-{factor}" for factor in self.factors.cols])

res = AttrDict(
params=params,
Expand Down Expand Up @@ -540,9 +539,9 @@ def __init__(
def from_formula(
cls,
formula: str,
data: pandas.DataFrame,
data: DataFrame,
*,
portfolios: pandas.DataFrame | None = None,
portfolios: DataFrame | None = None,
risk_free: bool = False,
sigma: linearmodels.typing.data.ArrayLike | None = None,
) -> LinearFactorModel:
Expand Down Expand Up @@ -693,12 +692,12 @@ def fit(
param_names = []
for portfolio in self.portfolios.cols:
param_names.append(f"alpha-{portfolio}")
for factor in self.factors.cols:
param_names.append(f"beta-{portfolio}-{factor}")
param_names.extend(
[f"beta-{portfolio}-{factor}" for factor in self.factors.cols]
)
if not excess_returns:
param_names.append("lambda-risk_free")
for factor in self.factors.cols:
param_names.append(f"lambda-{factor}")
param_names.extend([f"lambda-{factor}" for factor in self.factors.cols])

# Pivot vcv to remove unnecessary and have correct order
order = np.reshape(np.arange(s1), (nport, nf + 1))
Expand Down Expand Up @@ -777,7 +776,7 @@ def _moments(
sigma_inv = self._sigma_inv

f = self.factors.ndarray
nobs, nf, nport, _, s1, s2, s3 = self._boundaries()
nobs, nf, nport, _, _, _, _ = self._boundaries()
fc = np.c_[np.ones((nobs, 1)), f]
f_rep = np.tile(fc, (1, nport))
eps_rep = np.tile(eps, (nf + 1, 1))
Expand Down Expand Up @@ -844,9 +843,9 @@ def __init__(
def from_formula(
cls,
formula: str,
data: pandas.DataFrame,
data: DataFrame,
*,
portfolios: pandas.DataFrame | None = None,
portfolios: DataFrame | None = None,
risk_free: bool = False,
) -> LinearFactorModelGMM:
"""
Expand Down Expand Up @@ -1098,8 +1097,9 @@ def fit(
r2 = 1.0 - residual_ss / total_ss
param_names = []
for portfolio in self.portfolios.cols:
for factor in self.factors.cols:
param_names.append(f"beta-{portfolio}-{factor}")
param_names.extend(
[f"beta-{portfolio}-{factor}" for factor in self.factors.cols]
)
if not excess_returns:
param_names.append("lambda-risk_free")
param_names.extend([f"lambda-{f}" for f in self.factors.cols])
Expand Down
2 changes: 1 addition & 1 deletion linearmodels/asset_pricing/results.py
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ def __init__(self, results: AttrDict):
@property
def std_errors(self) -> pd.DataFrame:
"""Estimated parameter standard errors"""
se = cast(linearmodels.typing.data.Float64Array, np.sqrt(np.diag(self._cov)))
se = cast("linearmodels.typing.data.Float64Array", np.sqrt(np.diag(self._cov)))
ase = np.sqrt(np.diag(self._alpha_vcv))
nportfolio, nfactor = self._params.shape
nloadings = nportfolio * (nfactor - 1)
Expand Down
2 changes: 1 addition & 1 deletion linearmodels/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

logger = logging.getLogger(__name__)

cow = bool(os.environ.get("LM_TEST_COPY_ON_WRITE", False))
cow = bool(os.environ.get("LM_TEST_COPY_ON_WRITE", ""))
if cow:
try:
pd.options.mode.copy_on_write = cow
Expand Down
3 changes: 2 additions & 1 deletion linearmodels/datasets/birthweight/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from pandas import DataFrame

from linearmodels import datasets

DESCR = """
J. Mullahy (1997), "Instrumental-Variable Estimation of Count Data Models:
Applications to Models of Cigarette Smoking Behavior," Review of Economics
Expand All @@ -23,6 +25,5 @@


def load() -> DataFrame:
from linearmodels import datasets

return datasets.load(__file__, "birthweight.csv.bz2")
3 changes: 2 additions & 1 deletion linearmodels/datasets/card/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from pandas import DataFrame

from linearmodels import datasets

DESCR = """
D. Card (1995), "Using Geographic Variation in College Proximity to Estimate
the Return to Schooling," in Aspects of Labour Market Behavior: Essays in
Expand Down Expand Up @@ -44,6 +46,5 @@


def load() -> DataFrame:
from linearmodels import datasets

return datasets.load(__file__, "card.csv.bz2")
3 changes: 2 additions & 1 deletion linearmodels/datasets/fertility/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from pandas import DataFrame

from linearmodels import datasets

DESCR = """
W. Sander, "The Effect of Women's Schooling on Fertility," Economics Letters
40, 229-233.
Expand Down Expand Up @@ -35,6 +37,5 @@


def load() -> DataFrame:
from linearmodels import datasets

return datasets.load(__file__, "fertility.csv.bz2")
3 changes: 2 additions & 1 deletion linearmodels/datasets/french/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from pandas import DataFrame, to_datetime

from linearmodels import datasets

DESCR = """
Data from Ken French's data library
http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/data_library.html
Expand Down Expand Up @@ -44,7 +46,6 @@


def load() -> DataFrame:
from linearmodels import datasets

data = datasets.load(__file__, "french.csv.bz2")
data["dates"] = to_datetime(data.dates)
Expand Down
3 changes: 2 additions & 1 deletion linearmodels/datasets/fringe/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from pandas import DataFrame

from linearmodels import datasets

# noinspection SpellCheckingInspection,SpellCheckingInspection
DESCR = """
F. Vella (1993), "A Simple Estimator for Simultaneous Models with Censored
Expand Down Expand Up @@ -48,6 +50,5 @@


def load() -> DataFrame:
from linearmodels import datasets

return datasets.load(__file__, "fringe.csv.bz2")
3 changes: 2 additions & 1 deletion linearmodels/datasets/jobtraining/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from pandas import DataFrame

from linearmodels import datasets

DESCR = """
H. Holzer, R. Block, M. Cheatham, and J. Knott (1993), "Are Training Subsidies
Effective? The Michigan Experience," Industrial and Labor Relations Review 46,
Expand Down Expand Up @@ -39,6 +41,5 @@


def load() -> DataFrame:
from linearmodels import datasets

return datasets.load(__file__, "jobtraining.csv.bz2")
Loading
Loading