1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
|
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<!-- Created by GNU Texinfo 5.2, http://www.gnu.org/software/texinfo/ -->
<head>
<title>GNU Octave: Linear Least Squares</title>
<meta name="description" content="GNU Octave: Linear Least Squares">
<meta name="keywords" content="GNU Octave: Linear Least Squares">
<meta name="resource-type" content="document">
<meta name="distribution" content="global">
<meta name="Generator" content="makeinfo">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link href="index.html#Top" rel="start" title="Top">
<link href="Concept-Index.html#Concept-Index" rel="index" title="Concept Index">
<link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
<link href="Optimization.html#Optimization" rel="up" title="Optimization">
<link href="Statistics.html#Statistics" rel="next" title="Statistics">
<link href="Nonlinear-Programming.html#Nonlinear-Programming" rel="prev" title="Nonlinear Programming">
<style type="text/css">
<!--
a.summary-letter {text-decoration: none}
blockquote.smallquotation {font-size: smaller}
div.display {margin-left: 3.2em}
div.example {margin-left: 3.2em}
div.indentedblock {margin-left: 3.2em}
div.lisp {margin-left: 3.2em}
div.smalldisplay {margin-left: 3.2em}
div.smallexample {margin-left: 3.2em}
div.smallindentedblock {margin-left: 3.2em; font-size: smaller}
div.smalllisp {margin-left: 3.2em}
kbd {font-style:oblique}
pre.display {font-family: inherit}
pre.format {font-family: inherit}
pre.menu-comment {font-family: serif}
pre.menu-preformatted {font-family: serif}
pre.smalldisplay {font-family: inherit; font-size: smaller}
pre.smallexample {font-size: smaller}
pre.smallformat {font-family: inherit; font-size: smaller}
pre.smalllisp {font-size: smaller}
span.nocodebreak {white-space:nowrap}
span.nolinebreak {white-space:nowrap}
span.roman {font-family:serif; font-weight:normal}
span.sansserif {font-family:sans-serif; font-weight:normal}
ul.no-bullet {list-style: none}
-->
</style>
</head>
<body lang="en" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000">
<a name="Linear-Least-Squares"></a>
<div class="header">
<p>
Previous: <a href="Nonlinear-Programming.html#Nonlinear-Programming" accesskey="p" rel="prev">Nonlinear Programming</a>, Up: <a href="Optimization.html#Optimization" accesskey="u" rel="up">Optimization</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>
<hr>
<a name="Linear-Least-Squares-1"></a>
<h3 class="section">25.4 Linear Least Squares</h3>
<p>Octave also supports linear least squares minimization. That is,
Octave can find the parameter <em>b</em> such that the model
<em>y = x*b</em>
fits data <em>(x,y)</em> as well as possible, assuming zero-mean
Gaussian noise. If the noise is assumed to be isotropic the problem
can be solved using the ‘<samp>\</samp>’ or ‘<samp>/</samp>’ operators, or the <code>ols</code>
function. In the general case where the noise is assumed to be anisotropic
the <code>gls</code> is needed.
</p>
<a name="XREFols"></a><dl>
<dt><a name="index-ols"></a>Function File: <em>[<var>beta</var>, <var>sigma</var>, <var>r</var>] =</em> <strong>ols</strong> <em>(<var>y</var>, <var>x</var>)</em></dt>
<dd><p>Ordinary least squares estimation for the multivariate model
<em>y = x*b + e</em><!-- /@w --> with
<em>mean (e) = 0</em> and <em>cov (vec (e)) = kron (s, I)</em>.
where
<em>y</em> is a <em>t</em> by <em>p</em> matrix, <em>x</em> is a <em>t</em> by
<em>k</em> matrix, <em>b</em> is a <em>k</em> by <em>p</em> matrix, and
<em>e</em> is a <em>t</em> by <em>p</em> matrix.
</p>
<p>Each row of <var>y</var> and <var>x</var> is an observation and each column a
variable.
</p>
<p>The return values <var>beta</var>, <var>sigma</var>, and <var>r</var> are defined as
follows.
</p>
<dl compact="compact">
<dt><var>beta</var></dt>
<dd><p>The OLS estimator for <em>b</em>.
<var>beta</var> is calculated directly via <code>inv (x'*x) * x' * y</code> if the
matrix <code>x'*x</code> is of full rank.
Otherwise, <code><var>beta</var> = pinv (<var>x</var>) * <var>y</var></code> where
<code>pinv (<var>x</var>)</code> denotes the pseudoinverse of <var>x</var>.
</p>
</dd>
<dt><var>sigma</var></dt>
<dd><p>The OLS estimator for the matrix <var>s</var>,
</p>
<div class="example">
<pre class="example"><var>sigma</var> = (<var>y</var>-<var>x</var>*<var>beta</var>)'
* (<var>y</var>-<var>x</var>*<var>beta</var>)
/ (<var>t</var>-rank(<var>x</var>))
</pre></div>
</dd>
<dt><var>r</var></dt>
<dd><p>The matrix of OLS residuals, <code><var>r</var> = <var>y</var> - <var>x</var>*<var>beta</var></code>.
</p></dd>
</dl>
<p><strong>See also:</strong> <a href="#XREFgls">gls</a>, <a href="Basic-Matrix-Functions.html#XREFpinv">pinv</a>.
</p></dd></dl>
<a name="XREFgls"></a><dl>
<dt><a name="index-gls"></a>Function File: <em>[<var>beta</var>, <var>v</var>, <var>r</var>] =</em> <strong>gls</strong> <em>(<var>y</var>, <var>x</var>, <var>o</var>)</em></dt>
<dd><p>Generalized least squares estimation for the multivariate model
<em>y = x*b + e</em><!-- /@w --> with <em>mean (e) = 0</em> and
<em>cov (vec (e)) = (s^2) o</em>,
where
<em>y</em> is a <em>t</em> by <em>p</em> matrix, <em>x</em> is a <em>t</em> by
<em>k</em> matrix, <em>b</em> is a <em>k</em> by <em>p</em> matrix, <em>e</em>
is a <em>t</em> by <em>p</em> matrix, and <em>o</em> is a <em>t*p</em> by
<em>t*p</em> matrix.
</p>
<p>Each row of <var>y</var> and <var>x</var> is an observation and each column a
variable. The return values <var>beta</var>, <var>v</var>, and <var>r</var> are
defined as follows.
</p>
<dl compact="compact">
<dt><var>beta</var></dt>
<dd><p>The GLS estimator for <em>b</em>.
</p>
</dd>
<dt><var>v</var></dt>
<dd><p>The GLS estimator for <em>s^2</em>.
</p>
</dd>
<dt><var>r</var></dt>
<dd><p>The matrix of GLS residuals, <em>r = y - x*beta</em>.
</p></dd>
</dl>
<p><strong>See also:</strong> <a href="#XREFols">ols</a>.
</p></dd></dl>
<a name="XREFlsqnonneg"></a><dl>
<dt><a name="index-lsqnonneg"></a>Function File: <em><var>x</var> =</em> <strong>lsqnonneg</strong> <em>(<var>c</var>, <var>d</var>)</em></dt>
<dt><a name="index-lsqnonneg-1"></a>Function File: <em><var>x</var> =</em> <strong>lsqnonneg</strong> <em>(<var>c</var>, <var>d</var>, <var>x0</var>)</em></dt>
<dt><a name="index-lsqnonneg-2"></a>Function File: <em><var>x</var> =</em> <strong>lsqnonneg</strong> <em>(<var>c</var>, <var>d</var>, <var>x0</var>, <var>options</var>)</em></dt>
<dt><a name="index-lsqnonneg-3"></a>Function File: <em>[<var>x</var>, <var>resnorm</var>] =</em> <strong>lsqnonneg</strong> <em>(…)</em></dt>
<dt><a name="index-lsqnonneg-4"></a>Function File: <em>[<var>x</var>, <var>resnorm</var>, <var>residual</var>] =</em> <strong>lsqnonneg</strong> <em>(…)</em></dt>
<dt><a name="index-lsqnonneg-5"></a>Function File: <em>[<var>x</var>, <var>resnorm</var>, <var>residual</var>, <var>exitflag</var>] =</em> <strong>lsqnonneg</strong> <em>(…)</em></dt>
<dt><a name="index-lsqnonneg-6"></a>Function File: <em>[<var>x</var>, <var>resnorm</var>, <var>residual</var>, <var>exitflag</var>, <var>output</var>] =</em> <strong>lsqnonneg</strong> <em>(…)</em></dt>
<dt><a name="index-lsqnonneg-7"></a>Function File: <em>[<var>x</var>, <var>resnorm</var>, <var>residual</var>, <var>exitflag</var>, <var>output</var>, <var>lambda</var>] =</em> <strong>lsqnonneg</strong> <em>(…)</em></dt>
<dd><p>Minimize <code>norm (<var>c</var>*<var>x</var> - d)</code> subject to
<code><var>x</var> >= 0</code>. <var>c</var> and <var>d</var> must be real. <var>x0</var> is an
optional initial guess for <var>x</var>.
Currently, <code>lsqnonneg</code>
recognizes these options: <code>"MaxIter"</code>, <code>"TolX"</code>.
For a description of these options, see <a href="#XREFoptimset">optimset</a>.
</p>
<p>Outputs:
</p>
<ul>
<li> resnorm
<p>The squared 2-norm of the residual: norm (<var>c</var>*<var>x</var>-<var>d</var>)^2
</p>
</li><li> residual
<p>The residual: <var>d</var>-<var>c</var>*<var>x</var>
</p>
</li><li> exitflag
<p>An indicator of convergence. 0 indicates that the iteration count
was exceeded, and therefore convergence was not reached; >0 indicates
that the algorithm converged. (The algorithm is stable and will
converge given enough iterations.)
</p>
</li><li> output
<p>A structure with two fields:
</p>
<ul>
<li> <code>"algorithm"</code>: The algorithm used (<code>"nnls"</code>)
</li><li> <code>"iterations"</code>: The number of iterations taken.
</li></ul>
</li><li> lambda
<p>Not implemented.
</p></li></ul>
<p><strong>See also:</strong> <a href="#XREFoptimset">optimset</a>, <a href="Quadratic-Programming.html#XREFpqpnonneg">pqpnonneg</a>.
</p></dd></dl>
<a name="XREFoptimset"></a><dl>
<dt><a name="index-optimset"></a>Function File: <em></em> <strong>optimset</strong> <em>()</em></dt>
<dt><a name="index-optimset-1"></a>Function File: <em></em> <strong>optimset</strong> <em>(<var>par</var>, <var>val</var>, …)</em></dt>
<dt><a name="index-optimset-2"></a>Function File: <em></em> <strong>optimset</strong> <em>(<var>old</var>, <var>par</var>, <var>val</var>, …)</em></dt>
<dt><a name="index-optimset-3"></a>Function File: <em></em> <strong>optimset</strong> <em>(<var>old</var>, <var>new</var>)</em></dt>
<dd><p>Create options struct for optimization functions.
</p>
<p>Valid parameters are:
</p>
<dl compact="compact">
<dt>AutoScaling</dt>
<dt>ComplexEqn</dt>
<dt>Display</dt>
<dd><p>Request verbose display of results from optimizations. Values are:
</p>
<dl compact="compact">
<dt><code>"off"</code> [default]</dt>
<dd><p>No display.
</p>
</dd>
<dt><code>"iter"</code></dt>
<dd><p>Display intermediate results for every loop iteration.
</p>
</dd>
<dt><code>"final"</code></dt>
<dd><p>Display the result of the final loop iteration.
</p>
</dd>
<dt><code>"notify"</code></dt>
<dd><p>Display the result of the final loop iteration if the function has
failed to converge.
</p></dd>
</dl>
</dd>
<dt>FinDiffType</dt>
<dt>FunValCheck</dt>
<dd><p>When enabled, display an error if the objective function returns an invalid
value (a complex number, NaN, or Inf). Must be set to <code>"on"</code> or
<code>"off"</code> [default]. Note: the functions <code>fzero</code> and
<code>fminbnd</code> correctly handle Inf values and only complex values or NaN
will cause an error in this case.
</p>
</dd>
<dt>GradObj</dt>
<dd><p>When set to <code>"on"</code>, the function to be minimized must return a
second argument which is the gradient, or first derivative, of the
function at the point <var>x</var>. If set to <code>"off"</code> [default], the
gradient is computed via finite differences.
</p>
</dd>
<dt>Jacobian</dt>
<dd><p>When set to <code>"on"</code>, the function to be minimized must return a
second argument which is the Jacobian, or first derivative, of the
function at the point <var>x</var>. If set to <code>"off"</code> [default], the
Jacobian is computed via finite differences.
</p>
</dd>
<dt>MaxFunEvals</dt>
<dd><p>Maximum number of function evaluations before optimization stops.
Must be a positive integer.
</p>
</dd>
<dt>MaxIter</dt>
<dd><p>Maximum number of algorithm iterations before optimization stops.
Must be a positive integer.
</p>
</dd>
<dt>OutputFcn</dt>
<dd><p>A user-defined function executed once per algorithm iteration.
</p>
</dd>
<dt>TolFun</dt>
<dd><p>Termination criterion for the function output. If the difference in the
calculated objective function between one algorithm iteration and the next
is less than <code>TolFun</code> the optimization stops. Must be a positive
scalar.
</p>
</dd>
<dt>TolX</dt>
<dd><p>Termination criterion for the function input. If the difference in <var>x</var>,
the current search point, between one algorithm iteration and the next is
less than <code>TolX</code> the optimization stops. Must be a positive scalar.
</p>
</dd>
<dt>TypicalX</dt>
<dt>Updating</dt>
</dl>
</dd></dl>
<a name="XREFoptimget"></a><dl>
<dt><a name="index-optimget"></a>Function File: <em></em> <strong>optimget</strong> <em>(<var>options</var>, <var>parname</var>)</em></dt>
<dt><a name="index-optimget-1"></a>Function File: <em></em> <strong>optimget</strong> <em>(<var>options</var>, <var>parname</var>, <var>default</var>)</em></dt>
<dd><p>Return a specific option from a structure created by
<code>optimset</code>. If <var>parname</var> is not a field of the <var>options</var>
structure, return <var>default</var> if supplied, otherwise return an
empty matrix.
</p></dd></dl>
<hr>
<div class="header">
<p>
Previous: <a href="Nonlinear-Programming.html#Nonlinear-Programming" accesskey="p" rel="prev">Nonlinear Programming</a>, Up: <a href="Optimization.html#Optimization" accesskey="u" rel="up">Optimization</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>
</body>
</html>
|