Skip to content

Commit

Permalink
feat:First version of StudentTDistribution.
Browse files Browse the repository at this point in the history
  • Loading branch information
antononcube committed Jul 31, 2024
1 parent 1e9a1b9 commit eac6281
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 1 deletion.
1 change: 1 addition & 0 deletions lib/Statistics/Distributions.rakumod
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ constant \ExponentialDistribution is export := Statistics::Distributions::Define
constant \GammaDistribution is export := Statistics::Distributions::Defined::Gamma;
constant \MixtureDistribution is export := Statistics::Distributions::Defined::Mixture;
constant \NormalDistribution is export := Statistics::Distributions::Defined::Normal;
constant \StudentTDistribution is export := Statistics::Distributions::Defined::StudentT;
constant \ProductDistribution is export := Statistics::Distributions::Defined::Product;
constant \UniformDistribution is export := Statistics::Distributions::Defined::Uniform;

Expand Down
18 changes: 18 additions & 0 deletions lib/Statistics/Distributions/Defined.rakumod
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,24 @@ class Product is Generic is export {
}
#= Product Distribution objects are created with lists of distribution objects.

#| Student t-distribution class
class StudentT is Generic is export {
has Numeric:D $.nu = 1;
#= Degrees of freedom
has Numeric:D $.mean = 0;
#= Location
has Numeric:D $.sd = 1;
#= Scale
submethod BUILD(:ν(:$!nu) = 1, :µ(:$!mean) = 0, :σ(:$!sd) = 1) {}
multi method new($nu) { self.bless(:$nu, mean => 0, sd => 1) }
multi method new($nu, $mean, $sd) { self.bless(:$nu, :$mean, :$sd) }
multi method generate(UInt:D :$size) {
student-t-dist($!nu, $!mean, $!sd, :$size);
}
}
#= A Student t-distribution object is specified with a positive degrees of freedom parameter (nu), location parameter (mu), and scale parameter (sigma).


#| Uniform distribution class
class Uniform is Generic is export {
has Numeric:D $.min = 0;
Expand Down
13 changes: 12 additions & 1 deletion lib/Statistics/Distributions/Utilities.rakumod
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,6 @@ sub product-dist(@dists, UInt:D :$size = 1) is export {
}

#------------------------------------------------------------

sub chi-squared-dist($nu, UInt:D :$size = 1) is export {
die "The first argument is expected to be a positive number." unless $nu > 0;

Expand All @@ -198,4 +197,16 @@ sub chi-squared-dist($nu, UInt:D :$size = 1) is export {
}

return @variates.List;
}

#------------------------------------------------------------
sub student-t-dist($nu, $mean, $sd, Int :$size) is export {
my @variates;
for ^$size -> $ {
my $z = normal-dist(0, 1);
my $v = chi-squared-dist($nu).head;
my $t = $mean + $sd * ($z / sqrt($v / $nu));
@variates.push($t);
}
return @variates;
}
11 changes: 11 additions & 0 deletions t/02-random-variate.rakutest
Original file line number Diff line number Diff line change
Expand Up @@ -125,4 +125,15 @@ subtest {
'ChiSquareDistribution gives numbers';
}

# 20
subtest {
ok random-variate(StudentTDistribution.new(ν => 5));
ok random-variate(StudentTDistribution.new(ν => 5, μ => 1, σ => 3));
dies-ok { random-variate(StudentTDistribution.new(ν => -5)) };
isa-ok random-variate(StudentTDistribution.new(3.3)), Numeric:D,
'No size argument gives a number for StudentTDistribution';
is random-variate(StudentTDistribution.new(20, 1, 3), 10).all ~~ Numeric:D, True,
'StudentTDistribution gives numbers with three arguments';
}

done-testing;

0 comments on commit eac6281

Please sign in to comment.