MCPcopy
hub / github.com/ddbourgin/numpy-ml / update

Method update

numpy_ml/linear_models/linear_regression.py:62–140  ·  view source on GitHub ↗

r""" Incrementally update the linear least-squares coefficients for a set of new examples. Notes ----- The recursive least-squares algorithm [3]_ [4]_ is used to efficiently update the regression parameters as new examples become available. For

(self, X, y, weights=None)

Source from the content-addressed store, hash-verified

60 self._is_fit = False
61
62 def update(self, X, y, weights=None):
63 r"""
64 Incrementally update the linear least-squares coefficients for a set of
65 new examples.
66
67 Notes
68 -----
69 The recursive least-squares algorithm [3]_ [4]_ is used to efficiently
70 update the regression parameters as new examples become available. For
71 a single new example :math:`(\mathbf{x}_{t+1}, \mathbf{y}_{t+1})`, the
72 parameter updates are
73
74 .. math::
75
76 \beta_{t+1} = \left(
77 \mathbf{X}_{1:t}^\top \mathbf{X}_{1:t} +
78 \mathbf{x}_{t+1}\mathbf{x}_{t+1}^\top \right)^{-1}
79 \mathbf{X}_{1:t}^\top \mathbf{Y}_{1:t} +
80 \mathbf{x}_{t+1}^\top \mathbf{y}_{t+1}
81
82 where :math:`\beta_{t+1}` are the updated regression coefficients,
83 :math:`\mathbf{X}_{1:t}` and :math:`\mathbf{Y}_{1:t}` are the set of
84 examples observed from timestep 1 to *t*.
85
86 In the single-example case, the RLS algorithm uses the Sherman-Morrison
87 formula [5]_ to avoid re-inverting the covariance matrix on each new
88 update. In the multi-example case (i.e., where :math:`\mathbf{X}_{t+1}`
89 and :math:`\mathbf{y}_{t+1}` are matrices of `N` examples each), we use
90 the generalized Woodbury matrix identity [6]_ to update the inverse
91 covariance. This comes at a performance cost, but is still more
92 performant than doing multiple single-example updates if *N* is large.
93
94 References
95 ----------
96 .. [3] Gauss, C. F. (1821) *Theoria combinationis observationum
97 erroribus minimis obnoxiae*, Werke, 4. Gottinge
98 .. [4] https://en.wikipedia.org/wiki/Recursive_least_squares_filter
99 .. [5] https://en.wikipedia.org/wiki/Sherman%E2%80%93Morrison_formula
100 .. [6] https://en.wikipedia.org/wiki/Woodbury_matrix_identity
101
102 Parameters
103 ----------
104 X : :py:class:`ndarray <numpy.ndarray>` of shape `(N, M)`
105 A dataset consisting of `N` examples, each of dimension `M`
106 y : :py:class:`ndarray <numpy.ndarray>` of shape `(N, K)`
107 The targets for each of the `N` examples in `X`, where each target
108 has dimension `K`
109 weights : :py:class:`ndarray <numpy.ndarray>` of shape `(N,)` or None
110 Weights associated with the examples in `X`. Examples
111 with larger weights exert greater influence on model fit. When
112 `y` is a vector (i.e., `K = 1`), weights should be set to the
113 reciporical of the variance for each measurement (i.e., :math:`w_i
114 = 1/\sigma^2_i`). When `K > 1`, it is assumed that all columns of
115 `y` share the same weight :math:`w_i`. If None, examples are
116 weighted equally, resulting in the standard linear least squares
117 update. Default is None.
118
119 Returns

Callers 1

test_linear_regressionFunction · 0.95

Calls 2

_update1DMethod · 0.95
_update2DMethod · 0.95

Tested by 1

test_linear_regressionFunction · 0.76