App-Chart
view release on metacpan or search on metacpan
lib/App/Chart/doc/chart.html view on Meta::CPAN
<div class="nav-panel">
<p>
Next: <a href="#Other-Indicator-Packages" accesskey="n" rel="next">Other Indicator Packages</a>, Previous: <a href="#Indicators" accesskey="p" rel="prev">Indicators</a>, Up: <a href="#Top" accesskey="u" rel="up">Chart</a> [<a href="#Concept-In...
</div>
<h2 class="chapter" id="Common-Calculations-1"><span>11 Common Calculations<a class="copiable-link" href="#Common-Calculations-1"> ¶</a></span></h2>
<a class="index-entry-id" id="index-Common-calculations"></a>
<p>The following are algorithms and calculations shared among various indicators
and averages.
</p>
<ul class="mini-toc">
<li><a href="#Linear-Regression" accesskey="1">Linear Regression</a></li>
<li><a href="#Standard-Deviation" accesskey="2">Standard Deviation</a></li>
<li><a href="#True-Range" accesskey="3">True Range</a></li>
</ul>
<hr>
<div class="section-level-extent" id="Linear-Regression">
<div class="nav-panel">
<p>
Next: <a href="#Standard-Deviation" accesskey="n" rel="next">Standard Deviation</a>, Previous: <a href="#Common-Calculations" accesskey="p" rel="prev">Common Calculations</a>, Up: <a href="#Common-Calculations" accesskey="u" rel="up">Common Calculati...
</div>
<h3 class="section" id="Linear-Regression-1"><span>11.1 Linear Regression<a class="copiable-link" href="#Linear-Regression-1"> ¶</a></span></h3>
<a class="index-entry-id" id="index-Linear-regression"></a>
<a class="index-entry-id" id="index-Least-squares_002c-line"></a>
<p>The “least squares” or “linear regression” algorithm produces a best
fitting straight line through the middle of a set of N data points
<em class="math">x1,y1,
..., xN,yN</em>. In Chart this means a set of prices Y, and dates X
(with non-trading days collapsed out).
</p>
<p>For a possible fitted line <em class="math">L(X)= a + b*X</em>, the vertical distance
from the line to each point is squared, and a total deviation formed.
</p>
<div class="example">
<pre class="example-preformatted">SumSquares = (y1 - L(x1))^2 + ... + (yN - L(xN))^2
</pre></div>
<p>The line parameters <em class="math">a</em> and <em class="math">b</em> are then chosen to make SumSquares
as small as possible (hence the name “least squares”), and there’s just one
line with that smallest SumSquares. The calculation is made easier if the X
coordinates are shifted so that <em class="math">Mean(X)=0</em>. With that the formulas for
<em class="math">a</em> and <em class="math">b</em> are
</p>
<div class="example">
<pre class="example-preformatted"> y1 + ... + yN
a = Mean Y = -------------
N
x1*y1 + ... + xN*yN
b = -------------------
x1^2 + ... xN^2
</pre></div>
<p>A least squares fit is “best” under certain mathematical assumptions:
basically that the data points were a straight line to which normally
distributed random amounts (positive or negative) have been added. Of course
an underlying straight line is unlikely in market price data, or in economics
generally, and in particular any cyclical component invalidates the
assumptions. Even so the algorithm is quite widely used because it offers an
objective basis for fitting a line.
</p>
<a class="index-entry-id" id="index-Linear-regression-slope-indicator"></a>
<a class="index-entry-id" id="index-Regression-coefficient"></a>
<a class="index-entry-id" id="index-Coefficient_002c-regression"></a>
<a class="anchor" id="Linear-Regression-Slope"></a><ul class="mini-toc">
<li><a href="#Slope" accesskey="1">Slope</a></li>
<li><a href="#Standard-Error" accesskey="2">Standard Error</a></li>
<li><a href="#Additional-Resources-11" accesskey="3">Additional Resources</a></li>
</ul>
<div class="subsection-level-extent" id="Slope">
<h4 class="subsection"><span>11.1.1 Slope<a class="copiable-link" href="#Slope"> ¶</a></span></h4>
<p>The slope of the linear regression line, the <em class="math">b</em> above, is sometimes
called the <em class="dfn">regression coefficient</em>. This is available as an indicator
(Linear Regression Slope), to show how steep the fitted trend line is. The
units are price change per day, which is negative for a downward sloping line.
This may or may not be particularly useful so it’s under “Low Priority” in
the indicator lists.
</p>
<a class="index-entry-id" id="index-Standard-error-1"></a>
<a class="anchor" id="Linear-Regression-Standard-Error"></a></div>
<div class="subsection-level-extent" id="Standard-Error">
<h4 class="subsection"><span>11.1.2 Standard Error<a class="copiable-link" href="#Standard-Error"> ¶</a></span></h4>
<p>Standard error (stderr) is a statistical measure of how much values differ
from an assumed underlying curve. It’s calculated as the quadratic mean of
the vertical distances from each point to the curve.
</p>
<p>Standard error from a linear regression line <em class="math">y=a+bx</em> is
</p>
<div class="example">
<pre class="example-preformatted"> / (y1 - (a+b*x1))^2 + ... + (yN - (a+b*xN))^2 \
Stderr = sqrt | ------------------------------------------- |
\ N /
</pre></div>
<p>Notice the numerator is the same SumSquares which was minimized above.
Standard error is similar to standard deviation (see <a class="pxref" href="#Standard-Deviation">Standard Deviation</a>);
but where stddev takes differences from a horizontal line (the <em class="math">Y</em> mean),
stderr here goes from the sloping linear regression line.
</p>
<p>For reference, there’s no need to actually calculate the linear regression
<em class="math">a</em> and <em class="math">b</em>, the stderr can be formed directly as
</p>
<div class="example">
<pre class="example-preformatted"> / Covariance(X,Y)^2 \
Stderr = sqrt | Variance(Y) - ----------------- |
\ Variance(X) /
</pre></div>
<p>where variance and covariance are as follows (and notice they simplify if
<em class="math">X</em> values are chosen to make <em class="math">Mean(X)</em> zero),
</p>
<div class="example">
<pre class="example-preformatted">Covariance X,Y = Mean (X*Y) - (Mean X) * (Mean Y)
Variance X = Mean(X^2) - (Mean X)^2
</pre></div>
( run in 1.008 second using v1.01-cache-2.11-cpan-39bf76dae61 )