Skip to content

Commit

Permalink
Add Student's T distrubution CDF.
Browse files Browse the repository at this point in the history
  • Loading branch information
BrentSeidel committed Jun 14, 2024
1 parent b317a81 commit 35fe5cc
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 25 deletions.
14 changes: 14 additions & 0 deletions src/BBS-Numerical-Statistics.adb
Original file line number Diff line number Diff line change
Expand Up @@ -160,4 +160,18 @@ package body BBS.Numerical.Statistics is
return elem.Exp(part1 + part3 - part2);
end;
--
-- This uses the global deg_freedom so that it can be called as a function of
-- one variable by the integration routine.
--
function partial_studentT(x : f'Base) return f'Base is
begin
return studentT_pdf(x, deg_freedom);
end;
--
function studentT_cdf(a, b : F'Base; nu, steps : Positive) return F'Base is
tol : f'Base := 0.0001;
begin
deg_freedom := nu;
return integ.adapt_simpson(partial_studentT'Access, a, b, tol, steps);
end;
end;
5 changes: 4 additions & 1 deletion src/BBS-Numerical-Statistics.ads
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,15 @@ package BBS.Numerical.statistics is
--
function studentT_pdf(t : f'Base; nu : Positive) return f'Base;
--
function studentT_cdf(a, b : F'Base; nu, steps : Positive) return F'Base;
--
private
--
-- This is a bit of a hack to get a chi square function that can
-- This is a bit of a hack to get some function into a form that can
-- be integrated.
--
deg_freedom : Positive;
--
function partial_chi2(x : f'Base) return f'Base;
function partial_studentT(x : f'Base) return f'Base;
end;
51 changes: 27 additions & 24 deletions test/test_stats.adb
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ procedure test_stats is
(x => 5.0, y => 4.0));
res : linreg.simple_linreg_result;
val : real;
dof : Positive := 50;
sum : real;
dof : Positive := 20;
-- sum : real;
begin
Ada.Text_IO.Put_Line("Testing some of the numerical routines.");
res := linreg.simple_linear(data);
Expand All @@ -37,38 +37,41 @@ begin
float_io.Put(res.variance, 2, 3, 0);
Ada.Text_IO.New_Line;
Ada.Text_IO.Put_Line("Probability Distributions");
Ada.Text_IO.Put_Line(" Normal Chi^2");
Ada.Text_IO.Put_Line(" X PDF CDF PDF CDF");
Ada.Text_IO.Put_Line(" Normal Chi^2 Student's T");
Ada.Text_IO.Put_Line(" X PDF CDF PDF CDF PDF CDF");
for i in 0 .. 20 loop
val := real(i)*1.0;
Ada.Text_IO.Put(" ");
float_io.Put(val, 2, 2, 0);
Ada.Text_IO.Put(" ");
float_io.Put(stat.normal_pdf(val), 1, 5, 0);
float_io.Put(stat.normal_pdf(val), 1, 6, 3);
Ada.Text_IO.Put(" ");
float_io.Put(stat.normal_cdf(0.0, val, 20), 1, 5, 0);
float_io.Put(stat.normal_cdf(0.0, val, 20), 1, 6, 3);
Ada.Text_IO.Put(" ");
float_io.Put(stat.chi2_pdf(val, dof), 1, 5, 0);
float_io.Put(stat.chi2_pdf(val, dof), 1, 6, 3);
Ada.Text_IO.Put(" ");
float_io.Put(stat.chi2_cdf(0.0, val, dof, 20), 1, 5, 0);
float_io.Put(stat.chi2_cdf(0.0, val, dof, 20), 1, 6, 3);
Ada.Text_IO.Put(" ");
float_io.Put(stat.studentT_pdf(val, dof), 1, 6, 3);
Ada.Text_IO.Put(" ");
float_io.Put(stat.studentT_cdf(0.0, val, dof, 20), 1, 6, 3);
Ada.Text_IO.Put(" ");
float_io.Put(stat.studentT_pdf(val, dof), 1, 5, 0);
Ada.Text_IO.New_Line;
end loop;
Ada.Text_IO.New_Line;
for n in 0 .. 10 loop
Ada.Text_IO.put(" ");
float_io.put(real(n), 2, 0, 0);
Ada.Text_IO.Put("|");
sum := 0.0;
for k in 0 .. n loop
Ada.Text_IO.Put(" ");
val := funct.nChoosek(n, k);
float_io.Put(val, 4, 0, 0);
sum := sum + val;
end loop;
Ada.Text_IO.Put("| ");
float_io.Put(sum, 4, 0, 0);
Ada.Text_IO.New_Line;
end loop;
-- for n in 0 .. 10 loop
-- Ada.Text_IO.put(" ");
-- float_io.put(real(n), 2, 0, 0);
-- Ada.Text_IO.Put("|");
-- sum := 0.0;
-- for k in 0 .. n loop
-- Ada.Text_IO.Put(" ");
-- val := funct.nChoosek(n, k);
-- float_io.Put(val, 4, 0, 0);
-- sum := sum + val;
-- end loop;
-- Ada.Text_IO.Put("| ");
-- float_io.Put(sum, 4, 0, 0);
-- Ada.Text_IO.New_Line;
-- end loop;
end test_stats;

0 comments on commit 35fe5cc

Please sign in to comment.