package Dataset;
use Statistics::Descriptive;
use Statistics::Distributions;
use strict;
sub new {
my ($class) = shift;
my $self = bless {
_data => \@_,
_scale => 1.0,
_mean => 0.0,
_error => 0.0,
}, $class;
my $n = @_;
if ($n >= 1) {
my $stats = Statistics::Descriptive::Full->new();
$stats->add_data(@{$self->{_data}});
$self->{_mean} = $stats->mean();
if ($n >= 2) {
my $t = Statistics::Distributions::tdistr($n-1, 0.005);
$self->{_error} = $t * $stats->standard_deviation();
}
}
$self;
}
sub setScale {
my ($self, $scale) = @_;
$self->{_scale} = $scale;
}
sub scaleBy {
my ($self, $a) = @_;
$self->{_scale} *= $a;
}
sub getMean {
my $self = shift;
return $self->{_mean} * $self->{_scale};
}
sub getError {
my $self = shift;
return $self->{_error} * $self->{_scale};
}
sub divide {
my $self = shift;
my $rhs = shift;
my $minratio = ($self->{_mean} - $self->{_error}) /
($rhs->{_mean} + $rhs->{_error});
my $maxratio = ($self->{_mean} + $self->{_error}) /
($rhs->{_mean} - $rhs->{_error});
my $result = Dataset->new();
$result->{_mean} = ($minratio + $maxratio) / 2;
$result->{_error} = $result->{_mean} - $minratio;
$result->{_scale} = $self->{_scale} / $rhs->{_scale};
$result;
}
sub subtract {
my $self = shift;
my $rhs = shift;
my $result = Dataset->new();
$result->{_mean} = $self->{_mean} - $rhs->{_mean};
$result->{_error} = $self->{_error} + $rhs->{_error};
$result->{_scale} = $self->{_scale};
$result;
}
sub add {
my $self = shift;
my $rhs = shift;
my $result = Dataset->new();
$result->{_mean} = $self->{_mean} + $rhs->{_mean};
$result->{_error} = $self->{_error} + $rhs->{_error};
$result->{_scale} = $self->{_scale};
$result;
}
sub divideByScalar {
my $self = shift;
my $s = shift;
my $result = Dataset->new();
$result->{_mean} = $self->{_mean}/$s;
$result->{_error} = $self->{_error}/$s;
$result->{_scale} = $self->{_scale};
$result;
}
sub multiplyByScalar {
my $self = shift;
my $s = shift;
my $result = Dataset->new();
$result->{_mean} = $self->{_mean}*$s;
$result->{_error} = $self->{_error}*$s;
$result->{_scale} = $self->{_scale};
$result;
}
1;