Math-EMA
view release on metacpan or search on metacpan
NAME
Math::EMA - compute the exponential moving average
SYNOPSIS
use Math::EMA;
my $avg=Math::EMA->new(alpha=>0.926119, ema=>$initial_value);
$avg->set_param($iterations, $end_weight);
$avg->alpha=$new_alpha;
$avg->ema=$new_value;
$avg->add($some_value);
my $ema=$avg->ema;
INSTALLATION
perl Makefile.PL
make
make test
make install
where alpha is a number between 0 and 1.
That means a new value influences the average with a certain weight
(1-alpha). That weight then exponentially vanes when other values are
added.
How to choose alpha?
The value of alpha determines how fast a given value vanes but it never
completely drops out. Assume you can define a limit say after 10
iterations the weight of a certain value should be 1% or 0.01. Then
_____
_10 / `
alpha = \ / 0.01 = exp( log(0.01) / 10 )
\/
Methods
Math::EMA->new( key=>value, ... )
creates a new "Math::EMA" object. Parameters are passed as "key=>value"
pairs. Currently these keys are recognized:
initializes the average. If an uninitialized ema is used the first
value being added initializes it.
$obj->alpha
set or retrieve the current alpha
$obj->ema
set or retrieve the current average
$obj->set_param($iterations, $end_weight)
computes alpha from the passed values. After $iterations new values a
certain value should have a weight of $end_weight in the average.
SEE ALSO
<http://en.wikipedia.org/wiki/Moving_average#Exponential_moving_average>
<http://en.wikipedia.org/wiki/Exponential_smoothing#The_exponential_movi
ng_average>
AUTHOR
Torsten Förtsch, <torsten.foertsch@gmx.net>
lib/Math/EMA.pm view on Meta::CPAN
=encoding utf8
=head1 NAME
Math::EMA - compute the exponential moving average
=head1 SYNOPSIS
use Math::EMA;
my $avg=Math::EMA->new(alpha=>0.926119, ema=>$initial_value);
$avg->set_param($iterations, $end_weight);
$avg->alpha=$new_alpha;
$avg->ema=$new_value;
$avg->add($some_value);
my $ema=$avg->ema;
=head1 DESCRIPTION
This module computes an exponential moving average by the following formula
avg(n+1) = (1 - alpha) * new_value + alpha * avg(n)
where alpha is a number between 0 and 1.
That means a new value influences the average with a certain weight (1-alpha).
That weight then exponentially vanes when other values are added.
=head2 How to choose alpha?
The value of alpha determines how fast a given value vanes but it never
completely drops out. Assume you can define a limit say after 10
iterations the weight of a certain value should be 1% or 0.01. Then
_____
_10 / `
alpha = \ / 0.01 = exp( log(0.01) / 10 )
\/
=head2 Methods
=head3 Math::EMA-E<gt>new( key=E<gt>value, ... )
lib/Math/EMA.pm view on Meta::CPAN
=back
=head3 $obj-E<gt>alpha
set or retrieve the current alpha
=head3 $obj-E<gt>ema
set or retrieve the current average
=head3 $obj-E<gt>set_param($iterations, $end_weight)
computes alpha from the passed values. After C<$iterations> new values a
certain value should have a weight of C<$end_weight> in the average.
=head1 SEE ALSO
L<http://en.wikipedia.org/wiki/Moving_average#Exponential_moving_average>
L<http://en.wikipedia.org/wiki/Exponential_smoothing#The_exponential_moving_average>
=head1 AUTHOR
ok $avg, 'object created';
cmp_ok sprintf('%.10f', $avg->set_param(60, 0.01) ), '==', 0.9261187281,
'set_param';
cmp_ok sprintf('%.10f', $avg->alpha ), '==', 0.9261187281, 'retrieve alpha';
cmp_ok $avg->add(12), '==', 12, 'added the 1st value';
cmp_ok $avg->ema, '==', 12, 'ema after the 1st value';
$avg->add(0) for(1..60);
printf("# value after 60 iterations: %.10f\n", $avg->ema);
cmp_ok sprintf('%.10f', $avg->ema), '==', 0.12, 'ema after 60 iterations';
( run in 1.732 second using v1.01-cache-2.11-cpan-71847e10f99 )