Skip to content

Commit 50408aa

Browse files
authored
Merge pull request #654 from bashtage/bug-dummy
BUG: Remove array from dummy
2 parents f4686fa + eee583c commit 50408aa

File tree

6 files changed

+14
-22
lines changed

6 files changed

+14
-22
lines changed

linearmodels/asset_pricing/model.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -634,7 +634,7 @@ def fit(
634634

635635
# Step 1, n regressions to get B
636636
fc = np.c_[np.ones((nobs, 1)), f]
637-
b = lstsq(fc, p, rcond=None)[0] # nf+1 by np
637+
b = lstsq(fc, p, rcond=None)[0].astype(float) # nf+1 by np
638638
eps = p - fc @ b
639639
if excess_returns:
640640
betas = b[1:].T

linearmodels/iv/results.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ def conf_int(self, level: float = 0.95) -> DataFrame:
286286
-----
287287
Uses a t(df_resid) if ``debiased`` is True, else normal.
288288
"""
289-
ci_quantiles = [(1 - level) / 2, 1 - (1 - level) / 2]
289+
ci_quantiles = array([(1 - level) / 2, 1 - (1 - level) / 2])
290290
if self._debiased:
291291
q = stats.t.ppf(ci_quantiles, self.df_resid)
292292
else:
@@ -1348,6 +1348,7 @@ def anderson_rubin(self) -> InvalidTestStatistic | WaldTestStatistic:
13481348
"Test requires more instruments than " "endogenous variables.",
13491349
name=name,
13501350
)
1351+
assert self._liml_kappa is not None
13511352
stat = nobs * log(self._liml_kappa)
13521353
df = ninstr - nendog
13531354
null = "The model is not overidentified."
@@ -1385,6 +1386,7 @@ def basmann_f(self) -> InvalidTestStatistic | WaldTestStatistic:
13851386
)
13861387
df = ninstr - nendog
13871388
df_denom = nobs - (nexog + ninstr)
1389+
assert self._liml_kappa is not None
13881390
stat = (self._liml_kappa - 1) * df_denom / df
13891391
null = "The model is not overidentified."
13901392
return WaldTestStatistic(stat, null, df, df_denom=df_denom, name=name)

linearmodels/panel/model.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1521,6 +1521,8 @@ def _lsmr_path(
15211521
cond_mean = lsmr(wd, wx[:, i], atol=1e-8, btol=1e-8)[0]
15221522
cond_mean /= cond
15231523
wx_mean_l.append(cond_mean)
1524+
wx_mean: linearmodels.typing.data.Float64Array | csc_matrix
1525+
wy_mean: linearmodels.typing.data.Float64Array | csc_matrix
15241526
wx_mean = np.column_stack(wx_mean_l)
15251527
wy_mean = lsmr(wd, wy, atol=1e-8, btol=1e-8)[0]
15261528
wy_mean /= cond

linearmodels/panel/results.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ def conf_int(self, level: float = 0.95) -> DataFrame:
333333
-----
334334
Uses a t(df_resid) if ``debiased`` is True, else normal.
335335
"""
336-
ci_quantiles = [(1 - level) / 2, 1 - (1 - level) / 2]
336+
ci_quantiles = np.array([(1 - level) / 2, 1 - (1 - level) / 2])
337337
if self._debiased:
338338
q = stats.t.ppf(ci_quantiles, self.df_resid)
339339
else:

linearmodels/panel/utility.py

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from linearmodels.compat.pandas import ANNUAL_FREQ
44

55
from collections import defaultdict
6-
from typing import NamedTuple, TypeVar, cast
6+
from typing import Literal, NamedTuple, TypeVar, cast
77

88
import numpy as np
99
import numpy.random
@@ -122,15 +122,12 @@ def preconditioner(
122122
def dummy_matrix(
123123
cats: linearmodels.typing.data.ArrayLike,
124124
*,
125-
output_format: str = "csc",
126-
drop: str = "first",
125+
output_format: Literal["csc", "csr", "coo"] = "csc",
126+
drop: Literal["first", "last"] = "first",
127127
drop_all: bool = False,
128128
precondition: bool = True,
129129
) -> tuple[
130-
sp.csc_matrix
131-
| sp.csr_matrix
132-
| sp.coo_matrix
133-
| linearmodels.typing.data.Float64Array,
130+
sp.csc_matrix | sp.csr_matrix | sp.coo_matrix,
134131
linearmodels.typing.data.Float64Array,
135132
]:
136133
"""
@@ -146,7 +143,6 @@ def dummy_matrix(
146143
* "csc" - sparse matrix in compressed column form
147144
* "csr" - sparse matrix in compressed row form
148145
* "coo" - sparse matrix in coordinate form
149-
* "array" - dense numpy ndarray
150146
151147
drop: {"first", "last"}
152148
Exclude either the first or last category. This only applies when
@@ -199,7 +195,7 @@ def dummy_matrix(
199195
data["cols"].append(cols)
200196
total_dummies += ncategories - (i > 0)
201197

202-
if output_format in ("csc", "array"):
198+
if output_format == "csc":
203199
fmt = sp.csc_matrix
204200
elif output_format == "csr":
205201
fmt = sp.csr_matrix
@@ -213,9 +209,6 @@ def dummy_matrix(
213209
(np.concatenate(data["rows"]), np.concatenate(data["cols"])),
214210
)
215211
)
216-
if output_format == "array":
217-
out = out.toarray()
218-
219212
if precondition:
220213
out, cond = preconditioner(out, copy=False)
221214
else:

linearmodels/tests/panel/test_utility.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
"csc": csc_matrix,
2121
"csr": csr_matrix,
2222
"coo": coo_matrix,
23-
"array": np.ndarray,
2423
}
2524

2625
pytestmark = pytest.mark.filterwarnings(
@@ -83,19 +82,15 @@ def test_dummy_precondition():
8382
c1 = pd.Series(pd.Categorical(["a"] * 5 + ["b"] * 5 + ["c"] * 5))
8483
c2 = pd.Series(pd.Categorical(["A", "B", "C", "D", "E"] * 3))
8584
cats = pd.concat([c1, c2], axis=1)
86-
out_arr, cond_arr = dummy_matrix(
87-
cats, output_format="array", drop="last", precondition=True
88-
)
8985
csc = dummy_matrix(cats, output_format="csc", drop="last", precondition=True)
9086
out_csc: csc_matrix = csc[0]
9187
cond_csc: np.ndarray = csc[1]
9288
csr = dummy_matrix(cats, output_format="csr", drop="last", precondition=True)
9389
out_csr: csr_matrix = csr[0]
9490
cond_csr: np.ndarray = csr[1]
95-
assert_allclose((out_arr**2).sum(0), np.ones(out_arr.shape[1]))
96-
assert_allclose((out_csc.multiply(out_csc)).sum(0).A1, np.ones(out_arr.shape[1]))
97-
assert_allclose(cond_arr, cond_csc)
91+
assert_allclose((out_csc.multiply(out_csc)).sum(0).A1, np.ones(out_csc.shape[1]))
9892
assert_allclose(cond_csr, cond_csc)
93+
assert isinstance(out_csc, csc_matrix)
9994
assert isinstance(out_csr, csr_matrix)
10095

10196

0 commit comments

Comments
 (0)