| 1 | # Copyright 2007, 2009, 2010, 2011 Kevin Ryde | ||
| 2 | |||
| 3 | # This file is part of Chart. | ||
| 4 | # | ||
| 5 | # Chart is free software; you can redistribute it and/or modify it under the | ||
| 6 | # terms of the GNU General Public License as published by the Free Software | ||
| 7 | # Foundation; either version 3, or (at your option) any later version. | ||
| 8 | # | ||
| 9 | # Chart is distributed in the hope that it will be useful, but WITHOUT ANY | ||
| 10 | # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | ||
| 11 | # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more | ||
| 12 | # details. | ||
| 13 | # | ||
| 14 | # You should have received a copy of the GNU General Public License along | ||
| 15 | # with Chart. If not, see <http://www.gnu.org/licenses/>. | ||
| 16 | |||
| 17 | package App::Chart::Series::Derived::LinRegStderr; | ||
| 18 | use 5.010; | ||
| 19 | use strict; | ||
| 20 | use warnings; | ||
| 21 | use Carp; | ||
| 22 | use List::Util qw(min max); | ||
| 23 | use Locale::TextDomain ('App-Chart'); | ||
| 24 | |||
| 25 | use base 'App::Chart::Series::Indicator'; | ||
| 26 | use App::Chart::Series::Derived::SMA; | ||
| 27 | |||
| 28 | |||
| 29 | # http://mathworld.wolfram.com/LeastSquaresFitting.html | ||
| 30 | # Showing how to calculate the variance in the error amounts (e[i]) | ||
| 31 | # without calculating a,b parameters. | ||
| 32 | # | ||
| 33 | # | ||
| 34 | # | ||
| 35 | # Sum (y - (a+b*x))^2 | ||
| 36 | # stderr = sqrt ------------------- | ||
| 37 | # N^2 | ||
| 38 | # | ||
| 39 | # Sum ((y - My) - b*x)^2 | ||
| 40 | # = sqrt ---------------------- where My = Mean(Y) | ||
| 41 | # N^2 | ||
| 42 | # | ||
| 43 | # Sum (y - My)^2 - 2*My*b*x + b^2*x^2 | ||
| 44 | # = sqrt ----------------------------------- | ||
| 45 | # N^2 | ||
| 46 | # | ||
| 47 | # (y - My)^2 2*My*b*x - b^2*x^2 | ||
| 48 | # = sqrt Sum ---------- - Sum ------------------ | ||
| 49 | # N^2 N^2 | ||
| 50 | # | ||
| 51 | # The first term is Variance(Y), and with b = Mean(X*Y) - Mean(X) * Mean(Y) | ||
| 52 | # | ||
| 53 | # 2*M(y)*Sum(x) - M(X*Y)*Sum(x^2)/M(X)*M(Y) | ||
| 54 | # = sqrt Var(Y) - b * ----------------------------------------- | ||
| 55 | # N^2 | ||
| 56 | # | ||
| 57 | # .... | ||
| 58 | |||
| 59 | |||
| 60 | sub longname { __('Linear Regression Stderr') } | ||
| 61 | sub shortname { __('Linreg Stderr') } | ||
| 62 | sub manual { __p('manual-node','Linear Regression Standard Error') } | ||
| 63 | |||
| 64 | use constant | ||
| 65 | { type => 'indicator', | ||
| 66 | units => 'price', | ||
| 67 | priority => -10, | ||
| 68 | minimum => 0, | ||
| 69 | parameter_info => [ { name => __('Days'), | ||
| 70 | key => 'linregstderr_days', | ||
| 71 | type => 'integer', | ||
| 72 | minimum => 1, | ||
| 73 | default => 20 } ], | ||
| 74 | }; | ||
| 75 | |||
| 76 | sub new { | ||
| 77 | my ($class, $parent, $N) = @_; | ||
| 78 | |||
| 79 | $N //= parameter_info()->[0]->{'default'}; | ||
| 80 | ($N > 0) || croak "LinRegStderr bad N: $N"; | ||
| 81 | |||
| 82 | return $class->SUPER::new | ||
| 83 | (parent => $parent, | ||
| 84 | N => $N, | ||
| 85 | parameters => [ $N ], | ||
| 86 | arrays => { values => [] }, | ||
| 87 | array_aliases => { }); | ||
| 88 | } | ||
| 89 | *warmup_count = \&App::Chart::Series::Derived::SMA::warmup_count; # $N-1 | ||
| 90 | |||
| 91 | # Return the factor for 1/Variance(X) arising in the stderr formula. With | ||
| 92 | # X values -1.5,-0.5,0.5, 1.5 this means | ||
| 93 | # | ||
| 94 | # 1 | ||
| 95 | # -------------------------------------------- | ||
| 96 | # n * ((-1.5)^2 + (-0.5)^2 + (0.5)^2 + (1.5)^2) | ||
| 97 | # | ||
| 98 | # and the denominator here is (n^4 - n^2)/12. See devel/ema-omitted.pl for | ||
| 99 | # checking this. Same func in RSquared.pm too. | ||
| 100 | # | ||
| 101 | sub xfactor { | ||
| 102 | my ($N) = @_; | ||
| 103 | if ($N < 2) { return 1; } | ||
| 104 | return 12.0 / ($N*$N * ($N*$N - 1)); | ||
| 105 | } | ||
| 106 | |||
| 107 | sub proc { | ||
| 108 | my ($class, $N) = @_; | ||
| 109 | |||
| 110 | # @array,$pos is a circular list of the last $N many Y values. | ||
| 111 | # | ||
| 112 | # $count is the number of values in @array. | ||
| 113 | # | ||
| 114 | # $y_total is the sum of the values in @array. | ||
| 115 | # | ||
| 116 | # $yy_total is the sum of the squares of the values in @array. | ||
| 117 | # | ||
| 118 | # $x_total is the sum of the X values corresponding to the values in | ||
| 119 | # @array, which means 1+2+...+$count, which is a triangular number. This | ||
| 120 | # varies until $count==$N is reached, and is then a constant. | ||
| 121 | # | ||
| 122 | # $x_factor is the variance factor in the denominator, see `xfactor' above. | ||
| 123 | # This varies until $count==$N is reached, and is then a constant. | ||
| 124 | # | ||
| 125 | # $xy_total is the sum of the product x*y for each x,y pair, which means | ||
| 126 | # 1*y[1] + 2*y[2] + ... + $count*y[$count]. When shifting out an old Y, | ||
| 127 | # the weighting of each y in the sum is reduced by subtracting $y_total. | ||
| 128 | |||
| 129 | my @array; | ||
| 130 | my $pos = $N - 1; # initial extends | ||
| 131 | my $y_total = 0; | ||
| 132 | my $yy_total = 0; | ||
| 133 | my $x_factor = 0; | ||
| 134 | my $xy_total = 0; | ||
| 135 | |||
| 136 | my $count = 0; | ||
| 137 | return sub { | ||
| 138 | my ($y) = @_; | ||
| 139 | |||
| 140 | if ($count >= $N) { | ||
| 141 | # drop oldest point | ||
| 142 | my $prev = $array[$pos]; | ||
| 143 | $yy_total -= $prev ** 2; | ||
| 144 | $xy_total += ($count-1)*0.5 * $prev; | ||
| 145 | $y_total -= $prev; | ||
| 146 | } else { | ||
| 147 | # gaining a point | ||
| 148 | $count++; | ||
| 149 | $x_factor = xfactor($count); | ||
| 150 | $xy_total += $y_total * 0.5; # adj so 0.5 less each x | ||
| 151 | } | ||
| 152 | $array[$pos] = $y; | ||
| 153 | if (++$pos >= $N) { $pos = 0; } | ||
| 154 | |||
| 155 | |||
| 156 | # shift xy products onto 1 less x each | ||
| 157 | $xy_total -= $y_total; | ||
| 158 | |||
| 159 | # add this point | ||
| 160 | $y_total += $y; | ||
| 161 | $yy_total += $y*$y; | ||
| 162 | $xy_total += $y * ($count-1)*0.5; | ||
| 163 | |||
| 164 | return (sqrt(max (0, | ||
| 165 | $count*$yy_total - $y_total*$y_total # Var(Y) | ||
| 166 | - (($count * $xy_total)**2 # (Covar(X,Y))^2 | ||
| 167 | * $x_factor))) # 1 / (X variance) | ||
| 168 | / $count); | ||
| 169 | }; | ||
| 170 | } | ||
| 171 | |||
| 172 | 1; | ||
| 173 | __END__ |