# [Cascavel-pm] Analise de tendencia

Alex E. J. Falcão alfspsp em hotmail.com
Terça Agosto 10 11:42:15 CDT 2004

```Hi Folks,

Tempos atras mandei uma mensagem perguntando se alguem sabia de alguma
função para calcular valores futuros baseandos em um histórico. Varios me
responderam, mas infelizmente não era o que eu precisava. Eu queria algo
semelhante ao que é rezalido pela formula do "Excel" FORECAST, então fui no
site da microsoft e achei a explciação da composição da fórmula e resolvi
implementar.

o site é:
http://office.microsoft.com/assistance/preview.aspx?AssetID=HP052090961033&CTT=4&Origin=CH062528311033

Não manjo muito, então gostaria que a galera da lista mandesse opniões e
sugestões.

Segue abaixo o modulo:

##################################################
package Statistics::Forecast;

\$VERSION = '0.1';

use strict;

Statistics::Forecast

This is a Oriented Object module that calculates a future value by using
existing values. The new value is calculated by using linear regression.

use Statistics::Forecast;

# Create forecast object
my \$FCAST = new Statistics::Forecast("My Forecast Name");

\$FCAST->{DataX} = \@Array_X;
\$FCAST->{DataY} = \@Array_Y;
\$FCAST->{NextX} = \$NextX;

# Calculate the result
\$FCAST->calc;

# Get the result
my \$Result_Forecast = \$FCAST->{ForecastY);

The equation for Forecast is:

a+bx, where 'x' is the predicted value and
_    _
a = y + bx

b = sum((x+x)(y-y))/sum(x-x)**2;

=over

=item C<new>

Receives a forecast name, only to remember
and returns the blessed data structure as
a Statistics::Forecast object.

=cut

################################################################

sub new {
my \$classname= shift(@_);
my \$ForecastName = shift(@_) || "with no name";
my \$DataX = shift(@_) || undef;
my \$DataY = shift(@_) || undef;
my \$NextX = shift(@_) || undef;
my \$self = {
ForecastName => \$ForecastName,
DataX => \$DataX,
DataY => \$DataY,
NextX => \$NextX,

# Initialializing Acumulative variables
SumX => 0,
SumY => 0,
SumXY => 0,
SumXX => 0
};

bless \$self;
return \$self;

}

=item C<calc>

Calculate and return the forecast value.

=cut

################################################################

sub calc {
my \$self = shift;

# Verify if the inputed values are correct.
if (!\$self->{DataY}) { die "Cannot run without Y values" };
# if no X values were input, populate with 1, 2 ...
if (\$#{\$self->{DataX}} eq -1) {
for (my \$X=1; \$X <= \$#{\$self->{DataY}}+1; \$X++) {
\$self->{DataX}[\$X-1] = \$X;
}
}
if (\$#{\$self->{DataY}} != \$#{\$self->{DataX}}) { die "Cannot run with
different number of 'X' values." };
if (!\$self->{NextX}) { die "Cannot run with no data point which you
want to predict a value." };

# Calculate the Sum of Y, X, X*Y and X**2 values.
for (my \$X=0; \$X <= \$#{\$self->{DataX}}; \$X++) {
\$self->{SumY} += \$self->{DataY}[\$X];
\$self->{SumX} += \$self->{DataX}[\$X];
\$self->{SumXY} += (\$self->{DataX}[\$X] * \$self->{DataY}[\$X]);
\$self->{SumXX} += (\$self->{DataX}[\$X]**2);
}

\$self->{N} = \$#{\$self->{DataX}}+1;              # Number of Elements
\$self->{AvgX} = \$self->{SumX} / \$self->{N};     # X Average
\$self->{AvgY} = \$self->{SumY} / \$self->{N};     # Y Average

my \$B1 = (\$self->{N} * \$self->{SumXY} - \$self->{SumX} *
\$self->{SumY});
my \$B2 = (\$self->{N} * \$self->{SumXX} - \$self->{SumX}**2 );
my \$B = \$B1 / \$B2;
my \$A = \$self->{AvgY} - \$B*\$self->{AvgX};

\$self->{ForecastY} = \$A + \$B*\$self->{NextX}; # The forecast
}

################################################################

=item C<dump>

Prints data for debuging propose

=back

=cut

################################################################

sub dump {

my \$self = shift;
print "\n\n";
print "###########################################\n";
print "      This is a Forecast dump              \n";
print "###########################################\n";
print "\n";
if (\$self->{N}) {
print ".  Forecast Name   : ", \$self->{ForecastName}, "\n";
print ".  Number of elements: ", \$self->{N}, "\n";
print ".  --------------------------------------- \n";
print ".  X Values\n\t", join(";  ", @{\$self->{DataX}}), "\n";
print ".  Sum of X values   : ", \$self->{SumX}, "\n";
print ".  --------------------------------------- \n";
print ".  Y Values\n\t", join(";  ", @{\$self->{DataY}}), "\n";
print ".  Sum of Y values   : ", \$self->{SumY}, "\n";
print ".  --------------------------------------- \n";
print ".  Sum of X*Y values : ", \$self->{SumXY}, "\n";
print ".  Sum of X**2 values: ", \$self->{SumXX}, "\n";
print ".  --------------------------------------- \n";
print ".  Predict inputed   : ", \$self->{NextX}, "\n";
print ".  Forecast value    : ", \$self->{ForecastY}, "\n";
print ".  --------------------------------------- \n";
print ".  Thanks for using Statistics::Forecast\n\n";
} else {
print "Error: You have to use method <calc> \n";
print "       before dump the values.\n";
print "\n";
}
}

use Statistics::Forecast;

my @Y = (1,3,7,12);
my @X = (1,2,3,4);

my \$FCAST = new Statistics::Forecast("My Forecast");

\$FCAST->{DataX} = \@X;
\$FCAST->{DataY} = \@Y;
\$FCAST->{NextX} = 8;
\$FCAST->calc;

print "The Forecast \"", \$FCAST->{ForecastName}, "\" has the forecast
value: ", \$FCAST->{ForecastY}, "\n";

This module was developed by Alex Falcao(alfspsp em hotmail.com).

This is the first version and calculates forecast value.

0.1