| 1 | # Copyright 2008, 2009, 2010 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::Stddev; | ||
| 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 | sub longname { __('Standard Deviation') } | ||
| 29 | sub shortname { __('Stddev') } | ||
| 30 | sub manual { __p('manual-node','Standard Deviation') } | ||
| 31 | |||
| 32 | use constant | ||
| 33 | { priority => -10, | ||
| 34 | type => 'indicator', | ||
| 35 | units => 'price', | ||
| 36 | hlines => [ 0 ], | ||
| 37 | minimum => 0, | ||
| 38 | parameter_info => [ { name => __('Days'), | ||
| 39 | key => 'stddev_days', | ||
| 40 | type => 'integer', | ||
| 41 | minimum => 1, | ||
| 42 | default => 20 } ], | ||
| 43 | }; | ||
| 44 | |||
| 45 | sub new { | ||
| 46 | my ($class, $parent, $N) = @_; | ||
| 47 | ### Stddev new(): "@_" | ||
| 48 | |||
| 49 | $N //= parameter_info()->[0]->{'default'}; | ||
| 50 | ($N > 0) || croak "Stddev bad N: $N"; | ||
| 51 | |||
| 52 | return $class->SUPER::new | ||
| 53 | (parent => $parent, | ||
| 54 | N => $N, | ||
| 55 | parameters => [ $N ], | ||
| 56 | arrays => { values => [] }, | ||
| 57 | array_aliases => { }); | ||
| 58 | } | ||
| 59 | *warmup_count = \&App::Chart::Series::Derived::SMA::warmup_count; # $N-1 | ||
| 60 | |||
| 61 | # / p1^2 + p2^2 + ... + pN^2 (p1 + p2 + ... + pN)^2 \ | ||
| 62 | # Stddev = sqrt ( ------------------------ - ---------------------- ) | ||
| 63 | # \ N N^2 / | ||
| 64 | # | ||
| 65 | # rearranged to a single division, | ||
| 66 | # | ||
| 67 | # sqrt ( (p1^2 + p2^2 + ... + pN^2) * N - (p1 + p2 + ... + pN)^2 ) | ||
| 68 | # = ---------------------------------------------------------------- | ||
| 69 | # N | ||
| 70 | # | ||
| 71 | sub proc { | ||
| 72 | my ($class, $N) = @_; | ||
| 73 | if ($N == 0) { die "Stddev N==0"; } | ||
| 74 | $N = max (1, $N); | ||
| 75 | |||
| 76 | my @array; | ||
| 77 | my $count = 0; | ||
| 78 | my $pos = $N - 1; # initial extends | ||
| 79 | |||
| 80 | my $total = 0; | ||
| 81 | my $total_squares = 0; | ||
| 82 | |||
| 83 | return sub { | ||
| 84 | my ($value) = @_; | ||
| 85 | |||
| 86 | # drop old | ||
| 87 | if ($count >= $N) { | ||
| 88 | my $old = $array[$pos]; | ||
| 89 | $total -= $old; | ||
| 90 | $total_squares -= $old * $old; | ||
| 91 | } else { | ||
| 92 | $count++; | ||
| 93 | } | ||
| 94 | |||
| 95 | # add new | ||
| 96 | $total += ($array[$pos] = $value); | ||
| 97 | $total_squares += $value * $value; | ||
| 98 | if (++$pos >= $N) { $pos = 0; } | ||
| 99 | |||
| 100 | return sqrt(max (0, $total_squares*$count - $total*$total)) / $count; | ||
| 101 | }; | ||
| 102 | } | ||
| 103 | |||
| 104 | 1; | ||
| 105 | __END__ |