| 1 | # Copyright 2008, 2009, 2011, 2012 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::Median; | ||
| 18 | use 5.010; | ||
| 19 | use strict; | ||
| 20 | use warnings; | ||
| 21 | use Carp; | ||
| 22 | use List::Util qw(min max); | ||
| 23 | use POSIX (); | ||
| 24 | use Locale::TextDomain ('App-Chart'); | ||
| 25 | |||
| 26 | use base 'App::Chart::Series::Indicator'; | ||
| 27 | use App::Chart::Series::Derived::SMA; | ||
| 28 | |||
| 29 | |||
| 30 | sub longname { __('Moving Median') } | ||
| 31 | sub shortname { __('Median') } | ||
| 32 | sub manual { __p('manual-node','Moving Median') } | ||
| 33 | |||
| 34 | use constant | ||
| 35 | { type => 'average', | ||
| 36 | priority => -10, | ||
| 37 | parameter_info => [ { name => __('Days'), | ||
| 38 | key => 'median_days', | ||
| 39 | type => 'integer', | ||
| 40 | minimum => 1, | ||
| 41 | default => 20 } ], | ||
| 42 | }; | ||
| 43 | |||
| 44 | sub new { | ||
| 45 | my ($class, $parent, $N) = @_; | ||
| 46 | |||
| 47 | $N //= parameter_info()->[0]->{'default'}; | ||
| 48 | ($N > 0) || croak "Median bad N: $N"; | ||
| 49 | |||
| 50 | return $class->SUPER::new | ||
| 51 | (parent => $parent, | ||
| 52 | N => $N, | ||
| 53 | parameters => [ $N ], | ||
| 54 | arrays => { values => [] }, | ||
| 55 | array_aliases => { }); | ||
| 56 | } | ||
| 57 | *warmup_count = \&App::Chart::Series::Derived::SMA::warmup_count; # $N-1 | ||
| 58 | |||
| 59 | sub proc { | ||
| 60 | my ($class_or_self, $N) = @_; | ||
| 61 | $N = max ($N, 1); | ||
| 62 | |||
| 63 | my @array; | ||
| 64 | my @sorted; | ||
| 65 | my $mid1 = 0; | ||
| 66 | my $mid2 = 0; | ||
| 67 | return sub { | ||
| 68 | my ($value) = @_; | ||
| 69 | |||
| 70 | # add new | ||
| 71 | unshift @array, $value; | ||
| 72 | |||
| 73 | # drop old | ||
| 74 | if (@array > $N) { | ||
| 75 | pop @array; | ||
| 76 | } else { | ||
| 77 | # odd elems eg. 5 indexes 0,1,2,3,4 gives mid1==mid2==2 (N-1)/2 | ||
| 78 | # even elems eg. 4 indexes 0,1,2,3 gives mid1==1,mid2==2 f/c of (N-1)/2 | ||
| 79 | $mid1 = POSIX::floor ((@array - 1) / 2); | ||
| 80 | $mid2 = POSIX::ceil ((@array - 1) / 2); | ||
| 81 | } | ||
| 82 | |||
| 83 | @sorted = sort {$a<=>$b} @array; | ||
| 84 | return ($sorted[$mid1] + $sorted[$mid2]) / 2; | ||
| 85 | }; | ||
| 86 | } | ||
| 87 | |||
| 88 | 1; | ||
| 89 | __END__ | ||
| 90 | |||
| 91 | # # maintaining seq ... | ||
| 92 | # # | ||
| 93 | # sub fill_part { | ||
| 94 | # my ($self, $lo, $hi) = @_; | ||
| 95 | # my $parent = $self->{'parent'}; | ||
| 96 | # my $N = $self->{'N'}; | ||
| 97 | # | ||
| 98 | # my $start = $parent->find_before ($lo, $N-1); | ||
| 99 | # $parent->fill ($start, $hi); | ||
| 100 | # my $s = $self->values_array; | ||
| 101 | # my $p = $parent->values_array; | ||
| 102 | # | ||
| 103 | # $hi = min ($hi, $#$p); | ||
| 104 | # if ($#$s < $hi) { $#$s = $hi; } # pre-extend | ||
| 105 | # | ||
| 106 | # my $pos = 0; | ||
| 107 | # my @seq; | ||
| 108 | # | ||
| 109 | # foreach my $i ($start .. $lo-1) { | ||
| 110 | # my $value = $p->[$i] // next; | ||
| 111 | # $seq[$pos++] = [ $value ]; | ||
| 112 | # } | ||
| 113 | # my $count = $pos; | ||
| 114 | # | ||
| 115 | # my @sorted = sort {$a->[0] <=> $b->[0]} @seq; | ||
| 116 | # $#seq = $N-1; # pre-extend | ||
| 117 | # $#sorted = $N-1; # pre-extend | ||
| 118 | # ### warmup to count: $count | ||
| 119 | # | ||
| 120 | # foreach my $t ($lo .. $hi) { | ||
| 121 | # my $value = $p->[$t] // next; | ||
| 122 | # | ||
| 123 | # my $elem = $seq[$pos++]; | ||
| 124 | # if ($pos >= $N) { $pos = 0; } | ||
| 125 | # if ($count < $N) { $count++; } | ||
| 126 | # | ||
| 127 | # if ($elem) { | ||
| 128 | # @sorted = grep {$_ != $elem} @sorted; | ||
| 129 | # $elem->[0] = $value; | ||
| 130 | # } else { | ||
| 131 | # $elem = [ $value ]; | ||
| 132 | # } | ||
| 133 | # | ||
| 134 | # my $i; | ||
| 135 | # for ($i = 0; $i < $#sorted; $i++) { | ||
| 136 | # if ($sorted[0]->[0] >= $value) { | ||
| 137 | # last; | ||
| 138 | # } | ||
| 139 | # } | ||
| 140 | # splice @sorted, $i,0, $elem; | ||
| 141 | # | ||
| 142 | # $s->[$t] = $sorted[$count/2]->[0]; | ||
| 143 | # } | ||
| 144 | # } | ||
| 145 | |||
| 146 | |||
| 147 | # =head1 NAME | ||
| 148 | # | ||
| 149 | # App::Chart::Series::Derived::Median -- moving median | ||
| 150 | # | ||
| 151 | # =head1 SYNOPSIS | ||
| 152 | # | ||
| 153 | # my $series = $parent->median($N); | ||
| 154 | # | ||
| 155 | # =head1 DESCRIPTION | ||
| 156 | # | ||
| 157 | # ... | ||
| 158 | # | ||
| 159 | # =head1 SEE ALSO | ||
| 160 | # | ||
| 161 | # L<App::Chart::Series> | ||
| 162 | # | ||
| 163 | # =cut |