From a9fa1c7fcdaf5fd3b79f8edea57c95718309bc4e Mon Sep 17 00:00:00 2001 From: pr0m1th3as Date: Mon, 26 Feb 2024 21:02:05 +0200 Subject: [PATCH] update for exponential distribution object --- INDEX | 1 + inst/dist_wrap/fitdist.m | 30 ++++++++++++++++++++++++++---- inst/dist_wrap/makedist.m | 11 +++++++++-- 3 files changed, 36 insertions(+), 6 deletions(-) diff --git a/INDEX b/INDEX index db4724b3..956ff879 100644 --- a/INDEX +++ b/INDEX @@ -68,6 +68,7 @@ Descriptive Statistics trimmean var Distribution Classes + ExponentialDistribution GammaDistribution GeneralizedExtremeValueDistribution GeneralizedParetoDistribution diff --git a/inst/dist_wrap/fitdist.m b/inst/dist_wrap/fitdist.m index dfaaf697..bea6e7ab 100644 --- a/inst/dist_wrap/fitdist.m +++ b/inst/dist_wrap/fitdist.m @@ -188,11 +188,16 @@ endif case "exponential" - warning ("fitdist: 'Exponential' distribution not supported yet."); if (isempty (groupvar)) - varargout{1} = []; + varargout{1} = ExponentialDistribution.fit (x, alpha, censor, freq); else - varargout{1} = []; + pd = ExponentialDistribution.fit ... + (x(g==1), alpha, censor(g==1), freq(g==1)); + for i = 2:groups + pd(i) = ExponentialDistribution.fit ... + (x(g==i), alpha, censor(g==i), freq(g==i)); + endfor + varargout{1} = pd; varargout{2} = gn; varargout{3} = gl; endif @@ -230,7 +235,8 @@ case {"generalizedpareto", "gp"} if (any (x - theta < 0)) - error ("fitdist: invalid THETA value for generalized Pareto distribution."); + error (strcat (["fitdist: invalid THETA value for generalized"], ... + [" Pareto distribution."])); endif if (isempty (groupvar)) varargout{1} = GeneralizedParetoDistribution.fit ... @@ -473,6 +479,22 @@ ## Test output %!test +%! x = exprnd (1, 100, 1); +%! pd = fitdist (x, "exponential"); +%! [muhat, muci] = expfit (x); +%! assert ([pd.mu], muhat); +%! assert (paramci (pd), muci); +%!test +%! x1 = exprnd (1, 100, 1); +%! x2 = exprnd (5, 100, 1); +%! pd = fitdist ([x1; x2], "exponential", "By", [ones(100,1); 2*ones(100,1)]); +%! [muhat, muci] = expfit (x1); +%! assert ([pd(1).mu], muhat); +%! assert (paramci (pd(1)), muci); +%! [phat, pci] = expfit (x2); +%! assert ([pd(2).mu], muhat); +%! assert (paramci (pd(2)), muci); +%!test %! x = gamrnd (1, 1, 100, 1); %! pd = fitdist (x, "Gamma"); %! [phat, pci] = gamfit (x); diff --git a/inst/dist_wrap/makedist.m b/inst/dist_wrap/makedist.m index cc4f8c0d..7256b691 100644 --- a/inst/dist_wrap/makedist.m +++ b/inst/dist_wrap/makedist.m @@ -136,7 +136,7 @@ pd = []; case "exponential" - mu = 0; + mu = 1; while (numel (varargin) > 0) switch (tolower (varargin{1})) case "mu" @@ -147,7 +147,7 @@ endswitch varargin([1:2]) = []; endwhile - pd = []; + pd = ExponentialDistribution (mu); case "gamma" a = 1; @@ -532,6 +532,13 @@ ## Test output %!test +%! pd = makedist ("exponential"); +%! assert (class (pd), "ExponentialDistribution"); +%! assert (pd.mu, 1); +%!test +%! pd = makedist ("exponential", "mu", 5); +%! assert (pd.mu, 5); +%!test %! pd = makedist ("gamma"); %! assert (class (pd), "GammaDistribution"); %! assert (pd.a, 1);