Skip to content

Commit 3e5e92d

Browse files
authored
Merge pull request statsmodels#5962 from jbrockmendel/cycln
CLN: cython cleanups
2 parents 9298953 + 1137fd2 commit 3e5e92d

File tree

9 files changed

+47
-48
lines changed

9 files changed

+47
-48
lines changed

statsmodels/nonparametric/_smoothers_lowess.pyx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,8 @@ def lowess(np.ndarray[DTYPE_t, ndim = 1] endog,
138138

139139
y = endog # now just alias
140140
x = exog
141-
142-
if not 0<=frac<=1:
141+
142+
if not 0 <= frac <= 1:
143143
raise ValueError("Lowess `frac` must be in the range [0,1]!")
144144

145145
n = x.shape[0]

statsmodels/tsa/_exponential_smoothers.pyx

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ cimport numpy as np
66

77
np.import_array()
88

9+
910
cdef object _holt_init(double[::1] x, np.uint8_t[::1] xi, double[::1] p,
1011
y, double[::1] l, double[::1] b):
1112
"""Initialization for the Holt Models"""
@@ -30,10 +31,10 @@ cdef object _holt_init(double[::1] x, np.uint8_t[::1] xi, double[::1] p,
3031

3132
cdef double[::1] ensure_1d(object x):
3233
"""
33-
This is a work aound function that ensures that X is a 1-d array. It is needed since
34-
scipy.optimize.brute in version <= 1.0 calls squueze so that 1-d arrays are squeezed to
34+
This is a work aound function that ensures that X is a 1-d array. It is needed since
35+
scipy.optimize.brute in version <= 1.0 calls squueze so that 1-d arrays are squeezed to
3536
scalars.
36-
37+
3738
Fixed in SciPy 1.1
3839
"""
3940
if x.ndim == 0:
@@ -46,7 +47,7 @@ def _holt__(object x, np.uint8_t[::1] xi, double[::1] p, double[::1] y, double[:
4647
double[::1] b, double[::1] s, Py_ssize_t m, Py_ssize_t n, double max_seen):
4748
"""
4849
Compute the sum of squared residuals for Simple Exponential Smoothing
49-
50+
5051
Returns
5152
-------
5253
sse : float
@@ -105,7 +106,7 @@ def _holt_add_dam(object x, np.uint8_t[::1] xi, double[::1] p, double[::1] y, do
105106
cdef double alpha, beta, phi, betac, alphac, err, sse
106107
cdef double[::1] x_arr
107108
cdef Py_ssize_t i
108-
109+
109110
x_arr = ensure_1d(x)
110111
alpha, beta, phi, alphac, betac = _holt_init(x_arr, xi, p, y, l, b)
111112
if alpha == 0.0:

statsmodels/tsa/_innovations.pyx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#cython: language_level=3, wraparound=False, cdivision=True, boundscheck=False
22
import numpy as np
33

4-
cimport numpy as np
54

65
from statsmodels.tools.validation import (array_like, PandasWrapper,
76
int_like, float_like)

statsmodels/tsa/_stl.pyx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,15 +81,14 @@ work workspace of (n+2*np)*5 locations.
8181
"""
8282
import pandas as pd
8383
import numpy as np
84-
cimport numpy as np
8584
from libc.math cimport fabs, sqrt, isnan, NAN
8685

8786
from statsmodels.tsa.tsatools import freq_to_period
8887

89-
np.import_array()
9088

9189
def _is_pos_int(x, odd):
92-
valid = isinstance(x, (int, np.integer))
90+
valid = (isinstance(x, (int, np.integer))
91+
and not isinstance(x, np.timedelta64))
9392
valid = valid and not isinstance(x, (float, np.floating))
9493
try:
9594
valid = valid and x > 0

statsmodels/tsa/regime_switching/_hamilton_filter.pxd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"""
55
Hamilton filter declarations
66
7-
Author: Chad Fulton
7+
Author: Chad Fulton
88
License: Simplified-BSD
99
"""
1010

statsmodels/tsa/regime_switching/_kim_smoother.pxd

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,33 +11,33 @@ License: Simplified-BSD
1111
cimport numpy as np
1212

1313
cpdef skim_smoother_log(int nobs, int k_regimes, int order,
14-
np.float32_t [:,:,:] regime_transition,
15-
np.float32_t [:,:] predicted_joint_probabilities,
16-
np.float32_t [:,:] filtered_joint_probabilities,
17-
np.float32_t [:,:] smoothed_joint_probabilities)
14+
np.float32_t [:, :, :] regime_transition,
15+
np.float32_t [:, :] predicted_joint_probabilities,
16+
np.float32_t [:, :] filtered_joint_probabilities,
17+
np.float32_t [:, :] smoothed_joint_probabilities)
1818

1919
cpdef dkim_smoother_log(int nobs, int k_regimes, int order,
20-
np.float64_t [:,:,:] regime_transition,
21-
np.float64_t [:,:] predicted_joint_probabilities,
22-
np.float64_t [:,:] filtered_joint_probabilities,
23-
np.float64_t [:,:] smoothed_joint_probabilities)
20+
np.float64_t [:, :, :] regime_transition,
21+
np.float64_t [:, :] predicted_joint_probabilities,
22+
np.float64_t [:, :] filtered_joint_probabilities,
23+
np.float64_t [:, :] smoothed_joint_probabilities)
2424

2525
cpdef ckim_smoother_log(int nobs, int k_regimes, int order,
26-
np.complex64_t [:,:,:] regime_transition,
27-
np.complex64_t [:,:] predicted_joint_probabilities,
28-
np.complex64_t [:,:] filtered_joint_probabilities,
29-
np.complex64_t [:,:] smoothed_joint_probabilities)
26+
np.complex64_t [:, :, :] regime_transition,
27+
np.complex64_t [:, :] predicted_joint_probabilities,
28+
np.complex64_t [:, :] filtered_joint_probabilities,
29+
np.complex64_t [:, :] smoothed_joint_probabilities)
3030

3131
cpdef zkim_smoother_log(int nobs, int k_regimes, int order,
32-
np.complex128_t [:,:,:] regime_transition,
33-
np.complex128_t [:,:] predicted_joint_probabilities,
34-
np.complex128_t [:,:] filtered_joint_probabilities,
35-
np.complex128_t [:,:] smoothed_joint_probabilities)
32+
np.complex128_t [:, :, :] regime_transition,
33+
np.complex128_t [:, :] predicted_joint_probabilities,
34+
np.complex128_t [:, :] filtered_joint_probabilities,
35+
np.complex128_t [:, :] smoothed_joint_probabilities)
3636

3737
cdef void skim_smoother_log_iteration(int tt, int k_regimes, int order,
3838
np.float32_t [:] tmp_joint_probabilities,
3939
np.float32_t [:] tmp_probabilities_fraction,
40-
np.float32_t [:,:] regime_transition,
40+
np.float32_t [:, :] regime_transition,
4141
np.float32_t [:] predicted_joint_probabilities,
4242
np.float32_t [:] filtered_joint_probabilities,
4343
np.float32_t [:] prev_smoothed_joint_probabilities,
@@ -46,7 +46,7 @@ cdef void skim_smoother_log_iteration(int tt, int k_regimes, int order,
4646
cdef void dkim_smoother_log_iteration(int tt, int k_regimes, int order,
4747
np.float64_t [:] tmp_joint_probabilities,
4848
np.float64_t [:] tmp_probabilities_fraction,
49-
np.float64_t [:,:] regime_transition,
49+
np.float64_t [:, :] regime_transition,
5050
np.float64_t [:] predicted_joint_probabilities,
5151
np.float64_t [:] filtered_joint_probabilities,
5252
np.float64_t [:] prev_smoothed_joint_probabilities,
@@ -55,7 +55,7 @@ cdef void dkim_smoother_log_iteration(int tt, int k_regimes, int order,
5555
cdef void ckim_smoother_log_iteration(int tt, int k_regimes, int order,
5656
np.complex64_t [:] tmp_joint_probabilities,
5757
np.complex64_t [:] tmp_probabilities_fraction,
58-
np.complex64_t [:,:] regime_transition,
58+
np.complex64_t [:, :] regime_transition,
5959
np.complex64_t [:] predicted_joint_probabilities,
6060
np.complex64_t [:] filtered_joint_probabilities,
6161
np.complex64_t [:] prev_smoothed_joint_probabilities,
@@ -64,7 +64,7 @@ cdef void ckim_smoother_log_iteration(int tt, int k_regimes, int order,
6464
cdef void zkim_smoother_log_iteration(int tt, int k_regimes, int order,
6565
np.complex128_t [:] tmp_joint_probabilities,
6666
np.complex128_t [:] tmp_probabilities_fraction,
67-
np.complex128_t [:,:] regime_transition,
67+
np.complex128_t [:, :] regime_transition,
6868
np.complex128_t [:] predicted_joint_probabilities,
6969
np.complex128_t [:] filtered_joint_probabilities,
7070
np.complex128_t [:] prev_smoothed_joint_probabilities,

statsmodels/tsa/regime_switching/_kim_smoother.pyx.in

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,10 @@ if prefix == 's':
4444

4545

4646
cpdef {{prefix}}kim_smoother_log(int nobs, int k_regimes, int order,
47-
{{cython_type}} [:,:,:] regime_transition,
48-
{{cython_type}} [:,:] predicted_joint_probabilities,
49-
{{cython_type}} [:,:] filtered_joint_probabilities,
50-
{{cython_type}} [:,:] smoothed_joint_probabilities):
47+
{{cython_type}} [:, :, :] regime_transition,
48+
{{cython_type}} [:, :] predicted_joint_probabilities,
49+
{{cython_type}} [:, :] filtered_joint_probabilities,
50+
{{cython_type}} [:, :] smoothed_joint_probabilities):
5151
cdef int t, i, j, k, ix, regime_transition_t = 0, time_varying_regime_transition
5252
cdef:
5353
int k_regimes_order_m1 = k_regimes**(order - 1)
@@ -81,7 +81,7 @@ cpdef {{prefix}}kim_smoother_log(int nobs, int k_regimes, int order,
8181
cdef void {{prefix}}kim_smoother_log_iteration(int tt, int k_regimes, int order,
8282
{{cython_type}} [:] tmp_joint_probabilities,
8383
{{cython_type}} [:] tmp_probabilities_fraction,
84-
{{cython_type}} [:,:] regime_transition,
84+
{{cython_type}} [:, :] regime_transition,
8585
{{cython_type}} [:] predicted_joint_probabilities,
8686
{{cython_type}} [:] filtered_joint_probabilities,
8787
{{cython_type}} [:] prev_smoothed_joint_probabilities,

statsmodels/tsa/statespace/_smoothers/_univariate_diffuse.pyx.in

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ So, what we ought to have is:
2929
\hat \varepsilon_{t,i} = \sigma_{t,i}^2 F_{t,i}^{-1} (v_{t,i} - F_{t,i} K_{t,i}' r_{t,i})
3030
Var(\hat \varepsilon_{t,i}) = \sigma_{t,i}^2 - \sigma_{t,i}^4 F_{t,i}^{-2} (v_{t,i} - F_{t,i} K_{t,i}' r_{t,i})
3131

32-
Author: Chad Fulton
32+
Author: Chad Fulton
3333
License: Simplified-BSD
3434
"""
3535

@@ -100,8 +100,8 @@ cdef int {{prefix}}smoothed_estimators_measurement_univariate_diffuse({{prefix}}
100100
smoother.scaled_smoothed_diffuse1_estimator_cov[:, :, model.nobs-1] = 0
101101
smoother.scaled_smoothed_diffuse2_estimator_cov[:, :, model.nobs-1] = 0
102102

103-
# Smoothing error
104-
# (not used in the univariate approach)
103+
# Smoothing error
104+
# (not used in the univariate approach)
105105

106106
# Given r_{t,0}:
107107
# calculate r_{t-1,p}, ..., r_{t-1, 0} and N_{t-1,p}, ..., N_{t-1,0}
@@ -527,7 +527,7 @@ cdef int {{prefix}}smoothed_disturbances_univariate_diffuse({{prefix}}KalmanSmoo
527527

528528
# Temporary arrays
529529

530-
# $\\#_0 = R_t Q_t$
530+
# $\\#_0 = R_t Q_t$
531531
# $(m \times r) = (m \times r) (r \times r)$
532532
blas.{{prefix}}gemm("N", "N", &model._k_states, &model._k_posdef, &model._k_posdef,
533533
&alpha, model._selection, &model._k_states,
@@ -539,19 +539,19 @@ cdef int {{prefix}}smoothed_disturbances_univariate_diffuse({{prefix}}KalmanSmoo
539539
forecast_error_diffuse_cov = kfilter._forecast_error_diffuse_cov[i + i*kfilter.k_endog]
540540
obs_cov = model._obs_cov[i + i*model._k_endog]
541541

542-
# Smoothed measurement disturbance
542+
# Smoothed measurement disturbance
543543
if smoother.smoother_output & SMOOTHER_DISTURBANCE:
544-
if {{combined_prefix}}abs(forecast_error_diffuse_cov) > kfilter.tolerance_diffuse:
544+
if {{combined_prefix}}abs(forecast_error_diffuse_cov) > kfilter.tolerance_diffuse:
545545
smoother._smoothed_measurement_disturbance[i] = -obs_cov * smoother._smoothed_measurement_disturbance[i]
546546
elif not forecast_error_cov == 0:
547547
smoother._smoothed_measurement_disturbance[i] = obs_cov * (
548548
kfilter._forecast_error[i] / forecast_error_cov - smoother._smoothed_measurement_disturbance[i])
549549
else:
550550
smoother._smoothed_measurement_disturbance[i] = 0
551551

552-
# Smoothed measurement disturbance covariance matrix
552+
# Smoothed measurement disturbance covariance matrix
553553
if smoother.smoother_output & SMOOTHER_DISTURBANCE_COV:
554-
if {{combined_prefix}}abs(forecast_error_diffuse_cov) > kfilter.tolerance_diffuse:
554+
if {{combined_prefix}}abs(forecast_error_diffuse_cov) > kfilter.tolerance_diffuse:
555555
smoother._smoothed_measurement_disturbance_cov[i + i*kfilter.k_endog] = (
556556
obs_cov * (1 - obs_cov * smoother._smoothed_measurement_disturbance_cov[i + i*kfilter.k_endog]))
557557
elif not forecast_error_cov == 0:
@@ -560,14 +560,14 @@ cdef int {{prefix}}smoothed_disturbances_univariate_diffuse({{prefix}}KalmanSmoo
560560
else:
561561
smoother._smoothed_measurement_disturbance_cov[i + i*kfilter.k_endog] = obs_cov
562562

563-
# Smoothed state disturbance
563+
# Smoothed state disturbance
564564
if smoother.smoother_output & SMOOTHER_DISTURBANCE:
565565
blas.{{prefix}}gemv("T", &model._k_states, &model._k_posdef,
566566
&alpha, smoother._tmp0, &kfilter.k_states,
567567
smoother._input_scaled_smoothed_estimator, &inc,
568568
&beta, smoother._smoothed_state_disturbance, &inc)
569569

570-
# Smoothed state disturbance covariance matrix
570+
# Smoothed state disturbance covariance matrix
571571
if smoother.smoother_output & SMOOTHER_DISTURBANCE_COV:
572572
blas.{{prefix}}gemm("N", "N", &model._k_states, &model._k_posdef, &model._k_states,
573573
&alpha, smoother._input_scaled_smoothed_estimator_cov, &kfilter.k_states,

statsmodels/tsa/statespace/_tools.pyx.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -680,7 +680,7 @@ cdef int _{{prefix}}ldl({{cython_type}} * A, int n) except *:
680680
cdef:
681681
int info = 0
682682
int j, i, k
683-
cdef np.npy_intp dim[1]
683+
np.npy_intp dim[1]
684684
np.float64_t tol = 1e-15
685685
{{cython_type}} [:] v
686686

0 commit comments

Comments
 (0)