./PaxHeaders.3297/statistics-1.2.4 0000644 0000000 0000000 00000000132 12477120566 013463 x ustar 00 30 mtime=1425842550.061151696
30 atime=1425842549.945151694
30 ctime=1425842550.077151696
statistics-1.2.4/ 0000755 0001750 0001750 00000000000 12477120566 014560 5 ustar 00asnelt asnelt 0000000 0000000 statistics-1.2.4/PaxHeaders.3297/COPYING 0000644 0000000 0000000 00000000132 12477120565 014435 x ustar 00 30 mtime=1425842549.949151694
30 atime=1425842549.949151694
30 ctime=1425842550.077151696
statistics-1.2.4/COPYING 0000644 0001750 0001750 00000010450 12477120565 015612 0 ustar 00asnelt asnelt 0000000 0000000 inst/private/tbl_delim.m.m GPLv3+
inst/anderson_darling_cdf.m public domain
inst/anderson_darling_test.m public domain
inst/anovan.m GPLv3+
inst/betastat.m GPLv3+
inst/binostat.m GPLv3+
inst/boxplot.m GPLv3+
inst/caseread.m GPLv3+
inst/casewrite.m GPLv3+
inst/cdf.m GPLv3+
inst/chi2stat.m GPLv3+
inst/cl_multinom.m GPLv3+
inst/cmdscale.m GPLv3+
inst/combnk.m GPLv3+
inst/copulacdf.m GPLv3+
inst/copulapdf.m GPLv3+
inst/copularnd.m GPLv3+
inst/crossval.m GPLv3+
inst/dcov.m GPLv3+
inst/dendogram.m GPLv3+
inst/expstat.m GPLv3+
inst/ff2n.m public domain
inst/fstat.m GPLv3+
inst/fullfact.m public domain
inst/gamfit.m public domain
inst/gamlike.m public domain
inst/gamstat.m GPLv3+
inst/geomean.m GPLv3+
inst/geostat.m GPLv3+
inst/gevcdf.m GPLv3+
inst/gevfit_lmom.m GPLv3+
inst/gevfit.m GPLv3+
inst/gevinv.m GPLv3+
inst/gevlike.m GPLv3+
inst/gevpdf.m GPLv3+
inst/gevrnd.m GPLv3+
inst/gevstat.m GPLv3+
inst/harmmean.m GPLv3+
inst/hist3.m GPLv3+
inst/histfit.m GPLv3+
inst/hmmestimate.m GPLv3+
inst/hmmgenerate.m GPLv3+
inst/hmmviterbi.m GPLv3+
inst/iwishpdf.m GPLv3+
inst/iwishrnd.m GPLv3+
inst/hygestat.m GPLv3+
inst/jackknife.m GPLv3+
inst/jsucdf.m GPLv3+
inst/jsupdf.m GPLv3+
inst/kmeans.m GPLv3+
inst/linkage.m GPLv3+
inst/lognstat.m GPLv3+
inst/mad.m GPLv3+
inst/mnpdf.m GPLv3+
inst/mnrnd.m GPLv3+
inst/monotone_smooth.m GPLv3+
inst/mvncdf.m GPLv3+
inst/mvnpdf.m public domain
inst/mvnrnd.m GPLv3+
inst/mvtcdf.m GPLv3+
inst/mvtrnd.m GPLv3+
inst/nanmax.m GPLv3+
inst/nanmean.m GPLv3+
inst/nanmedian.m GPLv3+
inst/nanmin.m GPLv3+
inst/nanstd.m GPLv3+
inst/nansum.m GPLv3+
inst/nanvar.m.m GPLv3+
inst/nbinstat.m GPLv3+
inst/normalise_distribution.m GPLv3+
inst/normplot.m public domain
inst/normstat.m GPLv3+
inst/pcacov.m GPLv3+
inst/pcares.m GPLv3+
inst/pdist.m GPLv3+
inst/pdist2.m GPLv3+
inst/plsregress.m GPLv3+
inst/poisstat.m GPLv3+
inst/princomp.m public domain
inst/qrandn.m GPLv3+
inst/random.m GPLv3+
inst/randsample.m GPLv3+
inst/raylcdf.m GPLv3+
inst/raylinv.m GPLv3+
inst/raylpdf.m GPLv3+
inst/raylrnd.m GPLv3+
inst/raylstat.m GPLv3+
inst/regress_gp.m GPLv3+
inst/regress.m GPLv3+
inst/repanova.m.m GPLv3+
inst/runtest.m GPLv3+
inst/signtest.m GPLv3+
inst/squareform.m GPLv3+
inst/stepwisefit.m GPLv3+
inst/tabulate.m GPLv3+
inst/tblread.m GPLv3+
inst/tblwrite.m GPLv3+
inst/trimmean.m GPLv3+
inst/tstat.m GPLv3+
inst/ttest.m GPLv3+
inst/ttest2.m GPLv3+
inst/unidstat.m GPLv3+
inst/unifstat.m GPLv3+
inst/vartest.m GPLv3+
inst/vartest2.m GPLv3+
inst/vmpdf.m GPLv3+
inst/vmrnd.m GPLv3+
inst/wblstat.m GPLv3+
inst/wishpdf.m GPLv3+
inst/wishrnd.m GPLv3+
inst/ztest.m GPLv3+
statistics-1.2.4/PaxHeaders.3297/NEWS 0000644 0000000 0000000 00000000132 12477120565 014101 x ustar 00 30 mtime=1425842549.949151694
30 atime=1425842549.949151694
30 ctime=1425842550.077151696
statistics-1.2.4/NEWS 0000644 0001750 0001750 00000012110 12477120565 015251 0 ustar 00asnelt asnelt 0000000 0000000 Summary of important user-visible changes for statistics 1.2.4:
-------------------------------------------------------------------
** Made princomp work with nargout < 2.
** Renamed dendogram to dendrogram.
** Added isempty check to kmeans.
** Transposed output of hist3.
** Converted calculation in hmmviterbi to log space.
** Bug fixes for
stepwisefit wishrnd.
** Rewrite of cmdscale for improved compatibility.
** Fix in squareform for improved compatibility.
** New cvpartition class, with methods:
display repartition test training
** New sample data file fisheriris.txt for tests
** The following functions are new:
cdf crossval dcov pdist2 qrandn randsample signtest ttest ttest2
vartest vartest2 ztest
Summary of important user-visible changes for statistics 1.2.3:
-------------------------------------------------------------------
** Made sure that output of nanstd is real.
** Fixed second output of nanmax and nanmin.
** Corrected handle for outliers in boxplot.
** Bug fix and enhanced functionality for mvnrnd.
** The following functions are new:
wishrnd iwishrnd wishpdf iwishpdf cmdscale
Summary of important user-visible changes for statistics 1.2.2:
-------------------------------------------------------------------
** Fixed documentation of dendogram and hist3 to work with TexInfo 5.
Summary of important user-visible changes for statistics 1.2.1:
-------------------------------------------------------------------
** The following functions are new:
pcares pcacov runstest stepwisefit hist3
** dendogram now returns the leaf node numbers and order that the nodes were displayed in.
** New faster implementation of princomp.
Summary of important user-visible changes for statistics 1.2.0:
-------------------------------------------------------------------
** The following functions are new:
regress_gp dendogram plsregress
** New functions for the generalized extreme value (GEV) distribution:
gevcdf gevfit gevfit_lmom gevinv gevlike gevpdf gevrnd gevstat
** The interface of the following functions has been modified:
mvnrnd
** `kmeans' has been fixed to deal with clusters that contain only
one element.
** `normplot' has been fixed to avoid use of functions that have been
removed from Octave core. Also, the plot produced should now display some
aesthetic elements and appropriate legends.
** The help text of `mvtrnd' has been improved.
** Package is no longer autoloaded.
Summary of important user-visible changes for statistics 1.1.3:
-------------------------------------------------------------------
** The following functions are new in 1.1.3:
copularnd mvtrnd
** The functions mnpdf and mnrnd are now also usable for greater numbers
of categories for which the rows do not exactly sum to 1.
Summary of important user-visible changes for statistics 1.1.2:
-------------------------------------------------------------------
** The following functions are new in 1.1.2:
mnpdf mnrnd
** The package is now dependent on the io package (version 1.0.18 or
later) since the functions that it depended of from miscellaneous
package have been moved to io.
** The function `kmeans' now accepts the 'emptyaction' property with
the 'singleton' value. This allows for the kmeans algorithm to handle
empty cluster better. It also throws an error if the user does not
request an empty cluster handling, and there is an empty cluster.
Plus, the returned items are now a closer match to Matlab.
Summary of important user-visible changes for statistics 1.1.1:
-------------------------------------------------------------------
** The following functions are new in 1.1.1:
monotone_smooth kmeans jackknife
** Bug fixes on the functions:
normalise_distribution combnk
repanova
** The following functions were removed since equivalents are now
part of GNU octave core:
zscore
** boxplot.m now returns a structure with handles to the plot elemenets.
Summary of important user-visible changes for statistics 1.1.0:
-------------------------------------------------------------------
** IMPORTANT note about `fstat' shadowing core library function:
GNU octave's 3.2 release added a new function `fstat' to return
information of a file. Statistics' `fstat' computes F mean and
variance. Since MatLab's `fstat' is the equivalent to statistics'
`fstat' (not to core's `fstat'), and to avoid problems with the
statistics package, `fstat' has been deprecated in octave 3.4
and will be removed in Octave 3.8. In the mean time, please
ignore this warning when installing the package.
** The following functions are new in 1.1.0:
normalise_distribution repanova combnk
** The following functions were removed since equivalents are now
part of GNU octave core:
prctile
** The __tbl_delim__ function is now private.
** The function `boxplot' now accepts named arguments.
** Bug fixes on the functions:
harmmean nanmax nanmin regress
** Small improvements on help text.
statistics-1.2.4/PaxHeaders.3297/test 0000644 0000000 0000000 00000000132 12477120566 014305 x ustar 00 30 mtime=1425842550.065151696
30 atime=1425842550.061151696
30 ctime=1425842550.077151696
statistics-1.2.4/test/ 0000755 0001750 0001750 00000000000 12477120566 015537 5 ustar 00asnelt asnelt 0000000 0000000 statistics-1.2.4/test/PaxHeaders.3297/caseread.dat 0000644 0000000 0000000 00000000132 12477120566 016623 x ustar 00 30 mtime=1425842550.061151696
30 atime=1425842550.061151696
30 ctime=1425842550.077151696
statistics-1.2.4/test/caseread.dat 0000644 0001750 0001750 00000000012 12477120566 017771 0 ustar 00asnelt asnelt 0000000 0000000 a
bcd
ef statistics-1.2.4/test/PaxHeaders.3297/tblread-space.dat 0000644 0000000 0000000 00000000132 12477120566 017562 x ustar 00 30 mtime=1425842550.061151696
30 atime=1425842550.061151696
30 ctime=1425842550.077151696
statistics-1.2.4/test/tblread-space.dat 0000644 0001750 0001750 00000000024 12477120566 020733 0 ustar 00asnelt asnelt 0000000 0000000 a bc
de 1 2
f 3 4 statistics-1.2.4/test/PaxHeaders.3297/tblread-tab.dat 0000644 0000000 0000000 00000000132 12477120566 017235 x ustar 00 30 mtime=1425842550.065151696
30 atime=1425842550.065151696
30 ctime=1425842550.077151696
statistics-1.2.4/test/tblread-tab.dat 0000644 0001750 0001750 00000000070 12477120566 020407 0 ustar 00asnelt asnelt 0000000 0000000 a bc
de 1 2
f 3 4 statistics-1.2.4/PaxHeaders.3297/inst 0000644 0000000 0000000 00000000132 12477120566 014303 x ustar 00 30 mtime=1425842550.061151696
30 atime=1425842549.949151694
30 ctime=1425842550.077151696
statistics-1.2.4/inst/ 0000755 0001750 0001750 00000000000 12477120566 015535 5 ustar 00asnelt asnelt 0000000 0000000 statistics-1.2.4/inst/PaxHeaders.3297/squareform.m 0000644 0000000 0000000 00000000132 12477120566 016722 x ustar 00 30 mtime=1425842550.049151695
30 atime=1425842550.049151695
30 ctime=1425842550.077151696
statistics-1.2.4/inst/squareform.m 0000644 0001750 0001750 00000007533 12477120566 020107 0 ustar 00asnelt asnelt 0000000 0000000 ## Copyright (C) 2014 JD Walsh. Based on code (C) 2006, 2008 Bill Denney
##
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 3 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program; if not, see .
## -*- texinfo -*-
## @deftypefn {Function File} {@var{y} =} squareform (@var{x})
## @deftypefnx {Function File} {@var{y} =} squareform (@var{x}, @
## "tovector")
## @deftypefnx {Function File} {@var{y} =} squareform (@var{x}, @
## "tomatrix")
## Converts a vector from the @code{pdist} function into a distance matrix or
## a distance matrix back to vector form.
##
## If @var{x} is a vector, it must have
## @code{length(@var{x}) = @var{n} * (@var{n} - 1) / 2} for some integer
## @var{n}. The resulting matrix will be @var{n} by @var{n}.
##
## If @var{x} is a distance matrix, it must be square and the diagonal entries
## of @var{x} must all be zeros. @code{squareform} will generate a warning if
## @var{x} is not symmetric.
##
## The second argument is used to specify the output type in case there
## is a single element.
## @seealso{pdist}
## @end deftypefn
## Author: JD Walsh
## Created: 2014-11-09
## Description: Convert distance matrix from vector to square form and back
## Keywords: distance format
function y = squareform (x, method)
if ((nargin < 1) || (~isnumeric(x)))
usage (['squareform: must input distance matrix or vector; see help']);
endif
if (nargin < 2)
if (isscalar (x) || isvector (x))
method = 'tomatrix';
elseif (issquare (x))
method = 'tovector';
else
usage ('squareform: must input distance matrix or vector; see help');
endif
else
method = lower (method);
if (~any (strcmp ({'tovector' 'tomatrix'}, method)))
usage ('squareform: method must be either "tovector" or "tomatrix"');
endif
endif
if (strcmp ('tovector', method))
if (~issquare (x))
usage ('squareform: x is not a square matrix');
elseif (any (diag (x) ~= 0))
usage ('squareform: x is not a hollow matrix');
elseif (~issymmetric(x))
warning ('squareform:symmetric', ...
'squareform: x is not a symmetric matrix');
endif
sx = size (x, 1);
y = zeros (1, (sx - 1) * sx / 2);
idx = 1;
for i = 2 : sx
newidx = idx + sx - i;
y(1, idx:newidx) = x(i:sx, i-1);
idx = newidx + 1;
endfor
else
## we're converting to a matrix
## make sure that x is a column
x = x(:);
## the dimensions of y are the solution to the quadratic formula for:
## length (x) = (sy - 1) * (sy / 2)
sy = (1 + sqrt (1 + 8 * length (x))) / 2;
if (floor (sy) ~= sy)
usage ('squareform: incorrect vector size; see help');
else
y = zeros (sy);
for i = 1 : sy-1
step = sy - i;
y((sy-step+1):sy, i) = x(1:step);
x(1:step) = [];
endfor
y = y + y';
endif
endif
endfunction
## make sure that it can go both directions automatically
%!assert(squareform(1:6), [0 1 2 3;1 0 4 5;2 4 0 6;3 5 6 0])
%!assert(squareform(squareform(1:6)),[1:6])
%!assert(squareform([0 1 2 3;1 0 4 5;2 4 0 6;3 5 6 0]), [1:6])
## make sure that the command arguments force the correct behavior
## squareform(1, "tovector") correctly throws an error: invalid distance matrix
%!assert(squareform(1), [0 1;1 0])
%!assert(squareform(1, "tomatrix"), [0 1;1 0])
%!assert(squareform(0, "tovector"), zeros(1,0))
statistics-1.2.4/inst/PaxHeaders.3297/gevfit_lmom.m 0000644 0000000 0000000 00000000132 12477120565 017045 x ustar 00 30 mtime=1425842549.985151694
30 atime=1425842549.985151694
30 ctime=1425842550.077151696
statistics-1.2.4/inst/gevfit_lmom.m 0000644 0001750 0001750 00000006762 12477120565 020235 0 ustar 00asnelt asnelt 0000000 0000000 ## Copyright (C) 2012 Nir Krakauer
##
## This program is free software; you can redistribute it and/or modify it under
## the terms of the GNU General Public License as published by the Free Software
## Foundation; either version 3 of the License, or (at your option) any later
## version.
##
## This program is distributed in the hope that it will be useful, but WITHOUT
## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
## details.
##
## You should have received a copy of the GNU General Public License along with
## this program; if not, see .
## -*- texinfo -*-
## @deftypefn {Function File} {@var{paramhat}, @var{paramci} =} gevfit_lmom (@var{data})
## Find an estimator (@var{paramhat}) of the generalized extreme value (GEV) distribution fitting @var{data} using the method of L-moments.
##
## @subheading Arguments
##
## @itemize @bullet
## @item
## @var{data} is the vector of given values.
## @end itemize
##
## @subheading Return values
##
## @itemize @bullet
## @item
## @var{parmhat} is the 3-parameter maximum-likelihood parameter vector [@var{k}; @var{sigma}; @var{mu}], where @var{k} is the shape parameter of the GEV distribution, @var{sigma} is the scale parameter of the GEV distribution, and @var{mu} is the location parameter of the GEV distribution.
## @item
## @var{paramci} has the approximate 95% confidence intervals of the parameter values (currently not implemented).
##
## @end itemize
##
## @subheading Examples
##
## @example
## @group
## data = gevrnd (0.1, 1, 0, 100, 1);
## [pfit, pci] = gevfit_lmom (data);
## p1 = gevcdf (data,pfit(1),pfit(2),pfit(3));
## [f, x] = ecdf (data);
## plot(data, p1, 's', x, f)
## @end group
## @end example
## @seealso{gevfit}
## @subheading References
##
## @enumerate
## @item
## Ailliot, P.; Thompson, C. & Thomson, P. Mixed methods for fitting the GEV distribution, Water Resources Research, 2011, 47, W05551
##
## @end enumerate
## @end deftypefn
## Author: Nir Krakauer
## Description: L-moments parameter estimation for the generalized extreme value distribution
function [paramhat, paramci] = gevfit_lmom (data)
# Check arguments
if (nargin < 1)
print_usage;
endif
# find the L-moments
data = sort (data(:))';
n = numel(data);
L1 = mean(data);
L2 = sum(data .* (2*(1:n) - n - 1)) / (2*nchoosek(n, 2)); # or mean(triu(data' - data, 1, 'pack')) / 2;
b = bincoeff((1:n) - 1, 2);
L3 = sum(data .* (b - 2 * ((1:n) - 1) .* (n - (1:n)) + fliplr(b))) / (3*nchoosek(n, 3));
#match the moments to the GEV distribution
#first find k based on L3/L2
f = @(k) (L3/L2 + 3)/2 - limdiv((1 - 3^(k)), (1 - 2^(k)));
k = fzero(f, 0);
#next find sigma and mu given k
if abs(k) < 1E-8
sigma = L2 / log(2);
eg = 0.57721566490153286; %Euler-Mascheroni constant
mu = L1 - sigma * eg;
else
sigma = -k*L2 / (gamma(1 - k) * (1 - 2^(k)));
mu = L1 - sigma * ((gamma(1 - k) - 1) / k);
endif
paramhat = [k; sigma; mu];
if nargout > 1
paramci = NaN;
endif
endfunction
#internal function to accurately evaluate (1 - 3^k)/(1 - 2^k) in the limit as k --> 0
function c = limdiv(a, b)
# c = ifelse (abs(b) < 1E-8, log(3)/log(2), a ./ b);
if abs(b) < 1E-8
c = log(3)/log(2);
else
c = a / b;
endif
endfunction
%!test
%! data = 1:50;
%! [pfit, pci] = gevfit_lmom (data);
%! expected_p = [-0.28 15.01 20.22]';
%! assert (pfit, expected_p, 0.1);
statistics-1.2.4/inst/PaxHeaders.3297/anovan.m 0000644 0000000 0000000 00000000132 12477120565 016017 x ustar 00 30 mtime=1425842549.953151694
30 atime=1425842549.953151694
30 ctime=1425842550.077151696
statistics-1.2.4/inst/anovan.m 0000644 0001750 0001750 00000027376 12477120565 017213 0 ustar 00asnelt asnelt 0000000 0000000 ## Copyright (C) 2003-2005 Andy Adler
##
## This program is free software; you can redistribute it and/or modify it under
## the terms of the GNU General Public License as published by the Free Software
## Foundation; either version 3 of the License, or (at your option) any later
## version.
##
## This program is distributed in the hope that it will be useful, but WITHOUT
## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
## details.
##
## You should have received a copy of the GNU General Public License along with
## this program; if not, see .
## -*- texinfo -*-
## @deftypefn {Function File} {[@var{pval}, @var{f}, @var{df_b}, @var{df_e}] =} anovan (@var{data}, @var{grps})
## @deftypefnx {Function File} {[@var{pval}, @var{f}, @var{df_b}, @var{df_e}] =} anovan (@var{data}, @var{grps}, 'param1', @var{value1})
## Perform a multi-way analysis of variance (ANOVA). The goal is to test
## whether the population means of data taken from @var{k} different
## groups are all equal.
##
## Data is a single vector @var{data} with groups specified by
## a corresponding matrix of group labels @var{grps}, where @var{grps}
## has the same number of rows as @var{data}. For example, if
## @var{data} = [1.1;1.2]; @var{grps}= [1,2,1; 1,5,2];
## then data point 1.1 was measured under conditions 1,2,1 and
## data point 1.2 was measured under conditions 1,5,2.
## Note that groups do not need to be sequentially numbered.
##
## By default, a 'linear' model is used, computing the N main effects
## with no interactions. this may be modified by param 'model'
##
## p= anovan(data,groups, 'model', modeltype)
## - modeltype = 'linear': compute N main effects
## - modeltype = 'interaction': compute N effects and
## N*(N-1) two-factor interactions
## - modeltype = 'full': compute interactions at all levels
##
## Under the null of constant means, the statistic @var{f} follows an F
## distribution with @var{df_b} and @var{df_e} degrees of freedom.
##
## The p-value (1 minus the CDF of this distribution at @var{f}) is
## returned in @var{pval}.
##
## If no output argument is given, the standard one-way ANOVA table is
## printed.
##
## BUG: DFE is incorrect for modeltypes != full
## @end deftypefn
## Author: Andy Adler
## Based on code by: KH
## $Id$
##
## TESTING RESULTS:
## 1. ANOVA ACCURACY: www.itl.nist.gov/div898/strd/anova/anova.html
## Passes 'easy' test. Comes close on 'Average'. Fails 'Higher'.
## This could be fixed with higher precision arithmetic
## 2. Matlab anova2 test
## www.mathworks.com/access/helpdesk/help/toolbox/stats/anova2.html
## % From web site:
## popcorn= [ 5.5 4.5 3.5; 5.5 4.5 4.0; 6.0 4.0 3.0;
## 6.5 5.0 4.0; 7.0 5.5 5.0; 7.0 5.0 4.5];
## % Define groups so reps = 3
## groups = [ 1 1;1 2;1 3;1 1;1 2;1 3;1 1;1 2;1 3;
## 2 1;2 2;2 3;2 1;2 2;2 3;2 1;2 2;2 3 ];
## anovan( vec(popcorn'), groups, 'model', 'full')
## % Results same as Matlab output
## 3. Matlab anovan test
## www.mathworks.com/access/helpdesk/help/toolbox/stats/anovan.html
## % From web site
## y = [52.7 57.5 45.9 44.5 53.0 57.0 45.9 44.0]';
## g1 = [1 2 1 2 1 2 1 2];
## g2 = {'hi';'hi';'lo';'lo';'hi';'hi';'lo';'lo'};
## g3 = {'may'; 'may'; 'may'; 'may'; 'june'; 'june'; 'june'; 'june'};
## anovan( y', [g1',g2',g3'])
## % Fails because we always do interactions
function [PVAL, FSTAT, DF_B, DFE] = anovan (data, grps, varargin)
if nargin <= 1
usage ("anovan (data, grps)");
end
# test supplied parameters
modeltype= 'linear';
for idx= 3:2:nargin
param= varargin{idx-2};
value= varargin{idx-1};
if strcmp(param, 'model')
modeltype= value;
# elseif strcmp(param # add other parameters here
else
error(sprintf('parameter %s is not supported', param));
end
end
if ~isvector (data)
error ("anova: for `anova (data, grps)', data must be a vector");
endif
nd = size (grps,1); # number of data points
nw = size (grps,2); # number of anova "ways"
if (~ isvector (data) || (length(data) ~= nd))
error ("anova: grps must be a matrix of the same number of rows as data");
endif
[g,grp_map] = relabel_groups (grps);
if strcmp(modeltype, 'linear')
max_interact = 1;
elseif strcmp(modeltype,'interaction')
max_interact = 2;
elseif strcmp(modeltype,'full')
max_interact = rows(grps);
else
error(sprintf('modeltype %s is not supported', modeltype));
end
ng = length(grp_map);
int_tbl = interact_tbl (nw, ng, max_interact );
[gn, gs, gss] = raw_sums(data, g, ng, int_tbl);
stats_tbl = int_tbl(2:size(int_tbl,1),:)>0;
nstats= size(stats_tbl,1);
stats= zeros( nstats+1, 5); # SS, DF, MS, F, p
for i= 1:nstats
[SS, DF, MS]= factor_sums( gn, gs, gss, stats_tbl(i,:), ng, nw);
stats(i,1:3)= [SS, DF, MS];
end
# The Mean squared error is the data - avg for each possible measurement
# This calculation doesn't work unless there is replication for all grps
# SSE= sum( gss(sel) ) - sum( gs(sel).^2 ./ gn(sel) );
SST= gss(1) - gs(1)^2/gn(1);
SSE= SST - sum(stats(:,1));
sel = select_pat( ones(1,nw), ng, nw); %incorrect for modeltypes != full
DFE= sum( (gn(sel)-1).*(gn(sel)>0) );
MSE= SSE/DFE;
stats(nstats+1,1:3)= [SSE, DFE, MSE];
for i= 1:nstats
MS= stats(i,3);
DF= stats(i,2);
F= MS/MSE;
pval = 1 - fcdf (F, DF, DFE);
stats(i,4:5)= [F, pval];
end
if nargout==0;
printout( stats, stats_tbl );
else
PVAL= stats(1:nstats,5);
FSTAT=stats(1:nstats,4);
DF_B= stats(1:nstats,2);
DF_E= DFE;
end
endfunction
# relabel groups to a mapping from 1 to ng
# Input
# grps input grouping
# Output
# g relabelled grouping
# grp_map map from output to input grouping
function [g,grp_map] = relabel_groups(grps)
grp_vec= vec(grps);
s= sort (grp_vec);
uniq = 1+[0;find(diff(s))];
# mapping from new grps to old groups
grp_map = s(uniq);
# create new group g
ngroups= length(uniq);
g= zeros(size(grp_vec));
for i = 1:ngroups
g( find( grp_vec== grp_map(i) ) ) = i;
end
g= reshape(g, size(grps));
endfunction
# Create interaction table
#
# Input:
# nw number of "ways"
# ng number of ANOVA groups
# max_interact maximum number of interactions to consider
# default is nw
function int_tbl =interact_tbl(nw, ng, max_interact)
combin= 2^nw;
inter_tbl= zeros( combin, nw);
idx= (0:combin-1)';
for i=1:nw;
inter_tbl(:,i) = ( rem(idx,2^i) >= 2^(i-1) );
end
# find elements with more than max_interact 1's
idx = ( sum(inter_tbl',1) > max_interact );
inter_tbl(idx,:) =[];
combin= size(inter_tbl,1); # update value
#scale inter_tbl
# use ng+1 to map combinations of groups to integers
# this would be lots easier with a hash data structure
int_tbl = inter_tbl .* (ones(combin,1) * (ng+1).^(0:nw-1) );
endfunction
# Calculate sums for each combination
#
# Input:
# g relabelled grouping matrix
# ng number of ANOVA groups
# max_interact
#
# Output (virtual (ng+1)x(nw) matrices):
# gn number of data sums in each group
# gs sum of data in each group
# gss sumsqr of data in each group
function [gn, gs, gss] = raw_sums(data, g, ng, int_tbl);
nw= size(g,2);
ndata= size(g,1);
gn= gs= gss= zeros((ng+1)^nw, 1);
for i=1:ndata
# need offset by one for indexing
datapt= data(i);
idx = 1+ int_tbl*g(i,:)';
gn(idx) +=1;
gs(idx) +=datapt;
gss(idx) +=datapt^2;
end
endfunction
# Calcualte the various factor sums
# Input:
# gn number of data sums in each group
# gs sum of data in each group
# gss sumsqr of data in each group
# select binary vector of factor for this "way"?
# ng number of ANOVA groups
# nw number of ways
function [SS,DF]= raw_factor_sums( gn, gs, gss, select, ng, nw);
sel= select_pat( select, ng, nw);
ss_raw= gs(sel).^2 ./ gn(sel);
SS= sum( ss_raw( ~isnan(ss_raw) ));
if length(find(select>0))==1
DF= sum(gn(sel)>0)-1;
else
DF= 1; #this isn't the real DF, but needed to multiply
end
endfunction
function [SS, DF, MS]= factor_sums( gn, gs, gss, select, ng, nw);
SS=0;
DF=1;
ff = find(select);
lff= length(ff);
# zero terms added, one term subtracted, two added, etc
for i= 0:2^lff-1
remove= find( rem( floor( i * 2.^(-lff+1:0) ), 2) );
sel1= select;
if ~isempty(remove)
sel1( ff( remove ) )=0;
end
[raw_sum,raw_df]= raw_factor_sums(gn,gs,gss,sel1,ng,nw);
add_sub= (-1)^length(remove);
SS+= add_sub*raw_sum;
DF*= raw_df;
end
MS= SS/DF;
endfunction
# Calcualte the various factor sums
# Input:
# select binary vector of factor for this "way"?
# ng number of ANOVA groups
# nw number of ways
function sel= select_pat( select, ng, nw);
# if select(i) is zero, remove nonzeros
# if select(i) is zero, remove zero terms for i
field=[];
if length(select) ~= nw;
error("length of select must be = nw");
end
ng1= ng+1;
if isempty(field)
# expand 0:(ng+1)^nw in base ng+1
field= (0:(ng1)^nw-1)'* ng1.^(-nw+1:0);
field= rem( floor( field), ng1);
# select zero or non-zero elements
field= field>0;
end
sel= find( all( field == ones(ng1^nw,1)*select(:)', 2) );
endfunction
function printout( stats, stats_tbl );
nw= size( stats_tbl,2);
[jnk,order]= sort( sum(stats_tbl,2) );
printf('\n%d-way ANOVA Table (Factors A%s):\n\n', nw, ...
sprintf(',%c',toascii('A')+(1:nw-1)) );
printf('Source of Variation Sum Sqr df MeanSS Fval p-value\n');
printf('*********************************************************************\n');
printf('Error %10.2f %4d %10.2f\n', stats( size(stats,1),1:3));
for i= order(:)'
str= sprintf(' %c x',toascii('A')+find(stats_tbl(i,:)>0)-1 );
str= str(1:length(str)-2); # remove x
printf('Factor %15s %10.2f %4d %10.2f %7.3f %7.6f\n', ...
str, stats(i,:) );
end
printf('\n');
endfunction
#{
# Test Data from http://maths.sci.shu.ac.uk/distance/stats/14.shtml
data=[7 9 9 8 12 10 ...
9 8 10 11 13 13 ...
9 10 10 12 10 12]';
grp = [1,1; 1,1; 1,2; 1,2; 1,3; 1,3;
2,1; 2,1; 2,2; 2,2; 2,3; 2,3;
3,1; 3,1; 3,2; 3,2; 3,3; 3,3];
data=[7 9 9 8 12 10 9 8 ...
9 8 10 11 13 13 10 11 ...
9 10 10 12 10 12 10 12]';
grp = [1,4; 1,4; 1,5; 1,5; 1,6; 1,6; 1,7; 1,7;
2,4; 2,4; 2,5; 2,5; 2,6; 2,6; 2,7; 2,7;
3,4; 3,4; 3,5; 3,5; 3,6; 3,6; 3,7; 3,7];
# Test Data from http://maths.sci.shu.ac.uk/distance/stats/9.shtml
data=[9.5 11.1 11.9 12.8 ...
10.9 10.0 11.0 11.9 ...
11.2 10.4 10.8 13.4]';
grp= [1:4,1:4,1:4]';
# Test Data from http://maths.sci.shu.ac.uk/distance/stats/13.shtml
data=[7.56 9.68 11.65 ...
9.98 9.69 10.69 ...
7.23 10.49 11.77 ...
8.22 8.55 10.72 ...
7.59 8.30 12.36]';
grp = [1,1;1,2;1,3;
2,1;2,2;2,3;
3,1;3,2;3,3;
4,1;4,2;4,3;
5,1;5,2;5,3];
# Test Data from www.mathworks.com/
# access/helpdesk/help/toolbox/stats/linear10.shtml
data=[23 27 43 41 15 17 3 9 20 63 55 90];
grp= [ 1 1 1 1 2 2 2 2 3 3 3 3;
1 1 2 2 1 1 2 2 1 1 2 2]';
#}
statistics-1.2.4/inst/PaxHeaders.3297/gevlike.m 0000644 0000000 0000000 00000000132 12477120565 016163 x ustar 00 30 mtime=1425842549.989151695
30 atime=1425842549.989151695
30 ctime=1425842550.077151696
statistics-1.2.4/inst/gevlike.m 0000644 0001750 0001750 00000027220 12477120565 017343 0 ustar 00asnelt asnelt 0000000 0000000 ## Copyright (C) 2012 Nir Krakauer
##
## This program is free software; you can redistribute it and/or modify it under
## the terms of the GNU General Public License as published by the Free Software
## Foundation; either version 3 of the License, or (at your option) any later
## version.
##
## This program is distributed in the hope that it will be useful, but WITHOUT
## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
## details.
##
## You should have received a copy of the GNU General Public License along with
## this program; if not, see .
## -*- texinfo -*-
## @deftypefn {Function File} {@var{nlogL}, @var{Grad}, @var{ACOV} =} gevlike (@var{params}, @var{data})
## Compute the negative log-likelihood of data under the generalized extreme value (GEV) distribution with given parameter values.
##
## @subheading Arguments
##
## @itemize @bullet
## @item
## @var{params} is the 3-parameter vector [@var{k}, @var{sigma}, @var{mu}], where @var{k} is the shape parameter of the GEV distribution, @var{sigma} is the scale parameter of the GEV distribution, and @var{mu} is the location parameter of the GEV distribution.
## @item
## @var{data} is the vector of given values.
##
## @end itemize
##
## @subheading Return values
##
## @itemize @bullet
## @item
## @var{nlogL} is the negative log-likelihood.
## @item
## @var{Grad} is the 3 by 1 gradient vector (first derivative of the negative log likelihood with respect to the parameter values)
## @item
## @var{ACOV} is the 3 by 3 Fisher information matrix (second derivative of the negative log likelihood with respect to the parameter values)
##
## @end itemize
##
## @subheading Examples
##
## @example
## @group
## x = -5:-1;
## k = -0.2;
## sigma = 0.3;
## mu = 0.5;
## [L, ~, C] = gevlike ([k sigma mu], x);
## @end group
## @end example
##
## @subheading References
##
## @enumerate
## @item
## Rolf-Dieter Reiss and Michael Thomas. @cite{Statistical Analysis of Extreme Values with Applications to Insurance, Finance, Hydrology and Other Fields}. Chapter 1, pages 16-17, Springer, 2007.
##
## @end enumerate
## @seealso{gevcdf, gevfit, gevinv, gevpdf, gevrnd, gevstat}
## @end deftypefn
## Author: Nir Krakauer
## Description: Negative log-likelihood for the generalized extreme value distribution
function [nlogL, Grad, ACOV] = gevlike (params, data)
# Check arguments
if (nargin != 2)
print_usage;
endif
k = params(1);
sigma = params(2);
mu = params(3);
#calculate negative log likelihood
[nll, k_terms] = gevnll (data, k, sigma, mu);
nlogL = sum(nll(:));
#optionally calculate the first and second derivatives of the negative log likelihood with respect to parameters
if nargout > 1
[Grad, kk_terms] = gevgrad (data, k, sigma, mu, k_terms);
if nargout > 2
ACOV = gevfim (data, k, sigma, mu, k_terms, kk_terms);
endif
endif
endfunction
function [nlogL, k_terms] = gevnll (x, k, sigma, mu)
#internal function to calculate negative log likelihood for gevlike
#no input checking done
k_terms = [];
a = (x - mu) ./ sigma;
if all(k == 0)
nlogL = exp(-a) + a + log(sigma);
else
aa = k .* a;
if min(abs(aa)) < 1E-3 && max(abs(aa)) < 0.5 #use a series expansion to find the log likelihood more accurately when k is small
k_terms = 1; sgn = 1; i = 0;
while 1
sgn = -sgn; i++;
newterm = (sgn / (i + 1)) * (aa .^ i);
k_terms = k_terms + newterm;
if max(abs(newterm)) <= eps
break
endif
endwhile
nlogL = exp(-a .* k_terms) + a .* (k + 1) .* k_terms + log(sigma);
else
b = 1 + aa;
nlogL = b .^ (-1 ./ k) + (1 + 1 ./ k) .* log(b) + log(sigma);
nlogL(b <= 0) = Inf;
endif
endif
endfunction
function [G, kk_terms] = gevgrad (x, k, sigma, mu, k_terms)
#calculate the gradient of the negative log likelihood of data x with respect to the parameters of the generalized extreme value distribution for gevlike
#no input checking done
kk_terms = [];
G = ones(3, 1);
if k == 0 ##use the expressions for first derivatives that are the limits as k --> 0
a = (x - mu) ./ sigma;
f = exp(-a) - 1;
#k
#g = -(2 * x .* (mu .* (1 - f) - sigma .* f) + 2 .* sigma .* mu .* f + (x.^2 + mu.^2).*(f - 1)) ./ (2 * f .* sigma .^ 2);
g = a .* (1 + a .* f / 2);
G(1) = sum(g(:));
#sigma
g = (a .* f + 1) ./ sigma;
G(2) = sum(g(:));
#mu
g = f ./ sigma;
G(3) = sum(g(:));
return
endif
a = (x - mu) ./ sigma;
b = 1 + k .* a;
if any (b <= 0)
G(:) = 0; #negative log likelihood is locally infinite
return
endif
#k
c = log(b);
d = 1 ./ k + 1;
if nargin > 4 && ~isempty(k_terms) #use a series expansion to find the gradient more accurately when k is small
aa = k .* a;
f = exp(-a .* k_terms);
kk_terms = 0.5; sgn = 1; i = 0;
while 1
sgn = -sgn; i++;
newterm = (sgn * (i + 1) / (i + 2)) * (aa .^ i);
kk_terms = kk_terms + newterm;
if max(abs(newterm)) <= eps
break
endif
endwhile
g = a .* ((a .* kk_terms) .* (f - 1 - k) + k_terms);
else
g = (c ./ k - a ./ b) ./ (k .* b .^ (1/k)) - c ./ (k .^ 2) + a .* d ./ b;
endif
%keyboard
G(1) = sum(g(:));
#sigma
if nargin > 4 && ~isempty(k_terms) #use a series expansion to find the gradient more accurately when k is small
g = (1 - a .* (a .* k .* kk_terms - k_terms) .* (f - k - 1)) ./ sigma;
else
#g = (a .* b .^ (-d) - d .* k .* a ./ b + 1) ./ sigma;
g = (a .* b .^ (-d) - (k + 1) .* a ./ b + 1) ./ sigma;
endif
G(2) = sum(g(:));
#mu
if nargin > 4 && ~isempty(k_terms) #use a series expansion to find the gradient more accurately when k is small
g = -(a .* k .* kk_terms - k_terms) .* (f - k - 1) ./ sigma;
else
#g = (b .^ (-d) - d .* k ./ b) ./ sigma;
g = (b .^ (-d) - (k + 1) ./ b) ./ sigma;
end
G(3) = sum(g(:));
endfunction
function ACOV = gevfim (x, k, sigma, mu, k_terms, kk_terms)
#internal function to calculate the Fisher information matrix for gevlike
#no input checking done
#find the various second derivatives (used Maxima to help find the expressions)
ACOV = ones(3);
if k == 0 ##use the expressions for second derivatives that are the limits as k --> 0
#k, k
a = (x - mu) ./ sigma;
f = exp(-a);
#der = (x .* (24 * mu .^ 2 .* sigma .* (f - 1) + 24 * mu .* sigma .^ 2 .* f - 12 * mu .^ 3) + x .^ 3 .* (8 * sigma .* (f - 1) - 12*mu) + x .^ 2 .* (-12 * sigma .^ 2 .* f + 24 * mu .* sigma .* (1 - f) + 18 * mu .^ 2) - 12 * mu .^ 2 .* sigma .^ 2 .* f + 8 * mu .^ 3 .* sigma .* (1 - f) + 3 * (x .^ 4 + mu .^ 4)) ./ (12 .* f .* sigma .^ 4);
der = (a .^ 2) .* (a .* (a/4 - 2/3) .* f + 2/3 * a - 1);
ACOV(1, 1) = sum(der(:));
#sigma, sigma
der = (sigma .^ -2) .* (a .* ((a - 2) .* f + 2) - 1);
ACOV(2, 2) = sum(der(:));
#mu, mu
der = (sigma .^ -2) .* f;
ACOV(3, 3) = sum(der(:));
#k, sigma
#der = (x .^2 .* (2*sigma .* (f - 1) - 3*mu) + x .* (-2 * sigma .^ 2 .* f + 4 * mu .* sigma .* (1 - f) + 3 .* mu .^ 2) + 2 * mu .^ 2 .* sigma .* (f - 1) + 2 * mu * sigma .^ 2 * f + x .^ 3 - mu .^ 3) ./ (2 .* f .* sigma .^ 4);
der = (-a ./ sigma) .* (a .* (1 - a/2) .* f - a + 1);
ACOV(1, 2) = ACOV(2, 1) = sum(der(:));
#k, mu
#der = (x .* (2*sigma .* (f - 1) - 2*mu) - 2 * f .* sigma .^ 2 + 2 .* mu .* sigma .* (1 - f) + x .^ 2 + mu .^ 2)./ (2 .* f .* sigma .^ 3);
der = (-1 ./ sigma) .* (a .* (1 - a/2) .* f - a + 1);
ACOV(1, 3) = ACOV(3, 1) = sum(der(:));
#sigma, mu
der = (1 + (a - 1) .* f) ./ (sigma .^ 2);
ACOV(2, 3) = ACOV(3, 2) = sum(der(:));
return
endif
#general case
z = 1 + k .* (x - mu) ./ sigma;
#k, k
a = (x - mu) ./ sigma;
b = k .* a + 1;
c = log(b);
d = 1 ./ k + 1;
if nargin > 5 && ~isempty(kk_terms) #use a series expansion to find the derivatives more accurately when k is small
aa = k .* a;
f = exp(-a .* k_terms);
kkk_terms = 2/3; sgn = 1; i = 0;
while 1
sgn = -sgn; i++;
newterm = (sgn * (i + 1) * (i + 2) / (i + 3)) * (aa .^ i);
kkk_terms = kkk_terms + newterm;
if max(abs(newterm)) <= eps
break
endif
endwhile
der = (a .^ 2) .* (a .* (a .* kk_terms .^ 2 - kkk_terms) .* f + a .* (1 + k) .* kkk_terms - 2 * kk_terms);
else
der = ((((c ./ k.^2) - (a ./ (k .* b))) .^ 2) ./ (b .^ (1 ./ k))) + ...
((-2*c ./ k.^3) + (2*a ./ (k.^2 .* b)) + ((a ./ b) .^ 2 ./ k)) ./ (b .^ (1 ./ k)) + ...
2*c ./ k.^3 - ...
(2*a ./ (k.^2 .* b)) - (d .* (a ./ b) .^ 2);
endif
der(z <= 0) = 0; %no probability mass in this region
ACOV(1, 1) = sum(der(:));
#sigma, sigma
if nargin > 5 && ~isempty(kk_terms) #use a series expansion to find the derivatives more accurately when k is small
der = ((-2*a .* k_terms + 4 * a .^ 2 .* k .* kk_terms - a .^ 3 .* (k .^ 2) .* kkk_terms) .* (f - k - 1) + f .* ((a .* (k_terms - a .* k .* kk_terms)) .^ 2) - 1) ./ (sigma .^ 2);
else
der = (sigma .^ -2) .* (...
-2*a .* b .^ (-d) + ...
d .* k .* a .^ 2 .* (b .^ (-d-1)) + ...
2 .* d .* k .* a ./ b - ...
d .* (k .* a ./ b) .^ 2 - 1);
end
der(z <= 0) = 0; %no probability mass in this region
ACOV(2, 2) = sum(der(:));
#mu, mu
if nargin > 5 && ~isempty(kk_terms) #use a series expansion to find the derivatives involving k more accurately when k is small
der = (f .* (a .* k .* kk_terms - k_terms) .^ 2 - a .* k .^ 2 .* kkk_terms .* (f - k - 1)) ./ (sigma .^ 2);
else
der = (d .* (sigma .^ -2)) .* (...
k .* (b .^ (-d-1)) - ...
(k ./ b) .^ 2);
endif
der(z <= 0) = 0; %no probability mass in this region
ACOV(3, 3) = sum(der(:));
#k, mu
if nargin > 5 && ~isempty(kk_terms) #use a series expansion to find the derivatives involving k more accurately when k is small
der = 2 * a .* kk_terms .* (f - 1 - k) - a .^ 2 .* k_terms .* kk_terms .* f + k_terms; #k, a second derivative
der = -der ./ sigma;
else
der = ( (b .^ (-d)) .* (c ./ k - a ./ b) ./ k - ...
a .* (b .^ (-d-1)) + ...
((1 ./ k) - d) ./ b +
a .* k .* d ./ (b .^ 2)) ./ sigma;
endif
der(z <= 0) = 0; %no probability mass in this region
ACOV(1, 3) = ACOV(3, 1) = sum(der(:));
#k, sigma
der = a .* der;
der(z <= 0) = 0; %no probability mass in this region
ACOV(1, 2) = ACOV(2, 1) = sum(der(:));
#sigma, mu
if nargin > 5 && ~isempty(kk_terms) #use a series expansion to find the derivatives involving k more accurately when k is small
der = ((-k_terms + 3 * a .* k .* kk_terms - (a .* k) .^ 2 .* kkk_terms) .* (f - k - 1) + a .* (k_terms - a .* k .* kk_terms) .^ 2 .* f) ./ (sigma .^ 2);
else
der = ( -(b .^ (-d)) + ...
a .* k .* d .* (b .^ (-d-1)) + ...
(d .* k ./ b) - a .* (k./b).^2 .* d) ./ (sigma .^ 2);
end
der(z <= 0) = 0; %no probability mass in this region
ACOV(2, 3) = ACOV(3, 2) = sum(der(:));
endfunction
%!test
%! x = 1;
%! k = 0.2;
%! sigma = 0.3;
%! mu = 0.5;
%! [L, D, C] = gevlike ([k sigma mu], x);
%! expected_L = 0.75942;
%! expected_D = [0.53150; -0.67790; -2.40674];
%! expected_C = [-0.12547 1.77884 1.06731; 1.77884 16.40761 8.48877; 1.06731 8.48877 0.27979];
%! assert (L, expected_L, 0.001);
%! assert (D, expected_D, 0.001);
%! assert (C, expected_C, 0.001);
%!test
%! x = 1;
%! k = 0;
%! sigma = 0.3;
%! mu = 0.5;
%! [L, D, C] = gevlike ([k sigma mu], x);
%! expected_L = 0.65157;
%! expected_D = [0.54011; -1.17291; -2.70375];
%! expected_C = [0.090036 3.41229 2.047337; 3.412229 24.760027 12.510190; 2.047337 12.510190 2.098618];
%! assert (L, expected_L, 0.001);
%! assert (D, expected_D, 0.001);
%! assert (C, expected_C, 0.001);
%!test
%! x = -5:-1;
%! k = -0.2;
%! sigma = 0.3;
%! mu = 0.5;
%! [L, D, C] = gevlike ([k sigma mu], x);
%! expected_L = 3786.4;
%! expected_D = [6.4511e+04; -4.8194e+04; 3.0633e+03];
%! expected_C = -[-1.4937e+06 1.0083e+06 -6.1837e+04; 1.0083e+06 -8.1138e+05 4.0917e+04; -6.1837e+04 4.0917e+04 -2.0422e+03];
%! assert (L, expected_L, -0.001);
%! assert (D, expected_D, -0.001);
%! assert (C, expected_C, -0.001);
statistics-1.2.4/inst/PaxHeaders.3297/pcares.m 0000644 0000000 0000000 00000000132 12477120566 016013 x ustar 00 30 mtime=1425842550.033151695
30 atime=1425842550.033151695
30 ctime=1425842550.077151696
statistics-1.2.4/inst/pcares.m 0000644 0001750 0001750 00000005336 12477120566 017177 0 ustar 00asnelt asnelt 0000000 0000000 ## Copyright (C) 2013 Fernando Damian Nieuwveldt
##
## This program is free software; you can redistribute it and/or
## modify it under the terms of the GNU General Public License
## as published by the Free Software Foundation; either version 3
## of the License, or (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License along with
## this program; if not, see .
## -*- texinfo -*-
## @deftypefn {Function File} {[@var{residuals},@var{reconstructed}]}=pcares(@var{X}, @var{NDIM})
## @itemize @bullet
## @item
## @var{X} : N x P Matrix with N observations and P variables, the variables will be mean centered
## @item
## @var{ndim} : Is a scalar indicating the number of principal components to use and should be <= P
## @end itemize
##
## @subheading References
##
## @enumerate
## @item
## Jolliffe, I. T., Principal Component Analysis, 2nd Edition, Springer, 2002
##
## @end enumerate
## @end deftypefn
## Author: Fernando Damian Nieuwveldt
## Description: Residuals from Principal Components Analysis
function [residuals,reconstructed] = pcares(X,NDIM)
if (nargin ~= 2)
error('pcares takes two inputs: The data Matrix X and number of principal components NDIM')
endif
# Mean center data
Xcentered = bsxfun(@minus,X,mean(X));
# Apply svd to get the principal component coefficients
[U,S,V] = svd(Xcentered);
# Use only the first ndim PCA components
v = V(:,1:NDIM);
if (nargout == 2)
# Calculate the residuals
residuals = Xcentered - Xcentered * (v*v');
# Reconstructed data using ndim PCA components
reconstructed = X - residuals;
else
# Calculate the residuals
residuals = Xcentered - Xcentered * (v*v');
endif
endfunction
%!demo
%! X = [ 7 26 6 60;
%! 1 29 15 52;
%! 11 56 8 20;
%! 11 31 8 47;
%! 7 52 6 33;
%! 11 55 9 22;
%! 3 71 17 6;
%! 1 31 22 44;
%! 2 54 18 22;
%! 21 47 4 26;
%! 1 40 23 34;
%! 11 66 9 12;
%! 10 68 8 12
%! ];
%! # As we increase the number of principal components, the norm
%! # of the residuals matrix will decrease
%! r1 = pcares(X,1);
%! n1 = norm(r1)
%! r2 = pcares(X,2);
%! n2 = norm(r2)
%! r3 = pcares(X,3);
%! n3 = norm(r3)
%! r4 = pcares(X,4);
%! n4 = norm(r4)
statistics-1.2.4/inst/PaxHeaders.3297/pcacov.m 0000644 0000000 0000000 00000000132 12477120566 016011 x ustar 00 30 mtime=1425842550.033151695
30 atime=1425842550.033151695
30 ctime=1425842550.077151696
statistics-1.2.4/inst/pcacov.m 0000644 0001750 0001750 00000004546 12477120566 017177 0 ustar 00asnelt asnelt 0000000 0000000 ## Copyright (C) 2013 Fernando Damian Nieuwveldt
##
## This program is free software; you can redistribute it and/or
## modify it under the terms of the GNU General Public License
## as published by the Free Software Foundation; either version 3
## of the License, or (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License along with
## this program; if not, see .
## -*- texinfo -*-
## @deftypefn {Function File} {[@var{COEFF}]} = pcacov(@var{X})
## @deftypefnx {Function File} {[@var{COEFF},@var{latent}]} = pcacov(@var{X})
## @deftypefnx {Function File} {[@var{COEFF},@var{latent},@var{explained}]} = pcacov(@var{X})
## @itemize @bullet
## @item
## pcacov performs principal component analysis on the nxn covariance matrix X
## @item
## @var{COEFF} : a nxn matrix with columns containing the principal component coefficients
## @item
## @var{latent} : a vector containing the principal component variances
## @item
## @var{explained} : a vector containing the percentage of the total variance explained by each principal component
##
## @end itemize
##
## @subheading References
##
## @enumerate
## @item
## Jolliffe, I. T., Principal Component Analysis, 2nd Edition, Springer, 2002
##
## @end enumerate
## @end deftypefn
## Author: Fernando Damian Nieuwveldt
## Description: Principal Components Analysis using a covariance matrix
function [COEFF, latent, explained] = pcacov(X)
[U,S,V] = svd(X);
if nargout == 1
COEFF = U;
elseif nargout == 2
COEFF = U;
latent = diag(S);
else
COEFF = U;
latent = diag(S);
explained = 100*latent./sum(latent);
end
endfunction
%!demo
%! X = [ 7 26 6 60;
%! 1 29 15 52;
%! 11 56 8 20;
%! 11 31 8 47;
%! 7 52 6 33;
%! 11 55 9 22;
%! 3 71 17 6;
%! 1 31 22 44;
%! 2 54 18 22;
%! 21 47 4 26;
%! 1 40 23 34;
%! 11 66 9 12;
%! 10 68 8 12
%! ];
%! covx = cov(X);
%! [COEFF,latent,explained] = pcacov(covx)
statistics-1.2.4/inst/PaxHeaders.3297/vmrnd.m 0000644 0000000 0000000 00000000132 12477120566 015664 x ustar 00 30 mtime=1425842550.057151696
30 atime=1425842550.057151696
30 ctime=1425842550.077151696
statistics-1.2.4/inst/vmrnd.m 0000644 0001750 0001750 00000004740 12477120566 017046 0 ustar 00asnelt asnelt 0000000 0000000 ## Copyright (C) 2009 Soren Hauberg
##
## This program is free software; you can redistribute it and/or modify it under
## the terms of the GNU General Public License as published by the Free Software
## Foundation; either version 3 of the License, or (at your option) any later
## version.
##
## This program is distributed in the hope that it will be useful, but WITHOUT
## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
## details.
##
## You should have received a copy of the GNU General Public License along with
## this program; if not, see .
## -*- texinfo -*-
## @deftypefn {Function File} @var{theta} = vmrnd (@var{mu}, @var{k})
## @deftypefnx{Function File} @var{theta} = vmrnd (@var{mu}, @var{k}, @var{sz})
## Draw random angles from a Von Mises distribution with mean @var{mu} and
## concentration @var{k}.
##
## The Von Mises distribution has probability density function
## @example
## f (@var{x}) = exp (@var{k} * cos (@var{x} - @var{mu})) / @var{Z} ,
## @end example
## where @var{Z} is a normalisation constant.
##
## The output, @var{theta}, is a matrix of size @var{sz} containing random angles
## drawn from the given Von Mises distribution. By default, @var{mu} is 0
## and @var{k} is 1.
## @seealso{vmpdf}
## @end deftypefn
function theta = vmrnd (mu = 0, k = 1, sz = 1)
## Check input
if (!isreal (mu))
error ("vmrnd: first input must be a scalar");
endif
if (!isreal (k) || k <= 0)
error ("vmrnd: second input must be a real positive scalar");
endif
if (isscalar (sz))
sz = [sz, sz];
elseif (!isvector (sz))
error ("vmrnd: third input must be a scalar or a vector");
endif
## Simulate!
if (k < 1e-6)
## k is small: sample uniformly on circle
theta = 2 * pi * rand (sz) - pi;
else
a = 1 + sqrt (1 + 4 * k.^2);
b = (a - sqrt (2 * a)) / (2 * k);
r = (1 + b^2) / (2 * b);
N = prod (sz);
notdone = true (N, 1);
while (any (notdone))
u (:, notdone) = rand (3, N);
z (notdone) = cos (pi * u (1, notdone));
f (notdone) = (1 + r * z (notdone)) ./ (r + z (notdone));
c (notdone) = k * (r - f (notdone));
notdone = (u (2, :) >= c .* (2 - c)) & (log (c) - log (u (2, :)) + 1 - c < 0);
N = sum (notdone);
endwhile
theta = mu + sign (u (3, :) - 0.5) .* acos (f);
theta = reshape (theta, sz);
endif
endfunction
statistics-1.2.4/inst/PaxHeaders.3297/jsucdf.m 0000644 0000000 0000000 00000000132 12477120566 016014 x ustar 00 30 mtime=1425842550.009151695
30 atime=1425842550.009151695
30 ctime=1425842550.077151696
statistics-1.2.4/inst/jsucdf.m 0000644 0001750 0001750 00000004025 12477120566 017172 0 ustar 00asnelt asnelt 0000000 0000000 ## Copyright (C) 2006 Frederick (Rick) A Niles
##
## This program is free software; you can redistribute it and/or modify it under
## the terms of the GNU General Public License as published by the Free Software
## Foundation; either version 3 of the License, or (at your option) any later
## version.
##
## This program is distributed in the hope that it will be useful, but WITHOUT
## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
## details.
##
## You should have received a copy of the GNU General Public License along with
## this program; if not, see .
## -*- texinfo -*-
## @deftypefn {Function File} {} jsucdf (@var{x}, @var{alpha1}, @var{alpha2})
## For each element of @var{x}, compute the cumulative distribution
## function (CDF) at @var{x} of the Johnson SU distribution with shape parameters
## @var{alpha1} and @var{alpha2}.
##
## Default values are @var{alpha1} = 1, @var{alpha2} = 1.
## @end deftypefn
## Author: Frederick (Rick) A Niles
## Description: CDF of the Johnson SU distribution
## This function is derived from normcdf.m
## This is the TeX equation of this function:
##
## \[ F(x) = \Phi\left(\alpha_1 + \alpha_2
## \log\left(x + \sqrt{x^2 + 1} \right)\right) \]
##
## where \[ -\infty < x < \infty ; \alpha_2 > 0 \] and $\Phi$ is the
## standard normal cumulative distribution function. $\alpha_1$ and
## $\alpha_2$ are shape parameters.
function cdf = jsucdf (x, alpha1, alpha2)
if (! ((nargin == 1) || (nargin == 3)))
print_usage;
endif
if (nargin == 1)
m = 0;
v = 1;
endif
if (!isscalar (alpha1) || !isscalar(alpha2))
[retval, x, alpha1, alpha2] = common_size (x, alpha1, alpha2);
if (retval > 0)
error ("normcdf: x, alpha1 and alpha2 must be of common size or scalar");
endif
endif
one = ones (size (x));
cdf = stdnormal_cdf (alpha1 .* one + alpha2 .* log (x + sqrt(x.*x + one)));
endfunction
statistics-1.2.4/inst/PaxHeaders.3297/vmpdf.m 0000644 0000000 0000000 00000000132 12477120566 015652 x ustar 00 30 mtime=1425842550.057151696
30 atime=1425842550.057151696
30 ctime=1425842550.077151696
statistics-1.2.4/inst/vmpdf.m 0000644 0001750 0001750 00000003037 12477120566 017032 0 ustar 00asnelt asnelt 0000000 0000000 ## Copyright (C) 2009 Soren Hauberg
##
## This program is free software; you can redistribute it and/or modify it under
## the terms of the GNU General Public License as published by the Free Software
## Foundation; either version 3 of the License, or (at your option) any later
## version.
##
## This program is distributed in the hope that it will be useful, but WITHOUT
## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
## details.
##
## You should have received a copy of the GNU General Public License along with
## this program; if not, see .
## -*- texinfo -*-
## @deftypefn {Function File} @var{theta} = vmpdf (@var{x}, @var{mu}, @var{k})
## Evaluates the Von Mises probability density function.
##
## The Von Mises distribution has probability density function
## @example
## f (@var{x}) = exp (@var{k} * cos (@var{x} - @var{mu})) / @var{Z} ,
## @end example
## where @var{Z} is a normalisation constant. By default, @var{mu} is 0 and
## @var{k} is 1.
## @seealso{vmrnd}
## @end deftypefn
function p = vmpdf (x, mu = 0, k = 1)
## Check input
if (!isreal (x))
error ("vmpdf: first input must be real");
endif
if (!isreal (mu))
error ("vmpdf: second input must be a scalar");
endif
if (!isreal (k) || k <= 0)
error ("vmpdf: third input must be a real positive scalar");
endif
## Evaluate PDF
Z = 2 * pi * besseli (0, k);
p = exp (k * cos (x-mu)) / Z;
endfunction
statistics-1.2.4/inst/PaxHeaders.3297/qrandn.m 0000644 0000000 0000000 00000000132 12477120566 016021 x ustar 00 30 mtime=1425842550.037151695
30 atime=1425842550.037151695
30 ctime=1425842550.077151696
statistics-1.2.4/inst/qrandn.m 0000644 0001750 0001750 00000005337 12477120566 017206 0 ustar 00asnelt asnelt 0000000 0000000 ## Copyright (C) 2014 - Juan Pablo Carbajal
##
## This progrm is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 3 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program. If not, see .
## Author: Juan Pablo Carbajal
## -*- texinfo -*-
## @deftypefn {Function File} {@var{z} =} qrandn (@var{q}, @var{r},@var{c})
## @deftypefnx {Function File} {@var{z} =} qrandn (@var{q}, [@var{r},@var{c}])
## Returns random deviates drawn from a q-Gaussian distribution.
##
## Parameter @var{q} charcterizes the q-Gaussian distribution.
## The result has the size indicated by @var{s}.
##
## Reference:
## W. Thistleton, J. A. Marsh, K. Nelson, C. Tsallis (2006)
## "Generalized Box-Muller method for generating q-Gaussian random deviates"
## arXiv:cond-mat/0605570 http://arxiv.org/abs/cond-mat/0605570
##
## @seealso{rand, randn}
## @end deftypefn
function z = qrandn(q,R,C=[])
if !isscalar (q)
error ('Octave:invalid-input-arg', 'The parameter q must be a scalar.')
endif
# Check that q < 3
if q > 3
error ('Octave:invalid-input-arg', 'The parameter q must be lower than 3.');
endif
if numel (R) > 1
S = R;
elseif numel (R) ==1 && isempty (C)
S = [R,1];
elseif numel (R) ==1 && !isempty (C)
S = [R,C];
endif
# Calaulate the q to be used on the q-log
qGen = (1 + q) / (3 - q);
# Initialize the output vector
z = sqrt (-2 * log_q (rand (S),qGen)) .* sin (2*pi*rand (S));
endfunction
function a = log_q (x,q)
#
# Returns the q-log of x, using q
#
dq = 1 - q;
# Check to see if q = 1 (to double precision)
if abs (dq) < 10*eps
# If q is 1, use the usual natural logarithm
a = log (x);
else
# If q differs from 1, use the definition of the q-log
a = ( x .^ dq - 1 ) ./ dq;
endif
endfunction
%!demo
%! z = qrandn (-5, 5e6);
%! [c x] = hist (z,linspace(-1.5,1.5,200),1);
%! figure(1)
%! plot(x,c,"r."); axis tight; axis([-1.5,1.5]);
%!
%! z = qrandn (-0.14286, 5e6);
%! [c x] = hist (z,linspace(-2,2,200),1);
%! figure(2)
%! plot(x,c,"r."); axis tight; axis([-2,2]);
%!
%! z = qrandn (2.75, 5e6);
%! [c x] = hist (z,linspace(-1e3,1e3,1e3),1);
%! figure(3)
%! semilogy(x,c,"r."); axis tight; axis([-100,100]);
%!
%! # ---------
%! # Figures from the reference paper.
statistics-1.2.4/inst/PaxHeaders.3297/trimmean.m 0000644 0000000 0000000 00000000132 12477120566 016352 x ustar 00 30 mtime=1425842550.053151695
30 atime=1425842550.053151695
30 ctime=1425842550.077151696
statistics-1.2.4/inst/trimmean.m 0000644 0001750 0001750 00000003176 12477120566 017536 0 ustar 00asnelt asnelt 0000000 0000000 ## Copyright (C) 2001 Paul Kienzle
##
## This program is free software; you can redistribute it and/or modify it under
## the terms of the GNU General Public License as published by the Free Software
## Foundation; either version 3 of the License, or (at your option) any later
## version.
##
## This program is distributed in the hope that it will be useful, but WITHOUT
## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
## details.
##
## You should have received a copy of the GNU General Public License along with
## this program; if not, see .
## -*- texinfo -*-
## @deftypefn {Function File} {@var{a} =} trimmean (@var{x}, @var{p})
##
## Compute the trimmed mean.
##
## The trimmed mean of @var{x} is defined as the mean of @var{x} excluding the
## highest and lowest @var{p} percent of the data.
##
## For example
##
## @example
## mean ([-inf, 1:9, inf])
## @end example
##
## is NaN, while
##
## @example
## trimmean ([-inf, 1:9, inf], 10)
## @end example
##
## excludes the infinite values, which make the result 5.
##
## @seealso{mean}
## @end deftypefn
function a = trimmean(x, p, varargin)
if (nargin != 2 && nargin != 3)
print_usage;
endif
y = sort(x, varargin{:});
sz = size(x);
if nargin < 3
dim = min(find(sz>1));
if isempty(dim), dim=1; endif;
else
dim = varargin{1};
endif
idx = cell (0);
for i=1:length(sz), idx{i} = 1:sz(i); end;
trim = round(sz(dim)*p*0.01);
idx{dim} = 1+trim : sz(dim)-trim;
a = mean (y (idx{:}), varargin{:});
endfunction
statistics-1.2.4/inst/PaxHeaders.3297/wishrnd.m 0000644 0000000 0000000 00000000132 12477120566 016214 x ustar 00 30 mtime=1425842550.057151696
30 atime=1425842550.057151696
30 ctime=1425842550.077151696
statistics-1.2.4/inst/wishrnd.m 0000644 0001750 0001750 00000006374 12477120566 017403 0 ustar 00asnelt asnelt 0000000 0000000 ## Copyright (C) 2013 Nir Krakauer
##
## This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version.
##
## Octave is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License along with Octave; see the file COPYING. If not, see .
## -*- texinfo -*-
## @deftypefn {Function File} {} [@var{W}[, @var{D}]] = wishrnd (@var{Sigma}, @var{df}[, @var{D}][, @var{n}=1])
## Return a random matrix sampled from the Wishart distribution with given parameters
##
## Inputs: the @var{p} x @var{p} positive definite matrix @var{Sigma} and scalar degrees of freedom parameter @var{df} (and optionally the Cholesky factor @var{D} of @var{Sigma}).
## @var{df} can be non-integer as long as @var{df} > @var{p}
##
## Output: a random @var{p} x @var{p} matrix @var{W} from the Wishart(@var{Sigma}, @var{df}) distribution. If @var{n} > 1, then @var{W} is @var{p} x @var{p} x @var{n} and holds @var{n} such random matrices. (Optionally, the Cholesky factor @var{D} of @var{Sigma} is also returned.)
##
## Averaged across many samples, the mean of @var{W} should approach @var{df}*@var{Sigma}, and the variance of each element @var{W}_ij should approach @var{df}*(@var{Sigma}_ij^2 + @var{Sigma}_ii*@var{Sigma}_jj)
##
## Reference: Yu-Cheng Ku and Peter Bloomfield (2010), Generating Random Wishart Matrices with Fractional Degrees of Freedom in OX, http://www.gwu.edu/~forcpgm/YuChengKu-030510final-WishartYu-ChengKu.pdf
##
## @seealso{iwishrnd, wishpdf}
## @end deftypefn
## Author: Nir Krakauer
## Description: Compute the probability density function of the Wishart distribution
function [W, D] = wishrnd(Sigma, df, D, n=1)
if (nargin < 2)
print_usage ();
endif
if nargin < 3 || isempty(D)
try
D = chol(Sigma);
catch
error('wishrnd: Cholesky decomposition failed; Sigma probably not positive definite')
end_try_catch
endif
p = size(D, 1);
if df < p
df = floor(df); #distribution not defined for small noninteger df
df_isint = 1;
else
#check for integer degrees of freedom
df_isint = (df == floor(df));
endif
if ~df_isint
[ii, jj] = ind2sub([p, p], 1:(p*p));
endif
if n > 1
W = nan(p, p, n);
endif
for i = 1:n
if df_isint
Z = randn(df, p) * D;
W(:, :, i) = Z'*Z;
else
Z = diag(sqrt(chi2rnd(df - (0:(p-1))))); #fill diagonal
#note: chi2rnd(x) is equivalent to 2*randg(x/2), but the latter seems to offer no performance advantage
Z(ii > jj) = randn(p*(p-1)/2, 1); #fill lower triangle with normally distributed variates
Z = D * Z;
W(:, :, i) = Z*Z';
endif
endfor
endfunction
%!assert(size (wishrnd (1,2,1)), [1, 1]);
%!assert(size (wishrnd ([],2,1)), [1, 1]);
%!assert(size (wishrnd ([3 1; 1 3], 2.00001, [], 1)), [2, 2]);
%!assert(size (wishrnd (eye(2), 2, [], 3)), [2, 2, 3]);
%% Test input validation
%!error wishrnd ()
%!error wishrnd (1)
%!error wishrnd ([1; 1], 2)
statistics-1.2.4/inst/PaxHeaders.3297/vartest.m 0000644 0000000 0000000 00000000132 12477120566 016226 x ustar 00 30 mtime=1425842550.057151696
30 atime=1425842550.057151696
30 ctime=1425842550.077151696
statistics-1.2.4/inst/vartest.m 0000644 0001750 0001750 00000011062 12477120566 017403 0 ustar 00asnelt asnelt 0000000 0000000 ## Copyright (C) 2014 Tony Richardson
##
## This program is free software; you can redistribute it and/or modify it under
## the terms of the GNU General Public License as published by the Free Software
## Foundation; either version 3 of the License, or (at your option) any later
## version.
##
## This program is distributed in the hope that it will be useful, but WITHOUT
## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
## details.
##
## You should have received a copy of the GNU General Public License along with
## this program; if not, see .
## -*- texinfo -*-
## @deftypefn {Function File} {[@var{h}, @var{pval}, @var{ci}, @var{stats}] =} vartest (@var{x}, @var{y})
## @deftypefnx {Function File} {[@var{h}, @var{pval}, @var{ci}, @var{stats}] =} vartest (@var{x}, @var{y}, @var{Name}, @var{Value})
## Perform a F-test for equal variances.
##
## If the second argument @var{y} is a vector, a paired-t test of the
## hypothesis @code{mean (@var{x}) = mean (@var{y})} is performed.
##
## The argument @qcode{"alpha"} can be used to specify the significance level
## of the test (the default value is 0.05). The string
## argument @qcode{"tail"}, can be used to select the desired alternative
## hypotheses. If @qcode{"alt"} is @qcode{"both"} (default) the null is
## tested against the two-sided alternative @code{mean (@var{x}) != @var{m}}.
## If @qcode{"alt"} is @qcode{"right"} the one-sided
## alternative @code{mean (@var{x}) > @var{m}} is considered.
## Similarly for @qcode{"left"}, the one-sided alternative @code{mean
## (@var{x}) < @var{m}} is considered. When @qcode{"vartype"} is @qcode{"equal"}
## the variances are assumed to be equal (this is the default). When
## @qcode{"vartype"} is @qcode{"unequal"} the variances are not assumed equal.
## When argument @var{x} is a matrix the @qcode{"dim"} argument can be
## used to selection the dimension over which to perform the test.
## (The default is the first non-singleton dimension.)
##
## If @var{h} is 0 the null hypothesis is accepted, if it is 1 the null
## hypothesis is rejected. The p-value of the test is returned in @var{pval}.
## A 100(1-alpha)% confidence interval is returned in @var{ci}. @var{stats}
## is a structure containing the value of the test statistic (@var{tstat}),
## the degrees of freedom (@var{df}) and the sample standard deviation
## (@var{sd}).
##
## @end deftypefn
## Author: Tony Richardson
## Description: Test for mean of a normal sample with known variance
function [h, p, ci, stats] = vartest(x, v, varargin)
% Set default arguments
alpha = 0.05;
tail = 'both';
% Find the first non-singleton dimension of x
dim = min(find(size(x)~=1));
if isempty(dim), dim = 1; end
i = 1;
while ( i <= length(varargin) )
switch lower(varargin{i})
case 'alpha'
i = i + 1;
alpha = varargin{i};
case 'tail'
i = i + 1;
tail = varargin{i};
case 'dim'
i = i + 1;
dim = varargin{i};
otherwise
error('Invalid Name argument.',[]);
end
i = i + 1;
end
if ~isa(tail, 'char')
error('tail argument to vartest must be a string\n',[]);
end
s_var = var(x, 0, dim);
df = size(x, dim) - 1;
stats.chisqstat = df*s_var/v;
% Based on the "tail" argument determine the P-value, the critical values,
% and the confidence interval.
switch lower(tail)
case 'both'
p = 2*min(chi2cdf(stats.chisqstat,df),1-chi2cdf(stats.chisqstat,df));
ci = [df*s_var ./ (chi2inv(1-alpha/2,df)); df*s_var ./ (chi2inv(alpha/2,df))];
case 'left'
p = chi2cdf(stats.chisqstat,df);
chi2crit = chi2inv(alpha,df);
ci = [zeros(size(stats.chisqstat)); df*s_var ./ (chi2inv(alpha,df))];
case 'right'
p = 1 - chi2cdf(stats.chisqstat,df);
chi2crit = chi2inv(1-alpha,df);
ci = [df*s_var ./ (chi2inv(1-alpha,df)); inf*ones(size(stats.chisqstat))];
otherwise
error('Invalid fourth (tail) argument to vartest\n',[]);
end
% Reshape the ci array to match MATLAB shaping
if and(isscalar(stats.chisqstat), dim==2)
ci = ci(:)';
elseif size(stats.chisqstat,2)