Skip to content

Commit

Permalink
Merge pull request #602 from bashtage/doc-fixes
Browse files Browse the repository at this point in the history
DOC: Fix many aliases
  • Loading branch information
bashtage authored Jul 19, 2024
2 parents 5aecf58 + d9b5ea9 commit 9ab70f9
Show file tree
Hide file tree
Showing 35 changed files with 1,100 additions and 635 deletions.
15 changes: 15 additions & 0 deletions doc/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@

# More warnings
nitpicky = True
nitpick_ignore = []

with open("nitpick-exceptions") as nitpick_ex:
for line in nitpick_ex:
if line.strip() == "" or line.startswith("#"):
continue
dtype, target = line.split(None, 1)
target = target.strip()
nitpick_ignore.append((dtype, target))

# The short X.Y version
full_version = parse(linearmodels.__version__)
Expand Down Expand Up @@ -412,3 +421,9 @@

autosummary_generate = True
autoclass_content = "class"

autodoc_type_aliases = {
"ArrayLike": "linearmodels.typing.data.ArrayLike",
"IntArray": "linearmodels.typing.data.IntArray",
"Float64Array": "linearmodels.typing.data.Float64Array",
}
1 change: 1 addition & 0 deletions doc/source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ linearmodels
system/index
utility
compatibility
types
plan
contributing
changes
Expand Down
6 changes: 6 additions & 0 deletions doc/source/nitpick-exceptions
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# py:class ArrayLike
py:class IVDataLike
# py:class IntArray
# py:class Float64Array


27 changes: 27 additions & 0 deletions doc/source/types.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
Type Aliases
============

.. module:: linearmodels.typing.data

.. automodule:: linearmodels.typing.data

.. autoclass::

ArrayLike
Float64Array
Int64Array
Int32Array
IntArray
BoolArray
AnyArray
Uint32Array = np.ndarray[Any, np.dtype[np.uint32]] # pragma: no cover


.. module:: linearmodels.typing

.. automodule:: linearmodels.typing

.. autoclass::

Numeric
OptionalNumeric
2 changes: 1 addition & 1 deletion examples/panel_data-formats.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
"source": [
"orig_mi_data = data.set_index([\"fcode\", \"year\"])\n",
"# Subset to the relevant columns and drop missing to avoid warnings\n",
"mi_data = orig_mi_data[[\"lscrap\",\"hrsemp\"]]\n",
"mi_data = orig_mi_data[[\"lscrap\", \"hrsemp\"]]\n",
"mi_data = mi_data.dropna(axis=0, how=\"any\")\n",
"\n",
"print(mi_data.head())"
Expand Down
47 changes: 28 additions & 19 deletions linearmodels/asset_pricing/covariance.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,23 @@

from __future__ import annotations

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

from linearmodels.iv.covariance import (
KERNEL_LOOKUP,
cov_kernel,
kernel_optimal_bandwidth,
)
from linearmodels.typing import Float64Array
import linearmodels.typing.data


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: ndarray = empty((0,)) # pragma: no cover
self._moments: numpy.ndarray = empty((0,)) # pragma: no cover
self._check_kernel(kernel)
self._check_bandwidth(bandwidth)

Expand Down Expand Up @@ -60,7 +61,9 @@ def _check_bandwidth(self, bandwidth: float | None) -> None:
if bandwidth < 0:
raise ValueError("bandwidth must be non-negative.")

def _kernel_cov(self, z: Float64Array) -> Float64Array:
def _kernel_cov(
self, z: linearmodels.typing.data.Float64Array
) -> linearmodels.typing.data.Float64Array:
nobs = z.shape[0]
bw = self.bandwidth
kernel = self._kernel
Expand Down Expand Up @@ -96,10 +99,10 @@ class HeteroskedasticCovariance:

def __init__(
self,
xe: Float64Array,
xe: linearmodels.typing.data.Float64Array,
*,
jacobian: ndarray | None = None,
inv_jacobian: ndarray | None = None,
jacobian: numpy.ndarray | None = None,
inv_jacobian: numpy.ndarray | None = None,
center: bool = True,
debiased: bool = False,
df: int = 0,
Expand Down Expand Up @@ -131,7 +134,7 @@ def config(self) -> dict[str, str | float]:
return {"type": self.__class__.__name__}

@property
def s(self) -> Float64Array:
def s(self) -> linearmodels.typing.data.Float64Array:
"""
Score/moment condition covariance
Expand All @@ -149,7 +152,7 @@ def s(self) -> Float64Array:
return (out + out.T) / 2

@property
def jacobian(self) -> Float64Array:
def jacobian(self) -> linearmodels.typing.data.Float64Array:
"""The Jacobian"""
if self._jac is None:
assert self._inv_jac is not None
Expand All @@ -158,7 +161,7 @@ def jacobian(self) -> Float64Array:
return self._jac

@property
def inv_jacobian(self) -> Float64Array:
def inv_jacobian(self) -> linearmodels.typing.data.Float64Array:
"""Inverse Jacobian"""
if self._inv_jac is None:
assert self._jac is not None
Expand All @@ -172,7 +175,7 @@ def square(self) -> bool:
return self._square

@property
def cov(self) -> Float64Array:
def cov(self) -> linearmodels.typing.data.Float64Array:
"""
Compute parameter covariance
Expand Down Expand Up @@ -229,10 +232,10 @@ class KernelCovariance(HeteroskedasticCovariance, _HACMixin):

def __init__(
self,
xe: Float64Array,
xe: linearmodels.typing.data.Float64Array,
*,
jacobian: ndarray | None = None,
inv_jacobian: ndarray | None = None,
jacobian: numpy.ndarray | None = None,
inv_jacobian: numpy.ndarray | None = None,
kernel: str | None = None,
bandwidth: float | None = None,
center: bool = True,
Expand Down Expand Up @@ -262,7 +265,7 @@ def config(self) -> dict[str, str | float]:
return out

@property
def s(self) -> Float64Array:
def s(self) -> linearmodels.typing.data.Float64Array:
"""
Score/moment condition covariance
Expand All @@ -289,11 +292,15 @@ class HeteroskedasticWeight:
Flag indicating to center the moments when computing the weights
"""

def __init__(self, moments: Float64Array, center: bool = True) -> None:
def __init__(
self, moments: linearmodels.typing.data.Float64Array, center: bool = True
) -> None:
self._moments = moments
self._center = center

def w(self, moments: Float64Array) -> Float64Array:
def w(
self, moments: linearmodels.typing.data.Float64Array
) -> linearmodels.typing.data.Float64Array:
"""
Score/moment condition weighting matrix
Expand Down Expand Up @@ -335,7 +342,7 @@ class KernelWeight(HeteroskedasticWeight, _HACMixin):

def __init__(
self,
moments: Float64Array,
moments: linearmodels.typing.data.Float64Array,
center: bool = True,
kernel: str | None = None,
bandwidth: float | None = None,
Expand All @@ -344,7 +351,9 @@ def __init__(
_HACMixin.__init__(self, kernel, bandwidth)
super().__init__(moments, center=center)

def w(self, moments: Float64Array) -> Float64Array:
def w(
self, moments: linearmodels.typing.data.Float64Array
) -> linearmodels.typing.data.Float64Array:
"""
Score/moment condition weighting matrix
Expand Down
Loading

0 comments on commit 9ab70f9

Please sign in to comment.