Skip to content

Commit

Permalink
MAINT: Clean up code
Browse files Browse the repository at this point in the history
Fix small issues identified by ruff
Improve typing
  • Loading branch information
bashtage committed Oct 10, 2024
1 parent 53f86bb commit 70779ba
Show file tree
Hide file tree
Showing 19 changed files with 48 additions and 50 deletions.
2 changes: 1 addition & 1 deletion examples/asset-pricing_examples.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.12"
"version": "3.12.7"
},
"pycharm": {
"stem_cell": {
Expand Down
4 changes: 2 additions & 2 deletions examples/asset-pricing_formulas.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
"metadata": {},
"outputs": [],
"source": [
"from linearmodels.asset_pricing import LinearFactorModel, TradedFactorModel\n",
"from linearmodels.asset_pricing import LinearFactorModel\n",
"\n",
"formula = \"NoDur + Chems + S1V1 + S5V5 + S1M1 + S5M5 ~ MktRF + HML + Mom\"\n",
"mod = LinearFactorModel.from_formula(formula, data)\n",
Expand Down Expand Up @@ -139,7 +139,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.12"
"version": "3.12.7"
},
"pycharm": {
"stem_cell": {
Expand Down
2 changes: 1 addition & 1 deletion examples/iv_absorbing-regression.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.12"
"version": "3.12.7"
},
"pycharm": {
"stem_cell": {
Expand Down
4 changes: 1 addition & 3 deletions examples/iv_advanced-examples.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,6 @@
"metadata": {},
"outputs": [],
"source": [
"from statsmodels.api import OLS, add_constant\n",
"\n",
"data[\"const\"] = 1\n",
"controls = [\"const\"] + controls"
]
Expand Down Expand Up @@ -572,7 +570,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.12"
"version": "3.12.7"
},
"nbsphinx": {
"allow_errors": true
Expand Down
7 changes: 4 additions & 3 deletions examples/iv_basic-examples.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,11 @@
"outputs": [],
"source": [
"import numpy as np\n",
"from linearmodels.datasets import mroz\n",
"import pandas as pd\n",
"from statsmodels.api import add_constant\n",
"\n",
"from linearmodels.datasets import mroz\n",
"\n",
"print(mroz.DESCR)\n",
"data = mroz.load()\n",
"data = data.dropna()\n",
Expand Down Expand Up @@ -574,7 +576,6 @@
"outputs": [],
"source": [
"v = IV2SLS(data[endog], data[exog + instr], None, None).fit().resids\n",
"import pandas as pd\n",
"\n",
"res_direct = IV2SLS(\n",
" data[dep], pd.concat([v, data[exog]], axis=1), data[endog], data[instr]\n",
Expand Down Expand Up @@ -802,7 +803,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.12"
"version": "3.12.7"
},
"pycharm": {
"stem_cell": {
Expand Down
3 changes: 1 addition & 2 deletions examples/iv_using-formulas.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"from linearmodels.datasets import meps\n",
"from linearmodels.iv import IV2SLS\n",
"\n",
Expand Down Expand Up @@ -192,7 +191,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.12"
"version": "3.12.7"
},
"pycharm": {
"stem_cell": {
Expand Down
2 changes: 1 addition & 1 deletion examples/panel_data-formats.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.12"
"version": "3.12.7"
},
"pycharm": {
"stem_cell": {
Expand Down
4 changes: 3 additions & 1 deletion examples/panel_examples.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"outputs": [],
"source": [
"import pandas as pd\n",
"\n",
"from linearmodels.datasets import wage_panel\n",
"\n",
"data = wage_panel.load()\n",
Expand All @@ -54,6 +55,7 @@
"outputs": [],
"source": [
"import statsmodels.api as sm\n",
"\n",
"from linearmodels.panel import PooledOLS\n",
"\n",
"exog_vars = [\"black\", \"hisp\", \"exper\", \"expersq\", \"married\", \"educ\", \"union\", \"year\"]\n",
Expand Down Expand Up @@ -427,7 +429,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.12"
"version": "3.12.7"
},
"pycharm": {
"stem_cell": {
Expand Down
2 changes: 1 addition & 1 deletion examples/panel_using-formulas.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.12"
"version": "3.12.7"
},
"pycharm": {
"stem_cell": {
Expand Down
10 changes: 4 additions & 6 deletions examples/system_examples.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,10 @@
"outputs": [],
"source": [
"# Common libraries\n",
"%matplotlib inline\n",
"import matplotlib.pyplot as plt\n",
"import numpy as np\n",
"import pandas as pd\n",
"import seaborn as sns\n",
"import statsmodels.api as sm"
]
},
Expand Down Expand Up @@ -161,7 +162,6 @@
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"from linearmodels.system import SUR\n",
"\n",
"mod = SUR(mod_data)\n",
Expand All @@ -186,9 +186,6 @@
"regions = [k for k in mod_data.keys()]\n",
"corr = pd.DataFrame(cov / (std @ std.T), columns=regions, index=regions)\n",
"\n",
"import matplotlib.pyplot as plt\n",
"import seaborn as sns\n",
"\n",
"sns.heatmap(corr, vmax=0.8, square=True)\n",
"plt.show()\n",
"\n",
Expand Down Expand Up @@ -734,6 +731,7 @@
"outputs": [],
"source": [
"import statsmodels.api as sm\n",
"\n",
"from linearmodels.datasets import french\n",
"\n",
"data = french.load()\n",
Expand Down Expand Up @@ -780,7 +778,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.12"
"version": "3.12.7"
},
"pycharm": {
"stem_cell": {
Expand Down
3 changes: 2 additions & 1 deletion examples/system_formulas.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"source": [
"import numpy as np\n",
"import pandas as pd\n",
"\n",
"from linearmodels.datasets import fringe\n",
"\n",
"data = fringe.load()"
Expand Down Expand Up @@ -179,7 +180,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.12"
"version": "3.12.7"
},
"pycharm": {
"stem_cell": {
Expand Down
4 changes: 2 additions & 2 deletions examples/system_three-stage-ls.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
"metadata": {},
"outputs": [],
"source": [
"from linearmodels import IV2SLS, IV3SLS, SUR, IVSystemGMM"
"from linearmodels import IV2SLS, IV3SLS, IVSystemGMM"
]
},
{
Expand Down Expand Up @@ -353,7 +353,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.12"
"version": "3.12.7"
},
"pycharm": {
"stem_cell": {
Expand Down
6 changes: 3 additions & 3 deletions linearmodels/iv/absorbing.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ def lsmr_annihilate(
return column_stack(resids)


def category_product(cats: linearmodels.typing.data.AnyPandas) -> Series:
def category_product(cats: linearmodels.typing.AnyPandas) -> Series:
"""
Construct category from all combination of input categories
Expand Down Expand Up @@ -236,8 +236,8 @@ def category_interaction(


def category_continuous_interaction(
cat: linearmodels.typing.data.AnyPandas,
cont: linearmodels.typing.data.AnyPandas,
cat: linearmodels.typing.AnyPandas,
cont: linearmodels.typing.AnyPandas,
precondition: bool = True,
) -> sp.csc_matrix:
"""
Expand Down
6 changes: 2 additions & 4 deletions linearmodels/iv/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@
type_err = "Only ndarrays, DataArrays and Series and DataFrames are supported"


def convert_columns(
s: pd.Series, drop_first: bool
) -> linearmodels.typing.data.AnyPandas:
def convert_columns(s: pd.Series, drop_first: bool) -> linearmodels.typing.AnyPandas:
if isinstance(s.dtype, pd.CategoricalDtype):
out = pd.get_dummies(s, drop_first=drop_first)
# TODO: Remove once pandas typing fixed
Expand Down Expand Up @@ -172,7 +170,7 @@ def pandas(self) -> pd.DataFrame:
return self._pandas

@property
def ndarray(self) -> linearmodels.typing.data.NumericArray:
def ndarray(self) -> linearmodels.typing.NumericArray:
"""ndarray view of data, always 2d"""
return self._ndarray

Expand Down
16 changes: 8 additions & 8 deletions linearmodels/panel/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,10 @@ def __init__(self, df: pandas.DataFrame):
@classmethod
def from_array(
cls,
values: linearmodels.typing.data.NumericArray,
items: Sequence[linearmodels.typing.data.Label],
major_axis: Sequence[linearmodels.typing.data.Label],
minor_axis: Sequence[linearmodels.typing.data.Label],
values: linearmodels.typing.NumericArray,
items: Sequence[linearmodels.typing.Label],
major_axis: Sequence[linearmodels.typing.Label],
minor_axis: Sequence[linearmodels.typing.Label],
) -> _Panel:
index = list(product(minor_axis, major_axis))
multi_index = MultiIndex.from_tuples(index)
Expand Down Expand Up @@ -103,7 +103,7 @@ def to_frame(self) -> DataFrame:

def convert_columns(
s: pandas.Series, drop_first: bool
) -> linearmodels.typing.data.AnyPandas:
) -> linearmodels.typing.AnyPandas:
if is_string_dtype(s.dtype) and s.map(lambda v: isinstance(v, str)).all():
s = s.astype("category")

Expand Down Expand Up @@ -340,18 +340,18 @@ def nentity(self) -> int:
return self._n

@property
def vars(self) -> list[linearmodels.typing.data.Label]:
def vars(self) -> list[linearmodels.typing.Label]:
"""List of variable names"""
return list(self._frame.columns)

@property
def time(self) -> list[linearmodels.typing.data.Label]:
def time(self) -> list[linearmodels.typing.Label]:
"""List of time index names"""
index = self.index
return list(index.levels[1][index.codes[1]].unique())

@property
def entities(self) -> list[linearmodels.typing.data.Label]:
def entities(self) -> list[linearmodels.typing.Label]:
"""List of entity index names"""
index = self.index
return list(index.levels[0][index.codes[0]].unique())
Expand Down
6 changes: 3 additions & 3 deletions linearmodels/panel/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -1269,7 +1269,7 @@ def __init__(
self._singleton_index = None
self._drop_singletons()

def _collect_effects(self) -> linearmodels.typing.data.NumericArray:
def _collect_effects(self) -> linearmodels.typing.NumericArray:
if not self._has_effect:
return np.empty((self.dependent.shape[0], 0))
effects = []
Expand Down Expand Up @@ -1768,8 +1768,8 @@ def _info(self) -> tuple[Series, Series, DataFrame | None]:

@staticmethod
def _is_effect_nested(
effects: linearmodels.typing.data.NumericArray,
clusters: linearmodels.typing.data.NumericArray,
effects: linearmodels.typing.NumericArray,
clusters: linearmodels.typing.NumericArray,
) -> bool:
"""Determine whether an effect is nested by the covariance clusters"""
is_nested = np.zeros(effects.shape[1], dtype=bool)
Expand Down
10 changes: 5 additions & 5 deletions linearmodels/system/_utility.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@


def blocked_column_product(
x: linearmodels.typing.data.ArraySequence, s: linearmodels.typing.data.Float64Array
x: linearmodels.typing.ArraySequence, s: linearmodels.typing.data.Float64Array
) -> linearmodels.typing.data.Float64Array:
"""
Parameters
Expand All @@ -39,7 +39,7 @@ def blocked_column_product(


def blocked_diag_product(
x: linearmodels.typing.data.ArraySequence, s: linearmodels.typing.data.Float64Array
x: linearmodels.typing.ArraySequence, s: linearmodels.typing.data.Float64Array
) -> linearmodels.typing.data.Float64Array:
"""
Parameters
Expand Down Expand Up @@ -68,7 +68,7 @@ def blocked_diag_product(


def blocked_inner_prod(
x: linearmodels.typing.data.ArraySequence, s: linearmodels.typing.data.Float64Array
x: linearmodels.typing.ArraySequence, s: linearmodels.typing.data.Float64Array
) -> linearmodels.typing.data.Float64Array:
r"""
Parameters
Expand Down Expand Up @@ -135,8 +135,8 @@ def blocked_inner_prod(


def blocked_cross_prod(
x: linearmodels.typing.data.ArraySequence,
z: linearmodels.typing.data.ArraySequence,
x: linearmodels.typing.ArraySequence,
z: linearmodels.typing.ArraySequence,
s: linearmodels.typing.data.Float64Array,
) -> linearmodels.typing.data.Float64Array:
r"""
Expand Down
6 changes: 3 additions & 3 deletions linearmodels/system/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -2059,9 +2059,9 @@ def fit(

@staticmethod
def _blocked_gmm(
x: linearmodels.typing.data.ArraySequence,
y: linearmodels.typing.data.ArraySequence,
z: linearmodels.typing.data.ArraySequence,
x: linearmodels.typing.ArraySequence,
y: linearmodels.typing.ArraySequence,
z: linearmodels.typing.ArraySequence,
*,
w: linearmodels.typing.data.Float64Array,
constraints: LinearConstraint | None = None,
Expand Down
1 change: 1 addition & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ isort>=5.12
ipython
matplotlib
ruff
jupyterlab-code-formatter
flake8
jupyter
nbconvert
Expand Down

0 comments on commit 70779ba

Please sign in to comment.