Coverage for /home/martinb/.local/share/virtualenvs/camcops/lib/python3.6/site-packages/statsmodels/regression/_tools.py : 20%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1import numpy as np
2from statsmodels.tools.tools import Bunch
5class _MinimalWLS(object):
6 """
7 Minimal implementation of WLS optimized for performance.
9 Parameters
10 ----------
11 endog : array_like
12 1-d endogenous response variable. The dependent variable.
13 exog : array_like
14 A nobs x k array where `nobs` is the number of observations and `k`
15 is the number of regressors. An intercept is not included by default
16 and should be added by the user. See
17 :func:`statsmodels.tools.add_constant`.
18 weights : array_like, optional
19 1d array of weights. If you supply 1/W then the variables are pre-
20 multiplied by 1/sqrt(W). If no weights are supplied the default value
21 is 1 and WLS reults are the same as OLS.
22 check_endog : bool, optional
23 Flag indicating whether to check for inf/nan in endog.
24 If True and any are found, ValueError is raised.
25 check_weights : bool, optional
26 Flag indicating whether to check for inf/nan in weights.
27 If True and any are found, ValueError is raised.
29 Notes
30 -----
31 Provides only resid, scale, fittedvalues, model.weights which are used by
32 methods that iteratively apply WLS.
34 Does not perform any checks on the input data for type or shape
35 compatibility
36 """
38 msg = 'NaN, inf or invalid value detected in {0}, estimation infeasible.'
40 def __init__(self, endog, exog, weights=1.0, check_endog=False,
41 check_weights=False):
42 self.endog = endog
43 self.exog = exog
44 self.weights = weights
45 w_half = np.sqrt(weights)
46 if check_weights:
47 if not np.all(np.isfinite(w_half)):
48 raise ValueError(self.msg.format('weights'))
50 if check_endog:
51 if not np.all(np.isfinite(endog)):
52 raise ValueError(self.msg.format('endog'))
54 self.wendog = w_half * endog
55 if np.isscalar(weights):
56 self.wexog = w_half * exog
57 else:
58 self.wexog = w_half[:, None] * exog
60 def fit(self, method='pinv'):
61 """
62 Minimal implementation of WLS optimized for performance.
64 Parameters
65 ----------
66 method : str, optional
67 Method to use to estimate parameters. "pinv", "qr" or "lstsq"
69 * "pinv" uses the Moore-Penrose pseudoinverse
70 to solve the least squares problem.
71 * "qr" uses the QR factorization.
72 * "lstsq" uses the least squares implementation in numpy.linalg
74 Returns
75 -------
76 results : namedtuple
77 Named tuple containing the fewest terms needed to implement
78 iterative estimation in models. Currently
80 * params : Estimated parameters
81 * fittedvalues : Fit values using original data
82 * resid : Residuals using original data
83 * model : namedtuple with one field, weights
84 * scale : scale computed using weighted residuals
86 Notes
87 -----
88 Does not perform and checks on the input data
90 See Also
91 --------
92 statsmodels.regression.linear_model.WLS
93 """
94 if method == 'pinv':
95 pinv_wexog = np.linalg.pinv(self.wexog)
96 params = pinv_wexog.dot(self.wendog)
97 elif method == 'qr':
98 Q, R = np.linalg.qr(self.wexog)
99 params = np.linalg.solve(R, np.dot(Q.T, self.wendog))
100 else:
101 params, _, _, _ = np.linalg.lstsq(self.wexog, self.wendog,
102 rcond=-1)
103 return self.results(params)
105 def results(self, params):
106 """
107 Construct results
109 params : ndarray
110 Model parameters
112 Notes
113 -----
114 Allows results to be constructed from either existing parameters or
115 when estimated using using ``fit``
116 """
117 fitted_values = self.exog.dot(params)
118 resid = self.endog - fitted_values
119 wresid = self.wendog - self.wexog.dot(params)
120 df_resid = self.wexog.shape[0] - self.wexog.shape[1]
121 scale = np.dot(wresid, wresid) / df_resid
123 return Bunch(params=params, fittedvalues=fitted_values, resid=resid,
124 model=self, scale=scale)