├── .gitattributes ├── CONTRIBUTING.md ├── COPYING ├── DESCRIPTION ├── INDEX ├── NEWS ├── ONEWS-1.1.x ├── ONEWS-1.2.x ├── ONEWS-1.3.0 ├── ONEWS-1.4.x ├── ONEWS-1.5.x ├── ONEWS-1.6.x ├── README.md ├── doc └── statistics.png ├── inst ├── @cvpartition │ ├── cvpartition.m │ ├── display.m │ ├── get.m │ ├── repartition.m │ ├── set.m │ ├── test.m │ └── training.m ├── Classification │ ├── ClassificationDiscriminant.m │ ├── ClassificationGAM.m │ ├── ClassificationKNN.m │ ├── ClassificationNeuralNetwork.m │ ├── ClassificationPartitionedModel.m │ ├── ClassificationSVM.m │ ├── CompactClassificationDiscriminant.m │ ├── CompactClassificationGAM.m │ ├── CompactClassificationNeuralNetwork.m │ ├── CompactClassificationSVM.m │ ├── ConfusionMatrixChart.m │ └── private │ │ └── parseScoreTransform.m ├── Clustering │ ├── CalinskiHarabaszEvaluation.m │ ├── ClusterCriterion.m │ ├── DaviesBouldinEvaluation.m │ ├── GapEvaluation.m │ └── SilhouetteEvaluation.m ├── PKG_ADD ├── PKG_DEL ├── Regression │ └── RegressionGAM.m ├── adtest.m ├── anova1.m ├── anova2.m ├── anovan.m ├── bar3.m ├── bar3h.m ├── bartlett_test.m ├── barttest.m ├── binotest.m ├── boxplot.m ├── canoncorr.m ├── cdfcalc.m ├── cdfplot.m ├── chi2gof.m ├── chi2test.m ├── cholcov.m ├── cl_multinom.m ├── cluster.m ├── clusterdata.m ├── cmdscale.m ├── combnk.m ├── confusionchart.m ├── confusionmat.m ├── cophenet.m ├── correlation_test.m ├── crosstab.m ├── crossval.m ├── datasample.m ├── datasets │ ├── acetylene.mat │ ├── arrhythmia.mat │ ├── carbig.mat │ ├── carsmall.mat │ ├── cereal.mat │ ├── examgrades.mat │ ├── fail_load_model.mdl │ ├── fail_loadmodel.mdl │ ├── fisheriris.mat │ ├── gradespaired.mat │ ├── hald.mat │ ├── heart_scale.dat │ ├── kmeansdata.mat │ ├── lightbulb.mat │ ├── mileage.mat │ ├── morse.mat │ ├── patients.mat │ ├── popcorn.mat │ ├── rundist.mat │ ├── spectra.mat │ ├── stockreturns.mat │ └── weather.mat ├── dcov.m ├── dendrogram.m ├── dist_fit │ ├── betafit.m │ ├── betalike.m │ ├── binofit.m │ ├── binolike.m │ ├── bisafit.m │ ├── bisalike.m │ ├── burrfit.m │ ├── burrlike.m │ ├── evfit.m │ ├── evlike.m │ ├── expfit.m │ ├── explike.m │ ├── gamfit.m │ ├── gamlike.m │ ├── geofit.m │ ├── gevfit.m │ ├── gevfit_lmom.m │ ├── gevlike.m │ ├── gpfit.m │ ├── gplike.m │ ├── gumbelfit.m │ ├── gumbellike.m │ ├── hnfit.m │ ├── hnlike.m │ ├── invgfit.m │ ├── invglike.m │ ├── logifit.m │ ├── logilike.m │ ├── loglfit.m │ ├── logllike.m │ ├── lognfit.m │ ├── lognlike.m │ ├── nakafit.m │ ├── nakalike.m │ ├── nbinfit.m │ ├── nbinlike.m │ ├── normfit.m │ ├── normlike.m │ ├── poissfit.m │ ├── poisslike.m │ ├── raylfit.m │ ├── rayllike.m │ ├── ricefit.m │ ├── ricelike.m │ ├── tlsfit.m │ ├── tlslike.m │ ├── unidfit.m │ ├── unifit.m │ ├── wblfit.m │ └── wbllike.m ├── dist_fun │ ├── betacdf.m │ ├── betainv.m │ ├── betapdf.m │ ├── betarnd.m │ ├── binocdf.m │ ├── binoinv.m │ ├── binopdf.m │ ├── binornd.m │ ├── bisacdf.m │ ├── bisainv.m │ ├── bisapdf.m │ ├── bisarnd.m │ ├── burrcdf.m │ ├── burrinv.m │ ├── burrpdf.m │ ├── burrrnd.m │ ├── bvncdf.m │ ├── bvtcdf.m │ ├── cauchycdf.m │ ├── cauchyinv.m │ ├── cauchypdf.m │ ├── cauchyrnd.m │ ├── chi2cdf.m │ ├── chi2inv.m │ ├── chi2pdf.m │ ├── chi2rnd.m │ ├── copulacdf.m │ ├── copulapdf.m │ ├── copularnd.m │ ├── evcdf.m │ ├── evinv.m │ ├── evpdf.m │ ├── evrnd.m │ ├── expcdf.m │ ├── expinv.m │ ├── exppdf.m │ ├── exprnd.m │ ├── fcdf.m │ ├── finv.m │ ├── fpdf.m │ ├── frnd.m │ ├── gamcdf.m │ ├── gaminv.m │ ├── gampdf.m │ ├── gamrnd.m │ ├── geocdf.m │ ├── geoinv.m │ ├── geopdf.m │ ├── geornd.m │ ├── gevcdf.m │ ├── gevinv.m │ ├── gevpdf.m │ ├── gevrnd.m │ ├── gpcdf.m │ ├── gpinv.m │ ├── gppdf.m │ ├── gprnd.m │ ├── gumbelcdf.m │ ├── gumbelinv.m │ ├── gumbelpdf.m │ ├── gumbelrnd.m │ ├── hncdf.m │ ├── hninv.m │ ├── hnpdf.m │ ├── hnrnd.m │ ├── hygecdf.m │ ├── hygeinv.m │ ├── hygepdf.m │ ├── hygernd.m │ ├── invgcdf.m │ ├── invginv.m │ ├── invgpdf.m │ ├── invgrnd.m │ ├── iwishpdf.m │ ├── iwishrnd.m │ ├── jsucdf.m │ ├── jsupdf.m │ ├── laplacecdf.m │ ├── laplaceinv.m │ ├── laplacepdf.m │ ├── laplacernd.m │ ├── logicdf.m │ ├── logiinv.m │ ├── logipdf.m │ ├── logirnd.m │ ├── loglcdf.m │ ├── loglinv.m │ ├── loglpdf.m │ ├── loglrnd.m │ ├── logncdf.m │ ├── logninv.m │ ├── lognpdf.m │ ├── lognrnd.m │ ├── mnpdf.m │ ├── mnrnd.m │ ├── mvncdf.m │ ├── mvnpdf.m │ ├── mvnrnd.m │ ├── mvtcdf.m │ ├── mvtcdfqmc.m │ ├── mvtpdf.m │ ├── mvtrnd.m │ ├── nakacdf.m │ ├── nakainv.m │ ├── nakapdf.m │ ├── nakarnd.m │ ├── nbincdf.m │ ├── nbininv.m │ ├── nbinpdf.m │ ├── nbinrnd.m │ ├── ncfcdf.m │ ├── ncfinv.m │ ├── ncfpdf.m │ ├── ncfrnd.m │ ├── nctcdf.m │ ├── nctinv.m │ ├── nctpdf.m │ ├── nctrnd.m │ ├── ncx2cdf.m │ ├── ncx2inv.m │ ├── ncx2pdf.m │ ├── ncx2rnd.m │ ├── normcdf.m │ ├── norminv.m │ ├── normpdf.m │ ├── normrnd.m │ ├── plcdf.m │ ├── plinv.m │ ├── plpdf.m │ ├── plrnd.m │ ├── poisscdf.m │ ├── poissinv.m │ ├── poisspdf.m │ ├── poissrnd.m │ ├── raylcdf.m │ ├── raylinv.m │ ├── raylpdf.m │ ├── raylrnd.m │ ├── ricecdf.m │ ├── riceinv.m │ ├── ricepdf.m │ ├── ricernd.m │ ├── tcdf.m │ ├── tinv.m │ ├── tlscdf.m │ ├── tlsinv.m │ ├── tlspdf.m │ ├── tlsrnd.m │ ├── tpdf.m │ ├── tricdf.m │ ├── triinv.m │ ├── tripdf.m │ ├── trirnd.m │ ├── trnd.m │ ├── unidcdf.m │ ├── unidinv.m │ ├── unidpdf.m │ ├── unidrnd.m │ ├── unifcdf.m │ ├── unifinv.m │ ├── unifpdf.m │ ├── unifrnd.m │ ├── vmcdf.m │ ├── vminv.m │ ├── vmpdf.m │ ├── vmrnd.m │ ├── wblcdf.m │ ├── wblinv.m │ ├── wblpdf.m │ ├── wblrnd.m │ ├── wienrnd.m │ ├── wishpdf.m │ └── wishrnd.m ├── dist_obj │ ├── BetaDistribution.m │ ├── BinomialDistribution.m │ ├── BirnbaumSaundersDistribution.m │ ├── BurrDistribution.m │ ├── ExponentialDistribution.m │ ├── ExtremeValueDistribution.m │ ├── GammaDistribution.m │ ├── GeneralizedExtremeValueDistribution.m │ ├── GeneralizedParetoDistribution.m │ ├── HalfNormalDistribution.m │ ├── InverseGaussianDistribution.m │ ├── LogisticDistribution.m │ ├── LoglogisticDistribution.m │ ├── LognormalDistribution.m │ ├── LoguniformDistribution.m │ ├── MultinomialDistribution.m │ ├── NakagamiDistribution.m │ ├── NegativeBinomialDistribution.m │ ├── NormalDistribution.m │ ├── PiecewiseLinearDistribution.m │ ├── PoissonDistribution.m │ ├── RayleighDistribution.m │ ├── RicianDistribution.m │ ├── TriangularDistribution.m │ ├── UniformDistribution.m │ ├── WeibullDistribution.m │ ├── private │ │ ├── __disp__.m │ │ ├── __paramci__.m │ │ ├── __plot__.m │ │ └── __proflik__.m │ └── tLocationScaleDistribution.m ├── dist_stat │ ├── betastat.m │ ├── binostat.m │ ├── bisastat.m │ ├── burrstat.m │ ├── chi2stat.m │ ├── evstat.m │ ├── expstat.m │ ├── fstat.m │ ├── gamstat.m │ ├── geostat.m │ ├── gevstat.m │ ├── gpstat.m │ ├── hnstat.m │ ├── hygestat.m │ ├── invgstat.m │ ├── logistat.m │ ├── loglstat.m │ ├── lognstat.m │ ├── nakastat.m │ ├── nbinstat.m │ ├── ncfstat.m │ ├── nctstat.m │ ├── ncx2stat.m │ ├── normstat.m │ ├── plstat.m │ ├── poisstat.m │ ├── raylstat.m │ ├── ricestat.m │ ├── tlsstat.m │ ├── tristat.m │ ├── tstat.m │ ├── unidstat.m │ ├── unifstat.m │ └── wblstat.m ├── dist_wrap │ ├── cdf.m │ ├── fitdist.m │ ├── icdf.m │ ├── makedist.m │ ├── mle.m │ ├── pdf.m │ └── random.m ├── ecdf.m ├── einstein.m ├── evalclusters.m ├── ff2n.m ├── fillmissing.m ├── fishertest.m ├── fitcdiscr.m ├── fitcgam.m ├── fitcknn.m ├── fitcnet.m ├── fitcsvm.m ├── fitgmdist.m ├── fitlm.m ├── fitrgam.m ├── friedman.m ├── fullfact.m ├── geomean.m ├── glmfit.m ├── glmval.m ├── gmdistribution.m ├── grp2idx.m ├── grpstats.m ├── gscatter.m ├── harmmean.m ├── hist3.m ├── histfit.m ├── hmmestimate.m ├── hmmgenerate.m ├── hmmviterbi.m ├── hotelling_t2test.m ├── hotelling_t2test2.m ├── inconsistent.m ├── ismissing.m ├── isoutlier.m ├── jackknife.m ├── kmeans.m ├── knnsearch.m ├── kruskalwallis.m ├── kstest.m ├── kstest2.m ├── levene_test.m ├── linkage.m ├── loadmodel.m ├── logistic_regression.m ├── logit.m ├── mahal.m ├── manova1.m ├── manovacluster.m ├── mcnemar_test.m ├── mhsample.m ├── mnrfit.m ├── monotone_smooth.m ├── multcompare.m ├── nanmax.m ├── nanmin.m ├── nansum.m ├── normalise_distribution.m ├── normplot.m ├── optimalleaforder.m ├── pca.m ├── pcacov.m ├── pcares.m ├── pdist.m ├── pdist2.m ├── plsregress.m ├── ppplot.m ├── princomp.m ├── private │ ├── exact2xkCT.m │ └── getlinkfunctions.m ├── probit.m ├── procrustes.m ├── qqplot.m ├── qrandn.m ├── randsample.m ├── rangesearch.m ├── ranksum.m ├── regress.m ├── regress_gp.m ├── regression_ftest.m ├── regression_ttest.m ├── ridge.m ├── rmmissing.m ├── runstest.m ├── sampsizepwr.m ├── shadow9 │ ├── mad.m │ ├── mean.m │ ├── median.m │ ├── std.m │ └── var.m ├── sigma_pts.m ├── signrank.m ├── signtest.m ├── silhouette.m ├── slicesample.m ├── squareform.m ├── standardizeMissing.m ├── stepwisefit.m ├── tabulate.m ├── tiedrank.m ├── trimmean.m ├── ttest.m ├── ttest2.m ├── vartest.m ├── vartest2.m ├── vartestn.m ├── violin.m ├── wblplot.m ├── x2fx.m ├── ztest.m └── ztest2.m ├── io.github.gnu_octave.statistics.metainfo.xml └── src ├── Makefile ├── editDistance.cc ├── fcnn.cpp ├── fcnnpredict.cc ├── fcnntrain.cc ├── libsvmread.cc ├── libsvmwrite.cc ├── svm.cpp ├── svm.h ├── svm_model_octave.cc ├── svm_model_octave.h ├── svmpredict.cc └── svmtrain.cc /.gitattributes: -------------------------------------------------------------------------------- 1 | docs export-ignore 2 | -------------------------------------------------------------------------------- /DESCRIPTION: -------------------------------------------------------------------------------- 1 | Name: statistics 2 | Version: 1.7.4 3 | Date: 2025-4-13 4 | Author: various authors 5 | Maintainer: Andreas Bertsatos 6 | Title: Statistics 7 | Description: The Statistics package for GNU Octave. 8 | Categories: Statistics 9 | Depends: octave (>= 8.1.0) 10 | License: GPLv3+ 11 | Url: https://github.com/gnu-octave/statistics 12 | Tracker: https://github.com/gnu-octave/statistics/issues 13 | -------------------------------------------------------------------------------- /ONEWS-1.1.x: -------------------------------------------------------------------------------- 1 | Summary of important user-visible changes for statistics 1.1.3: 2 | ------------------------------------------------------------------- 3 | 4 | ** The following functions are new in 1.1.3: 5 | 6 | copularnd mvtrnd 7 | 8 | ** The functions mnpdf and mnrnd are now also usable for greater numbers 9 | of categories for which the rows do not exactly sum to 1. 10 | 11 | Summary of important user-visible changes for statistics 1.1.2: 12 | ------------------------------------------------------------------- 13 | 14 | ** The following functions are new in 1.1.2: 15 | 16 | mnpdf mnrnd 17 | 18 | ** The package is now dependent on the io package (version 1.0.18 or 19 | later) since the functions that it depended of from miscellaneous 20 | package have been moved to io. 21 | 22 | ** The function `kmeans' now accepts the 'emptyaction' property with 23 | the 'singleton' value. This allows for the kmeans algorithm to handle 24 | empty cluster better. It also throws an error if the user does not 25 | request an empty cluster handling, and there is an empty cluster. 26 | Plus, the returned items are now a closer match to Matlab. 27 | 28 | Summary of important user-visible changes for statistics 1.1.1: 29 | ------------------------------------------------------------------- 30 | 31 | ** The following functions are new in 1.1.1: 32 | 33 | monotone_smooth kmeans jackknife 34 | 35 | ** Bug fixes on the functions: 36 | 37 | normalise_distribution combnk 38 | repanova 39 | 40 | ** The following functions were removed since equivalents are now 41 | part of GNU octave core: 42 | 43 | zscore 44 | 45 | ** boxplot.m now returns a structure with handles to the plot elemenets. 46 | 47 | Summary of important user-visible changes for statistics 1.1.0: 48 | ------------------------------------------------------------------- 49 | 50 | ** IMPORTANT note about `fstat' shadowing core library function: 51 | 52 | GNU octave's 3.2 release added a new function `fstat' to return 53 | information of a file. Statistics' `fstat' computes F mean and 54 | variance. Since MatLab's `fstat' is the equivalent to statistics' 55 | `fstat' (not to core's `fstat'), and to avoid problems with the 56 | statistics package, `fstat' has been deprecated in octave 3.4 57 | and will be removed in Octave 3.8. In the mean time, please 58 | ignore this warning when installing the package. 59 | 60 | ** The following functions are new in 1.1.0: 61 | 62 | normalise_distribution repanova combnk 63 | 64 | ** The following functions were removed since equivalents are now 65 | part of GNU octave core: 66 | 67 | prctile 68 | 69 | ** The __tbl_delim__ function is now private. 70 | 71 | ** The function `boxplot' now accepts named arguments. 72 | 73 | ** Bug fixes on the functions: 74 | 75 | harmmean nanmax nanmin regress 76 | 77 | ** Small improvements on help text. 78 | -------------------------------------------------------------------------------- /ONEWS-1.2.x: -------------------------------------------------------------------------------- 1 | Summary of important user-visible changes for statistics 1.2.4: 2 | ------------------------------------------------------------------- 3 | 4 | ** Made princomp work with nargout < 2. 5 | 6 | ** Renamed dendogram to dendrogram. 7 | 8 | ** Added isempty check to kmeans. 9 | 10 | ** Transposed output of hist3. 11 | 12 | ** Converted calculation in hmmviterbi to log space. 13 | 14 | ** Bug fixes for 15 | stepwisefit wishrnd. 16 | 17 | ** Rewrite of cmdscale for improved compatibility. 18 | 19 | ** Fix in squareform for improved compatibility. 20 | 21 | ** New cvpartition class, with methods: 22 | 23 | display repartition test training 24 | 25 | ** New sample data file fisheriris.txt for tests 26 | 27 | ** The following functions are new: 28 | 29 | cdf crossval dcov pdist2 qrandn randsample signtest ttest ttest2 30 | vartest vartest2 ztest 31 | 32 | 33 | Summary of important user-visible changes for statistics 1.2.3: 34 | ------------------------------------------------------------------- 35 | 36 | ** Made sure that output of nanstd is real. 37 | 38 | ** Fixed second output of nanmax and nanmin. 39 | 40 | ** Corrected handle for outliers in boxplot. 41 | 42 | ** Bug fix and enhanced functionality for mvnrnd. 43 | 44 | ** The following functions are new: 45 | 46 | wishrnd iwishrnd wishpdf iwishpdf cmdscale 47 | 48 | Summary of important user-visible changes for statistics 1.2.2: 49 | ------------------------------------------------------------------- 50 | 51 | ** Fixed documentation of dendogram and hist3 to work with TexInfo 5. 52 | 53 | Summary of important user-visible changes for statistics 1.2.1: 54 | ------------------------------------------------------------------- 55 | 56 | ** The following functions are new: 57 | 58 | pcares pcacov runstest stepwisefit hist3 59 | 60 | ** dendogram now returns the leaf node numbers and order that the nodes were displayed in. 61 | 62 | ** New faster implementation of princomp. 63 | 64 | Summary of important user-visible changes for statistics 1.2.0: 65 | ------------------------------------------------------------------- 66 | 67 | ** The following functions are new: 68 | 69 | regress_gp dendogram plsregress 70 | 71 | ** New functions for the generalized extreme value (GEV) distribution: 72 | 73 | gevcdf gevfit gevfit_lmom gevinv gevlike gevpdf gevrnd gevstat 74 | 75 | ** The interface of the following functions has been modified: 76 | 77 | mvnrnd 78 | 79 | ** `kmeans' has been fixed to deal with clusters that contain only 80 | one element. 81 | 82 | ** `normplot' has been fixed to avoid use of functions that have been 83 | removed from Octave core. Also, the plot produced should now display some 84 | aesthetic elements and appropriate legends. 85 | 86 | ** The help text of `mvtrnd' has been improved. 87 | 88 | ** Package is no longer autoloaded. 89 | -------------------------------------------------------------------------------- /ONEWS-1.3.0: -------------------------------------------------------------------------------- 1 | Summary of important user-visible changes for statistics 1.3.0: 2 | ------------------------------------------------------------------- 3 | 4 | ** The following functions are new: 5 | 6 | bbscdf bbsinv bbspdf bbsrnd 7 | binotest 8 | burrcdf burrinv burrpdf burrrnd 9 | gpcdf gpinv gppdf gprnd 10 | grp2idx 11 | mahal 12 | mvtpdf 13 | nakacdf nakainv nakapdf nakarnd 14 | pdf 15 | tricdf triinv tripdf trirnd 16 | violin 17 | 18 | ** Other functions that have been changed for smaller bugfixes, increased 19 | Matlab compatibility, or performance: 20 | 21 | betastat 22 | binostat 23 | cdf 24 | combnk 25 | gevfit 26 | hist3 27 | kmeans 28 | linkage 29 | randsample 30 | squareform 31 | ttest 32 | -------------------------------------------------------------------------------- /doc/statistics.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gnu-octave/statistics/ba093ae5f8dd58432dadccac967881fc58e079e6/doc/statistics.png -------------------------------------------------------------------------------- /inst/@cvpartition/display.m: -------------------------------------------------------------------------------- 1 | ## Copyright (C) 2014 Nir Krakauer 2 | ## 3 | ## This file is part of the statistics package for GNU Octave. 4 | ## 5 | ## This program is free software; you can redistribute it and/or modify 6 | ## it under the terms of the GNU General Public License as published by 7 | ## the Free Software Foundation; either version 3 of the License, or 8 | ## (at your option) any later version. 9 | ## 10 | ## This program is distributed in the hope that it will be useful, 11 | ## but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | ## GNU General Public License for more details. 14 | ## 15 | ## You should have received a copy of the GNU General Public License 16 | ## along with this program; If not, see . 17 | 18 | ## -*- texinfo -*- 19 | ## @deftypefn {statistics} {} display (@var{C}) 20 | ## 21 | ## Display a @samp{cvpartition} object, @var{C}. 22 | ## 23 | ## @seealso{@@cvpartition/cvpartition} 24 | ## @end deftypefn 25 | 26 | function display (C) 27 | 28 | if (nargin != 1) 29 | print_usage (); 30 | endif 31 | 32 | switch C.Type 33 | case "kfold" 34 | str = "K-fold"; 35 | case "given" 36 | str = "Given"; 37 | case "holdout" 38 | str = "HoldOut"; 39 | case "leaveout" 40 | str = "Leave-One-Out"; 41 | case "resubstitution" 42 | str = "Resubstitution"; 43 | otherwise 44 | str = "Unknown-type"; 45 | endswitch 46 | 47 | disp([str " cross validation partition"]) 48 | disp([" N: " num2str(C.NumObservations)]) 49 | disp(["NumTestSets: " num2str(C.NumTestSets)]) 50 | disp([" TrainSize: " num2str(C.TrainSize')]) 51 | disp([" TestSize: " num2str(C.TestSize')]) 52 | 53 | endfunction 54 | 55 | %!test 56 | %! C = cvpartition (ones (10, 1), "KFold", 5); 57 | %! s = evalc ("display (C)"); 58 | %! sout = "K-fold cross validation partition"; 59 | %! assert (strcmpi (s(1:length (sout)), sout), true); 60 | %!test 61 | %! C = cvpartition (ones (10, 1), "HoldOut", 5); 62 | %! s = evalc ("display (C)"); 63 | %! sout = "HoldOut cross validation partition"; 64 | %! assert (strcmpi (s(1:length (sout)), sout), true); 65 | %!test 66 | %! C = cvpartition (ones (10, 1), "LeaveOut", 5); 67 | %! s = evalc ("display (C)"); 68 | %! sout = "Leave-One-Out cross validation partition"; 69 | %! assert (strcmpi (s(1:length (sout)), sout), true); 70 | %!test 71 | %! C = cvpartition (ones (10, 1), "resubstitution", 5); 72 | %! s = evalc ("display (C)"); 73 | %! sout = "Resubstitution cross validation partition"; 74 | %! assert (strcmpi (s(1:length (sout)), sout), true); 75 | %!test 76 | %! C = cvpartition (ones (10, 1), "Given", 5); 77 | %! s = evalc ("display (C)"); 78 | %! sout = "Given cross validation partition"; 79 | %! assert (strcmpi (s(1:length (sout)), sout), true); 80 | 81 | %!error display () 82 | -------------------------------------------------------------------------------- /inst/@cvpartition/get.m: -------------------------------------------------------------------------------- 1 | ## Copyright (C) 2014 Nir Krakauer 2 | ## 3 | ## This file is part of the statistics package for GNU Octave. 4 | ## 5 | ## This program is free software; you can redistribute it and/or modify 6 | ## it under the terms of the GNU General Public License as published by 7 | ## the Free Software Foundation; either version 3 of the License, or 8 | ## (at your option) any later version. 9 | ## 10 | ## This program is distributed in the hope that it will be useful, 11 | ## but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | ## GNU General Public License for more details. 14 | ## 15 | ## You should have received a copy of the GNU General Public License 16 | ## along with this program; If not, see . 17 | 18 | ## -*- texinfo -*- 19 | ## @deftypefn {statistics} {@var{s} = get} (@var{C}, @var{f}) 20 | ## 21 | ## Get a field, @var{f}, from a @samp{cvpartition} object, @var{C}. 22 | ## 23 | ## @seealso{@@cvpartition/cvpartition} 24 | ## @end deftypefn 25 | 26 | function s = get (c, f) 27 | if (nargin == 1) 28 | s = c; 29 | elseif (nargin == 2) 30 | if (ischar (f)) 31 | switch (f) 32 | case {"classes", "inds", "n_classes", "NumObservations", ... 33 | "NumTestSets", "TestSize", "TrainSize", "Type"} 34 | s = eval (["struct(c)." f]); 35 | otherwise 36 | error ("get: invalid property %s.", f); 37 | endswitch 38 | else 39 | error ("get: expecting the property to be a string."); 40 | endif 41 | else 42 | print_usage (); 43 | endif 44 | endfunction 45 | 46 | %!shared C 47 | %! C = cvpartition (ones (10, 1), "KFold", 5); 48 | %!assert (get (C, "NumObservations"), 10); 49 | %!assert (get (C, "NumTestSets"), 5); 50 | %!assert (get (C, "TrainSize"), ones(5,1) * 8); 51 | %!assert (get (C, "TestSize"), ones (5,1) * 2); 52 | %!assert (get (C, "inds"), [1 1 2 2 3 3 4 4 5 5]'); 53 | %!assert (get (C, "Type"), "kfold"); 54 | 55 | %!error get (C, "some") 56 | %!error get (C, 25) 57 | %!error get (C, {25}) 58 | -------------------------------------------------------------------------------- /inst/@cvpartition/repartition.m: -------------------------------------------------------------------------------- 1 | ## Copyright (C) 2014 Nir Krakauer 2 | ## 3 | ## This file is part of the statistics package for GNU Octave. 4 | ## 5 | ## This program is free software; you can redistribute it and/or modify 6 | ## it under the terms of the GNU General Public License as published by 7 | ## the Free Software Foundation; either version 3 of the License, or 8 | ## (at your option) any later version. 9 | ## 10 | ## This program is distributed in the hope that it will be useful, 11 | ## but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | ## GNU General Public License for more details. 14 | ## 15 | ## You should have received a copy of the GNU General Public License 16 | ## along with this program; If not, see . 17 | 18 | ## -*- texinfo -*- 19 | ## @deftypefn {statistics} {@var{Cnew} =} repartition (@var{C}) 20 | ## 21 | ## Return a new cvpartition object. 22 | ## 23 | ## @var{C} should be a @samp{cvpartition} object. @var{Cnew} will use the same 24 | ## partition_type as @var{C} but redo any randomization performed (currently, 25 | ## only the HoldOut type uses randomization). 26 | ## 27 | ## @seealso{@@cvpartition/cvpartition} 28 | ## @end deftypefn 29 | 30 | function Cnew = repartition (C) 31 | 32 | if (nargin < 1 || nargin > 2) 33 | print_usage (); 34 | endif 35 | 36 | Cnew = C; 37 | 38 | switch (C.Type) 39 | case 'kfold' 40 | case 'given' 41 | case 'holdout' # Currently, only the HoldOut method uses randomization 42 | n = C.NumObservations; 43 | k = C.TestSize; 44 | n_classes = C.n_classes; 45 | if (k < 1) 46 | f = k; # target fraction to sample 47 | k = round (k * n); #number of samples 48 | else 49 | f = k / n; 50 | endif 51 | inds = zeros (n, 1, "logical"); 52 | if (n_classes == 1) 53 | inds(randsample(n, k)) = true; #indices for test set 54 | else # sample from each class 55 | j = C.classes; #integer class labels 56 | n_per_class = accumarray (j, 1); 57 | n_classes = numel (n_per_class); 58 | k_check = 0; 59 | for i = 1:n_classes 60 | ki = round(f*n_per_class(i)); 61 | inds(find(j == i)(randsample(n_per_class(i), ki))) = true; 62 | k_check += ki; 63 | endfor 64 | if (k_check < k) # add random elements to test set to make it k 65 | inds(find(!inds)(randsample(n - k_check, k - k_check))) = true; 66 | elseif (k_check > k) # remove random elements from test set 67 | inds(find(inds)(randsample(k_check, k_check - k))) = false; 68 | endif 69 | endif 70 | Cnew.inds = inds; 71 | case "leaveout" 72 | case "resubstitution" 73 | endswitch 74 | endfunction 75 | 76 | %!test 77 | %! C = cvpartition (ones (10, 1), "KFold", 5); 78 | %! Cnew = repartition (C); 79 | %! assert (isa (Cnew, "cvpartition"), true); 80 | %!test 81 | %! C = cvpartition (ones (100, 1), "HoldOut", 5); 82 | %! Cnew = repartition (C); 83 | %! indC = get (C, "inds"); 84 | %! indCnew = get (Cnew, "inds"); 85 | %! assert (isequal (indC, indCnew), false); 86 | -------------------------------------------------------------------------------- /inst/@cvpartition/set.m: -------------------------------------------------------------------------------- 1 | ## Copyright (C) 2014 Nir Krakauer 2 | ## 3 | ## This file is part of the statistics package for GNU Octave. 4 | ## 5 | ## This program is free software; you can redistribute it and/or modify 6 | ## it under the terms of the GNU General Public License as published by 7 | ## the Free Software Foundation; either version 3 of the License, or 8 | ## (at your option) any later version. 9 | ## 10 | ## This program is distributed in the hope that it will be useful, 11 | ## but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | ## GNU General Public License for more details. 14 | ## 15 | ## You should have received a copy of the GNU General Public License 16 | ## along with this program; If not, see . 17 | 18 | ## -*- texinfo -*- 19 | ## @deftypefn {statistics} {@var{Cnew} =} set (@var{C}, @var{field}, @var{value}) 20 | ## 21 | ## Set @var{field}, in a @samp{cvpartition} object, @var{C}. 22 | ## 23 | ## @seealso{@@cvpartition/cvpartition} 24 | ## @end deftypefn 25 | 26 | function s = set (c, varargin) 27 | s = struct(c); 28 | if (length (varargin) < 2 || rem (length (varargin), 2) != 0) 29 | error ("set: expecting field/value pairs."); 30 | endif 31 | while (length (varargin) > 1) 32 | prop = varargin{1}; 33 | val = varargin{2}; 34 | varargin(1:2) = []; 35 | if (ischar (prop)) 36 | switch (prop) 37 | case {"classes", "inds", "n_classes", "NumObservations", ... 38 | "NumTestSets", "TestSize", "TrainSize", "Type"} 39 | s = setfield (s, prop, val); 40 | otherwise 41 | error ("set: invalid field %s.", prop); 42 | endswitch 43 | else 44 | error ("set: expecting the field to be a string."); 45 | endif 46 | endwhile 47 | s = class (s, "cvpartition"); 48 | endfunction 49 | 50 | %!shared C 51 | %! C = cvpartition (ones (10, 1), "KFold", 5); 52 | %!test 53 | %! Cnew = set (C, "inds", [1 2 2 2 3 4 3 4 5 5]'); 54 | %! assert (get (Cnew, "inds"), [1 2 2 2 3 4 3 4 5 5]'); 55 | %!error set (C) 56 | %!error set (C, "NumObservations") 57 | %!error set (C, "some", 15) 58 | %!error set (C, 15, 15) 59 | -------------------------------------------------------------------------------- /inst/@cvpartition/test.m: -------------------------------------------------------------------------------- 1 | ## Copyright (C) 2014 Nir Krakauer 2 | ## 3 | ## This file is part of the statistics package for GNU Octave. 4 | ## 5 | ## This program is free software; you can redistribute it and/or modify 6 | ## it under the terms of the GNU General Public License as published by 7 | ## the Free Software Foundation; either version 3 of the License, or 8 | ## (at your option) any later version. 9 | ## 10 | ## This program is distributed in the hope that it will be useful, 11 | ## but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | ## GNU General Public License for more details. 14 | ## 15 | ## You should have received a copy of the GNU General Public License 16 | ## along with this program; If not, see . 17 | 18 | ## -*- texinfo -*- 19 | ## @deftypefn {statistics} {@var{inds} =} test (@var{C}, [@var{i}]) 20 | ## 21 | ## Return logical vector for testing-subset indices from a @samp{cvpartition} 22 | ## object, @var{C}. @var{i} is the fold index (default is 1). 23 | ## 24 | ## @seealso{@@cvpartition/cvpartition, @@cvpartition/training} 25 | ## @end deftypefn 26 | 27 | function inds = test (C, i = []) 28 | 29 | if (nargin < 1 || nargin > 2) 30 | print_usage (); 31 | endif 32 | 33 | if (nargin < 2 || isempty (i)) 34 | i = 1; 35 | endif 36 | 37 | switch (C.Type) 38 | case {"kfold", "given"} 39 | inds = C.inds == i; 40 | case "holdout" 41 | inds = C.inds; 42 | case "leaveout" 43 | inds = zeros(C.NumObservations, 1, "logical"); 44 | inds(i) = true; 45 | case "resubstitution" 46 | inds = ones(C.NumObservations, 1, "logical"); 47 | endswitch 48 | 49 | endfunction 50 | 51 | %!shared C 52 | %! C = cvpartition (ones (10, 1), "KFold", 5); 53 | %!assert (test (C, 1), logical ([1 1 0 0 0 0 0 0 0 0]')) 54 | %!assert (test (C, 2), logical ([0 0 1 1 0 0 0 0 0 0]')) 55 | %!assert (test (C, 3), logical ([0 0 0 0 1 1 0 0 0 0]')) 56 | %!assert (test (C, 4), logical ([0 0 0 0 0 0 1 1 0 0]')) 57 | %!assert (test (C, 5), logical ([0 0 0 0 0 0 0 0 1 1]')) 58 | %!test 59 | %! C = set (C, "inds", [1 2 2 2 3 4 3 4 5 5]'); 60 | %!assert (test (C), logical ([1 0 0 0 0 0 0 0 0 0]')) 61 | %!assert (test (C, 2), logical ([0 1 1 1 0 0 0 0 0 0]')) 62 | %!assert (test (C, 3), logical ([0 0 0 0 1 0 1 0 0 0]')) 63 | -------------------------------------------------------------------------------- /inst/@cvpartition/training.m: -------------------------------------------------------------------------------- 1 | ## Copyright (C) 2014 Nir Krakauer 2 | ## 3 | ## This file is part of the statistics package for GNU Octave. 4 | ## 5 | ## This program is free software; you can redistribute it and/or modify 6 | ## it under the terms of the GNU General Public License as published by 7 | ## the Free Software Foundation; either version 3 of the License, or 8 | ## (at your option) any later version. 9 | ## 10 | ## This program is distributed in the hope that it will be useful, 11 | ## but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | ## GNU General Public License for more details. 14 | ## 15 | ## You should have received a copy of the GNU General Public License 16 | ## along with this program; If not, see . 17 | 18 | ## -*- texinfo -*- 19 | ## @deftypefn {statistics} {@var{inds} =} training (@var{C}, [@var{i}]) 20 | ## 21 | ## Return logical vector for training-subset indices from a @samp{cvpartition} 22 | ## object, @var{C}. @var{i} is the fold index (default is 1). 23 | ## 24 | ## @seealso{@@cvpartition/cvpartition, @@cvpartition/test} 25 | ## @end deftypefn 26 | 27 | function inds = training (C, i = []) 28 | 29 | if (nargin < 1 || nargin > 2) 30 | print_usage (); 31 | endif 32 | 33 | if (nargin < 2 || isempty (i)) 34 | i = 1; 35 | endif 36 | 37 | switch (C.Type) 38 | case {"kfold", "given"} 39 | inds = C.inds != i; 40 | case "holdout" 41 | inds = ! C.inds; 42 | case "leaveout" 43 | inds = ones (C.NumObservations, 1, "logical"); 44 | inds(i) = false; 45 | case "resubstitution" 46 | inds = ones (C.NumObservations, 1, "logical"); 47 | endswitch 48 | 49 | endfunction 50 | 51 | 52 | %!shared C 53 | %! C = cvpartition (ones (10, 1), "KFold", 5); 54 | %!assert (training (C, 1), logical ([0 0 1 1 1 1 1 1 1 1]')) 55 | %!assert (training (C, 2), logical ([1 1 0 0 1 1 1 1 1 1]')) 56 | %!assert (training (C, 3), logical ([1 1 1 1 0 0 1 1 1 1]')) 57 | %!assert (training (C, 4), logical ([1 1 1 1 1 1 0 0 1 1]')) 58 | %!assert (training (C, 5), logical ([1 1 1 1 1 1 1 1 0 0]')) 59 | %!test 60 | %! C = set (C, "inds", [1 2 2 2 3 4 3 4 5 5]'); 61 | %!assert (training (C), logical ([0 1 1 1 1 1 1 1 1 1]')) 62 | %!assert (training (C, 2), logical ([1 0 0 0 1 1 1 1 1 1]')) 63 | %!assert (training (C, 3), logical ([1 1 1 1 0 1 0 1 1 1]')) 64 | -------------------------------------------------------------------------------- /inst/Classification/private/parseScoreTransform.m: -------------------------------------------------------------------------------- 1 | ## Copyright (C) 2024 Andreas Bertsatos 2 | ## 3 | ## This file is part of the statistics package for GNU Octave. 4 | ## 5 | ## This program is free software: you can redistribute it and/or 6 | ## modify it under the terms of the GNU General Public License as 7 | ## published by the Free Software Foundation, either version 3 of the 8 | ## License, or (at your option) any later version. 9 | ## 10 | ## This program is distributed in the hope that it will be useful, but 11 | ## WITHOUT ANY WARRANTY; without even the implied warranty of 12 | ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | ## General Public License for more details. 14 | ## 15 | ## You should have received a copy of the GNU General Public License 16 | ## along with this program; see the file COPYING. If not, see 17 | ## . 18 | 19 | ## -*- texinfo -*- 20 | ## @deftypefn {Private Function} parseScoreTransform (@var{pd}, @var{classname}) 21 | ## 22 | ## Parse ScoreTransform for Classification objects. 23 | ## 24 | ## @end deftypefn 25 | 26 | function f = parseScoreTransform (ScoreTransform, classname) 27 | 28 | stList = {"doublelogit", "invlogit", "ismax", "logit", "none", ... 29 | "identity", "sign", "symmetric", "symmetricismax", ... 30 | "symmetriclogit"}; 31 | if (! (ischar (ScoreTransform) || 32 | strcmp (class (ScoreTransform), "function_handle"))) 33 | error (strcat (["%s: 'ScoreTransform' must be a character"], ... 34 | [" vector or a function handle."]), classname); 35 | endif 36 | if (! ismember (ScoreTransform, stList)) 37 | error ("%s: unrecognized 'ScoreTransform' function.", classname); 38 | endif 39 | ## Handle ScoreTransform here 40 | if (is_function_handle (ScoreTransform)) 41 | m = eye (5); 42 | if (! isequal (size (m), size (ScoreTransform (m)))) 43 | error (strcat (["%s: function handle for 'ScoreTransform' must"], ... 44 | [" return the same size as its input."]), classname); 45 | endif 46 | f = ScoreTransform; 47 | else 48 | if (strcmpi ("doublelogit", ScoreTransform)) 49 | f = @(x) 1 ./ (1 + exp .^ (-2 * x)); 50 | elseif (strcmpi ("invlogit", ScoreTransform)) 51 | f = @(x) log (x ./ (1 - x)); 52 | elseif (strcmpi ("ismax", ScoreTransform)) 53 | f = eval (sprintf ("@(x) ismax (x)")); 54 | elseif (strcmpi ("logit", ScoreTransform)) 55 | f = @(x) 1 ./ (1 + exp .^ (-x)); 56 | elseif (any (strcmpi ({"identity", "none"}, ScoreTransform))) 57 | f = 'none'; 58 | elseif (strcmpi ("sign", ScoreTransform)) 59 | f = @(x) sign (x); 60 | elseif (strcmpi ("symmetric", ScoreTransform)) 61 | f = @(x) 2 * x - 1; 62 | elseif (strcmpi ("symmetricismax", ScoreTransform)) 63 | f = eval (sprintf ("@(x) symmetricismax (x)")); 64 | elseif (strcmpi ("symmetriclogit", ScoreTransform)) 65 | f = @(x) 2 ./ (1 + exp .^ (-x)) - 1; 66 | endif 67 | endif 68 | 69 | endfunction 70 | 71 | ## Helper functions for ScoreTransform 72 | function out = ismax (score) 73 | out = score; 74 | out(score == max (score)) = 1; 75 | out(score != max (score)) = 0; 76 | endfunction 77 | 78 | function out = symmetricismax (score) 79 | out = score; 80 | out(score == max (score)) = 1; 81 | out(score != max (score)) = -1; 82 | endfunction 83 | -------------------------------------------------------------------------------- /inst/PKG_ADD: -------------------------------------------------------------------------------- 1 | if (compare_versions (version (), "9", "<")) 2 | a1_e324kporit985_itogj3_dirlist = ... 3 | {"Classification", "Clustering", "datasets", "dist_fit", "dist_fun", ... 4 | "dist_obj", "dist_stat", "dist_wrap", "Regression", "shadow9"}; 5 | else 6 | a1_e324kporit985_itogj3_dirlist = ... 7 | {"Classification", "Clustering", "datasets", "dist_fit", "dist_fun", ... 8 | "dist_obj", "dist_stat", "dist_wrap", "Regression"}; 9 | endif 10 | 11 | d_2seRTE546_oyi_795jg09_dirname = fileparts (canonicalize_file_name ... 12 | (mfilename ("fullpath"))); 13 | 14 | for iiII123DRT_idx = 1:length (a1_e324kporit985_itogj3_dirlist) 15 | addpath (fullfile (d_2seRTE546_oyi_795jg09_dirname, ... 16 | a1_e324kporit985_itogj3_dirlist{iiII123DRT_idx})); 17 | endfor 18 | 19 | clear a1_e324kporit985_itogj3_dirlist 20 | clear d_2seRTE546_oyi_795jg09_dirname iiII123DRT_idx 21 | 22 | warning ("off", "Octave:data-file-in-path") 23 | -------------------------------------------------------------------------------- /inst/PKG_DEL: -------------------------------------------------------------------------------- 1 | clear -f editDistance libsvmread libsvmwrite svmpredict svmtrain 2 | 3 | if (compare_versions (version (), "9", "<")) 4 | a1_e324kporit985_itogj3_dirlist = ... 5 | {"Classification", "Clustering", "datasets", "dist_fit", "dist_fun", ... 6 | "dist_obj", "dist_stat", "dist_wrap", "Regression", "shadow9"}; 7 | else 8 | a1_e324kporit985_itogj3_dirlist = ... 9 | {"Classification", "Clustering", "datasets", "dist_fit", "dist_fun", ... 10 | "dist_obj", "dist_stat", "dist_wrap", "Regression"}; 11 | endif 12 | 13 | d_2seRTE546_oyi_795jg09_dirname = fileparts (canonicalize_file_name ... 14 | (mfilename ("fullpath"))); 15 | 16 | for iiII123DRT_idx = 1:length (a1_e324kporit985_itogj3_dirlist) 17 | rmpath (fullfile (d_2seRTE546_oyi_795jg09_dirname, ... 18 | a1_e324kporit985_itogj3_dirlist{iiII123DRT_idx})); 19 | endfor 20 | 21 | clear a1_e324kporit985_itogj3_dirlist 22 | clear d_2seRTE546_oyi_795jg09_dirname iiII123DRT_idx 23 | -------------------------------------------------------------------------------- /inst/canoncorr.m: -------------------------------------------------------------------------------- 1 | ## Copyright (C) 2016-2019 by Nir Krakauer 2 | ## 3 | ## This file is part of the statistics package for GNU Octave. 4 | ## 5 | ## This program is free software; you can redistribute it and/or modify it under 6 | ## the terms of the GNU General Public License as published by the Free Software 7 | ## Foundation; either version 3 of the License, or (at your option) any later 8 | ## version. 9 | ## 10 | ## This program is distributed in the hope that it will be useful, but WITHOUT 11 | ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 12 | ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 13 | ## details. 14 | ## 15 | ## You should have received a copy of the GNU General Public License along with 16 | ## this program; if not, see . 17 | 18 | ## -*- texinfo -*- 19 | ## @deftypefn {statistics} {[@var{A}, @var{B}, @var{r}, @var{U}, @var{V}] =} canoncorr (@var{X}, @var{Y}) 20 | ## 21 | ## Canonical correlation analysis. 22 | ## 23 | ## Given @var{X} (size @var{k}*@var{m}) and @var{Y} (@var{k}*@var{n}), returns 24 | ## projection matrices of canonical coefficients @var{A} (size @var{m}*@var{d}, 25 | ## where @var{d} is the smallest of @var{m}, @var{n}, @var{d}) and @var{B} 26 | ## (size @var{m}*@var{d}); the canonical correlations @var{r} (1*@var{d}, 27 | ## arranged in decreasing order); the canonical variables @var{U}, @var{V} 28 | ## (both @var{k}*@var{d}, with orthonormal columns); and @var{stats}, 29 | ## a structure containing results from Bartlett's chi-square and Rao's F tests 30 | ## of significance. 31 | ## 32 | ## @seealso{princomp} 33 | ## @end deftypefn 34 | 35 | function [A,B,r,U,V,stats] = canoncorr (X,Y) 36 | 37 | k = size (X, 1); # should also be size (Y, 1) 38 | m = size (X, 2); 39 | n = size (Y, 2); 40 | d = min ([k m n]); 41 | 42 | X = center (X); 43 | Y = center (Y); 44 | 45 | [Qx Rx] = qr (X, 0); 46 | [Qy Ry] = qr (Y, 0); 47 | 48 | [U S V] = svd (Qx' * Qy, "econ"); 49 | 50 | A = Rx \ U(:, 1:d); 51 | B = Ry \ V(:, 1:d); 52 | 53 | ## A, B are scaled to make the covariance matrices of the outputs U, V 54 | ## identity matrices 55 | f = sqrt (k-1); 56 | A .*= f; 57 | B .*= f; 58 | 59 | if (nargout > 2) 60 | r = max(0, min(diag(S), 1))'; 61 | endif 62 | if (nargout > 3) 63 | U = X * A; 64 | endif 65 | if (nargout > 4) 66 | V = Y * B; 67 | endif 68 | 69 | if (nargout > 5) 70 | Wilks = fliplr(cumprod(fliplr((1 - r .^ 2)))); 71 | chisq = - (k - 1 - (m + n + 1)/2) * log(Wilks); 72 | df1 = (m - (1:d) + 1) .* (n - (1:d) + 1); 73 | pChisq = 1 - chi2cdf (chisq, df1); 74 | s = sqrt((df1.^2 - 4) ./ ((m - (1:d) + 1).^2 + (n - (1:d) + 1).^2 - 5)); 75 | df2 = (k - 1 - (m + n + 1)/2) * s - df1/2 + 1; 76 | ls = Wilks .^ (1 ./ s); 77 | F = (1 ./ ls - 1) .* (df2 ./ df1); 78 | pF = 1 - fcdf (F, df1, df2); 79 | stats.Wilks = Wilks; 80 | stats.df1 = df1; 81 | stats.df2 = df2; 82 | stats.F = F; 83 | stats.pF = pF; 84 | stats.chisq = chisq; 85 | stats.pChisq = pChisq; 86 | endif 87 | endfunction 88 | 89 | %!shared X,Y,A,B,r,U,V,k 90 | %! k = 10; 91 | %! X = [1:k; sin(1:k); cos(1:k)]'; Y = [tan(1:k); tanh((1:k)/k)]'; 92 | %! [A,B,r,U,V,stats] = canoncorr (X,Y); 93 | %!assert (A, [-0.329229 0.072908; 0.074870 1.389318; -0.069302 -0.024109], 1E-6); 94 | %!assert (B, [-0.017086 -0.398402; -4.475049 -0.824538], 1E-6); 95 | %!assert (r, [0.99590 0.26754], 1E-5); 96 | %!assert (U, center(X) * A, 10*eps); 97 | %!assert (V, center(Y) * B, 10*eps); 98 | %!assert (cov(U), eye(size(U, 2)), 10*eps); 99 | %!assert (cov(V), eye(size(V, 2)), 10*eps); 100 | %! rand ("state", 1); [A,B,r] = canoncorr (rand(5, 10),rand(5, 20)); 101 | %!assert (r, ones(1, 5), 10*eps); 102 | -------------------------------------------------------------------------------- /inst/cdfcalc.m: -------------------------------------------------------------------------------- 1 | ## Copyright (C) 2022 Andreas Bertsatos 2 | ## 3 | ## This file is part of the statistics package for GNU Octave. 4 | ## 5 | ## This program is free software; you can redistribute it and/or modify it under 6 | ## the terms of the GNU General Public License as published by the Free Software 7 | ## Foundation; either version 3 of the License, or (at your option) any later 8 | ## version. 9 | ## 10 | ## This program is distributed in the hope that it will be useful, but WITHOUT 11 | ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 12 | ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 13 | ## details. 14 | ## 15 | ## You should have received a copy of the GNU General Public License along with 16 | ## this program; if not, see . 17 | 18 | ## -*- texinfo -*- 19 | ## @deftypefn {statistics} {[@var{yCDF}, @var{xCDF}, @var{n}, @var{emsg}, @var{eid}] =} cdfcalc (@var{x}) 20 | ## 21 | ## Calculate an empirical cumulative distribution function. 22 | ## 23 | ## @code{[@var{yCDF}, @var{xCDF}] = cdfcalc (@var{x})} calculates an empirical 24 | ## cumulative distribution function (CDF) of the observations in the data sample 25 | ## vector @var{x}. @var{x} may be a row or column vector, and represents a 26 | ## random sample of observations from some underlying distribution. On return 27 | ## @var{xCDF} is the set of @var{x} values at which the CDF increases. 28 | ## At XCDF(i), the function increases from YCDF(i) to YCDF(i+1). 29 | ## 30 | ## @code{[@var{yCDF}, @var{xCDF}, @var{n}] = cdfcalc (@var{x})} also returns 31 | ## @var{n}, the sample size. 32 | ## 33 | ## @code{[@var{yCDF}, @var{xCDF}, @var{n}, @var{emsg}, @var{eid}] = cdfcalc 34 | ## (@var{x})} also returns an error message and error id if @var{x} is not a 35 | ## vector or if it contains no values other than NaN. 36 | ## 37 | ## @seealso{cdfplot} 38 | ## @end deftypefn 39 | 40 | function [yCDF, xCDF, n, emsg, eid] = cdfcalc (x) 41 | ## Check number of input and output argument 42 | narginchk (1,1); 43 | nargoutchk (2,5); 44 | ## Add defaults 45 | yCDF = []; 46 | xCDF = []; 47 | n = 0; 48 | ## Check that x is a vector 49 | if (! isvector (x)) 50 | warning ("cdfcalc: vector required as input."); 51 | emsg = "VectorRequired"; 52 | eid = "VectorRequired"; 53 | return 54 | endif 55 | ## Remove NaNs and check if there are remaining data to calculate ecdf 56 | x = x(! isnan (x)); 57 | n = length (x); 58 | if (n == 0) 59 | warning ("cdfcalc: not enough data."); 60 | emsg = "NotEnoughData"; 61 | eid = "NotEnoughData"; 62 | return 63 | endif 64 | ## Sort data in ascending order 65 | x = sort (x(:)); 66 | ## Get cumulative sums 67 | yCDF = (1:n)' / n; 68 | ## Remove duplicates, keep the last one 69 | keep_idx = ([diff(x(:)); 1] > 0); 70 | xCDF = x(keep_idx); 71 | yCDF = [0; yCDF(keep_idx)]; 72 | emsg = ''; 73 | eid = ''; 74 | endfunction 75 | 76 | %!test 77 | %! x = [2, 4, 3, 2, 4, 3, 2, 5, 6, 4]; 78 | %! [yCDF, xCDF, n, emsg, eid] = cdfcalc (x); 79 | %! assert (yCDF, [0, 0.3, 0.5, 0.8, 0.9, 1]'); 80 | %! assert (xCDF, [2, 3, 4, 5, 6]'); 81 | %! assert (n, 10); 82 | %!shared x 83 | %! x = [2, 4, 3, 2, 4, 3, 2, 5, 6, 4]; 84 | %!error yCDF = cdfcalc (x); 85 | %!error [yCDF, xCDF] = cdfcalc (); 86 | %!error [yCDF, xCDF] = cdfcalc (x, x); 87 | %!warning [yCDF, xCDF] = cdfcalc (ones(10,2)); 88 | -------------------------------------------------------------------------------- /inst/cdfplot.m: -------------------------------------------------------------------------------- 1 | ## Copyright (C) 2022 Andreas Bertsatos 2 | ## 3 | ## This file is part of the statistics package for GNU Octave. 4 | ## 5 | ## This program is free software; you can redistribute it and/or modify it under 6 | ## the terms of the GNU General Public License as published by the Free Software 7 | ## Foundation; either version 3 of the License, or (at your option) any later 8 | ## version. 9 | ## 10 | ## This program is distributed in the hope that it will be useful, but WITHOUT 11 | ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 12 | ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 13 | ## details. 14 | ## 15 | ## You should have received a copy of the GNU General Public License along with 16 | ## this program; if not, see . 17 | 18 | ## -*- texinfo -*- 19 | ## @deftypefn {statistics} {@var{hCDF} =} cdfplot (@var{x}) 20 | ## @deftypefnx {statistics} {[@var{hCDF}, @var{stats}] =} cdfplot (@var{x}) 21 | ## 22 | ## Display an empirical cumulative distribution function. 23 | ## 24 | ## @code{@var{hCDF} = cdfplot (@var{x})} plots an empirical cumulative 25 | ## distribution function (CDF) of the observations in the data sample vector 26 | ## @var{x}. @var{x} may be a row or column vector, and represents a random 27 | ## sample of observations from some underlying distribution. 28 | ## 29 | ## @code{cdfplot} plots F(x), the empirical (or sample) CDF versus the 30 | ## observations in @var{x}. The empirical CDF, F(x), is defined as follows: 31 | ## 32 | ## F(x) = (Number of observations <= x) / (Total number of observations) 33 | ## 34 | ## for all values in the sample vector @var{x}. NaNs are ignored. @var{hCDF} 35 | ## is the handle of the empirical CDF curve (a handle hraphics 'line' object). 36 | ## 37 | ## @code{[@var{hCDF}, @var{stats}] = cdfplot (@var{x})} also returns a structure 38 | ## with the following fields as a statistical summary. 39 | ## 40 | ## @multitable @columnfractions 0.05 0.3 0.65 41 | ## @item @tab STATS.min @tab minimum value of @var{x} 42 | ## @item @tab STATS.max @tab maximum value of @var{x} 43 | ## @item @tab STATS.mean @tab sample mean of @var{x} 44 | ## @item @tab STATS.median @tab sample median (50th percentile) of @var{x} 45 | ## @item @tab STATS.std @tab sample standard deviation of @var{x} 46 | ## @end multitable 47 | ## 48 | ## @seealso{qqplot, cdfcalc} 49 | ## @end deftypefn 50 | 51 | function [hCDF, stats] = cdfplot (x) 52 | ## Check number of input arguments 53 | narginchk (1,1); 54 | ## Calculate sample cdf 55 | [yy, xx, ~, ~, eid] = cdfcalc (x); 56 | ## Check for errors returned from cdfcalc 57 | if (strcmpi (eid, "VectorRequired")) 58 | error ("cdfplot: vector required as input."); 59 | elseif (strcmpi (eid, "NotEnoughData")) 60 | error("cdfplot: not enough data."); 61 | endif 62 | ## Create vectors for plotting 63 | k = length (xx); 64 | n = reshape (repmat (1:k, 2, 1), 2*k, 1); 65 | xCDF = [-Inf; xx(n); Inf]; 66 | yCDF = [0; 0; yy(1+n)]; 67 | ## Plot cdf 68 | h = plot (xCDF, yCDF); 69 | grid ('on') 70 | xlabel ("x") 71 | ylabel ("F(x)") 72 | title ("CDF plot of x"); 73 | 74 | ## Return requested output arguments 75 | if (nargout > 0) 76 | hCDF = h; 77 | endif 78 | if (nargout > 1) 79 | stats.min = nanmin (x); 80 | stats.max = nanmax (x); 81 | stats.mean = mean (x, "omitnan"); 82 | stats.median = median (x, "omitnan"); 83 | stats.std = std (x, "omitnan"); 84 | endif 85 | endfunction 86 | 87 | %!demo 88 | %! x = randn(100,1); 89 | %! cdfplot (x); 90 | 91 | ## Test results 92 | %!test 93 | %! hf = figure ("visible", "off"); 94 | %! unwind_protect 95 | %! x = [2, 4, 3, 2, 4, 3, 2, 5, 6, 4]; 96 | %! [hCDF, stats] = cdfplot (x); 97 | %! assert (stats.min, 2); 98 | %! assert (stats.max, 6); 99 | %! assert (stats.median, 3.5); 100 | %! assert (stats.std, 1.35400640077266, 1e-14); 101 | %! unwind_protect_cleanup 102 | %! close (hf); 103 | %! end_unwind_protect 104 | %!test 105 | %! hf = figure ("visible", "off"); 106 | %! unwind_protect 107 | %! x = randn(100,1); 108 | %! cdfplot (x); 109 | %! unwind_protect_cleanup 110 | %! close (hf); 111 | %! end_unwind_protect 112 | 113 | ## Test input validation 114 | %!error cdfplot (); 115 | %!error cdfplot ([x',x']); 116 | %!error cdfplot ([NaN, NaN, NaN, NaN]); 117 | -------------------------------------------------------------------------------- /inst/combnk.m: -------------------------------------------------------------------------------- 1 | ## Copyright (C) 2010 Soren Hauberg 2 | ## 3 | ## This file is part of the statistics package for GNU Octave. 4 | ## 5 | ## This program is free software; you can redistribute it and/or modify it under 6 | ## the terms of the GNU General Public License as published by the Free Software 7 | ## Foundation; either version 3 of the License, or (at your option) any later 8 | ## version. 9 | ## 10 | ## This program is distributed in the hope that it will be useful, but WITHOUT 11 | ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 12 | ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 13 | ## details. 14 | ## 15 | ## You should have received a copy of the GNU General Public License along with 16 | ## this program; if not, see . 17 | 18 | ## -*- texinfo -*- 19 | ## @deftypefn {statistics} {@var{c} =} combnk (@var{data}, @var{k}) 20 | ## 21 | ## Return all combinations of @var{k} elements in @var{data}. 22 | ## 23 | ## @end deftypefn 24 | 25 | function retval = combnk (data, k) 26 | ## Check input 27 | if (nargin != 2) 28 | print_usage; 29 | elseif (! isvector (data)) 30 | error ("combnk: first input argument must be a vector"); 31 | elseif (!isreal (k) || k != round (k) || k < 0) 32 | error ("combnk: second input argument must be a non-negative integer"); 33 | endif 34 | 35 | ## Simple checks 36 | n = numel (data); 37 | if (k == 0 || k > n) 38 | retval = resize (data, 0, k); 39 | elseif (k == n) 40 | retval = data (:).'; 41 | else 42 | retval = __combnk__ (data, k); 43 | endif 44 | 45 | ## For some odd reason Matlab seems to treat strings differently compared to 46 | ## other data-types... 47 | if (ischar (data)) 48 | retval = flipud (retval); 49 | endif 50 | endfunction 51 | 52 | function retval = __combnk__ (data, k) 53 | ## Recursion stopping criteria 54 | if (k == 1) 55 | retval = data (:); 56 | else 57 | ## Process data 58 | n = numel (data); 59 | if (iscell (data)) 60 | retval = {}; 61 | else 62 | retval = []; 63 | endif 64 | for j = 1:n 65 | C = __combnk__ (data ((j+1):end), k-1); 66 | C = cat (2, repmat (data (j), rows (C), 1), C); 67 | if (! isempty (C)) 68 | if (isempty (retval)) 69 | retval = C; 70 | else 71 | retval = [retval; C]; 72 | endif 73 | endif 74 | endfor 75 | endif 76 | endfunction 77 | 78 | %!demo 79 | %! c = combnk (1:5, 2); 80 | %! disp ("All pairs of integers between 1 and 5:"); 81 | %! disp (c); 82 | 83 | %!test 84 | %! c = combnk (1:3, 2); 85 | %! assert (c, [1, 2; 1, 3; 2, 3]); 86 | 87 | %!test 88 | %! c = combnk (1:3, 6); 89 | %! assert (isempty (c)); 90 | 91 | %!test 92 | %! c = combnk ({1, 2, 3}, 2); 93 | %! assert (c, {1, 2; 1, 3; 2, 3}); 94 | 95 | %!test 96 | %! c = combnk ("hello", 2); 97 | %! assert (c, ["lo"; "lo"; "ll"; "eo"; "el"; "el"; "ho"; "hl"; "hl"; "he"]); 98 | -------------------------------------------------------------------------------- /inst/datasets/acetylene.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gnu-octave/statistics/ba093ae5f8dd58432dadccac967881fc58e079e6/inst/datasets/acetylene.mat -------------------------------------------------------------------------------- /inst/datasets/arrhythmia.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gnu-octave/statistics/ba093ae5f8dd58432dadccac967881fc58e079e6/inst/datasets/arrhythmia.mat -------------------------------------------------------------------------------- /inst/datasets/carbig.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gnu-octave/statistics/ba093ae5f8dd58432dadccac967881fc58e079e6/inst/datasets/carbig.mat -------------------------------------------------------------------------------- /inst/datasets/carsmall.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gnu-octave/statistics/ba093ae5f8dd58432dadccac967881fc58e079e6/inst/datasets/carsmall.mat -------------------------------------------------------------------------------- /inst/datasets/cereal.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gnu-octave/statistics/ba093ae5f8dd58432dadccac967881fc58e079e6/inst/datasets/cereal.mat -------------------------------------------------------------------------------- /inst/datasets/examgrades.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gnu-octave/statistics/ba093ae5f8dd58432dadccac967881fc58e079e6/inst/datasets/examgrades.mat -------------------------------------------------------------------------------- /inst/datasets/fisheriris.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gnu-octave/statistics/ba093ae5f8dd58432dadccac967881fc58e079e6/inst/datasets/fisheriris.mat -------------------------------------------------------------------------------- /inst/datasets/gradespaired.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gnu-octave/statistics/ba093ae5f8dd58432dadccac967881fc58e079e6/inst/datasets/gradespaired.mat -------------------------------------------------------------------------------- /inst/datasets/hald.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gnu-octave/statistics/ba093ae5f8dd58432dadccac967881fc58e079e6/inst/datasets/hald.mat -------------------------------------------------------------------------------- /inst/datasets/kmeansdata.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gnu-octave/statistics/ba093ae5f8dd58432dadccac967881fc58e079e6/inst/datasets/kmeansdata.mat -------------------------------------------------------------------------------- /inst/datasets/lightbulb.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gnu-octave/statistics/ba093ae5f8dd58432dadccac967881fc58e079e6/inst/datasets/lightbulb.mat -------------------------------------------------------------------------------- /inst/datasets/mileage.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gnu-octave/statistics/ba093ae5f8dd58432dadccac967881fc58e079e6/inst/datasets/mileage.mat -------------------------------------------------------------------------------- /inst/datasets/morse.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gnu-octave/statistics/ba093ae5f8dd58432dadccac967881fc58e079e6/inst/datasets/morse.mat -------------------------------------------------------------------------------- /inst/datasets/patients.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gnu-octave/statistics/ba093ae5f8dd58432dadccac967881fc58e079e6/inst/datasets/patients.mat -------------------------------------------------------------------------------- /inst/datasets/popcorn.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gnu-octave/statistics/ba093ae5f8dd58432dadccac967881fc58e079e6/inst/datasets/popcorn.mat -------------------------------------------------------------------------------- /inst/datasets/rundist.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gnu-octave/statistics/ba093ae5f8dd58432dadccac967881fc58e079e6/inst/datasets/rundist.mat -------------------------------------------------------------------------------- /inst/datasets/spectra.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gnu-octave/statistics/ba093ae5f8dd58432dadccac967881fc58e079e6/inst/datasets/spectra.mat -------------------------------------------------------------------------------- /inst/datasets/stockreturns.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gnu-octave/statistics/ba093ae5f8dd58432dadccac967881fc58e079e6/inst/datasets/stockreturns.mat -------------------------------------------------------------------------------- /inst/datasets/weather.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gnu-octave/statistics/ba093ae5f8dd58432dadccac967881fc58e079e6/inst/datasets/weather.mat -------------------------------------------------------------------------------- /inst/dist_fit/gevfit_lmom.m: -------------------------------------------------------------------------------- 1 | ## Copyright (C) 2012 Nir Krakauer 2 | ## 3 | ## This file is part of the statistics package for GNU Octave. 4 | ## 5 | ## This program is free software; you can redistribute it and/or modify it under 6 | ## the terms of the GNU General Public License as published by the Free Software 7 | ## Foundation; either version 3 of the License, or (at your option) any later 8 | ## version. 9 | ## 10 | ## This program is distributed in the hope that it will be useful, but WITHOUT 11 | ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 12 | ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 13 | ## details. 14 | ## 15 | ## You should have received a copy of the GNU General Public License along with 16 | ## this program; if not, see . 17 | 18 | ## -*- texinfo -*- 19 | ## @deftypefn {statistics} {[@var{paramhat}, @var{paramci}] =} gevfit_lmom (@var{data}) 20 | ## 21 | ## Find an estimator (@var{paramhat}) of the generalized extreme value (GEV) 22 | ## distribution fitting @var{data} using the method of L-moments. 23 | ## 24 | ## @subheading Arguments 25 | ## 26 | ## @itemize @bullet 27 | ## @item 28 | ## @var{data} is the vector of given values. 29 | ## @end itemize 30 | ## 31 | ## @subheading Return values 32 | ## 33 | ## @itemize @bullet 34 | ## @item 35 | ## @var{parmhat} is the 3-parameter maximum-likelihood parameter vector 36 | ## [@var{k}; @var{sigma}; @var{mu}], where @var{k} is the shape parameter of the 37 | ## GEV distribution, @var{sigma} is the scale parameter of the GEV distribution, 38 | ## and @var{mu} is the location parameter of the GEV distribution. 39 | ## @item 40 | ## @var{paramci} has the approximate 95% confidence intervals of the parameter 41 | ## values (currently not implemented). 42 | ## 43 | ## @end itemize 44 | ## 45 | ## @subheading Examples 46 | ## 47 | ## @example 48 | ## @group 49 | ## data = gevrnd (0.1, 1, 0, 100, 1); 50 | ## [pfit, pci] = gevfit_lmom (data); 51 | ## p1 = gevcdf (data,pfit(1),pfit(2),pfit(3)); 52 | ## [f, x] = ecdf (data); 53 | ## plot(data, p1, 's', x, f) 54 | ## @end group 55 | ## @end example 56 | ## @seealso{gevfit} 57 | ## @subheading References 58 | ## 59 | ## @enumerate 60 | ## @item 61 | ## Ailliot, P.; Thompson, C. & Thomson, P. Mixed methods for fitting the GEV 62 | ## distribution, Water Resources Research, 2011, 47, W05551 63 | ## 64 | ## @end enumerate 65 | ## @end deftypefn 66 | 67 | function [paramhat, paramci] = gevfit_lmom (data) 68 | 69 | # Check arguments 70 | if (nargin < 1) 71 | print_usage; 72 | endif 73 | 74 | # find the L-moments 75 | data = sort (data(:))'; 76 | n = numel(data); 77 | L1 = mean(data); 78 | L2 = sum(data .* (2*(1:n) - n - 1)) / (2*nchoosek(n, 2)); # or mean(triu(data' - data, 1, 'pack')) / 2; 79 | b = bincoeff((1:n) - 1, 2); 80 | L3 = sum(data .* (b - 2 * ((1:n) - 1) .* (n - (1:n)) + fliplr(b))) / (3*nchoosek(n, 3)); 81 | 82 | #match the moments to the GEV distribution 83 | #first find k based on L3/L2 84 | f = @(k) (L3/L2 + 3)/2 - limdiv((1 - 3^(k)), (1 - 2^(k))); 85 | k = fzero(f, 0); 86 | 87 | #next find sigma and mu given k 88 | if abs(k) < 1E-8 89 | sigma = L2 / log(2); 90 | eg = 0.57721566490153286; %Euler-Mascheroni constant 91 | mu = L1 - sigma * eg; 92 | else 93 | sigma = -k*L2 / (gamma(1 - k) * (1 - 2^(k))); 94 | mu = L1 - sigma * ((gamma(1 - k) - 1) / k); 95 | endif 96 | 97 | paramhat = [k; sigma; mu]; 98 | 99 | if nargout > 1 100 | paramci = NaN; 101 | endif 102 | endfunction 103 | 104 | #internal function to accurately evaluate (1 - 3^k)/(1 - 2^k) in the limit as k --> 0 105 | function c = limdiv(a, b) 106 | # c = ifelse (abs(b) < 1E-8, log(3)/log(2), a ./ b); 107 | if abs(b) < 1E-8 108 | c = log(3)/log(2); 109 | else 110 | c = a / b; 111 | endif 112 | endfunction 113 | 114 | 115 | %!xtest <31070> 116 | %! data = 1:50; 117 | %! [pfit, pci] = gevfit_lmom (data); 118 | %! expected_p = [-0.28 15.01 20.22]'; 119 | %! assert (pfit, expected_p, 0.1); 120 | -------------------------------------------------------------------------------- /inst/dist_fit/poisslike.m: -------------------------------------------------------------------------------- 1 | ## Copyright (C) 2023 Andreas Bertsatos 2 | ## 3 | ## This file is part of the statistics package for GNU Octave. 4 | ## 5 | ## This program is free software: you can redistribute it and/or 6 | ## modify it under the terms of the GNU General Public License as 7 | ## published by the Free Software Foundation, either version 3 of the 8 | ## License, or (at your option) any later version. 9 | ## 10 | ## This program is distributed in the hope that it will be useful, but 11 | ## WITHOUT ANY WARRANTY; without even the implied warranty of 12 | ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | ## General Public License for more details. 14 | ## 15 | ## You should have received a copy of the GNU General Public License 16 | ## along with this program; see the file COPYING. If not, see 17 | ## . 18 | 19 | ## -*- texinfo -*- 20 | ## @deftypefn {statistics} {@var{nlogL} =} poisslike (@var{lambda}, @var{x}) 21 | ## @deftypefnx {statistics} {[@var{nlogL}, @var{avar}] =} poisslike (@var{lambda}, @var{x}) 22 | ## @deftypefnx {statistics} {[@dots{}] =} poisslike (@var{lambda}, @var{x}, @var{freq}) 23 | ## 24 | ## Negative log-likelihood for the Poisson distribution. 25 | ## 26 | ## @code{@var{nlogL} = poisslike (@var{lambda}, @var{x})} returns the negative 27 | ## log likelihood of the data in @var{x} corresponding to the Poisson 28 | ## distribution with rate parameter @var{lambda}. @var{x} must be a vector of 29 | ## non-negative values. 30 | ## 31 | ## @code{[@var{nlogL}, @var{avar}] = poisslike (@var{lambda}, @var{x})} also 32 | ## returns the inverse of Fisher's information matrix, @var{avar}. If the input 33 | ## rate parameter, @var{lambda}, is the maximum likelihood estimate, @var{avar} 34 | ## is its asymptotic variance. 35 | ## 36 | ## @code{[@dots{}] = poisslike (@var{lambda}, @var{x}, @var{freq})} accepts a 37 | ## frequency vector, @var{freq}, of the same size as @var{x}. @var{freq} 38 | ## typically contains integer frequencies for the corresponding elements in 39 | ## @var{x}, but it can contain any non-integer non-negative values. By default, 40 | ## or if left empty, @qcode{@var{freq} = ones (size (@var{x}))}. 41 | ## 42 | ## Further information about the Poisson distribution can be found at 43 | ## @url{https://en.wikipedia.org/wiki/Poisson_distribution} 44 | ## 45 | ## @seealso{poisscdf, poissinv, poisspdf, poissrnd, poissfit, poisstat} 46 | ## @end deftypefn 47 | 48 | function [nlogL, avar] = poisslike (lambda, x, freq) 49 | 50 | ## Check input arguments 51 | if (nargin < 2) 52 | error ("poisslike: function called with too few input arguments."); 53 | endif 54 | 55 | if (! isscalar (lambda) || ! isnumeric (lambda) || lambda <= 0) 56 | error ("poisslike: LAMBDA must be a positive scalar."); 57 | endif 58 | 59 | if (! isvector (x) || any (x < 0)) 60 | error ("poisslike: X must be a vector of non-negative values."); 61 | endif 62 | 63 | if (nargin < 3 || isempty (freq)) 64 | freq = ones (size (x)); 65 | elseif (! isequal (size (x), size (freq))) 66 | error ("poisslike: X and FREQ vectors mismatch."); 67 | elseif (any (freq < 0)) 68 | error ("poisslike: FREQ must not contain negative values."); 69 | endif 70 | 71 | ## Compute negative log-likelihood and asymptotic covariance 72 | n = sum (freq); 73 | nlogL = - sum (freq .* log (poisspdf (x, lambda))); 74 | avar = lambda / n; 75 | 76 | endfunction 77 | 78 | ## Test output 79 | %!test 80 | %! x = [1 3 2 4 5 4 3 4]; 81 | %! [nlogL, avar] = poisslike (3.25, x); 82 | %! assert (nlogL, 13.9533, 1e-4) 83 | %!test 84 | %! x = [1 2 3 4 5]; 85 | %! f = [1 1 2 3 1]; 86 | %! [nlogL, avar] = poisslike (3.25, x, f); 87 | %! assert (nlogL, 13.9533, 1e-4) 88 | 89 | ## Test input validation 90 | %!error poisslike (1) 91 | %!error poisslike ([1 2 3], [1 2]) 92 | %!error ... 93 | %! poisslike (3.25, ones (10, 2)) 94 | %!error ... 95 | %! poisslike (3.25, [1 2 3 -4 5]) 96 | %!error ... 97 | %! poisslike (3.25, ones (10, 1), ones (8,1)) 98 | %!error ... 99 | %! poisslike (3.25, ones (1, 8), [1 1 1 1 1 1 1 -1]) 100 | -------------------------------------------------------------------------------- /inst/dist_fun/chi2inv.m: -------------------------------------------------------------------------------- 1 | ## Copyright (C) 2012 Rik Wehbring 2 | ## Copyright (C) 1995-2016 Kurt Hornik 3 | ## Copyright (C) 2023 Andreas Bertsatos 4 | ## 5 | ## This file is part of the statistics package for GNU Octave. 6 | ## 7 | ## This program is free software; you can redistribute it and/or modify it under 8 | ## the terms of the GNU General Public License as published by the Free Software 9 | ## Foundation; either version 3 of the License, or (at your option) any later 10 | ## version. 11 | ## 12 | ## This program is distributed in the hope that it will be useful, but WITHOUT 13 | ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 14 | ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 15 | ## details. 16 | ## 17 | ## You should have received a copy of the GNU General Public License along with 18 | ## this program; if not, see . 19 | 20 | ## -*- texinfo -*- 21 | ## @deftypefn {statistics} {@var{x} =} chi2inv (@var{p}, @var{df}) 22 | ## 23 | ## Inverse of the chi-squared cumulative distribution function (iCDF). 24 | ## 25 | ## For each element of @var{p}, compute the quantile (the inverse of the CDF) of 26 | ## the chi-squared distribution with @var{df} degrees of freedom. The size of 27 | ## @var{x} is the common size of @var{p} and @var{df}. A scalar input functions 28 | ## as a constant matrix of the same size as the other inputs. 29 | ## 30 | ## Further information about the chi-squared distribution can be found at 31 | ## @url{https://en.wikipedia.org/wiki/Chi-squared_distribution} 32 | ## 33 | ## @seealso{chi2cdf, chi2pdf, chi2rnd, chi2stat} 34 | ## @end deftypefn 35 | 36 | function x = chi2inv (p, df) 37 | 38 | ## Check for valid number of input arguments 39 | if (nargin < 2) 40 | error ("chi2inv: function called with too few input arguments."); 41 | endif 42 | 43 | ## Check for common size of P and DF 44 | if (! isscalar (p) || ! isscalar (df)) 45 | [retval, p, df] = common_size (p, df); 46 | if (retval > 0) 47 | error ("chi2inv: P and DF must be of common size or scalars."); 48 | endif 49 | endif 50 | 51 | ## Check for P and DF being reals 52 | if (iscomplex (p) || iscomplex (df)) 53 | error ("chi2inv: P and DF must not be complex."); 54 | endif 55 | 56 | ## Compute chi-squared iCDF 57 | x = gaminv (p, df/2, 2); 58 | 59 | endfunction 60 | 61 | %!demo 62 | %! ## Plot various iCDFs from the chi-squared distribution 63 | %! p = 0.001:0.001:0.999; 64 | %! x1 = chi2inv (p, 1); 65 | %! x2 = chi2inv (p, 2); 66 | %! x3 = chi2inv (p, 3); 67 | %! x4 = chi2inv (p, 4); 68 | %! x5 = chi2inv (p, 6); 69 | %! x6 = chi2inv (p, 9); 70 | %! plot (p, x1, "-b", p, x2, "-g", p, x3, "-r", ... 71 | %! p, x4, "-c", p, x5, "-m", p, x6, "-y") 72 | %! grid on 73 | %! ylim ([0, 8]) 74 | %! legend ({"df = 1", "df = 2", "df = 3", ... 75 | %! "df = 4", "df = 6", "df = 9"}, "location", "northwest") 76 | %! title ("Chi-squared iCDF") 77 | %! xlabel ("probability") 78 | %! ylabel ("values in x") 79 | 80 | ## Test output 81 | %!shared p 82 | %! p = [-1 0 0.3934693402873666 1 2]; 83 | %!assert (chi2inv (p, 2*ones (1,5)), [NaN 0 1 Inf NaN], 5*eps) 84 | %!assert (chi2inv (p, 2), [NaN 0 1 Inf NaN], 5*eps) 85 | %!assert (chi2inv (p, 2*[0 1 NaN 1 1]), [NaN 0 NaN Inf NaN], 5*eps) 86 | %!assert (chi2inv ([p(1:2) NaN p(4:5)], 2), [NaN 0 NaN Inf NaN], 5*eps) 87 | 88 | ## Test class of input preserved 89 | %!assert (chi2inv ([p, NaN], 2), [NaN 0 1 Inf NaN NaN], 5*eps) 90 | %!assert (chi2inv (single ([p, NaN]), 2), single ([NaN 0 1 Inf NaN NaN]), 5*eps ("single")) 91 | %!assert (chi2inv ([p, NaN], single (2)), single ([NaN 0 1 Inf NaN NaN]), 5*eps ("single")) 92 | 93 | ## Test input validation 94 | %!error chi2inv () 95 | %!error chi2inv (1) 96 | %!error chi2inv (1,2,3) 97 | %!error ... 98 | %! chi2inv (ones (3), ones (2)) 99 | %!error ... 100 | %! chi2inv (ones (2), ones (3)) 101 | %!error chi2inv (i, 2) 102 | %!error chi2inv (2, i) 103 | -------------------------------------------------------------------------------- /inst/dist_fun/chi2pdf.m: -------------------------------------------------------------------------------- 1 | ## Copyright (C) 2012 Rik Wehbring 2 | ## Copyright (C) 1995-2016 Kurt Hornik 3 | ## Copyright (C) 2023 Andreas Bertsatos 4 | ## 5 | ## This file is part of the statistics package for GNU Octave. 6 | ## 7 | ## This program is free software; you can redistribute it and/or modify it under 8 | ## the terms of the GNU General Public License as published by the Free Software 9 | ## Foundation; either version 3 of the License, or (at your option) any later 10 | ## version. 11 | ## 12 | ## This program is distributed in the hope that it will be useful, but WITHOUT 13 | ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 14 | ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 15 | ## details. 16 | ## 17 | ## You should have received a copy of the GNU General Public License along with 18 | ## this program; if not, see . 19 | 20 | ## -*- texinfo -*- 21 | ## @deftypefn {statistics} {@var{y} =} chi2pdf (@var{x}, @var{df}) 22 | ## 23 | ## Chi-squared probability density function (PDF). 24 | ## 25 | ## For each element of @var{x}, compute the probability density function (PDF) 26 | ## of the chi-squared distribution with @var{df} degrees of freedom. The size 27 | ## of @var{y} is the common size of @var{x} and @var{df}. A scalar input 28 | ## functions as a constant matrix of the same size as the other inputs. 29 | ## 30 | ## Further information about the chi-squared distribution can be found at 31 | ## @url{https://en.wikipedia.org/wiki/Chi-squared_distribution} 32 | ## 33 | ## @seealso{chi2cdf, chi2pdf, chi2rnd, chi2stat} 34 | ## @end deftypefn 35 | 36 | function y = chi2pdf (x, df) 37 | 38 | ## Check for valid number of input arguments 39 | if (nargin < 2) 40 | error ("chi2pdf: function called with too few input arguments."); 41 | endif 42 | 43 | ## Check for common size of X and DF 44 | if (! isscalar (x) || ! isscalar (df)) 45 | [retval, x, df] = common_size (x, df); 46 | if (retval > 0) 47 | error ("chi2pdf: X and DF must be of common size or scalars."); 48 | endif 49 | endif 50 | 51 | ## Check for X and DF being reals 52 | if (iscomplex (x) || iscomplex (df)) 53 | error ("chi2pdf: X and DF must not be complex."); 54 | endif 55 | 56 | ## Compute chi-squared PDF 57 | y = gampdf (x, df/2, 2); 58 | 59 | endfunction 60 | 61 | %!demo 62 | %! ## Plot various PDFs from the chi-squared distribution 63 | %! x = 0:0.01:8; 64 | %! y1 = chi2pdf (x, 1); 65 | %! y2 = chi2pdf (x, 2); 66 | %! y3 = chi2pdf (x, 3); 67 | %! y4 = chi2pdf (x, 4); 68 | %! y5 = chi2pdf (x, 6); 69 | %! y6 = chi2pdf (x, 9); 70 | %! plot (x, y1, "-b", x, y2, "-g", x, y3, "-r", ... 71 | %! x, y4, "-c", x, y5, "-m", x, y6, "-y") 72 | %! grid on 73 | %! xlim ([0, 8]) 74 | %! ylim ([0, 0.5]) 75 | %! legend ({"df = 1", "df = 2", "df = 3", ... 76 | %! "df = 4", "df = 6", "df = 9"}, "location", "northeast") 77 | %! title ("Chi-squared PDF") 78 | %! xlabel ("values in x") 79 | %! ylabel ("density") 80 | 81 | ## Test output 82 | %!shared x, y 83 | %! x = [-1 0 0.5 1 Inf]; 84 | %! y = [0, 1/2 * exp(-x(2:5)/2)]; 85 | %!assert (chi2pdf (x, 2*ones (1,5)), y) 86 | %!assert (chi2pdf (x, 2), y) 87 | %!assert (chi2pdf (x, 2*[1 0 NaN 1 1]), [y(1) NaN NaN y(4:5)]) 88 | %!assert (chi2pdf ([x, NaN], 2), [y, NaN]) 89 | 90 | ## Test class of input preserved 91 | %!assert (chi2pdf (single ([x, NaN]), 2), single ([y, NaN])) 92 | %!assert (chi2pdf ([x, NaN], single (2)), single ([y, NaN])) 93 | 94 | ## Test input validation 95 | %!error chi2pdf () 96 | %!error chi2pdf (1) 97 | %!error chi2pdf (1,2,3) 98 | %!error ... 99 | %! chi2pdf (ones (3), ones (2)) 100 | %!error ... 101 | %! chi2pdf (ones (2), ones (3)) 102 | %!error chi2pdf (i, 2) 103 | %!error chi2pdf (2, i) 104 | -------------------------------------------------------------------------------- /inst/dist_fun/geopdf.m: -------------------------------------------------------------------------------- 1 | ## Copyright (C) 2012 Rik Wehbring 2 | ## Copyright (C) 1995-2016 Kurt Hornik 3 | ## Copyright (C) 2023 Andreas Bertsatos 4 | ## 5 | ## This file is part of the statistics package for GNU Octave. 6 | ## 7 | ## This program is free software; you can redistribute it and/or modify it under 8 | ## the terms of the GNU General Public License as published by the Free Software 9 | ## Foundation; either version 3 of the License, or (at your option) any later 10 | ## version. 11 | ## 12 | ## This program is distributed in the hope that it will be useful, but WITHOUT 13 | ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 14 | ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 15 | ## details. 16 | ## 17 | ## You should have received a copy of the GNU General Public License along with 18 | ## this program; if not, see . 19 | 20 | ## -*- texinfo -*- 21 | ## @deftypefn {statistics} {@var{y} =} geopdf (@var{x}, @var{ps}) 22 | ## 23 | ## Geometric probability density function (PDF). 24 | ## 25 | ## For each element of @var{x}, compute the probability density function (PDF) 26 | ## of the geometric distribution with probability of success parameter @var{ps}. 27 | ## The size of @var{y} is the common size of @var{x} and @var{ps}. A scalar 28 | ## input functions as a constant matrix of the same size as the other inputs. 29 | ## 30 | ## The geometric distribution models the number of failures (@var{x}) of a 31 | ## Bernoulli trial with probability @var{ps} before the first success. 32 | ## 33 | ## Further information about the geometric distribution can be found at 34 | ## @url{https://en.wikipedia.org/wiki/Geometric_distribution} 35 | ## 36 | ## @seealso{geocdf, geoinv, geornd, geofit, geostat} 37 | ## @end deftypefn 38 | 39 | function y = geopdf (x, ps) 40 | 41 | ## Check for valid number of input arguments 42 | if (nargin < 2) 43 | error ("geopdf: function called with too few input arguments."); 44 | endif 45 | 46 | ## Check for common size of X and PS 47 | if (! isscalar (x) || ! isscalar (ps)) 48 | [retval, x, ps] = common_size (x, ps); 49 | if (retval > 0) 50 | error ("geopdf: X and PS must be of common size or scalars."); 51 | endif 52 | endif 53 | 54 | ## Check for X and PS being reals 55 | if (iscomplex (x) || iscomplex (ps)) 56 | error ("geopdf: X and PS must not be complex."); 57 | endif 58 | 59 | ## Check for class type 60 | if (isa (x, "single") || isa (ps, "single")) 61 | y = zeros (size (x), "single"); 62 | else 63 | y = zeros (size (x)); 64 | endif 65 | 66 | ## Return NaN for out of range parameters 67 | k = isnan (x) | (x == Inf) | !(ps >= 0) | !(ps <= 1); 68 | y(k) = NaN; 69 | 70 | ## Get valid instances 71 | k = (x >= 0) & (x < Inf) & (x == fix (x)) & (ps > 0) & (ps <= 1); 72 | 73 | ## Compute CDF 74 | if (isscalar (ps)) 75 | y(k) = ps * ((1 - ps) .^ x(k)); 76 | else 77 | y(k) = ps(k) .* ((1 - ps(k)) .^ x(k)); 78 | endif 79 | 80 | endfunction 81 | 82 | %!demo 83 | %! ## Plot various PDFs from the geometric distribution 84 | %! x = 0:10; 85 | %! y1 = geopdf (x, 0.2); 86 | %! y2 = geopdf (x, 0.5); 87 | %! y3 = geopdf (x, 0.7); 88 | %! plot (x, y1, "*b", x, y2, "*g", x, y3, "*r") 89 | %! grid on 90 | %! ylim ([0, 0.8]) 91 | %! legend ({"ps = 0.2", "ps = 0.5", "ps = 0.7"}, "location", "northeast") 92 | %! title ("Geometric PDF") 93 | %! xlabel ("values in x (number of failures)") 94 | %! ylabel ("density") 95 | 96 | ## Test output 97 | %!shared x, y 98 | %! x = [-1 0 1 Inf]; 99 | %! y = [0, 1/2, 1/4, NaN]; 100 | %!assert (geopdf (x, 0.5*ones (1,4)), y) 101 | %!assert (geopdf (x, 0.5), y) 102 | %!assert (geopdf (x, 0.5*[-1 NaN 4 1]), [NaN NaN NaN y(4)]) 103 | %!assert (geopdf ([x, NaN], 0.5), [y, NaN]) 104 | 105 | ## Test class of input preserved 106 | %!assert (geopdf (single ([x, NaN]), 0.5), single ([y, NaN]), 5*eps ("single")) 107 | %!assert (geopdf ([x, NaN], single (0.5)), single ([y, NaN]), 5*eps ("single")) 108 | 109 | ## Test input validation 110 | %!error geopdf () 111 | %!error geopdf (1) 112 | %!error geopdf (1,2,3) 113 | %!error geopdf (ones (3), ones (2)) 114 | %!error geopdf (ones (2), ones (3)) 115 | %!error geopdf (i, 2) 116 | %!error geopdf (2, i) 117 | -------------------------------------------------------------------------------- /inst/dist_fun/iwishpdf.m: -------------------------------------------------------------------------------- 1 | ## Copyright (C) 2013 Nir Krakauer 2 | ## 3 | ## This file is part of the statistics package for GNU Octave. 4 | ## 5 | ## This program is free software; you can redistribute it and/or modify it under 6 | ## the terms of the GNU General Public License as published by the Free Software 7 | ## Foundation; either version 3 of the License, or (at your option) any later 8 | ## version. 9 | ## 10 | ## This program is distributed in the hope that it will be useful, but WITHOUT 11 | ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 12 | ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 13 | ## details. 14 | ## 15 | ## You should have received a copy of the GNU General Public License along with 16 | ## this program; if not, see . 17 | 18 | ## -*- texinfo -*- 19 | ## @deftypefn {statistics} {@var{y} =} iwishpdf (@var{W}, @var{Tau}, @var{df}, @var{log_y}=false) 20 | ## 21 | ## Compute the probability density function of the inverse Wishart distribution. 22 | ## 23 | ## Inputs: A @var{p} x @var{p} matrix @var{W} where to find the PDF and the 24 | ## @var{p} x @var{p} positive definite scale matrix @var{Tau} and scalar degrees 25 | ## of freedom parameter @var{df} characterizing the inverse Wishart distribution. 26 | ## (For the density to be finite, need @var{df} > (@var{p} - 1).) 27 | ## If the flag @var{log_y} is set, return the log probability density -- this 28 | ## helps avoid underflow when the numerical value of the density is very small. 29 | ## 30 | ## Output: @var{y} is the probability density of Wishart(@var{Sigma}, @var{df}) 31 | ## at @var{W}. 32 | ## 33 | ## @seealso{iwishrnd, wishpdf, wishrnd} 34 | ## @end deftypefn 35 | 36 | function y = iwishpdf (W, Tau, df, log_y=false) 37 | 38 | if (nargin < 3) 39 | print_usage (); 40 | endif 41 | 42 | p = size (Tau, 1); 43 | 44 | if (df <= (p - 1)) 45 | error ("iwishpdf: DF too small, no finite densities exist."); 46 | endif 47 | 48 | ## Calculate the logarithm of G_d(df/2), the multivariate gamma function 49 | g = (p * (p - 1) / 4) * log (pi); 50 | for i = 1:p 51 | g = g + log (gamma ((df + (1 - i)) / 2)); 52 | endfor 53 | 54 | C = chol (W); 55 | 56 | ## Use formulas for determinant of positive definite matrix for better 57 | ## efficiency and numerical accuracy 58 | logdet_W = 2*sum(log(diag(C))); 59 | logdet_Tau = 2*sum(log(diag(chol(Tau)))); 60 | 61 | y = -(df * p) / 2 * log (2) + (df / 2) * logdet_Tau - g ... 62 | -((df + p + 1) / 2) * logdet_W - trace (Tau * chol2inv (C)) / 2; 63 | 64 | if (! log_y) 65 | y = exp (y); 66 | endif 67 | endfunction 68 | 69 | ## Test results cross-checked against diwish function in R MCMCpack library 70 | %!assert(iwishpdf(4, 3, 3.1), 0.04226595, 1E-7); 71 | %!assert(iwishpdf([2 -0.3;-0.3 4], [1 0.3;0.3 1], 4), 1.60166e-05, 1E-10); 72 | %!assert(iwishpdf([6 2 5; 2 10 -5; 5 -5 25], ... 73 | %! [9 5 5; 5 10 -8; 5 -8 22], 5.1), 4.946831e-12, 1E-17); 74 | 75 | ## Test input validation 76 | %!error iwishpdf () 77 | %!error iwishpdf (1, 2) 78 | %!error iwishpdf (1, 2, 0) 79 | -------------------------------------------------------------------------------- /inst/dist_fun/iwishrnd.m: -------------------------------------------------------------------------------- 1 | ## Copyright (C) 2013 Nir Krakauer 2 | ## 3 | ## This file is part of the statistics package for GNU Octave. 4 | ## 5 | ## This program is free software; you can redistribute it and/or modify it under 6 | ## the terms of the GNU General Public License as published by the Free Software 7 | ## Foundation; either version 3 of the License, or (at your option) any later 8 | ## version. 9 | ## 10 | ## This program is distributed in the hope that it will be useful, but WITHOUT 11 | ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 12 | ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 13 | ## details. 14 | ## 15 | ## You should have received a copy of the GNU General Public License along with 16 | ## this program; if not, see . 17 | 18 | ## -*- texinfo -*- 19 | ## @deftypefn {statistics} {[@var{W}, @var{DI}] =} iwishrnd (@var{Tau}, @var{df}, @var{DI}, @var{n}=1) 20 | ## 21 | ## Return a random matrix sampled from the inverse Wishart distribution with 22 | ## given parameters. 23 | ## 24 | ## Inputs: the @math{p x p} positive definite matrix @var{Tau} and scalar 25 | ## degrees of freedom parameter @var{df} (and optionally the transposed Cholesky 26 | ## factor @var{DI} of @var{Sigma} = @code{inv(Tau)}). 27 | ## 28 | ## @var{df} can be non-integer as long as @math{@var{df} > d} 29 | ## 30 | ## Output: a random @math{p x p} matrix @var{W} from the inverse 31 | ## Wishart(@var{Tau}, @var{df}) distribution. (@code{inv(W)} is from the 32 | ## Wishart(@code{inv(Tau)}, @var{df}) distribution.) If @var{n} > 1, 33 | ## then @var{W} is @var{p} x @var{p} x @var{n} and holds @var{n} such random 34 | ## matrices. (Optionally, the transposed Cholesky factor @var{DI} of @var{Sigma} 35 | ## is also returned.) 36 | ## 37 | ## Averaged across many samples, the mean of @var{W} should approach 38 | ## @var{Tau} / (@var{df} - @var{p} - 1). 39 | ## 40 | ## @subheading References 41 | ## 42 | ## @enumerate 43 | ## @item 44 | ## Yu-Cheng Ku and Peter Bloomfield (2010), Generating Random Wishart Matrices 45 | ## with Fractional Degrees of Freedom in OX, 46 | ## http://www.gwu.edu/~forcpgm/YuChengKu-030510final-WishartYu-ChengKu.pdf 47 | ## @end enumerate 48 | ## 49 | ## @seealso{iwishpdf, wishpdf, wishrnd} 50 | ## @end deftypefn 51 | 52 | function [W, DI] = iwishrnd (Tau, df, DI, n = 1) 53 | 54 | if (nargin < 2) 55 | print_usage (); 56 | endif 57 | 58 | if (nargin < 3 || isempty (DI)) 59 | try 60 | D = chol (inv (Tau)); 61 | catch 62 | error (strcat (["iwishrnd: Cholesky decomposition failed;"], ... 63 | [" TAU probably not positive definite."])); 64 | end_try_catch 65 | DI = D'; 66 | else 67 | D = DI'; 68 | endif 69 | 70 | w = wishrnd ([], df, D, n); 71 | 72 | if (n > 1) 73 | p = size (D, 1); 74 | W = nan (p, p, n); 75 | endif 76 | 77 | for i = 1:n 78 | W(:, :, i) = inv (w(:, :, i)); 79 | endfor 80 | 81 | endfunction 82 | 83 | 84 | 85 | %!assert(size (iwishrnd (1,2,1)), [1, 1]); 86 | %!assert(size (iwishrnd ([],2,1)), [1, 1]); 87 | %!assert(size (iwishrnd ([3 1; 1 3], 2.00001, [], 1)), [2, 2]); 88 | %!assert(size (iwishrnd (eye(2), 2, [], 3)), [2, 2, 3]); 89 | 90 | %% Test input validation 91 | %!error iwishrnd () 92 | %!error iwishrnd (1) 93 | %!error iwishrnd ([-3 1; 1 3],1) 94 | %!error iwishrnd ([1; 1],1) 95 | -------------------------------------------------------------------------------- /inst/dist_fun/jsucdf.m: -------------------------------------------------------------------------------- 1 | ## Copyright (C) 2006 Frederick (Rick) A Niles 2 | ## Copyright (C) 2023 Andreas Bertsatos 3 | ## 4 | ## This file is part of the statistics package for GNU Octave. 5 | ## 6 | ## This program is free software; you can redistribute it and/or modify it under 7 | ## the terms of the GNU General Public License as published by the Free Software 8 | ## Foundation; either version 3 of the License, or (at your option) any later 9 | ## version. 10 | ## 11 | ## This program is distributed in the hope that it will be useful, but WITHOUT 12 | ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 | ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 14 | ## details. 15 | ## 16 | ## You should have received a copy of the GNU General Public License along with 17 | ## this program; if not, see . 18 | 19 | ## -*- texinfo -*- 20 | ## @deftypefn {statistics} {@var{p} =} jsucdf (@var{x}) 21 | ## @deftypefnx {statistics} {@var{p} =} jsucdf (@var{x}, @var{alpha1}) 22 | ## @deftypefnx {statistics} {@var{p} =} jsucdf (@var{x}, @var{alpha1}, @var{alpha2}) 23 | ## 24 | ## Johnson SU cumulative distribution function (CDF). 25 | ## 26 | ## For each element of @var{x}, return the cumulative distribution functions 27 | ## (CDF) at @var{x} of the Johnson SU distribution with shape parameters 28 | ## @var{alpha1} and @var{alpha2}. The size of @var{p} is the common size of the 29 | ## input arguments @var{x}, @var{alpha1}, and @var{alpha2}. A scalar input 30 | ## functions as a constant matrix of the same size as the other 31 | ## 32 | ## Default values are @var{alpha1} = 1, @var{alpha2} = 1. 33 | ## 34 | ## @seealso{jsupdf} 35 | ## @end deftypefn 36 | 37 | function p = jsucdf (x, alpha1, alpha2) 38 | 39 | if (nargin < 1 || nargin > 3) 40 | print_usage; 41 | endif 42 | 43 | if (nargin == 1) 44 | alpha1 = 1; 45 | alpha2 = 1; 46 | elseif (nargin == 2) 47 | alpha2 = 1; 48 | endif 49 | 50 | if (! isscalar (x) || ! isscalar (alpha1) || ! isscalar(alpha2)) 51 | [retval, x, alpha1, alpha2] = common_size (x, alpha1, alpha2); 52 | if (retval > 0) 53 | error (strcat (["jsucdf: X, ALPHA1, and ALPHA2 must be of common"], ... 54 | [" size or scalars."])); 55 | endif 56 | endif 57 | 58 | one = ones (size (x)); 59 | p = stdnormal_cdf (alpha1 .* one + alpha2 .* log (x + sqrt (x .* x + one))); 60 | 61 | endfunction 62 | 63 | %!error jsucdf () 64 | %!error jsucdf (1, 2, 3, 4) 65 | %!error ... 66 | %! jsucdf (1, ones (2), ones (3)) 67 | -------------------------------------------------------------------------------- /inst/dist_fun/jsupdf.m: -------------------------------------------------------------------------------- 1 | ## Copyright (C) 2006 Frederick (Rick) A Niles 2 | ## Copyright (C) 2023 Andreas Bertsatos 3 | ## 4 | ## This file is part of the statistics package for GNU Octave. 5 | ## 6 | ## This program is free software; you can redistribute it and/or modify it under 7 | ## the terms of the GNU General Public License as published by the Free Software 8 | ## Foundation; either version 3 of the License, or (at your option) any later 9 | ## version. 10 | ## 11 | ## This program is distributed in the hope that it will be useful, but WITHOUT 12 | ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 | ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 14 | ## details. 15 | ## 16 | ## You should have received a copy of the GNU General Public License along with 17 | ## this program; if not, see . 18 | 19 | ## -*- texinfo -*- 20 | ## @deftypefn {statistics} {@var{y} =} jsupdf (@var{x}) 21 | ## @deftypefnx {statistics} {@var{y} =} jsupdf (@var{x}, @var{alpha1}) 22 | ## @deftypefnx {statistics} {@var{y} =} jsupdf (@var{x}, @var{alpha1}, @var{alpha2}) 23 | ## 24 | ## Johnson SU probability density function (PDF). 25 | ## 26 | ## For each element of @var{x}, compute the probability density function (PDF) 27 | ## at @var{x} of the Johnson SU distribution with shape parameters @var{alpha1} 28 | ## and @var{alpha2}. The size of @var{p} is the common size of the input 29 | ## arguments @var{x}, @var{alpha1}, and @var{alpha2}. A scalar input functions 30 | ## as a constant matrix of the same size as the other 31 | ## 32 | ## Default values are @var{alpha1} = 1, @var{alpha2} = 1. 33 | ## 34 | ## @seealso{jsucdf} 35 | ## @end deftypefn 36 | 37 | function y = jsupdf (x, alpha1, alpha2) 38 | 39 | if (nargin < 1 || nargin > 3) 40 | print_usage; 41 | endif 42 | 43 | if (nargin == 1) 44 | alpha1 = 1; 45 | alpha2 = 1; 46 | elseif (nargin == 2) 47 | alpha2 = 1; 48 | endif 49 | 50 | if (! isscalar (x) || ! isscalar (alpha1) || ! isscalar(alpha2)) 51 | [retval, x, alpha1, alpha2] = common_size (x, alpha1, alpha2); 52 | if (retval > 0) 53 | error (strcat (["jsupdf: X, ALPHA1, and ALPHA2 must be of common"], ... 54 | [" size or scalars."])); 55 | endif 56 | endif 57 | 58 | one = ones (size (x)); 59 | sr = sqrt (x .* x + one); 60 | y = (alpha2 ./ sr) .* ... 61 | stdnormal_pdf (alpha1 .* one + alpha2 .* log (x + sr)); 62 | 63 | endfunction 64 | 65 | %!error jsupdf () 66 | %!error jsupdf (1, 2, 3, 4) 67 | %!error ... 68 | %! jsupdf (1, ones (2), ones (3)) 69 | 70 | -------------------------------------------------------------------------------- /inst/dist_fun/raylinv.m: -------------------------------------------------------------------------------- 1 | ## Copyright (C) 2006, 2007 Arno Onken 2 | ## Copyright (C) 2023 Andreas Bertsatos 3 | ## 4 | ## This file is part of the statistics package for GNU Octave. 5 | ## 6 | ## This program is free software; you can redistribute it and/or modify it under 7 | ## the terms of the GNU General Public License as published by the Free Software 8 | ## Foundation; either version 3 of the License, or (at your option) any later 9 | ## version. 10 | ## 11 | ## This program is distributed in the hope that it will be useful, but WITHOUT 12 | ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 | ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 14 | ## details. 15 | ## 16 | ## You should have received a copy of the GNU General Public License along with 17 | ## this program; if not, see . 18 | 19 | ## -*- texinfo -*- 20 | ## @deftypefn {statistics} {@var{x} =} raylinv (@var{p}, @var{sigma}) 21 | ## 22 | ## Inverse of the Rayleigh cumulative distribution function (iCDF). 23 | ## 24 | ## For each element of @var{p}, compute the quantile (the inverse of the CDF) of 25 | ## the Rayleigh distribution with scale parameter @var{sigma}. The size of 26 | ## @var{x} is the common size of @var{p} and @var{sigma}. A scalar input 27 | ## functions as a constant matrix of the same size as the other inputs. 28 | ## 29 | ## Further information about the Rayleigh distribution can be found at 30 | ## @url{https://en.wikipedia.org/wiki/Rayleigh_distribution} 31 | ## 32 | ## @seealso{raylcdf, raylpdf, raylrnd, raylfit, rayllike, raylstat} 33 | ## @end deftypefn 34 | 35 | function x = raylinv (p, sigma) 36 | 37 | ## Check for valid number of input arguments 38 | if (nargin < 2) 39 | error ("raylinv: function called with too few input arguments."); 40 | endif 41 | 42 | ## Check for common size of P and SIGMA 43 | if (! isscalar (p) || ! isscalar (sigma)) 44 | [retval, p, sigma] = common_size (p, sigma); 45 | if (retval > 0) 46 | error ("raylinv: P and SIGMA must be of common size or scalars."); 47 | endif 48 | endif 49 | 50 | ## Check for X and SIGMA being reals 51 | if (iscomplex (p) || iscomplex (sigma)) 52 | error ("raylinv: P and SIGMA must not be complex."); 53 | endif 54 | 55 | ## Calculate Rayleigh iCDF 56 | x = sqrt (-2 .* log (1 - p) .* sigma .^ 2); 57 | 58 | ## Check for valid parameter and support 59 | k = find (p == 1); 60 | if (any (k)) 61 | x(k) = Inf; 62 | endif 63 | 64 | k = find (! (p >= 0) | ! (p <= 1) | ! (sigma > 0)); 65 | if (any (k)) 66 | x(k) = NaN; 67 | endif 68 | 69 | endfunction 70 | 71 | %!demo 72 | %! ## Plot various iCDFs from the Rayleigh distribution 73 | %! p = 0.001:0.001:0.999; 74 | %! x1 = raylinv (p, 0.5); 75 | %! x2 = raylinv (p, 1); 76 | %! x3 = raylinv (p, 2); 77 | %! x4 = raylinv (p, 3); 78 | %! x5 = raylinv (p, 4); 79 | %! plot (p, x1, "-b", p, x2, "g", p, x3, "-r", p, x4, "-m", p, x5, "-k") 80 | %! grid on 81 | %! ylim ([0, 10]) 82 | %! legend ({"σ = 0,5", "σ = 1", "σ = 2", ... 83 | %! "σ = 3", "σ = 4"}, "location", "northwest") 84 | %! title ("Rayleigh iCDF") 85 | %! xlabel ("probability") 86 | %! ylabel ("values in x") 87 | 88 | ## Test output 89 | %!test 90 | %! p = 0:0.1:0.5; 91 | %! sigma = 1:6; 92 | %! x = raylinv (p, sigma); 93 | %! expected_x = [0.0000, 0.9181, 2.0041, 3.3784, 5.0538, 7.0645]; 94 | %! assert (x, expected_x, 0.001); 95 | %!test 96 | %! p = 0:0.1:0.5; 97 | %! x = raylinv (p, 0.5); 98 | %! expected_x = [0.0000, 0.2295, 0.3340, 0.4223, 0.5054, 0.5887]; 99 | %! assert (x, expected_x, 0.001); 100 | 101 | ## Test input validation 102 | %!error raylinv () 103 | %!error raylinv (1) 104 | %!error ... 105 | %! raylinv (ones (3), ones (2)) 106 | %!error ... 107 | %! raylinv (ones (2), ones (3)) 108 | %!error raylinv (i, 2) 109 | %!error raylinv (2, i) 110 | -------------------------------------------------------------------------------- /inst/dist_fun/raylpdf.m: -------------------------------------------------------------------------------- 1 | ## Copyright (C) 2006, 2007 Arno Onken 2 | ## Copyright (C) 2023 Andreas Bertsatos 3 | ## 4 | ## This file is part of the statistics package for GNU Octave. 5 | ## 6 | ## This program is free software; you can redistribute it and/or modify it under 7 | ## the terms of the GNU General Public License as published by the Free Software 8 | ## Foundation; either version 3 of the License, or (at your option) any later 9 | ## version. 10 | ## 11 | ## This program is distributed in the hope that it will be useful, but WITHOUT 12 | ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 | ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 14 | ## details. 15 | ## 16 | ## You should have received a copy of the GNU General Public License along with 17 | ## this program; if not, see . 18 | 19 | ## -*- texinfo -*- 20 | ## @deftypefn {statistics} {@var{y} =} raylpdf (@var{x}, @var{sigma}) 21 | ## 22 | ## Rayleigh probability density function (PDF). 23 | ## 24 | ## For each element of @var{x}, compute the probability density function (PDF) 25 | ## of the Rayleigh distribution with scale parameter @var{sigma}. The size of 26 | ## @var{p} is the common size of @var{x} and @var{sigma}. A scalar input 27 | ## functions as a constant matrix of the same size as the other inputs. 28 | ## 29 | ## Further information about the Rayleigh distribution can be found at 30 | ## @url{https://en.wikipedia.org/wiki/Rayleigh_distribution} 31 | ## 32 | ## @seealso{raylcdf, raylinv, raylrnd, raylfit, rayllike, raylstat} 33 | ## @end deftypefn 34 | 35 | function y = raylpdf (x, sigma) 36 | 37 | ## Check for valid number of input arguments 38 | if (nargin < 2) 39 | error ("raylpdf: function called with too few input arguments."); 40 | endif 41 | 42 | ## Check for common size of X and SIGMA 43 | if (! isscalar (x) || ! isscalar (sigma)) 44 | [retval, x, sigma] = common_size (x, sigma); 45 | if (retval > 0) 46 | error ("raylpdf: X and SIGMA must be of common size or scalars."); 47 | endif 48 | endif 49 | 50 | ## Check for X and SIGMA being reals 51 | if (iscomplex (x) || iscomplex (sigma)) 52 | error ("raylpdf: X and SIGMA must not be complex."); 53 | endif 54 | 55 | ## Calculate Rayleigh PDF 56 | y = x .* exp ((-x .^ 2) ./ (2 .* sigma .^ 2)) ./ (sigma .^ 2); 57 | 58 | ## Continue argument check 59 | k = find (! isfinite (x) | ! (sigma > 0)); 60 | if (any (k)) 61 | y(k) = NaN; 62 | endif 63 | k = x < 0; 64 | if (any (k)) 65 | y(k) = 0; 66 | endif 67 | 68 | endfunction 69 | 70 | %!demo 71 | %! ## Plot various PDFs from the Rayleigh distribution 72 | %! x = 0:0.01:10; 73 | %! y1 = raylpdf (x, 0.5); 74 | %! y2 = raylpdf (x, 1); 75 | %! y3 = raylpdf (x, 2); 76 | %! y4 = raylpdf (x, 3); 77 | %! y5 = raylpdf (x, 4); 78 | %! plot (x, y1, "-b", x, y2, "g", x, y3, "-r", x, y4, "-m", x, y5, "-k") 79 | %! grid on 80 | %! ylim ([0, 1.25]) 81 | %! legend ({"σ = 0,5", "σ = 1", "σ = 2", ... 82 | %! "σ = 3", "σ = 4"}, "location", "northeast") 83 | %! title ("Rayleigh PDF") 84 | %! xlabel ("values in x") 85 | %! ylabel ("density") 86 | 87 | ## Test output 88 | %!test 89 | %! x = 0:0.5:2.5; 90 | %! sigma = 1:6; 91 | %! y = raylpdf (x, sigma); 92 | %! expected_y = [0.0000, 0.1212, 0.1051, 0.0874, 0.0738, 0.0637]; 93 | %! assert (y, expected_y, 0.001); 94 | %!test 95 | %! x = 0:0.5:2.5; 96 | %! y = raylpdf (x, 0.5); 97 | %! expected_y = [0.0000, 1.2131, 0.5413, 0.0667, 0.0027, 0.0000]; 98 | %! assert (y, expected_y, 0.001); 99 | 100 | ## Test input validation 101 | %!error raylpdf () 102 | %!error raylpdf (1) 103 | %!error ... 104 | %! raylpdf (ones (3), ones (2)) 105 | %!error ... 106 | %! raylpdf (ones (2), ones (3)) 107 | %!error raylpdf (i, 2) 108 | %!error raylpdf (2, i) 109 | -------------------------------------------------------------------------------- /inst/dist_fun/wienrnd.m: -------------------------------------------------------------------------------- 1 | ## Copyright (C) 1995-2017 Friedrich Leisch 2 | ## Copyright (C) 2023 Andreas Bertsatos 3 | ## 4 | ## This file is part of the statistics package for GNU Octave. 5 | ## 6 | ## This program is free software; you can redistribute it and/or modify it under 7 | ## the terms of the GNU General Public License as published by the Free Software 8 | ## Foundation; either version 3 of the License, or (at your option) any later 9 | ## version. 10 | ## 11 | ## This program is distributed in the hope that it will be useful, but WITHOUT 12 | ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 | ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 14 | ## details. 15 | ## 16 | ## You should have received a copy of the GNU General Public License along with 17 | ## this program; if not, see . 18 | 19 | ## -*- texinfo -*- 20 | ## @deftypefn {statistics} {@var{r} =} wienrnd (@var{t}, @var{d}, @var{n}) 21 | ## 22 | ## Return a simulated realization of the @var{d}-dimensional Wiener Process 23 | ## on the interval [0, @var{t}]. 24 | ## 25 | ## If @var{d} is omitted, @var{d} = 1 is used. The first column of the 26 | ## return matrix contains time, the remaining columns contain the Wiener 27 | ## process. 28 | ## 29 | ## The optional parameter @var{n} defines the number of summands used for 30 | ## simulating the process over an interval of length 1. If @var{n} is 31 | ## omitted, @var{n} = 1000 is used. 32 | ## @end deftypefn 33 | 34 | function r = wienrnd (t, d, n) 35 | 36 | if (nargin == 1) 37 | d = 1; 38 | n = 1000; 39 | elseif (nargin == 2) 40 | n = 1000; 41 | elseif (nargin > 3) 42 | print_usage (); 43 | endif 44 | 45 | if (! isscalar (t) || ! isscalar (d) || ! isscalar (n)) 46 | error ("wienrnd: T, D, and N must all be scalars."); 47 | endif 48 | 49 | if (! (fix (t) == t) || ! (fix (d) == d) || ! (fix (n) == n) 50 | || t <= 0 || d <= 0 || n <= 0) 51 | error ("wienrnd: T, D, and N must all be positive integers."); 52 | endif 53 | 54 | r = randn (n * t, d); 55 | r = cumsum (r) / sqrt (n); 56 | 57 | r = [((1: n*t)' / n), r]; 58 | 59 | endfunction 60 | 61 | %!error wienrnd (0) 62 | %!error wienrnd (1, 3, -50) 63 | %!error wienrnd (5, 0) 64 | %!error wienrnd (0.4, 3, 5) 65 | %!error wienrnd ([1 4], 3, 5) 66 | -------------------------------------------------------------------------------- /inst/dist_fun/wishpdf.m: -------------------------------------------------------------------------------- 1 | ## Copyright (C) 2013 Nir Krakauer 2 | ## 3 | ## This file is part of the statistics package for GNU Octave. 4 | ## 5 | ## This program is free software; you can redistribute it and/or modify it under 6 | ## the terms of the GNU General Public License as published by the Free Software 7 | ## Foundation; either version 3 of the License, or (at your option) any later 8 | ## version. 9 | ## 10 | ## This program is distributed in the hope that it will be useful, but WITHOUT 11 | ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 12 | ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 13 | ## details. 14 | ## 15 | ## You should have received a copy of the GNU General Public License along with 16 | ## this program; if not, see . 17 | 18 | ## -*- texinfo -*- 19 | ## @deftypefn {statistics} {@var{y} =} wishpdf (@var{W}, @var{Sigma}, @var{df}, @var{log_y}=false) 20 | ## 21 | ## Compute the probability density function of the Wishart distribution 22 | ## 23 | ## Inputs: A @var{p} x @var{p} matrix @var{W} where to find the PDF. The @var{p} 24 | ## x @var{p} positive definite matrix @var{Sigma} and scalar degrees of freedom 25 | ## parameter @var{df} characterizing the Wishart distribution. (For the density 26 | ## to be finite, need @var{df} > (@var{p} - 1).) 27 | ## 28 | ## If the flag @var{log_y} is set, return the log probability density -- this 29 | ## helps avoid underflow when the numerical value of the density is very small 30 | ## 31 | ## Output: @var{y} is the probability density of Wishart(@var{Sigma}, @var{df}) 32 | ## at @var{W}. 33 | ## 34 | ## @seealso{wishrnd, iwishpdf, iwishrnd} 35 | ## @end deftypefn 36 | 37 | function y = wishpdf (W, Sigma, df, log_y=false) 38 | 39 | if (nargin < 3) 40 | print_usage (); 41 | endif 42 | 43 | p = size(Sigma, 1); 44 | 45 | if (df <= (p - 1)) 46 | error ("wishpdf: DF too small, no finite densities exist."); 47 | endif 48 | 49 | ## calculate the logarithm of G_d(df/2), the multivariate gamma function 50 | g = (p * (p-1) / 4) * log(pi); 51 | for i = 1:p 52 | g = g + log(gamma((df + (1 - i))/2)); 53 | endfor 54 | 55 | C = chol(Sigma); 56 | 57 | ## use formulas for determinant of positive definite matrix for better 58 | ## efficiency and numerical accuracy 59 | logdet_W = 2*sum(log(diag(chol(W)))); 60 | logdet_Sigma = 2*sum(log(diag(C))); 61 | 62 | y = -(df*p)/2 * log(2) - (df/2)*logdet_Sigma - g + ... 63 | ((df - p - 1)/2)*logdet_W - trace(chol2inv(C)*W)/2; 64 | 65 | if ~log_y 66 | y = exp(y); 67 | endif 68 | endfunction 69 | 70 | ##test results cross-checked against dwish function in R MCMCpack library 71 | %!assert(wishpdf(4, 3, 3.1), 0.07702496, 1E-7); 72 | %!assert(wishpdf([2 -0.3;-0.3 4], [1 0.3;0.3 1], 4), 0.004529741, 1E-7); 73 | %!assert(wishpdf([6 2 5; 2 10 -5; 5 -5 25], [9 5 5; 5 10 -8; 5 -8 22], 5.1), 4.474865e-10, 1E-15); 74 | 75 | %% Test input validation 76 | %!error wishpdf () 77 | %!error wishpdf (1, 2) 78 | %!error wishpdf (1, 2, 0) 79 | 80 | %!error wishpdf (1, 2) 81 | -------------------------------------------------------------------------------- /inst/dist_fun/wishrnd.m: -------------------------------------------------------------------------------- 1 | ## Copyright (C) 2013-2019 Nir Krakauer 2 | ## 3 | ## This file is part of the statistics package for GNU Octave. 4 | ## 5 | ## This program is free software; you can redistribute it and/or modify it under 6 | ## the terms of the GNU General Public License as published by the Free Software 7 | ## Foundation; either version 3 of the License, or (at your option) any later 8 | ## version. 9 | ## 10 | ## This program is distributed in the hope that it will be useful, but WITHOUT 11 | ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 12 | ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 13 | ## details. 14 | ## 15 | ## You should have received a copy of the GNU General Public License along with 16 | ## this program; if not, see . 17 | 18 | ## -*- texinfo -*- 19 | ## @deftypefn {statistics} {[@var{W}, @var{D}] =} wishrnd (@var{Sigma}, @var{df}, @var{D}, @var{n}=1) 20 | ## 21 | ## Return a random matrix sampled from the Wishart distribution with given 22 | ## parameters 23 | ## 24 | ## Inputs: the @math{p x p} positive definite matrix @var{Sigma} (or the 25 | ## lower-triangular Cholesky factor @var{D} of @var{Sigma}) and scalar degrees 26 | ## of freedom parameter @var{df}. 27 | ## 28 | ## @var{df} can be non-integer as long as @math{@var{df} > p} 29 | ## 30 | ## Output: a random @math{p x p} matrix @var{W} from the 31 | ## Wishart(@var{Sigma}, @var{df}) distribution. If @var{n} > 1, then @var{W} is 32 | ## @var{p} x @var{p} x @var{n} and holds @var{n} such random matrices. 33 | ## (Optionally, the lower-triangular Cholesky factor @var{D} of @var{Sigma} is 34 | ## also returned.) 35 | ## 36 | ## Averaged across many samples, the mean of @var{W} should approach 37 | ## @var{df}*@var{Sigma}, and the variance of each element @var{W}_ij should 38 | ## approach @var{df}*(@var{Sigma}_ij^2 + @var{Sigma}_ii*@var{Sigma}_jj) 39 | ## 40 | ## @subheading References 41 | ## 42 | ## @enumerate 43 | ## @item 44 | ## Yu-Cheng Ku and Peter Bloomfield (2010), Generating Random Wishart Matrices 45 | ## with Fractional Degrees of Freedom in OX, 46 | ## http://www.gwu.edu/~forcpgm/YuChengKu-030510final-WishartYu-ChengKu.pdf 47 | ## @end enumerate 48 | ## 49 | ## @seealso{wishpdf, iwishpdf, iwishrnd} 50 | ## @end deftypefn 51 | 52 | function [W, D] = wishrnd (Sigma, df, D, n=1) 53 | 54 | if (nargin < 2) 55 | print_usage (); 56 | endif 57 | 58 | if nargin < 3 || isempty(D) 59 | try 60 | D = chol(Sigma, 'lower'); 61 | catch 62 | error (strcat (["iwishrnd: Cholesky decomposition failed;"], ... 63 | [" SIGMA probably not positive definite."])); 64 | end_try_catch 65 | endif 66 | 67 | p = size(D, 1); 68 | 69 | if df < p 70 | df = floor(df); #distribution not defined for small noninteger df 71 | df_isint = 1; 72 | else 73 | #check for integer degrees of freedom 74 | df_isint = (df == floor(df)); 75 | endif 76 | 77 | if ~df_isint 78 | [ii, jj] = ind2sub([p, p], 1:(p*p)); 79 | endif 80 | 81 | if n > 1 82 | W = nan(p, p, n); 83 | endif 84 | 85 | for i = 1:n 86 | if df_isint 87 | Z = D * randn(p, df); 88 | else 89 | Z = diag(sqrt(chi2rnd(df - (0:(p-1))))); #fill diagonal 90 | ##note: chi2rnd(x) is equivalent to 2*randg(x/2), but the latter seems to 91 | ## offer no performance advantage 92 | Z(ii > jj) = randn(p*(p-1)/2, 1); #fill lower triangle 93 | Z = D * Z; 94 | endif 95 | W(:, :, i) = Z*Z'; 96 | endfor 97 | endfunction 98 | 99 | 100 | %!assert(size (wishrnd (1,2)), [1, 1]); 101 | %!assert(size (wishrnd (1,2,[])), [1, 1]); 102 | %!assert(size (wishrnd (1,2,1)), [1, 1]); 103 | %!assert(size (wishrnd ([],2,1)), [1, 1]); 104 | %!assert(size (wishrnd ([3 1; 1 3], 2.00001, [], 1)), [2, 2]); 105 | %!assert(size (wishrnd (eye(2), 2, [], 3)), [2, 2, 3]); 106 | 107 | %% Test input validation 108 | %!error wishrnd () 109 | %!error wishrnd (1) 110 | %!error wishrnd ([1; 1], 2) 111 | -------------------------------------------------------------------------------- /inst/dist_obj/private/__disp__.m: -------------------------------------------------------------------------------- 1 | ## Copyright (C) 2024 Andreas Bertsatos 2 | ## 3 | ## This file is part of the statistics package for GNU Octave. 4 | ## 5 | ## This program is free software: you can redistribute it and/or 6 | ## modify it under the terms of the GNU General Public License as 7 | ## published by the Free Software Foundation, either version 3 of the 8 | ## License, or (at your option) any later version. 9 | ## 10 | ## This program is distributed in the hope that it will be useful, but 11 | ## WITHOUT ANY WARRANTY; without even the implied warranty of 12 | ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | ## General Public License for more details. 14 | ## 15 | ## You should have received a copy of the GNU General Public License 16 | ## along with this program; see the file COPYING. If not, see 17 | ## . 18 | 19 | ## -*- texinfo -*- 20 | ## @deftypefn {Private Function} __disp__ (@var{pd}, @var{distname}) 21 | ## 22 | ## Display summary of a probability distribution object. 23 | ## 24 | ## @end deftypefn 25 | 26 | function ci = __disp__ (pd, distname) 27 | 28 | if (isscalar (pd)) 29 | 30 | ## Handle special case of PiecewiseLinearDistribution 31 | if (strcmpi (pd.DistributionName, "PiecewiseLinearDistribution")) 32 | ## Print distribution header 33 | fprintf (" %s\n\n", pd.DistributionName); 34 | ## Print parameter values 35 | for i = 1:numel (pd.x) 36 | fprintf ("F(%g) = %g\n", pd.x(i), pd.Fx(i)); 37 | endfor 38 | ## Print trunctation interval if applicable 39 | if (pd.IsTruncated) 40 | fprintf (" Truncated to the interval [%g, %g]\n\n", pd.Truncation); 41 | else 42 | fprintf ("\n"); 43 | endif 44 | 45 | ## Handle special case of MultinomialDistribution 46 | elseif (strcmpi (pd.DistributionName, "MultinomialDistribution")) 47 | ## Print distribution header 48 | fprintf (" %s\n\n", pd.DistributionName); 49 | ## Print parameter values 50 | fprintf (" Probabilities:\n"); 51 | fprintf (" %0.4f", pd.Probabilities); 52 | fprintf ("\n\n"); 53 | ## Print trunctation interval if applicable 54 | if (pd.IsTruncated) 55 | fprintf (" Truncated to the interval [%g, %g]\n\n", pd.Truncation); 56 | endif 57 | 58 | ## Handle all other cases 59 | else 60 | ## Get required length for parameter values 61 | PVlen = max (arrayfun (@(x) numel (sprintf ("%g", x)), ... 62 | pd.ParameterValues)); 63 | PVstr = sprintf ("%%%dg", PVlen); 64 | 65 | ## Prepare template for fitted and not fitted distributions 66 | pat1 = [" %+7s = ", PVstr, " [%g, %g]\n"]; 67 | pat2 = [" %+7s = ", PVstr, "\n"]; 68 | 69 | ## Grad distributions that are non fittable 70 | if (any (strcmpi (pd.DistributionCode, {"unif", "tri", "logu"}))) 71 | fitted = false; 72 | ParameterIsFixed = true; 73 | elseif (all (pd.ParameterIsFixed)) 74 | fitted = false; 75 | ParameterIsFixed = pd.ParameterIsFixed; 76 | else 77 | fitted = true; 78 | ParameterIsFixed = pd.ParameterIsFixed; 79 | endif 80 | 81 | ## Print distribution header 82 | fprintf (" %s\n\n", pd.DistributionName); 83 | fprintf (" %s\n", distname); 84 | ## Print parameter values 85 | for i = 1:pd.NumParameters 86 | if (fitted && ! ParameterIsFixed(i)) 87 | fprintf (pat1, pd.ParameterNames{i}, pd.ParameterValues(i), ... 88 | pd.ParameterCI(1,i), pd.ParameterCI(2,i)); 89 | else 90 | fprintf (pat2, pd.ParameterNames{i}, pd.ParameterValues(i)); 91 | endif 92 | endfor 93 | ## Print trunctation interval if applicable 94 | if (pd.IsTruncated) 95 | fprintf (" Truncated to the interval [%g, %g]\n\n", pd.Truncation); 96 | else 97 | fprintf ("\n"); 98 | endif 99 | endif 100 | else 101 | fprintf ("%dx%d %s array\n", size (pd), class (pd)); 102 | endif 103 | 104 | endfunction 105 | -------------------------------------------------------------------------------- /inst/dist_obj/private/__paramci__.m: -------------------------------------------------------------------------------- 1 | ## Copyright (C) 2024 Andreas Bertsatos 2 | ## 3 | ## This file is part of the statistics package for GNU Octave. 4 | ## 5 | ## This program is free software: you can redistribute it and/or 6 | ## modify it under the terms of the GNU General Public License as 7 | ## published by the Free Software Foundation, either version 3 of the 8 | ## License, or (at your option) any later version. 9 | ## 10 | ## This program is distributed in the hope that it will be useful, but 11 | ## WITHOUT ANY WARRANTY; without even the implied warranty of 12 | ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | ## General Public License for more details. 14 | ## 15 | ## You should have received a copy of the GNU General Public License 16 | ## along with this program; see the file COPYING. If not, see 17 | ## . 18 | 19 | ## -*- texinfo -*- 20 | ## @deftypefn {Private Function} [@var{nlogl}, @var{param}, @var{other}] = __paramci__ (@var{pd}, @var{varargin}) 21 | ## 22 | ## Compute the cofindence intervals for the selected parameters of a 23 | ## probability distribution object. 24 | ## 25 | ## @end deftypefn 26 | 27 | function ci = __paramci__ (pd, varargin) 28 | 29 | ## Get Distribution specific info 30 | distname = pd.DistributionCode; 31 | parnames = pd.ParameterNames(! pd.ParameterIsFixed); 32 | 33 | ## Add defaults and parse optional arguments 34 | alpha = 0.05; 35 | param = logical ([1:numel(parnames)]); 36 | if (mod (numel (varargin), 2) != 0) 37 | error ("paramci: optional arguments must be in NAME-VALUE pairs."); 38 | endif 39 | while (numel (varargin) > 0) 40 | switch (tolower (varargin{1})) 41 | 42 | case "alpha" 43 | alpha = varargin{2}; 44 | if (! isscalar (alpha) || ! isnumeric (alpha) || ... 45 | alpha <= 0 || alpha >= 1) 46 | error ("paramci: invalid VALUE for 'Alpha' argument."); 47 | endif 48 | 49 | case "parameter" 50 | if (! isvector (varargin{2}) || 51 | ((iscellstr (varargin{2}) || isnumeric (varargin{2})) && 52 | numel (varargin{2}) > numel (parnames))) 53 | error ("paramci: invalid VALUE size for 'Parameter' argument."); 54 | endif 55 | if (iscellstr (varargin{2})) 56 | tmp = cellfun (@(x) strcmpi (x, parnames), varargin{2}, ... 57 | "UniformOutput", false); 58 | param = or (tmp{:}); 59 | elseif (isnumeric (varargin{2})) 60 | param = ismember (parnames, varargin{2}); 61 | else # assume it is a character vector 62 | param = strcmpi (varargin{2}, parnames); 63 | endif 64 | if (! any (param)) 65 | error ("paramci: unknown distribution parameter."); 66 | endif 67 | 68 | case {"type", "logflag"} 69 | printf ("paramci: '%s' argument not supported yet.", varargin{1}); 70 | 71 | otherwise 72 | error ("paramci: invalid NAME for optional argument."); 73 | 74 | endswitch 75 | varargin([1:2]) = []; 76 | endwhile 77 | 78 | ## Get confidence intervals for all parameters from selected distribution 79 | if (strcmpi (distname, "bino")) 80 | ntrials = pd.N; 81 | [~, ci] = mle (pd.InputData.data, "distribution", distname, ... 82 | "alpha", alpha, "ntrials", ntrials, ... 83 | "frequency", pd.InputData.freq); 84 | 85 | elseif (strcmpi (distname, "gp")) 86 | theta = pd.theta; 87 | [~, ci] = mle (pd.InputData.data, "distribution", distname, ... 88 | "alpha", alpha, "theta", theta, ... 89 | "frequency", pd.InputData.freq); 90 | 91 | elseif (strcmpi (distname, "hn")) 92 | mu = pd.mu; 93 | [~, ci] = mle (pd.InputData.data, "distribution", distname, ... 94 | "alpha", alpha, "mu", mu, "frequency", pd.InputData.freq); 95 | 96 | elseif (! pd.CensoringAllowed) 97 | [~, ci] = mle (pd.InputData.data, "distribution", distname, ... 98 | "alpha", alpha, "frequency", pd.InputData.freq); 99 | 100 | else 101 | [~, ci] = mle (pd.InputData.data, "distribution", distname, ... 102 | "alpha", alpha, "censoring", pd.InputData.cens, ... 103 | "frequency", pd.InputData.freq); 104 | 105 | endif 106 | 107 | ## Return ci only for requested parameters 108 | ci(:, !param) = []; 109 | endfunction 110 | -------------------------------------------------------------------------------- /inst/dist_stat/bisastat.m: -------------------------------------------------------------------------------- 1 | ## Copyright (C) 2024 Andreas Bertsatos 2 | ## 3 | ## This file is part of the statistics package for GNU Octave. 4 | ## 5 | ## This program is free software; you can redistribute it and/or modify it under 6 | ## the terms of the GNU General Public License as published by the Free Software 7 | ## Foundation; either version 3 of the License, or (at your option) any later 8 | ## version. 9 | ## 10 | ## This program is distributed in the hope that it will be useful, but WITHOUT 11 | ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 12 | ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 13 | ## details. 14 | ## 15 | ## You should have received a copy of the GNU General Public License along with 16 | ## this program; if not, see . 17 | 18 | ## -*- texinfo -*- 19 | ## @deftypefn {statistics} {[@var{m}, @var{v}] =} bisastat (@var{beta}, @var{gamma}) 20 | ## 21 | ## Compute statistics of the Birnbaum-Saunders distribution. 22 | ## 23 | ## @code{[@var{m}, @var{v}] = bisastat (@var{beta}, @var{gamma})} returns the 24 | ## mean and variance of the Birnbaum-Saunders distribution with scale parameter 25 | ## @var{beta} and shape parameter @var{gamma}. 26 | ## 27 | ## The size of @var{m} (mean) and @var{v} (variance) is the common size of the 28 | ## input arguments. A scalar input functions as a constant matrix of the 29 | ## same size as the other inputs. 30 | ## 31 | ## Further information about the Birnbaum-Saunders distribution can be found at 32 | ## @url{https://en.wikipedia.org/wiki/Birnbaum%E2%80%93Saunders_distribution} 33 | ## 34 | ## @seealso{bisacdf, bisainv, bisapdf, bisarnd, bisafit, bisalike} 35 | ## @end deftypefn 36 | 37 | function [m, v] = bisastat (beta, gamma) 38 | 39 | ## Check for valid number of input arguments 40 | if (nargin < 2) 41 | error ("bisastat: function called with too few input arguments."); 42 | endif 43 | 44 | ## Check for BETA and GAMMA being numeric 45 | if (! (isnumeric (beta) && isnumeric (gamma))) 46 | error ("bisastat: BETA and GAMMA must be numeric."); 47 | endif 48 | 49 | ## Check for BETA and GAMMA being real 50 | if (iscomplex (beta) || iscomplex (gamma)) 51 | error ("bisastat: BETA and GAMMA must not be complex."); 52 | endif 53 | 54 | ## Check for common size of BETA and GAMMA 55 | if (! isscalar (beta) || ! isscalar (gamma)) 56 | [retval, beta, gamma] = common_size (beta, gamma); 57 | if (retval > 0) 58 | error ("bisastat: BETA and GAMMA must be of common size or scalars."); 59 | endif 60 | endif 61 | 62 | ## Calculate moments 63 | m = beta .* (1 + ((gamma .^ 2) ./ 2)); 64 | v = ((beta .* gamma) .^ 2) .* (1 + ((5 .* (gamma .^ 2)) ./ 4)); 65 | 66 | ## Continue argument check 67 | beta = find (! (beta > 0) | ! (beta < Inf) | ! (gamma > 0) | ! (gamma < Inf)); 68 | if (any (beta)) 69 | m(beta) = NaN; 70 | v(beta) = NaN; 71 | endif 72 | 73 | endfunction 74 | 75 | ## Input validation tests 76 | %!error bisastat () 77 | %!error bisastat (1) 78 | %!error bisastat ({}, 2) 79 | %!error bisastat (1, "") 80 | %!error bisastat (i, 2) 81 | %!error bisastat (1, i) 82 | %!error ... 83 | %! bisastat (ones (3), ones (2)) 84 | %!error ... 85 | %! bisastat (ones (2), ones (3)) 86 | 87 | ## Output validation tests 88 | %!test 89 | %! beta = 1:6; 90 | %! gamma = 1:0.2:2; 91 | %! [m, v] = bisastat (beta, gamma); 92 | %! expected_m = [1.50, 3.44, 5.94, 9.12, 13.10, 18]; 93 | %! expected_v = [2.25, 16.128, 60.858, 172.032, 409.050, 864]; 94 | %! assert (m, expected_m, 1e-2); 95 | %! assert (v, expected_v, 1e-3); 96 | %!test 97 | %! beta = 1:6; 98 | %! [m, v] = bisastat (beta, 1.5); 99 | %! expected_m = [2.125, 4.25, 6.375, 8.5, 10.625, 12.75]; 100 | %! expected_v = [8.5781, 34.3125, 77.2031, 137.2500, 214.4531, 308.8125]; 101 | %! assert (m, expected_m, 1e-3); 102 | %! assert (v, expected_v, 1e-4); 103 | -------------------------------------------------------------------------------- /inst/dist_stat/chi2stat.m: -------------------------------------------------------------------------------- 1 | ## Copyright (C) 2006, 2007 Arno Onken 2 | ## Copyright (C) 2024 Andreas Bertsatos 3 | ## 4 | ## This file is part of the statistics package for GNU Octave. 5 | ## 6 | ## This program is free software; you can redistribute it and/or modify it under 7 | ## the terms of the GNU General Public License as published by the Free Software 8 | ## Foundation; either version 3 of the License, or (at your option) any later 9 | ## version. 10 | ## 11 | ## This program is distributed in the hope that it will be useful, but WITHOUT 12 | ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 | ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 14 | ## details. 15 | ## 16 | ## You should have received a copy of the GNU General Public License along with 17 | ## this program; if not, see . 18 | 19 | ## -*- texinfo -*- 20 | ## @deftypefn {statistics} {[@var{m}, @var{v}] =} chi2stat (@var{df}) 21 | ## 22 | ## Compute statistics of the chi-squared distribution. 23 | ## 24 | ## @code{[@var{m}, @var{v}] = chi2stat (@var{df})} returns the mean and 25 | ## variance of the chi-squared distribution with @var{df} degrees of freedom. 26 | ## 27 | ## The size of @var{m} (mean) and @var{v} (variance) is the same size of the 28 | ## input argument. 29 | ## 30 | ## Further information about the chi-squared distribution can be found at 31 | ## @url{https://en.wikipedia.org/wiki/Chi-squared_distribution} 32 | ## 33 | ## @seealso{chi2cdf, chi2inv, chi2pdf, chi2rnd} 34 | ## @end deftypefn 35 | 36 | function [m, v] = chi2stat (df) 37 | 38 | ## Check for valid number of input arguments 39 | if (nargin < 1) 40 | error ("chi2stat: function called with too few input arguments."); 41 | endif 42 | 43 | ## Check for DF being numeric 44 | if (! isnumeric (df)) 45 | error ("chi2stat: DF must be numeric."); 46 | endif 47 | 48 | ## Check for DF being real 49 | if (iscomplex (df)) 50 | error ("chi2stat: DF must not be complex."); 51 | endif 52 | 53 | ## Calculate moments 54 | m = df; 55 | v = 2 .* df; 56 | 57 | ## Continue argument check 58 | k = find (! (df > 0) | ! (df < Inf)); 59 | if (any (k)) 60 | m(k) = NaN; 61 | v(k) = NaN; 62 | endif 63 | 64 | endfunction 65 | 66 | ## Input validation tests 67 | %!error chi2stat () 68 | %!error chi2stat ({}) 69 | %!error chi2stat ("") 70 | %!error chi2stat (i) 71 | 72 | ## Output validation tests 73 | %!test 74 | %! df = 1:6; 75 | %! [m, v] = chi2stat (df); 76 | %! assert (m, df); 77 | %! assert (v, [2, 4, 6, 8, 10, 12], 0.001); 78 | -------------------------------------------------------------------------------- /inst/dist_stat/evstat.m: -------------------------------------------------------------------------------- 1 | ## Copyright (C) 2022-2024 Andreas Bertsatos 2 | ## 3 | ## This file is part of the statistics package for GNU Octave. 4 | ## 5 | ## This program is free software: you can redistribute it and/or 6 | ## modify it under the terms of the GNU General Public License as 7 | ## published by the Free Software Foundation, either version 3 of the 8 | ## License, or (at your option) any later version. 9 | ## 10 | ## This program is distributed in the hope that it will be useful, but 11 | ## WITHOUT ANY WARRANTY; without even the implied warranty of 12 | ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | ## General Public License for more details. 14 | ## 15 | ## You should have received a copy of the GNU General Public License 16 | ## along with this program; see the file COPYING. If not, see 17 | ## . 18 | 19 | ## -*- texinfo -*- 20 | ## @deftypefn {statistics} {[@var{m}, @var{v}] =} evstat (@var{mu}, @var{sigma}) 21 | ## 22 | ## Compute statistics of the extreme value distribution. 23 | ## 24 | ## @code{[@var{m}, @var{v}] = evstat (@var{mu}, @var{sigma})} returns the mean 25 | ## and variance of the extreme value distribution (also known as the Gumbel 26 | ## or the type I generalized extreme value distribution) with location parameter 27 | ## @var{mu} and scale parameter @var{sigma}. 28 | ## 29 | ## The size of @var{m} (mean) and @var{v} (variance) is the common size of the 30 | ## input arguments. A scalar input functions as a constant matrix of the 31 | ## same size as the other inputs. 32 | ## 33 | ## The type 1 extreme value distribution is also known as the Gumbel 34 | ## distribution. This version is suitable for modeling minima. The mirror image 35 | ## of this distribution can be used to model maxima by negating @var{x}. If 36 | ## @var{y} has a Weibull distribution, then @code{@var{x} = log (@var{y})} has 37 | ## the type 1 extreme value distribution. 38 | ## 39 | ## Further information about the Gumbel distribution can be found at 40 | ## @url{https://en.wikipedia.org/wiki/Gumbel_distribution} 41 | ## 42 | ## @seealso{evcdf, evinv, evpdf, evrnd, evfit, evlike} 43 | ## @end deftypefn 44 | 45 | function [m, v] = evstat (mu, sigma) 46 | 47 | ## Check for valid number of input arguments 48 | if (nargin < 2) 49 | error ("evstat: function called with too few input arguments."); 50 | endif 51 | 52 | ## Check for MU and SIGMA being numeric 53 | if (! (isnumeric (mu) && isnumeric (sigma))) 54 | error ("evstat: MU and SIGMA must be numeric."); 55 | endif 56 | 57 | ## Check for MU and SIGMA being real 58 | if (iscomplex (mu) || iscomplex (sigma)) 59 | error ("evstat: MU and SIGMA must not be complex."); 60 | endif 61 | 62 | ## Check for common size of MU and SIGMA 63 | if (! isscalar (mu) || ! isscalar (sigma)) 64 | [retval, mu, sigma] = common_size (mu, sigma); 65 | if (retval > 0) 66 | error ("evstat: MU and SIGMA must be of common size or scalars."); 67 | endif 68 | endif 69 | 70 | ## Return NaNs for out of range values of SIGMA 71 | sigma(sigma <= 0) = NaN; 72 | 73 | ## Calculate mean and variance 74 | m = mu + psi(1) .* sigma; 75 | v = (pi .* sigma) .^ 2 ./ 6; 76 | 77 | endfunction 78 | 79 | ## Input validation tests 80 | %!error evstat () 81 | %!error evstat (1) 82 | %!error evstat ({}, 2) 83 | %!error evstat (1, "") 84 | %!error evstat (i, 2) 85 | %!error evstat (1, i) 86 | %!error ... 87 | %! evstat (ones (3), ones (2)) 88 | %!error ... 89 | %! evstat (ones (2), ones (3)) 90 | 91 | ## Output validation tests 92 | %!shared x, y0, y1 93 | %! x = [-5, 0, 1, 2, 3]; 94 | %! y0 = [NaN, NaN, 0.4228, 0.8456, 1.2684]; 95 | %! y1 = [-5.5772, -3.4633, -3.0405, -2.6177, -2.1949]; 96 | %!assert (evstat (x, x), y0, 1e-4) 97 | %!assert (evstat (x, x+6), y1, 1e-4) 98 | %!assert (evstat (x, x-6), NaN (1,5)) 99 | -------------------------------------------------------------------------------- /inst/dist_stat/expstat.m: -------------------------------------------------------------------------------- 1 | ## Copyright (C) 2006, 2007 Arno Onken 2 | ## Copyright (C) 2024 Andreas Bertsatos 3 | ## 4 | ## This file is part of the statistics package for GNU Octave. 5 | ## 6 | ## This program is free software; you can redistribute it and/or modify it under 7 | ## the terms of the GNU General Public License as published by the Free Software 8 | ## Foundation; either version 3 of the License, or (at your option) any later 9 | ## version. 10 | ## 11 | ## This program is distributed in the hope that it will be useful, but WITHOUT 12 | ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 | ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 14 | ## details. 15 | ## 16 | ## You should have received a copy of the GNU General Public License along with 17 | ## this program; if not, see . 18 | 19 | ## -*- texinfo -*- 20 | ## @deftypefn {statistics} {[@var{m}, @var{v}] =} expstat (@var{mu}) 21 | ## 22 | ## Compute statistics of the exponential distribution. 23 | ## 24 | ## @code{[@var{m}, @var{v}] = expstat (@var{mu})} returns the mean and 25 | ## variance of the exponential distribution with mean parameter @var{mu}. 26 | ## 27 | ## The size of @var{m} (mean) and @var{v} (variance) is the same size of the 28 | ## input argument. 29 | ## 30 | ## A common alternative parameterization of the exponential distribution is to 31 | ## use the parameter @math{λ} defined as the mean number of events in an 32 | ## interval as opposed to the parameter @math{μ}, which is the mean wait time 33 | ## for an event to occur. @math{λ} and @math{μ} are reciprocals, 34 | ## i.e. @math{μ = 1 / λ}. 35 | ## 36 | ## Further information about the exponential distribution can be found at 37 | ## @url{https://en.wikipedia.org/wiki/Exponential_distribution} 38 | ## 39 | ## @seealso{expcdf, expinv, exppdf, exprnd, expfit, explike} 40 | ## @end deftypefn 41 | 42 | function [m, v] = expstat (mu) 43 | 44 | ## Check for valid number of input arguments 45 | if (nargin < 1) 46 | error ("expstat: function called with too few input arguments."); 47 | endif 48 | 49 | ## Check for MU being numeric 50 | if (! isnumeric (mu)) 51 | error ("expstat: MU must be numeric."); 52 | endif 53 | 54 | ## Check for MU being real 55 | if (iscomplex (mu)) 56 | error ("expstat: MU must not be complex."); 57 | endif 58 | 59 | ## Calculate moments 60 | m = mu; 61 | v = m .^ 2; 62 | 63 | ## Continue argument check 64 | k = find (! (mu > 0) | ! (mu < Inf)); 65 | if (any (k)) 66 | m(k) = NaN; 67 | v(k) = NaN; 68 | endif 69 | 70 | endfunction 71 | 72 | ## Input validation tests 73 | %!error expstat () 74 | %!error expstat ({}) 75 | %!error expstat ("") 76 | %!error expstat (i) 77 | 78 | ## Output validation tests 79 | %!test 80 | %! mu = 1:6; 81 | %! [m, v] = expstat (mu); 82 | %! assert (m, [1, 2, 3, 4, 5, 6], 0.001); 83 | %! assert (v, [1, 4, 9, 16, 25, 36], 0.001); 84 | -------------------------------------------------------------------------------- /inst/dist_stat/fstat.m: -------------------------------------------------------------------------------- 1 | ## Copyright (C) 2006, 2007 Arno Onken 2 | ## Copyright (C) 2024 Andreas Bertsatos 3 | ## 4 | ## This file is part of the statistics package for GNU Octave. 5 | ## 6 | ## This program is free software; you can redistribute it and/or modify it under 7 | ## the terms of the GNU General Public License as published by the Free Software 8 | ## Foundation; either version 3 of the License, or (at your option) any later 9 | ## version. 10 | ## 11 | ## This program is distributed in the hope that it will be useful, but WITHOUT 12 | ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 | ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 14 | ## details. 15 | ## 16 | ## You should have received a copy of the GNU General Public License along with 17 | ## this program; if not, see . 18 | 19 | ## -*- texinfo -*- 20 | ## @deftypefn {statistics} {[@var{m}, @var{v}] =} fstat (@var{df1}, @var{df2}) 21 | ## 22 | ## Compute statistics of the @math{F}-distribution. 23 | ## 24 | ## @code{[@var{m}, @var{v}] = fstat (@var{df1}, @var{df2})} returns the mean and 25 | ## variance of the @math{F}-distribution with @var{df1} and @var{df2} degrees 26 | ## of freedom. 27 | ## 28 | ## The size of @var{m} (mean) and @var{v} (variance) is the common size of the 29 | ## input arguments. A scalar input functions as a constant matrix of the 30 | ## same size as the other inputs. 31 | ## 32 | ## Further information about the @math{F}-distribution can be found at 33 | ## @url{https://en.wikipedia.org/wiki/F-distribution} 34 | ## 35 | ## @seealso{fcdf, finv, fpdf, frnd} 36 | ## @end deftypefn 37 | 38 | function [m, v] = fstat (df1, df2) 39 | 40 | ## Check for valid number of input arguments 41 | if (nargin < 2) 42 | error ("fstat: function called with too few input arguments."); 43 | endif 44 | 45 | ## Check for DF1 and DF2 being numeric 46 | if (! (isnumeric (df1) && isnumeric (df2))) 47 | error ("fstat: DF1 and DF2 must be numeric."); 48 | endif 49 | 50 | ## Check for DF1 and DF2 being real 51 | if (iscomplex (df1) || iscomplex (df2)) 52 | error ("fstat: DF1 and DF2 must not be complex."); 53 | endif 54 | 55 | ## Check for common size of DF1 and DF2 56 | if (! isscalar (df1) || ! isscalar (df2)) 57 | [retval, df1, df2] = common_size (df1, df2); 58 | if (retval > 0) 59 | error ("fstat: DF1 and DF2 must be of common size or scalars."); 60 | endif 61 | endif 62 | 63 | ## Calculate moments 64 | m = df2 ./ (df2 - 2); 65 | v = (2 .* (df2 .^ 2) .* (df1 + df2 - 2)) ./ ... 66 | (df1 .* ((df2 - 2) .^ 2) .* (df2 - 4)); 67 | 68 | ## Continue argument check 69 | k = find (! (df1 > 0) | ! (df1 < Inf) | ! (df2 > 2) | ! (df2 < Inf)); 70 | if (any (k)) 71 | m(k) = NaN; 72 | v(k) = NaN; 73 | endif 74 | 75 | k = find (! (df2 > 4)); 76 | if (any (k)) 77 | v(k) = NaN; 78 | endif 79 | 80 | endfunction 81 | 82 | ## Input validation tests 83 | %!error fstat () 84 | %!error fstat (1) 85 | %!error fstat ({}, 2) 86 | %!error fstat (1, "") 87 | %!error fstat (i, 2) 88 | %!error fstat (1, i) 89 | %!error ... 90 | %! fstat (ones (3), ones (2)) 91 | %!error ... 92 | %! fstat (ones (2), ones (3)) 93 | 94 | ## Output validation tests 95 | %!test 96 | %! df1 = 1:6; 97 | %! df2 = 5:10; 98 | %! [m, v] = fstat (df1, df2); 99 | %! expected_mn = [1.6667, 1.5000, 1.4000, 1.3333, 1.2857, 1.2500]; 100 | %! expected_v = [22.2222, 6.7500, 3.4844, 2.2222, 1.5869, 1.2153]; 101 | %! assert (m, expected_mn, 0.001); 102 | %! assert (v, expected_v, 0.001); 103 | %!test 104 | %! df1 = 1:6; 105 | %! [m, v] = fstat (df1, 5); 106 | %! expected_mn = [1.6667, 1.6667, 1.6667, 1.6667, 1.6667, 1.6667]; 107 | %! expected_v = [22.2222, 13.8889, 11.1111, 9.7222, 8.8889, 8.3333]; 108 | %! assert (m, expected_mn, 0.001); 109 | %! assert (v, expected_v, 0.001); 110 | -------------------------------------------------------------------------------- /inst/dist_stat/geostat.m: -------------------------------------------------------------------------------- 1 | ## Copyright (C) 2006, 2007 Arno Onken 2 | ## Copyright (C) 2024 Andreas Bertsatos 3 | ## 4 | ## This file is part of the statistics package for GNU Octave. 5 | ## 6 | ## This program is free software; you can redistribute it and/or modify it under 7 | ## the terms of the GNU General Public License as published by the Free Software 8 | ## Foundation; either version 3 of the License, or (at your option) any later 9 | ## version. 10 | ## 11 | ## This program is distributed in the hope that it will be useful, but WITHOUT 12 | ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 | ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 14 | ## details. 15 | ## 16 | ## You should have received a copy of the GNU General Public License along with 17 | ## this program; if not, see . 18 | 19 | ## -*- texinfo -*- 20 | ## @deftypefn {statistics} {[@var{m}, @var{v}] =} geostat (@var{ps}) 21 | ## 22 | ## Compute statistics of the geometric distribution. 23 | ## 24 | ## @code{[@var{m}, @var{v}] = geostat (@var{ps})} returns the mean and 25 | ## variance of the geometric distribution with probability of success parameter 26 | ## @var{ps}. 27 | ## 28 | ## The size of @var{m} (mean) and @var{v} (variance) is the same size of the 29 | ## input argument. 30 | ## 31 | ## Further information about the geometric distribution can be found at 32 | ## @url{https://en.wikipedia.org/wiki/Geometric_distribution} 33 | ## 34 | ## @seealso{geocdf, geoinv, geopdf, geornd, geofit} 35 | ## @end deftypefn 36 | 37 | function [m, v] = geostat (ps) 38 | 39 | ## Check for valid number of input arguments 40 | if (nargin < 1) 41 | error ("geostat: function called with too few input arguments."); 42 | endif 43 | 44 | ## Check for PS being numeric 45 | if (! isnumeric (ps)) 46 | error ("geostat: PS must be numeric."); 47 | endif 48 | 49 | ## Check for PS being real 50 | if (iscomplex (ps)) 51 | error ("geostat: PS must not be complex."); 52 | endif 53 | 54 | ## Calculate moments 55 | q = 1 - ps; 56 | m = q ./ ps; 57 | v = q ./ (ps .^ 2); 58 | 59 | ## Continue argument check 60 | k = find (! (ps >= 0) | ! (ps <= 1)); 61 | if (any (k)) 62 | m(k) = NaN; 63 | v(k) = NaN; 64 | endif 65 | 66 | endfunction 67 | 68 | ## Input validation tests 69 | %!error geostat () 70 | %!error geostat ({}) 71 | %!error geostat ("") 72 | %!error geostat (i) 73 | 74 | ## Output validation tests 75 | %!test 76 | %! ps = 1 ./ (1:6); 77 | %! [m, v] = geostat (ps); 78 | %! assert (m, [0, 1, 2, 3, 4, 5], 0.001); 79 | %! assert (v, [0, 2, 6, 12, 20, 30], 0.001); 80 | -------------------------------------------------------------------------------- /inst/dist_stat/hnstat.m: -------------------------------------------------------------------------------- 1 | ## Copyright (C) 2024 Andreas Bertsatos 2 | ## 3 | ## This file is part of the statistics package for GNU Octave. 4 | ## 5 | ## This program is free software; you can redistribute it and/or modify it under 6 | ## the terms of the GNU General Public License as published by the Free Software 7 | ## Foundation; either version 3 of the License, or (at your option) any later 8 | ## version. 9 | ## 10 | ## This program is distributed in the hope that it will be useful, but WITHOUT 11 | ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 12 | ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 13 | ## details. 14 | ## 15 | ## You should have received a copy of the GNU General Public License along with 16 | ## this program; if not, see . 17 | 18 | ## -*- texinfo -*- 19 | ## @deftypefn {statistics} {[@var{m}, @var{v}] =} hnstat (@var{mu}, @var{sigma}) 20 | ## 21 | ## Compute statistics of the half-normal distribution. 22 | ## 23 | ## @code{[@var{m}, @var{v}] = hnstat (@var{mu}, @var{sigma})} returns the mean 24 | ## and variance of the half-normal distribution with non-centrality (distance) 25 | ## parameter @var{mu} and scale parameter @var{sigma}. 26 | ## 27 | ## The size of @var{m} (mean) and @var{v} (variance) is the common size of the 28 | ## input arguments. A scalar input functions as a constant matrix of the 29 | ## same size as the other inputs. 30 | ## 31 | ## Further information about the half-normal distribution can be found at 32 | ## @url{https://en.wikipedia.org/wiki/Half-normal_distribution} 33 | ## 34 | ## @seealso{norminv, norminv, normpdf, normrnd, normfit, normlike} 35 | ## @end deftypefn 36 | 37 | function [m, v] = hnstat (mu, sigma) 38 | 39 | ## Check for valid number of input arguments 40 | if (nargin < 2) 41 | error ("hnstat: function called with too few input arguments."); 42 | endif 43 | 44 | ## Check for MU and SIGMA being numeric 45 | if (! (isnumeric (mu) && isnumeric (sigma))) 46 | error ("hnstat: MU and SIGMA must be numeric."); 47 | endif 48 | 49 | ## Check for MU and SIGMA being real 50 | if (iscomplex (mu) || iscomplex (sigma)) 51 | error ("hnstat: MU and SIGMA must not be complex."); 52 | endif 53 | 54 | ## Check for common size of MU and SIGMA 55 | if (! isscalar (mu) || ! isscalar (sigma)) 56 | [retval, mu, sigma] = common_size (mu, sigma); 57 | if (retval > 0) 58 | error ("hnstat: MU and SIGMA must be of common size or scalars."); 59 | endif 60 | endif 61 | 62 | ## Calculate moments 63 | m = mu + (sigma .* sqrt (2)) ./ sqrt (pi); 64 | v = sigma .^ 2 .* (1 - 2 / pi); 65 | 66 | ## Continue argument check 67 | k = find (! (sigma > 0) | ! (sigma < Inf)); 68 | if (any (k)) 69 | m(k) = NaN; 70 | v(k) = NaN; 71 | endif 72 | 73 | endfunction 74 | 75 | ## Input validation tests 76 | %!error hnstat () 77 | %!error hnstat (1) 78 | %!error hnstat ({}, 2) 79 | %!error hnstat (1, "") 80 | %!error hnstat (i, 2) 81 | %!error hnstat (1, i) 82 | %!error ... 83 | %! hnstat (ones (3), ones (2)) 84 | %!error ... 85 | %! hnstat (ones (2), ones (3)) 86 | 87 | ## Output validation tests 88 | %!test 89 | %! [m, v] = hnstat (0, 1); 90 | %! assert (m, 0.7979, 1e-4); 91 | %! assert (v, 0.3634, 1e-4); 92 | %!test 93 | %! [m, v] = hnstat (2, 1); 94 | %! assert (m, 2.7979, 1e-4); 95 | %! assert (v, 0.3634, 1e-4); 96 | %!test 97 | %! [m, v] = hnstat (2, 2); 98 | %! assert (m, 3.5958, 1e-4); 99 | %! assert (v, 1.4535, 1e-4); 100 | %!test 101 | %! [m, v] = hnstat (2, 2.5); 102 | %! assert (m, 3.9947, 1e-4); 103 | %! assert (v, 2.2711, 1e-4); 104 | %!test 105 | %! [m, v] = hnstat (1.5, 0.5); 106 | %! assert (m, 1.8989, 1e-4); 107 | %! assert (v, 0.0908, 1e-4); 108 | %!test 109 | %! [m, v] = hnstat (-1.5, 0.5); 110 | %! assert (m, -1.1011, 1e-4); 111 | %! assert (v, 0.0908, 1e-4); 112 | -------------------------------------------------------------------------------- /inst/dist_stat/invgstat.m: -------------------------------------------------------------------------------- 1 | ## Copyright (C) 2024 Andreas Bertsatos 2 | ## 3 | ## This file is part of the statistics package for GNU Octave. 4 | ## 5 | ## This program is free software; you can redistribute it and/or modify it under 6 | ## the terms of the GNU General Public License as published by the Free Software 7 | ## Foundation; either version 3 of the License, or (at your option) any later 8 | ## version. 9 | ## 10 | ## This program is distributed in the hope that it will be useful, but WITHOUT 11 | ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 12 | ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 13 | ## details. 14 | ## 15 | ## You should have received a copy of the GNU General Public License along with 16 | ## this program; if not, see . 17 | 18 | ## -*- texinfo -*- 19 | ## @deftypefn {statistics} {[@var{m}, @var{v}] =} invgstat (@var{mu}, @var{lambda}) 20 | ## 21 | ## Compute statistics of the inverse Gaussian distribution. 22 | ## 23 | ## @code{[@var{m}, @var{v}] = invgstat (@var{mu}, @var{lambda})} returns the 24 | ## mean and variance of the inverse Gaussian distribution with mean parameter 25 | ## @var{mu} and shape parameter @var{lambda}. 26 | ## 27 | ## The size of @var{m} (mean) and @var{v} (variance) is the common size of the 28 | ## input arguments. A scalar input functions as a constant matrix of the 29 | ## same size as the other inputs. 30 | ## 31 | ## Further information about the inverse Gaussian distribution can be found at 32 | ## @url{https://en.wikipedia.org/wiki/Inverse_Gaussian_distribution} 33 | ## 34 | ## @seealso{invgcdf, invginv, invgpdf, invgrnd, invgfit, invglike} 35 | ## @end deftypefn 36 | 37 | function [m, v] = invgstat (mu, lambda) 38 | 39 | ## Check for valid number of input arguments 40 | if (nargin < 2) 41 | error ("invgstat: function called with too few input arguments."); 42 | endif 43 | 44 | ## Check for MU and LAMBDA being numeric 45 | if (! (isnumeric (mu) && isnumeric (lambda))) 46 | error ("invgstat: MU and LAMBDA must be numeric."); 47 | endif 48 | 49 | ## Check for MU and LAMBDA being real 50 | if (iscomplex (mu) || iscomplex (lambda)) 51 | error ("invgstat: MU and LAMBDA must not be complex."); 52 | endif 53 | 54 | ## Check for common size of MU and LAMBDA 55 | if (! isscalar (mu) || ! isscalar (lambda)) 56 | [retval, mu, lambda] = common_size (mu, lambda); 57 | if (retval > 0) 58 | error ("invgstat: MU and LAMBDA must be of common size or scalars."); 59 | endif 60 | endif 61 | 62 | ## Calculate moments 63 | m = mu; 64 | v = (mu .^ 3) ./ lambda; 65 | 66 | ## Continue argument check 67 | m(lambda <= 0 | mu <= 0) = NaN; 68 | v(lambda <= 0 | mu <= 0) = NaN; 69 | 70 | endfunction 71 | 72 | ## Input validation tests 73 | %!error invgstat () 74 | %!error invgstat (1) 75 | %!error invgstat ({}, 2) 76 | %!error invgstat (1, "") 77 | %!error invgstat (i, 2) 78 | %!error invgstat (1, i) 79 | %!error ... 80 | %! invgstat (ones (3), ones (2)) 81 | %!error ... 82 | %! invgstat (ones (2), ones (3)) 83 | 84 | ## Output validation tests 85 | %!test 86 | %! [m, v] = invgstat (1, 1); 87 | %! assert (m, 1); 88 | %! assert (v, 1); 89 | %!test 90 | %! [m, v] = invgstat (2, 1); 91 | %! assert (m, 2); 92 | %! assert (v, 8); 93 | %!test 94 | %! [m, v] = invgstat (2, 2); 95 | %! assert (m, 2); 96 | %! assert (v, 4); 97 | %!test 98 | %! [m, v] = invgstat (2, 2.5); 99 | %! assert (m, 2); 100 | %! assert (v, 3.2); 101 | %!test 102 | %! [m, v] = invgstat (1.5, 0.5); 103 | %! assert (m, 1.5); 104 | %! assert (v, 6.75); 105 | -------------------------------------------------------------------------------- /inst/dist_stat/logistat.m: -------------------------------------------------------------------------------- 1 | ## Copyright (C) 2024 Andreas Bertsatos 2 | ## 3 | ## This file is part of the statistics package for GNU Octave. 4 | ## 5 | ## This program is free software; you can redistribute it and/or modify it under 6 | ## the terms of the GNU General Public License as published by the Free Software 7 | ## Foundation; either version 3 of the License, or (at your option) any later 8 | ## version. 9 | ## 10 | ## This program is distributed in the hope that it will be useful, but WITHOUT 11 | ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 12 | ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 13 | ## details. 14 | ## 15 | ## You should have received a copy of the GNU General Public License along with 16 | ## this program; if not, see . 17 | 18 | ## -*- texinfo -*- 19 | ## @deftypefn {statistics} {[@var{m}, @var{v}] =} logistat (@var{mu}, @var{sigma}) 20 | ## 21 | ## Compute statistics of the logistic distribution. 22 | ## 23 | ## @code{[@var{m}, @var{v}] = logistat (@var{mu}, @var{sigma})} returns the mean 24 | ## and variance of the logistic distribution with mean parameter @var{mu} and 25 | ## scale parameter @var{sigma}. 26 | ## 27 | ## The size of @var{m} (mean) and @var{v} (variance) is the common size of the 28 | ## input arguments. A scalar input functions as a constant matrix of the 29 | ## same size as the other inputs. 30 | ## 31 | ## Further information about the logistic distribution can be found at 32 | ## @url{https://en.wikipedia.org/wiki/Logistic_distribution} 33 | ## 34 | ## @seealso{logicdf, logiinv, logipdf, logirnd, logifit, logilike} 35 | ## @end deftypefn 36 | 37 | function [m, v] = logistat (mu, sigma) 38 | 39 | ## Check for valid number of input arguments 40 | if (nargin < 2) 41 | error ("logistat: function called with too few input arguments."); 42 | endif 43 | 44 | ## Check for MU and SIGMA being numeric 45 | if (! (isnumeric (mu) && isnumeric (sigma))) 46 | error ("logistat: MU and SIGMA must be numeric."); 47 | endif 48 | 49 | ## Check for MU and SIGMA being real 50 | if (iscomplex (mu) || iscomplex (sigma)) 51 | error ("logistat: MU and SIGMA must not be complex."); 52 | endif 53 | 54 | ## Check for common size of MU and SIGMA 55 | if (! isscalar (mu) || ! isscalar (sigma)) 56 | [retval, mu, sigma] = common_size (mu, sigma); 57 | if (retval > 0) 58 | error ("logistat: MU and SIGMA must be of common size or scalars."); 59 | endif 60 | endif 61 | 62 | ## Calculate moments 63 | m = mu; 64 | v = (sigma .^ 2 .* pi .^ 2) ./ 3; 65 | 66 | ## Continue argument check 67 | m(sigma <= 0) = NaN; 68 | v(sigma <= 0) = NaN; 69 | 70 | endfunction 71 | 72 | ## Input validation tests 73 | %!error logistat () 74 | %!error logistat (1) 75 | %!error logistat ({}, 2) 76 | %!error logistat (1, "") 77 | %!error logistat (i, 2) 78 | %!error logistat (1, i) 79 | %!error ... 80 | %! logistat (ones (3), ones (2)) 81 | %!error ... 82 | %! logistat (ones (2), ones (3)) 83 | 84 | ## Output validation tests 85 | %!test 86 | %! [m, v] = logistat (0, 1); 87 | %! assert (m, 0); 88 | %! assert (v, 3.2899, 0.001); 89 | %!test 90 | %! [m, v] = logistat (0, 0.8); 91 | %! assert (m, 0); 92 | %! assert (v, 2.1055, 0.001); 93 | %!test 94 | %! [m, v] = logistat (1, 0.6); 95 | %! assert (m, 1); 96 | %! assert (v, 1.1844, 0.001); 97 | %!test 98 | %! [m, v] = logistat (0, 0.4); 99 | %! assert (m, 0); 100 | %! assert (v, 0.5264, 0.001); 101 | %!test 102 | %! [m, v] = logistat (-1, 0.2); 103 | %! assert (m, -1); 104 | %! assert (v, 0.1316, 0.001); 105 | -------------------------------------------------------------------------------- /inst/dist_stat/lognstat.m: -------------------------------------------------------------------------------- 1 | ## Copyright (C) 2006, 2007 Arno Onken 2 | ## Copyright (C) 2024 Andreas Bertsatos 3 | ## 4 | ## This file is part of the statistics package for GNU Octave. 5 | ## 6 | ## This program is free software; you can redistribute it and/or modify it under 7 | ## the terms of the GNU General Public License as published by the Free Software 8 | ## Foundation; either version 3 of the License, or (at your option) any later 9 | ## version. 10 | ## 11 | ## This program is distributed in the hope that it will be useful, but WITHOUT 12 | ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 | ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 14 | ## details. 15 | ## 16 | ## You should have received a copy of the GNU General Public License along with 17 | ## this program; if not, see . 18 | 19 | ## -*- texinfo -*- 20 | ## @deftypefn {statistics} {[@var{m}, @var{v}] =} lognstat (@var{mu}, @var{sigma}) 21 | ## 22 | ## Compute statistics of the lognormal distribution. 23 | ## 24 | ## @code{[@var{m}, @var{v}] = lognstat (@var{mu}, @var{sigma})} returns the mean 25 | ## and variance of the lognormal distribution with mean parameter @var{mu} and 26 | ## standard deviation parameter @var{sigma}, each corresponding to the 27 | ## associated normal distribution. 28 | ## 29 | ## The size of @var{m} (mean) and @var{v} (variance) is the common size of the 30 | ## input arguments. A scalar input functions as a constant matrix of the 31 | ## same size as the other inputs. 32 | ## 33 | ## Further information about the lognormal distribution can be found at 34 | ## @url{https://en.wikipedia.org/wiki/Log-normal_distribution} 35 | ## 36 | ## @seealso{logncdf, logninv, lognpdf, lognrnd, lognfit, lognlike} 37 | ## @end deftypefn 38 | 39 | function [m, v] = lognstat (mu, sigma) 40 | 41 | ## Check for valid number of input arguments 42 | if (nargin < 2) 43 | error ("lognstat: function called with too few input arguments."); 44 | endif 45 | 46 | ## Check for MU and SIGMA being numeric 47 | if (! (isnumeric (mu) && isnumeric (sigma))) 48 | error ("lognstat: MU and SIGMA must be numeric."); 49 | endif 50 | 51 | ## Check for MU and SIGMA being real 52 | if (iscomplex (mu) || iscomplex (sigma)) 53 | error ("lognstat: MU and SIGMA must not be complex."); 54 | endif 55 | 56 | ## Check for common size of MU and SIGMA 57 | if (! isscalar (mu) || ! isscalar (sigma)) 58 | [retval, mu, sigma] = common_size (mu, sigma); 59 | if (retval > 0) 60 | error ("lognstat: MU and SIGMA must be of common size or scalars."); 61 | endif 62 | endif 63 | 64 | ## Calculate moments 65 | m = exp (mu + (sigma .^ 2) ./ 2); 66 | v = (exp (sigma .^ 2) - 1) .* exp (2 .* mu + sigma .^ 2); 67 | 68 | ## Continue argument check 69 | k = find (! (sigma >= 0) | ! (sigma < Inf)); 70 | if (any (k)) 71 | m(k) = NaN; 72 | v(k) = NaN; 73 | endif 74 | 75 | endfunction 76 | 77 | ## Input validation tests 78 | %!error lognstat () 79 | %!error lognstat (1) 80 | %!error lognstat ({}, 2) 81 | %!error lognstat (1, "") 82 | %!error lognstat (i, 2) 83 | %!error lognstat (1, i) 84 | %!error ... 85 | %! lognstat (ones (3), ones (2)) 86 | %!error ... 87 | %! lognstat (ones (2), ones (3)) 88 | 89 | ## Output validation tests 90 | %!test 91 | %! mu = 0:0.2:1; 92 | %! sigma = 0.2:0.2:1.2; 93 | %! [m, v] = lognstat (mu, sigma); 94 | %! expected_m = [1.0202, 1.3231, 1.7860, 2.5093, 3.6693, 5.5845]; 95 | %! expected_v = [0.0425, 0.3038, 1.3823, 5.6447, 23.1345, 100.4437]; 96 | %! assert (m, expected_m, 0.001); 97 | %! assert (v, expected_v, 0.001); 98 | %!test 99 | %! sigma = 0.2:0.2:1.2; 100 | %! [m, v] = lognstat (0, sigma); 101 | %! expected_m = [1.0202, 1.0833, 1.1972, 1.3771, 1.6487, 2.0544]; 102 | %! expected_v = [0.0425, 0.2036, 0.6211, 1.7002, 4.6708, 13.5936]; 103 | %! assert (m, expected_m, 0.001); 104 | %! assert (v, expected_v, 0.001); 105 | -------------------------------------------------------------------------------- /inst/dist_stat/nakastat.m: -------------------------------------------------------------------------------- 1 | ## Copyright (C) 2024 Andreas Bertsatos 2 | ## 3 | ## This file is part of the statistics package for GNU Octave. 4 | ## 5 | ## This program is free software; you can redistribute it and/or modify it under 6 | ## the terms of the GNU General Public License as published by the Free Software 7 | ## Foundation; either version 3 of the License, or (at your option) any later 8 | ## version. 9 | ## 10 | ## This program is distributed in the hope that it will be useful, but WITHOUT 11 | ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 12 | ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 13 | ## details. 14 | ## 15 | ## You should have received a copy of the GNU General Public License along with 16 | ## this program; if not, see . 17 | 18 | ## -*- texinfo -*- 19 | ## @deftypefn {statistics} {[@var{m}, @var{v}] =} nakastat (@var{mu}, @var{omega}) 20 | ## 21 | ## Compute statistics of the Nakagami distribution. 22 | ## 23 | ## @code{[@var{m}, @var{v}] = nakastat (@var{mu}, @var{omega})} returns the mean 24 | ## and variance of the Nakagami distribution with shape parameter @var{mu} and 25 | ## spread parameter @var{omega}. 26 | ## 27 | ## The size of @var{m} (mean) and @var{v} (variance) is the common size of the 28 | ## input arguments. A scalar input functions as a constant matrix of the 29 | ## same size as the other inputs. 30 | ## 31 | ## Further information about the Nakagami distribution can be found at 32 | ## @url{https://en.wikipedia.org/wiki/Normal_distribution} 33 | ## 34 | ## @seealso{nakacdf, nakainv, nakapdf, nakarnd, nakafit, nakalike} 35 | ## @end deftypefn 36 | 37 | function [m, v] = nakastat (mu, omega) 38 | 39 | ## Check for valid number of input arguments 40 | if (nargin < 2) 41 | error ("nakastat: function called with too few input arguments."); 42 | endif 43 | 44 | ## Check for MU and OMEGA being numeric 45 | if (! (isnumeric (mu) && isnumeric (omega))) 46 | error ("nakastat: MU and OMEGA must be numeric."); 47 | endif 48 | 49 | ## Check for MU and OMEGA being real 50 | if (iscomplex (mu) || iscomplex (omega)) 51 | error ("nakastat: MU and OMEGA must not be complex."); 52 | endif 53 | 54 | ## Check for common size of MU and OMEGA 55 | if (! isscalar (mu) || ! isscalar (omega)) 56 | [retval, mu, omega] = common_size (mu, omega); 57 | if (retval > 0) 58 | error ("nakastat: MU and OMEGA must be of common size or scalars."); 59 | endif 60 | endif 61 | 62 | ## Calculate moments 63 | g = gamma (mu + 0.5) ./ gamma (mu); 64 | m = g .* sqrt (omega ./ mu); 65 | v = omega .* (1 - ((1 ./ mu) .* (g .^ 2))); 66 | 67 | ## Continue argument check 68 | knan = mu < 0.5 | omega <= 0; 69 | m(knan) = NaN; 70 | v(knan) = NaN; 71 | 72 | endfunction 73 | 74 | ## Input validation tests 75 | %!error nakastat () 76 | %!error nakastat (1) 77 | %!error nakastat ({}, 2) 78 | %!error nakastat (1, "") 79 | %!error nakastat (i, 2) 80 | %!error nakastat (1, i) 81 | %!error ... 82 | %! nakastat (ones (3), ones (2)) 83 | %!error ... 84 | %! nakastat (ones (2), ones (3)) 85 | 86 | ## Output validation tests 87 | %!test 88 | %! [m, v] = nakastat (1, 1); 89 | %! assert (m, 0.8862269254, 1e-10); 90 | %! assert (v, 0.2146018366, 1e-10); 91 | %!test 92 | %! [m, v] = nakastat (1, 2); 93 | %! assert (m, 1.25331413731, 1e-10); 94 | %! assert (v, 0.42920367321, 1e-10); 95 | %!test 96 | %! [m, v] = nakastat (2, 1); 97 | %! assert (m, 0.93998560299, 1e-10); 98 | %! assert (v, 0.11642706618, 1e-10); 99 | -------------------------------------------------------------------------------- /inst/dist_stat/nbinstat.m: -------------------------------------------------------------------------------- 1 | ## Copyright (C) 2006, 2007 Arno Onken 2 | ## Copyright (C) 2024 Andreas Bertsatos 3 | ## 4 | ## This file is part of the statistics package for GNU Octave. 5 | ## 6 | ## This program is free software; you can redistribute it and/or modify it under 7 | ## the terms of the GNU General Public License as published by the Free Software 8 | ## Foundation; either version 3 of the License, or (at your option) any later 9 | ## version. 10 | ## 11 | ## This program is distributed in the hope that it will be useful, but WITHOUT 12 | ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 | ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 14 | ## details. 15 | ## 16 | ## You should have received a copy of the GNU General Public License along with 17 | ## this program; if not, see . 18 | 19 | ## -*- texinfo -*- 20 | ## @deftypefn {statistics} {[@var{m}, @var{v}] =} nbinstat (@var{r}, @var{ps}) 21 | ## 22 | ## Compute statistics of the negative binomial distribution. 23 | ## 24 | ## @code{[@var{m}, @var{v}] = nbinstat (@var{r}, @var{ps})} returns the mean 25 | ## and variance of the negative binomial distribution with parameters @var{r} 26 | ## and @var{ps}, where @var{r} is the number of successes until the experiment 27 | ## is stopped and @var{ps} is the probability of success in each experiment, 28 | ## given the number of failures in @var{x}. 29 | ## 30 | ## The size of @var{m} (mean) and @var{v} (variance) is the common size of the 31 | ## input arguments. A scalar input functions as a constant matrix of the 32 | ## same size as the other inputs. 33 | ## 34 | ## Further information about the negative binomial distribution can be found at 35 | ## @url{https://en.wikipedia.org/wiki/Negative_binomial_distribution} 36 | ## 37 | ## @seealso{nbincdf, nbininv, nbininv, nbinrnd, nbinfit, nbinlike} 38 | ## @end deftypefn 39 | 40 | function [m, v] = nbinstat (r, ps) 41 | 42 | ## Check for valid number of input arguments 43 | if (nargin < 2) 44 | error ("nbinstat: function called with too few input arguments."); 45 | endif 46 | 47 | ## Check for R and PS being numeric 48 | if (! (isnumeric (r) && isnumeric (ps))) 49 | error ("nbinstat: R and PS must be numeric."); 50 | endif 51 | 52 | ## Check for R and PS being real 53 | if (iscomplex (r) || iscomplex (ps)) 54 | error ("nbinstat: R and PS must not be complex."); 55 | endif 56 | 57 | ## Check for common size of R and PS 58 | if (! isscalar (r) || ! isscalar (ps)) 59 | [retval, r, ps] = common_size (r, ps); 60 | if (retval > 0) 61 | error ("nbinstat: R and PS must be of common size or scalars."); 62 | endif 63 | endif 64 | 65 | ## Calculate moments 66 | q = 1 - ps; 67 | m = r .* q ./ ps; 68 | v = r .* q ./ (ps .^ 2); 69 | 70 | ## Continue argument check 71 | k = find (! (r > 0) | ! (r < Inf) | ! (ps > 0) | ! (ps < 1)); 72 | if (any (k)) 73 | m(k) = NaN; 74 | v(k) = NaN; 75 | endif 76 | 77 | endfunction 78 | 79 | ## Input validation tests 80 | %!error nbinstat () 81 | %!error nbinstat (1) 82 | %!error nbinstat ({}, 2) 83 | %!error nbinstat (1, "") 84 | %!error nbinstat (i, 2) 85 | %!error nbinstat (1, i) 86 | %!error ... 87 | %! nbinstat (ones (3), ones (2)) 88 | %!error ... 89 | %! nbinstat (ones (2), ones (3)) 90 | 91 | ## Output validation tests 92 | %!test 93 | %! r = 1:4; 94 | %! ps = 0.2:0.2:0.8; 95 | %! [m, v] = nbinstat (r, ps); 96 | %! expected_m = [ 4.0000, 3.0000, 2.0000, 1.0000]; 97 | %! expected_v = [20.0000, 7.5000, 3.3333, 1.2500]; 98 | %! assert (m, expected_m, 0.001); 99 | %! assert (v, expected_v, 0.001); 100 | %!test 101 | %! r = 1:4; 102 | %! [m, v] = nbinstat (r, 0.5); 103 | %! expected_m = [1, 2, 3, 4]; 104 | %! expected_v = [2, 4, 6, 8]; 105 | %! assert (m, expected_m, 0.001); 106 | %! assert (v, expected_v, 0.001); 107 | -------------------------------------------------------------------------------- /inst/dist_stat/ncx2stat.m: -------------------------------------------------------------------------------- 1 | ## Copyright (C) 2022-2024 Andreas Bertsatos 2 | ## 3 | ## This file is part of the statistics package for GNU Octave. 4 | ## 5 | ## This program is free software; you can redistribute it and/or modify it under 6 | ## the terms of the GNU General Public License as published by the Free Software 7 | ## Foundation; either version 3 of the License, or (at your option) any later 8 | ## version. 9 | ## 10 | ## This program is distributed in the hope that it will be useful, but WITHOUT 11 | ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 12 | ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 13 | ## details. 14 | ## 15 | ## You should have received a copy of the GNU General Public License along with 16 | ## this program; if not, see . 17 | 18 | ## -*- texinfo -*- 19 | ## @deftypefn {statistics} {[@var{m}, @var{v}] =} ncx2stat (@var{df}, @var{lambda}) 20 | ## 21 | ## Compute statistics for the noncentral chi-squared distribution. 22 | ## 23 | ## @code{[@var{m}, @var{v}] = ncx2stat (@var{df}, @var{lambda})} returns the 24 | ## mean and variance of the noncentral chi-squared distribution with @var{df} 25 | ## degrees of freedom and noncentrality parameter @var{lambda}. 26 | ## 27 | ## The size of @var{m} (mean) and @var{v} (variance) is the common size of the 28 | ## input arguments. A scalar input functions as a constant matrix of the 29 | ## same size as the other inputs. 30 | ## 31 | ## Further information about the noncentral chi-squared distribution can be 32 | ## found at @url{https://en.wikipedia.org/wiki/Noncentral_chi-squared_distribution} 33 | ## 34 | ## @seealso{ncx2cdf, ncx2inv, ncx2pdf, ncx2rnd} 35 | ## @end deftypefn 36 | 37 | function [m, v] = ncx2stat (df, lambda) 38 | 39 | ## Check for valid number of input arguments 40 | if (nargin < 2) 41 | error ("ncx2stat: function called with too few input arguments."); 42 | endif 43 | 44 | ## Check for DF and LAMBDA being numeric 45 | if (! (isnumeric (df) && isnumeric (lambda))) 46 | error ("ncx2stat: DF and LAMBDA must be numeric."); 47 | endif 48 | 49 | ## Check for DF and LAMBDA being real 50 | if (iscomplex (df) || iscomplex (lambda)) 51 | error ("ncx2stat: DF and LAMBDA must not be complex."); 52 | endif 53 | 54 | ## Check for common size of DF and LAMBDA 55 | if (! isscalar (df) || ! isscalar (lambda)) 56 | [retval, df, lambda] = common_size (df, lambda); 57 | if (retval > 0) 58 | error ("ncx2stat: DF and LAMBDA must be of common size or scalars."); 59 | endif 60 | endif 61 | 62 | ## Initialize mean and variance 63 | if (isa (df, "single") || isa (lambda, "single")) 64 | m = NaN (size (df), "single"); 65 | v = m; 66 | else 67 | m = NaN (size (df)); 68 | v = m; 69 | endif 70 | 71 | ## Compute mean and variance for valid parameter values. 72 | k = (df > 0 & lambda >= 0); 73 | if (any (k(:))) 74 | m(k) = lambda(k) + df(k); 75 | v(k) = 2 * (df(k) + 2 * (lambda(k))); 76 | endif 77 | 78 | endfunction 79 | 80 | ## Input validation tests 81 | %!error ncx2stat () 82 | %!error ncx2stat (1) 83 | %!error ncx2stat ({}, 2) 84 | %!error ncx2stat (1, "") 85 | %!error ncx2stat (i, 2) 86 | %!error ncx2stat (1, i) 87 | %!error ... 88 | %! ncx2stat (ones (3), ones (2)) 89 | %!error ... 90 | %! ncx2stat (ones (2), ones (3)) 91 | 92 | ## Output validation tests 93 | %!shared df, d1 94 | %! df = [2, 0, -1, 1, 4]; 95 | %! d1 = [1, NaN, 3, -1, 2]; 96 | %!assert (ncx2stat (df, d1), [3, NaN, NaN, NaN, 6]); 97 | %!assert (ncx2stat ([df(1:2), df(4:5)], 1), [3, NaN, 2, 5]); 98 | %!assert (ncx2stat ([df(1:2), df(4:5)], 3), [5, NaN, 4, 7]); 99 | %!assert (ncx2stat ([df(1:2), df(4:5)], 2), [4, NaN, 3, 6]); 100 | %!assert (ncx2stat (2, [d1(1), d1(3:5)]), [3, 5, NaN, 4]); 101 | %!assert (ncx2stat (0, [d1(1), d1(3:5)]), [NaN, NaN, NaN, NaN]); 102 | %!assert (ncx2stat (1, [d1(1), d1(3:5)]), [2, 4, NaN, 3]); 103 | %!assert (ncx2stat (4, [d1(1), d1(3:5)]), [5, 7, NaN, 6]); 104 | -------------------------------------------------------------------------------- /inst/dist_stat/normstat.m: -------------------------------------------------------------------------------- 1 | ## Copyright (C) 2006, 2007 Arno Onken 2 | ## Copyright (C) 2024 Andreas Bertsatos 3 | ## 4 | ## This file is part of the statistics package for GNU Octave. 5 | ## 6 | ## This program is free software; you can redistribute it and/or modify it under 7 | ## the terms of the GNU General Public License as published by the Free Software 8 | ## Foundation; either version 3 of the License, or (at your option) any later 9 | ## version. 10 | ## 11 | ## This program is distributed in the hope that it will be useful, but WITHOUT 12 | ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 | ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 14 | ## details. 15 | ## 16 | ## You should have received a copy of the GNU General Public License along with 17 | ## this program; if not, see . 18 | 19 | ## -*- texinfo -*- 20 | ## @deftypefn {statistics} {[@var{m}, @var{v}] =} normstat (@var{mu}, @var{sigma}) 21 | ## 22 | ## Compute statistics of the normal distribution. 23 | ## 24 | ## @code{[@var{m}, @var{v}] = normstat (@var{mu}, @var{sigma})} returns the mean 25 | ## and variance of the normal distribution with non-centrality (distance) 26 | ## parameter @var{mu} and scale parameter @var{sigma}. 27 | ## 28 | ## The size of @var{m} (mean) and @var{v} (variance) is the common size of the 29 | ## input arguments. A scalar input functions as a constant matrix of the 30 | ## same size as the other inputs. 31 | ## 32 | ## Further information about the normal distribution can be found at 33 | ## @url{https://en.wikipedia.org/wiki/Normal_distribution} 34 | ## 35 | ## @seealso{norminv, norminv, normpdf, normrnd, normfit, normlike} 36 | ## @end deftypefn 37 | 38 | function [m, v] = normstat (mu, sigma) 39 | 40 | ## Check for valid number of input arguments 41 | if (nargin < 2) 42 | error ("normstat: function called with too few input arguments."); 43 | endif 44 | 45 | ## Check for MU and SIGMA being numeric 46 | if (! (isnumeric (mu) && isnumeric (sigma))) 47 | error ("normstat: MU and SIGMA must be numeric."); 48 | endif 49 | 50 | ## Check for MU and SIGMA being real 51 | if (iscomplex (mu) || iscomplex (sigma)) 52 | error ("normstat: MU and SIGMA must not be complex."); 53 | endif 54 | 55 | ## Check for common size of MU and SIGMA 56 | if (! isscalar (mu) || ! isscalar (sigma)) 57 | [retval, mu, sigma] = common_size (mu, sigma); 58 | if (retval > 0) 59 | error ("normstat: MU and SIGMA must be of common size or scalars."); 60 | endif 61 | endif 62 | 63 | ## Calculate moments 64 | m = mu; 65 | v = sigma .* sigma; 66 | 67 | ## Continue argument check 68 | k = find (! (sigma > 0) | ! (sigma < Inf)); 69 | if (any (k)) 70 | m(k) = NaN; 71 | v(k) = NaN; 72 | endif 73 | 74 | endfunction 75 | 76 | ## Input validation tests 77 | %!error normstat () 78 | %!error normstat (1) 79 | %!error normstat ({}, 2) 80 | %!error normstat (1, "") 81 | %!error normstat (i, 2) 82 | %!error normstat (1, i) 83 | %!error ... 84 | %! normstat (ones (3), ones (2)) 85 | %!error ... 86 | %! normstat (ones (2), ones (3)) 87 | 88 | ## Output validation tests 89 | %!test 90 | %! mu = 1:6; 91 | %! sigma = 0.2:0.2:1.2; 92 | %! [m, v] = normstat (mu, sigma); 93 | %! expected_v = [0.0400, 0.1600, 0.3600, 0.6400, 1.0000, 1.4400]; 94 | %! assert (m, mu); 95 | %! assert (v, expected_v, 0.001); 96 | %!test 97 | %! sigma = 0.2:0.2:1.2; 98 | %! [m, v] = normstat (0, sigma); 99 | %! expected_mn = [0, 0, 0, 0, 0, 0]; 100 | %! expected_v = [0.0400, 0.1600, 0.3600, 0.6400, 1.0000, 1.4400]; 101 | %! assert (m, expected_mn, 0.001); 102 | %! assert (v, expected_v, 0.001); 103 | -------------------------------------------------------------------------------- /inst/dist_stat/plstat.m: -------------------------------------------------------------------------------- 1 | ## Copyright (C) 2024 Andreas Bertsatos 2 | ## 3 | ## This file is part of the statistics package for GNU Octave. 4 | ## 5 | ## This program is free software; you can redistribute it and/or modify it under 6 | ## the terms of the GNU General Public License as published by the Free Software 7 | ## Foundation; either version 3 of the License, or (at your option) any later 8 | ## version. 9 | ## 10 | ## This program is distributed in the hope that it will be useful, but WITHOUT 11 | ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 12 | ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 13 | ## details. 14 | ## 15 | ## You should have received a copy of the GNU General Public License along with 16 | ## this program; if not, see . 17 | 18 | ## -*- texinfo -*- 19 | ## @deftypefn {statistics} {[@var{m}, @var{v}] =} plstat (@var{x}, @var{Fx}) 20 | ## 21 | ## Compute statistics of the piecewise linear distribution. 22 | ## 23 | ## @code{[@var{m}, @var{v}] = plstat (@var{x}, @var{Fx})} returns the mean, 24 | ## @var{m}, and variance, @var{v}, of the piecewise linear distribution with a 25 | ## vector of @var{x} values at which the CDF changes slope and a vector of CDF 26 | ## values @var{Fx} that correspond to each value in @var{x}. Both @var{x} and 27 | ## @var{Fx} must be vectors of the same size and at least 2-elements long. 28 | ## 29 | ## Further information about the piecewise linear distribution can be found at 30 | ## @url{https://en.wikipedia.org/wiki/Piecewise_linear_function} 31 | ## 32 | ## @seealso{plcdf, plinv, plpdf, plrnd} 33 | ## @end deftypefn 34 | 35 | function [m, v] = plstat (x, Fx) 36 | 37 | ## Check for valid number of input arguments 38 | if (nargin < 2) 39 | error ("plstat: function called with too few input arguments."); 40 | endif 41 | 42 | ## Check for common size of X and FX 43 | if (! isvector (x) || ! isvector (Fx) || ! isequal (size (x), size (Fx))) 44 | error ("plstat: X and FX must be vectors of equal size."); 45 | endif 46 | 47 | ## Check for X and FX being at least 2-elements long 48 | if (length (x) < 2 || length (Fx) < 2) 49 | error ("plstat: X and FX must be at least two-elements long."); 50 | endif 51 | 52 | ## Check for Fx being bounded in [0, 1] 53 | if (any (Fx < 0) || any (Fx > 1)) 54 | error ("plstat: FX must be bounded in the range [0, 1]."); 55 | endif 56 | 57 | ## Check for X and FX being reals 58 | if (iscomplex (x) || iscomplex (Fx)) 59 | error ("plstat: X and FX must not be complex."); 60 | endif 61 | 62 | ## Compute the mean and variance 63 | x_m = (x(1:end-1) + x(2:end)) / 2; 64 | dFx = diff (Fx); 65 | m = dot (dFx, x_m); 66 | x_v = diff(x) .^ 2 / 12; 67 | v = dot (dFx, x_v + (x_m - m) .^ 2); 68 | 69 | endfunction 70 | 71 | ## Test output 72 | %!shared x, Fx 73 | %! x = [0, 1, 3, 4, 7, 10]; 74 | %! Fx = [0, 0.2, 0.5, 0.6, 0.7, 1]; 75 | %!assert (plstat (x, Fx), 4.15) 76 | %!test 77 | %! [m, v] = plstat (x, Fx); 78 | %! assert (v, 10.3775, 1e-14) 79 | 80 | ## Test input validation 81 | %!error plstat () 82 | %!error plstat (1) 83 | %!error ... 84 | %! plstat ([0, 1, 2], [0, 1]) 85 | %!error ... 86 | %! plstat ([0], [1]) 87 | %!error ... 88 | %! plstat ([0, 1, 2], [0, 1, 1.5]) 89 | %!error ... 90 | %! plstat ([0, 1, 2], [0, i, 1]) 91 | %!error ... 92 | %! plstat ([0, i, 2], [0, 0.5, 1]) 93 | %!error ... 94 | %! plstat ([0, i, 2], [0, 0.5i, 1]) 95 | -------------------------------------------------------------------------------- /inst/dist_stat/poisstat.m: -------------------------------------------------------------------------------- 1 | ## Copyright (C) 2006, 2007 Arno Onken 2 | ## Copyright (C) 2024 Andreas Bertsatos 3 | ## 4 | ## This file is part of the statistics package for GNU Octave. 5 | ## 6 | ## This program is free software; you can redistribute it and/or modify it under 7 | ## the terms of the GNU General Public License as published by the Free Software 8 | ## Foundation; either version 3 of the License, or (at your option) any later 9 | ## version. 10 | ## 11 | ## This program is distributed in the hope that it will be useful, but WITHOUT 12 | ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 | ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 14 | ## details. 15 | ## 16 | ## You should have received a copy of the GNU General Public License along with 17 | ## this program; if not, see . 18 | 19 | ## -*- texinfo -*- 20 | ## @deftypefn {statistics} {[@var{m}, @var{v}] =} poisstat (@var{lambda}) 21 | ## 22 | ## Compute statistics of the Poisson distribution. 23 | ## 24 | ## @code{[@var{m}, @var{v}] = poisstat (@var{lambda})} returns the mean and 25 | ## variance of the Poisson distribution with rate parameter @var{lambda}. 26 | ## 27 | ## The size of @var{m} (mean) and @var{v} (variance) is the same size of the 28 | ## input argument. 29 | ## 30 | ## Further information about the Poisson distribution can be found at 31 | ## @url{https://en.wikipedia.org/wiki/Poisson_distribution} 32 | ## 33 | ## @seealso{poisscdf, poissinv, poisspdf, poissrnd, poissfit, poisslike} 34 | ## @end deftypefn 35 | 36 | function [m, v] = poisstat (lambda) 37 | 38 | ## Check for valid number of input arguments 39 | if (nargin < 1) 40 | error ("poisstat: function called with too few input arguments."); 41 | endif 42 | 43 | ## Check for SIGMA being numeric 44 | if (! isnumeric (lambda)) 45 | error ("poisstat: SIGMA must be numeric."); 46 | endif 47 | 48 | ## Check for SIGMA being real 49 | if (iscomplex (lambda)) 50 | error ("poisstat: SIGMA must not be complex."); 51 | endif 52 | 53 | ## Set moments 54 | m = lambda; 55 | v = lambda; 56 | 57 | ## Continue argument check 58 | k = find (! (lambda > 0) | ! (lambda < Inf)); 59 | if (any (k)) 60 | m(k) = NaN; 61 | v(k) = NaN; 62 | endif 63 | 64 | endfunction 65 | 66 | ## Input validation tests 67 | %!error poisstat () 68 | %!error poisstat ({}) 69 | %!error poisstat ("") 70 | %!error poisstat (i) 71 | 72 | ## Output validation tests 73 | %!test 74 | %! lambda = 1 ./ (1:6); 75 | %! [m, v] = poisstat (lambda); 76 | %! assert (m, lambda); 77 | %! assert (v, lambda); 78 | -------------------------------------------------------------------------------- /inst/dist_stat/raylstat.m: -------------------------------------------------------------------------------- 1 | ## Copyright (C) 2006, 2007 Arno Onken 2 | ## Copyright (C) 2024 Andreas Bertsatos 3 | ## 4 | ## This file is part of the statistics package for GNU Octave. 5 | ## 6 | ## This program is free software; you can redistribute it and/or modify it under 7 | ## the terms of the GNU General Public License as published by the Free Software 8 | ## Foundation; either version 3 of the License, or (at your option) any later 9 | ## version. 10 | ## 11 | ## This program is distributed in the hope that it will be useful, but WITHOUT 12 | ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 | ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 14 | ## details. 15 | ## 16 | ## You should have received a copy of the GNU General Public License along with 17 | ## this program; if not, see . 18 | 19 | ## -*- texinfo -*- 20 | ## @deftypefn {statistics} {[@var{m}, @var{v}] =} raylstat (@var{sigma}) 21 | ## 22 | ## Compute statistics of the Rayleigh distribution. 23 | ## 24 | ## @code{[@var{m}, @var{v}] = raylstat (@var{sigma})} returns the mean and 25 | ## variance of the Rayleigh distribution with scale parameter @var{sigma}. 26 | ## 27 | ## The size of @var{m} (mean) and @var{v} (variance) is the same size of the 28 | ## input argument. 29 | ## 30 | ## Further information about the Rayleigh distribution can be found at 31 | ## @url{https://en.wikipedia.org/wiki/Rayleigh_distribution} 32 | ## 33 | ## @seealso{raylcdf, raylinv, raylpdf, raylrnd, raylfit, rayllike} 34 | ## @end deftypefn 35 | 36 | function [m, v] = raylstat (sigma) 37 | 38 | ## Check for valid number of input arguments 39 | if (nargin < 1) 40 | error ("raylstat: function called with too few input arguments."); 41 | endif 42 | 43 | ## Check for SIGMA being numeric 44 | if (! isnumeric (sigma)) 45 | error ("raylstat: SIGMA must be numeric."); 46 | endif 47 | 48 | ## Check for SIGMA being real 49 | if (iscomplex (sigma)) 50 | error ("raylstat: SIGMA must not be complex."); 51 | endif 52 | 53 | ## Calculate moments 54 | m = sigma .* sqrt (pi ./ 2); 55 | v = (2 - pi ./ 2) .* sigma .^ 2; 56 | 57 | ## Continue argument check 58 | k = find (! (sigma > 0)); 59 | if (any (k)) 60 | m(k) = NaN; 61 | v(k) = NaN; 62 | endif 63 | 64 | endfunction 65 | 66 | ## Input validation tests 67 | %!error raylstat () 68 | %!error raylstat ({}) 69 | %!error raylstat ("") 70 | %!error raylstat (i) 71 | 72 | ## Output validation tests 73 | %!test 74 | %! sigma = 1:6; 75 | %! [m, v] = raylstat (sigma); 76 | %! expected_m = [1.2533, 2.5066, 3.7599, 5.0133, 6.2666, 7.5199]; 77 | %! expected_v = [0.4292, 1.7168, 3.8628, 6.8673, 10.7301, 15.4513]; 78 | %! assert (m, expected_m, 0.001); 79 | %! assert (v, expected_v, 0.001); 80 | -------------------------------------------------------------------------------- /inst/dist_stat/tristat.m: -------------------------------------------------------------------------------- 1 | ## Copyright (C) 2024 Andreas Bertsatos 2 | ## 3 | ## This file is part of the statistics package for GNU Octave. 4 | ## 5 | ## This program is free software; you can redistribute it and/or modify it under 6 | ## the terms of the GNU General Public License as published by the Free Software 7 | ## Foundation; either version 3 of the License, or (at your option) any later 8 | ## version. 9 | ## 10 | ## This program is distributed in the hope that it will be useful, but WITHOUT 11 | ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 12 | ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 13 | ## details. 14 | ## 15 | ## You should have received a copy of the GNU General Public License along with 16 | ## this program; if not, see . 17 | 18 | ## -*- texinfo -*- 19 | ## @deftypefn {statistics} {[@var{m}, @var{v}] =} tristat (@var{a}, @var{b}, @var{c}) 20 | ## 21 | ## Compute statistics of the Triangular distribution. 22 | ## 23 | ## @code{[@var{m}, @var{v}] = tristat (@var{a}, @var{b}, @var{c})} returns the 24 | ## mean and variance of the Triangular distribution with lower limit parameter 25 | ## @var{a}, peak location (mode) parameter @var{b}, and upper limit parameter 26 | ## @var{c}. 27 | ## 28 | ## The size of @var{m} (mean) and @var{v} (variance) is the common size of the 29 | ## input arguments. A scalar input functions as a constant matrix of the 30 | ## same size as the other inputs. 31 | ## 32 | ## Note that the order of the parameter input arguments has been changed after 33 | ## statistics version 1.6.3 in order to be MATLAB compatible with the parameters 34 | ## used in the TriangularDistribution probability distribution object. More 35 | ## specifically, the positions of the parameters @var{b} and @var{c} have been 36 | ## swapped. As a result, the naming conventions no longer coinside with those 37 | ## used in Wikipedia, in which @math{b} denotes the upper limit and @math{c} 38 | ## denotes the mode or peak parameter. 39 | ## 40 | ## Further information about the triangular distribution can be found at 41 | ## @url{https://en.wikipedia.org/wiki/Triangular_distribution} 42 | ## 43 | ## @seealso{tcdf, tinv, tpdf, trnd} 44 | ## @end deftypefn 45 | 46 | function [m, v] = tristat (a, b, c) 47 | 48 | ## Check for valid number of input arguments 49 | if (nargin < 3) 50 | error ("tristat: function called with too few input arguments."); 51 | endif 52 | 53 | ## Check for A, B, and C being numeric 54 | if (! (isnumeric (a) && isnumeric (b) && isnumeric (c))) 55 | error ("tristat: A, B, and C must be numeric."); 56 | endif 57 | 58 | ## Check for A, B, and C being real 59 | if (! (isreal (a) && isreal (b) && isreal (c))) 60 | error ("tristat: A, B, and C must be real."); 61 | endif 62 | 63 | ## Calculate moments 64 | m = (a + b + c) ./ 3; 65 | v = (a .^ 2 + b .^ 2 + c .^ 2 - a .* b - a .* c - b .* c) ./ 18; 66 | 67 | ## Continue argument check 68 | k = find (! (a < c) | ! (a <= b & b <= c)); 69 | if (any (k)) 70 | m(k) = NaN; 71 | v(k) = NaN; 72 | endif 73 | 74 | endfunction 75 | 76 | ## Input validation tests 77 | %!error tristat () 78 | %!error tristat (1) 79 | %!error tristat (1, 2) 80 | %!error tristat ("i", 2, 1) 81 | %!error tristat (0, "d", 1) 82 | %!error tristat (0, 3, {}) 83 | %!error tristat (i, 2, 1) 84 | %!error tristat (0, i, 1) 85 | %!error tristat (0, 3, i) 86 | 87 | ## Output validation tests 88 | %!test 89 | %! a = 1:5; 90 | %! b = 3:7; 91 | %! c = 5:9; 92 | %! [m, v] = tristat (a, b, c); 93 | %! expected_m = [3, 4, 5, 6, 7]; 94 | %! assert (m, expected_m); 95 | %! assert (v, ones (1, 5) * (2/3)); 96 | -------------------------------------------------------------------------------- /inst/dist_stat/tstat.m: -------------------------------------------------------------------------------- 1 | ## Copyright (C) 2006, 2007 Arno Onken 2 | ## Copyright (C) 2024 Andreas Bertsatos 3 | ## 4 | ## This file is part of the statistics package for GNU Octave. 5 | ## 6 | ## This program is free software; you can redistribute it and/or modify it under 7 | ## the terms of the GNU General Public License as published by the Free Software 8 | ## Foundation; either version 3 of the License, or (at your option) any later 9 | ## version. 10 | ## 11 | ## This program is distributed in the hope that it will be useful, but WITHOUT 12 | ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 | ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 14 | ## details. 15 | ## 16 | ## You should have received a copy of the GNU General Public License along with 17 | ## this program; if not, see . 18 | 19 | ## -*- texinfo -*- 20 | ## @deftypefn {statistics} {[@var{m}, @var{v}] =} tstat (@var{df}) 21 | ## 22 | ## Compute statistics of the Student's T distribution. 23 | ## 24 | ## @code{[@var{m}, @var{v}] = tstat (@var{df})} returns the mean and variance of 25 | ## the Student's T distribution with @var{df} degrees of freedom. 26 | ## 27 | ## The size of @var{m} (mean) and @var{v} (variance) is the same size of the 28 | ## input argument. 29 | ## 30 | ## Further information about the Student's T distribution can be found at 31 | ## @url{https://en.wikipedia.org/wiki/Student%27s_t-distribution} 32 | ## 33 | ## @seealso{tcdf, tinv, tpdf, trnd} 34 | ## @end deftypefn 35 | 36 | function [m, v] = tstat (df) 37 | 38 | ## Check for valid number of input arguments 39 | if (nargin < 1) 40 | error ("tstat: function called with too few input arguments."); 41 | endif 42 | 43 | ## Check for DF being numeric 44 | if (! isnumeric (df)) 45 | error ("tstat: DF must be numeric."); 46 | endif 47 | 48 | ## Check for DF being real 49 | if (iscomplex (df)) 50 | error ("tstat: DF must not be complex."); 51 | endif 52 | 53 | ## Calculate moments 54 | m = zeros (size (df)); 55 | v = df ./ (df - 2); 56 | 57 | ## Continue argument check 58 | k = find (! (df > 1) | ! (df < Inf)); 59 | if (any (k)) 60 | m(k) = NaN; 61 | v(k) = NaN; 62 | endif 63 | k = find (! (df > 2) & (df < Inf)); 64 | if (any (k)) 65 | v(k) = Inf; 66 | endif 67 | 68 | endfunction 69 | 70 | ## Input validation tests 71 | %!error tstat () 72 | %!error tstat ({}) 73 | %!error tstat ("") 74 | %!error tstat (i) 75 | 76 | ## Output validation tests 77 | %!test 78 | %! df = 3:8; 79 | %! [m, v] = tstat (df); 80 | %! expected_m = [0, 0, 0, 0, 0, 0]; 81 | %! expected_v = [3.0000, 2.0000, 1.6667, 1.5000, 1.4000, 1.3333]; 82 | %! assert (m, expected_m); 83 | %! assert (v, expected_v, 0.001); 84 | -------------------------------------------------------------------------------- /inst/dist_stat/unidstat.m: -------------------------------------------------------------------------------- 1 | ## Copyright (C) 2006, 2007 Arno Onken 2 | ## Copyright (C) 2024 Andreas Bertsatos 3 | ## 4 | ## This file is part of the statistics package for GNU Octave. 5 | ## 6 | ## This program is free software; you can redistribute it and/or modify it under 7 | ## the terms of the GNU General Public License as published by the Free Software 8 | ## Foundation; either version 3 of the License, or (at your option) any later 9 | ## version. 10 | ## 11 | ## This program is distributed in the hope that it will be useful, but WITHOUT 12 | ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 | ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 14 | ## details. 15 | ## 16 | ## You should have received a copy of the GNU General Public License along with 17 | ## this program; if not, see . 18 | 19 | ## -*- texinfo -*- 20 | ## @deftypefn {statistics} {[@var{m}, @var{v}] =} unidstat (@var{df}) 21 | ## 22 | ## Compute statistics of the discrete uniform cumulative distribution. 23 | ## 24 | ## @code{[@var{m}, @var{v}] = unidstat (@var{df})} returns the mean and variance 25 | ## of the discrete uniform cumulative distribution with parameter @var{N}, which 26 | ## corresponds to the maximum observable value and must be a positive natural 27 | ## number. 28 | ## 29 | ## The size of @var{m} (mean) and @var{v} (variance) is the same size of the 30 | ## input argument. 31 | ## 32 | ## Further information about the discrete uniform distribution can be found at 33 | ## @url{https://en.wikipedia.org/wiki/Discrete_uniform_distribution} 34 | ## 35 | ## @seealso{unidcdf, unidinv, unidpdf, unidrnd, unidfit} 36 | ## @end deftypefn 37 | 38 | function [m, v] = unidstat (N) 39 | 40 | ## Check for valid number of input arguments 41 | if (nargin < 1) 42 | error ("unidstat: function called with too few input arguments."); 43 | endif 44 | 45 | ## Check for N being numeric 46 | if (! isnumeric (N)) 47 | error ("unidstat: N must be numeric."); 48 | endif 49 | 50 | ## Check for N being real 51 | if (iscomplex (N)) 52 | error ("unidstat: N must not be complex."); 53 | endif 54 | 55 | ## Calculate moments 56 | m = (N + 1) ./ 2; 57 | v = ((N .^ 2) - 1) ./ 12; 58 | 59 | ## Continue argument check 60 | k = find (! (N > 0) | ! (N < Inf) | ! (N == round (N))); 61 | if (any (k)) 62 | m(k) = NaN; 63 | v(k) = NaN; 64 | endif 65 | 66 | endfunction 67 | 68 | ## Input validation tests 69 | %!error unidstat () 70 | %!error unidstat ({}) 71 | %!error unidstat ("") 72 | %!error unidstat (i) 73 | 74 | ## Output validation tests 75 | %!test 76 | %! N = 1:6; 77 | %! [m, v] = unidstat (N); 78 | %! expected_m = [1.0000, 1.5000, 2.0000, 2.5000, 3.0000, 3.5000]; 79 | %! expected_v = [0.0000, 0.2500, 0.6667, 1.2500, 2.0000, 2.9167]; 80 | %! assert (m, expected_m, 0.001); 81 | %! assert (v, expected_v, 0.001); 82 | -------------------------------------------------------------------------------- /inst/dist_stat/unifstat.m: -------------------------------------------------------------------------------- 1 | ## Copyright (C) 2006, 2007 Arno Onken 2 | ## Copyright (C) 2024 Andreas Bertsatos 3 | ## 4 | ## This file is part of the statistics package for GNU Octave. 5 | ## 6 | ## This program is free software; you can redistribute it and/or modify it under 7 | ## the terms of the GNU General Public License as published by the Free Software 8 | ## Foundation; either version 3 of the License, or (at your option) any later 9 | ## version. 10 | ## 11 | ## This program is distributed in the hope that it will be useful, but WITHOUT 12 | ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 | ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 14 | ## details. 15 | ## 16 | ## You should have received a copy of the GNU General Public License along with 17 | ## this program; if not, see . 18 | 19 | ## -*- texinfo -*- 20 | ## @deftypefn {statistics} {[@var{m}, @var{v}] =} unifstat (@var{df}) 21 | ## 22 | ## Compute statistics of the continuous uniform cumulative distribution. 23 | ## 24 | ## @code{[@var{m}, @var{v}] = unifstat (@var{df})} returns the mean and variance 25 | ## of the continuous uniform cumulative distribution with parameters @var{a} and 26 | ## @var{b}, which define the lower and upper bounds of the interval 27 | ## @qcode{[@var{a}, @var{b}]}. 28 | ## 29 | ## The size of @var{m} (mean) and @var{v} (variance) is the common size of the 30 | ## input arguments. A scalar input functions as a constant matrix of the 31 | ## same size as the other inputs. 32 | ## 33 | ## Further information about the continuous uniform distribution can be found at 34 | ## @url{https://en.wikipedia.org/wiki/Continuous_uniform_distribution} 35 | ## 36 | ## @seealso{unifcdf, unifinv, unifpdf, unifrnd, unifit} 37 | ## @end deftypefn 38 | 39 | function [m, v] = unifstat (a, b) 40 | 41 | ## Check for valid number of input arguments 42 | if (nargin < 2) 43 | error ("unifstat: function called with too few input arguments."); 44 | endif 45 | 46 | ## Check for A and B being numeric 47 | if (! (isnumeric (a) && isnumeric (b))) 48 | error ("unifstat: A and B must be numeric."); 49 | endif 50 | 51 | ## Check for A and B being real 52 | if (iscomplex (a) || iscomplex (b)) 53 | error ("unifstat: A and B must not be complex."); 54 | endif 55 | 56 | ## Check for common size of A and B 57 | if (! isscalar (a) || ! isscalar (b)) 58 | [retval, a, b] = common_size (a, b); 59 | if (retval > 0) 60 | error ("unifstat: A and B must be of common size or scalars."); 61 | endif 62 | endif 63 | 64 | ## Calculate moments 65 | m = (a + b) ./ 2; 66 | v = ((b - a) .^ 2) ./ 12; 67 | 68 | ## Continue argument check 69 | k = find (! (-Inf < a) | ! (a < b) | ! (b < Inf)); 70 | if (any (k)) 71 | m(k) = NaN; 72 | v(k) = NaN; 73 | endif 74 | 75 | endfunction 76 | 77 | ## Input validation tests 78 | %!error unifstat () 79 | %!error unifstat (1) 80 | %!error unifstat ({}, 2) 81 | %!error unifstat (1, "") 82 | %!error unifstat (i, 2) 83 | %!error unifstat (1, i) 84 | %!error ... 85 | %! unifstat (ones (3), ones (2)) 86 | %!error ... 87 | %! unifstat (ones (2), ones (3)) 88 | 89 | ## Output validation tests 90 | %!test 91 | %! a = 1:6; 92 | %! b = 2:2:12; 93 | %! [m, v] = unifstat (a, b); 94 | %! expected_m = [1.5000, 3.0000, 4.5000, 6.0000, 7.5000, 9.0000]; 95 | %! expected_v = [0.0833, 0.3333, 0.7500, 1.3333, 2.0833, 3.0000]; 96 | %! assert (m, expected_m, 0.001); 97 | %! assert (v, expected_v, 0.001); 98 | %!test 99 | %! a = 1:6; 100 | %! [m, v] = unifstat (a, 10); 101 | %! expected_m = [5.5000, 6.0000, 6.5000, 7.0000, 7.5000, 8.0000]; 102 | %! expected_v = [6.7500, 5.3333, 4.0833, 3.0000, 2.0833, 1.3333]; 103 | %! assert (m, expected_m, 0.001); 104 | %! assert (v, expected_v, 0.001); 105 | -------------------------------------------------------------------------------- /inst/dist_stat/wblstat.m: -------------------------------------------------------------------------------- 1 | ## Copyright (C) 2006, 2007 Arno Onken 2 | ## Copyright (C) 2024 Andreas Bertsatos 3 | ## 4 | ## This file is part of the statistics package for GNU Octave. 5 | ## 6 | ## This program is free software; you can redistribute it and/or modify it under 7 | ## the terms of the GNU General Public License as published by the Free Software 8 | ## Foundation; either version 3 of the License, or (at your option) any later 9 | ## version. 10 | ## 11 | ## This program is distributed in the hope that it will be useful, but WITHOUT 12 | ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 | ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 14 | ## details. 15 | ## 16 | ## You should have received a copy of the GNU General Public License along with 17 | ## this program; if not, see . 18 | 19 | ## -*- texinfo -*- 20 | ## @deftypefn {statistics} {[@var{m}, @var{v}] =} wblstat (@var{lambda}, @var{k}) 21 | ## 22 | ## Compute statistics of the Weibull distribution. 23 | ## 24 | ## @code{[@var{m}, @var{v}] = wblstat (@var{lambda}, @var{k})} returns the mean 25 | ## and variance of the Weibull distribution with scale parameter @var{lambda} 26 | ## and shape parameter @var{k}. 27 | ## 28 | ## The size of @var{m} (mean) and @var{v} (variance) is the common size of the 29 | ## input arguments. A scalar input functions as a constant matrix of the 30 | ## same size as the other inputs. 31 | ## 32 | ## Further information about the Weibull distribution can be found at 33 | ## @url{https://en.wikipedia.org/wiki/Weibull_distribution} 34 | ## 35 | ## @seealso{wblcdf, wblinv, wblpdf, wblrnd, wblfit, wbllike, wblplot} 36 | ## @end deftypefn 37 | 38 | function [m, v] = wblstat (lambda, k) 39 | 40 | ## Check for valid number of input arguments 41 | if (nargin < 2) 42 | error ("wblstat: function called with too few input arguments."); 43 | endif 44 | 45 | ## Check for LAMBDA and K being numeric 46 | if (! (isnumeric (lambda) && isnumeric (k))) 47 | error ("wblstat: LAMBDA and K must be numeric."); 48 | endif 49 | 50 | ## Check for LAMBDA and K being real 51 | if (iscomplex (lambda) || iscomplex (k)) 52 | error ("wblstat: LAMBDA and K must not be complex."); 53 | endif 54 | 55 | ## Check for common size of LAMBDA and K 56 | if (! isscalar (lambda) || ! isscalar (k)) 57 | [retval, lambda, k] = common_size (lambda, k); 58 | if (retval > 0) 59 | error ("wblstat: LAMBDA and K must be of common size or scalars."); 60 | endif 61 | endif 62 | 63 | ## Calculate moments 64 | m = lambda .* gamma (1 + 1 ./ k); 65 | v = (lambda .^ 2) .* gamma (1 + 2 ./ k) - m .^ 2; 66 | 67 | ## Continue argument check 68 | is_nan = find (! (lambda > 0) | ! (lambda < Inf) | ! (k > 0) | ! (k < Inf)); 69 | if (any (is_nan)) 70 | m(is_nan) = NaN; 71 | v(is_nan) = NaN; 72 | endif 73 | 74 | endfunction 75 | 76 | ## Input validation tests 77 | %!error wblstat () 78 | %!error wblstat (1) 79 | %!error wblstat ({}, 2) 80 | %!error wblstat (1, "") 81 | %!error wblstat (i, 2) 82 | %!error wblstat (1, i) 83 | %!error ... 84 | %! wblstat (ones (3), ones (2)) 85 | %!error ... 86 | %! wblstat (ones (2), ones (3)) 87 | 88 | ## Output validation tests 89 | %!test 90 | %! lambda = 3:8; 91 | %! k = 1:6; 92 | %! [m, v] = wblstat (lambda, k); 93 | %! expected_m = [3.0000, 3.5449, 4.4649, 5.4384, 6.4272, 7.4218]; 94 | %! expected_v = [9.0000, 3.4336, 2.6333, 2.3278, 2.1673, 2.0682]; 95 | %! assert (m, expected_m, 0.001); 96 | %! assert (v, expected_v, 0.001); 97 | %!test 98 | %! k = 1:6; 99 | %! [m, v] = wblstat (6, k); 100 | %! expected_m = [ 6.0000, 5.3174, 5.3579, 5.4384, 5.5090, 5.5663]; 101 | %! expected_v = [36.0000, 7.7257, 3.7920, 2.3278, 1.5923, 1.1634]; 102 | %! assert (m, expected_m, 0.001); 103 | %! assert (v, expected_v, 0.001); 104 | -------------------------------------------------------------------------------- /inst/ff2n.m: -------------------------------------------------------------------------------- 1 | ## Copyright (C) 2022 Andreas Bertsatos 2 | ## based on public domain work by Paul Kienzle 3 | ## 4 | ## This file is part of the statistics package for GNU Octave. 5 | ## 6 | ## This program is free software; you can redistribute it and/or modify it under 7 | ## the terms of the GNU General Public License as published by the Free Software 8 | ## Foundation; either version 3 of the License, or (at your option) any later 9 | ## version. 10 | ## 11 | ## This program is distributed in the hope that it will be useful, but WITHOUT 12 | ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 | ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 14 | ## details. 15 | ## 16 | ## You should have received a copy of the GNU General Public License along with 17 | ## this program; if not, see . 18 | 19 | ## -*- texinfo -*- 20 | ## @deftypefn {statistics} {@var{dFF2} =} ff2n (@var{n}) 21 | ## 22 | ## Two-level full factorial design. 23 | ## 24 | ## @code{@var{dFF2} = ff2n (@var{n})} gives factor settings dFF2 for a two-level 25 | ## full factorial design with n factors. @var{dFF2} is m-by-n, where m is the 26 | ## number of treatments in the full-factorial design. Each row of @var{dFF2} 27 | ## corresponds to a single treatment. Each column contains the settings for a 28 | ## single factor, with values of 0 and 1 for the two levels. 29 | ## 30 | ## @seealso {fullfact} 31 | ## @end deftypefn 32 | 33 | function A = ff2n (n) 34 | if (nargin != 1) 35 | error ("ff2n: wrong number of input arguments."); 36 | endif 37 | if (floor (n) != n || numel (n) != 1 || n < 1 ... 38 | || ! isfinite (n) || ! isreal (n)) 39 | error ("ff2n: @var{N} must be a positive integer scalar."); 40 | endif 41 | A = fullfact (2 * ones (1, n)) - 1; 42 | endfunction 43 | 44 | %!error ff2n (); 45 | %!error ff2n (2, 5); 46 | %!error ff2n (2.5); 47 | %!error ff2n (0); 48 | %!error ff2n (-3); 49 | %!error ff2n (3+2i); 50 | %!error ff2n (Inf); 51 | %!error ff2n (NaN); 52 | %!test 53 | %! A = ff2n (3); 54 | %! assert (A, fullfact (3)); 55 | %!test 56 | %! A = ff2n (8); 57 | %! assert (A, fullfact (8)); 58 | 59 | -------------------------------------------------------------------------------- /inst/fullfact.m: -------------------------------------------------------------------------------- 1 | ## Copyright (C) 2022 Andreas Bertsatos 2 | ## based on public domain work by Paul Kienzle 3 | ## 4 | ## This file is part of the statistics package for GNU Octave. 5 | ## 6 | ## This program is free software; you can redistribute it and/or modify it under 7 | ## the terms of the GNU General Public License as published by the Free Software 8 | ## Foundation; either version 3 of the License, or (at your option) any later 9 | ## version. 10 | ## 11 | ## This program is distributed in the hope that it will be useful, but WITHOUT 12 | ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 | ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 14 | ## details. 15 | ## 16 | ## You should have received a copy of the GNU General Public License along with 17 | ## this program; if not, see . 18 | 19 | ## -*- texinfo -*- 20 | ## @deftypefn {statistics} {@var{A} =} fullfact (@var{N}) 21 | ## 22 | ## Full factorial design. 23 | ## 24 | ## If @var{N} is a scalar, return the full factorial design with @var{N} binary 25 | ## choices, 0 and 1. 26 | ## 27 | ## If @var{N} is a vector, return the full factorial design with ordinal choices 28 | ## 1 through @var{n_i} for each factor @var{i}. 29 | ## 30 | ## Values in @var{N} must be positive integers. 31 | ## 32 | ## @seealso {ff2n} 33 | ## @end deftypefn 34 | 35 | function A = fullfact(n) 36 | if (nargin != 1) 37 | error ("fullfact: wrong number of input arguments."); 38 | endif 39 | if length(n) == 1 40 | if (floor (n) != n || n < 1 || ! isfinite (n) || ! isreal (n)) 41 | error ("fullfact: @var{N} must be a positive integer."); 42 | endif 43 | ## Combinatorial design with n binary choices 44 | A = fullfact(2*ones(1,n))-1; 45 | else 46 | if (any (floor (n) != n) || any (n < 1) || any (! isfinite (n)) ... 47 | || any (! isreal (n))) 48 | error ("fullfact: values in @var{N} must be positive integers."); 49 | endif 50 | ## Combinatorial design with n(i) ordinal choices 51 | A = [1:n(end)]'; 52 | for i=length(n)-1:-1:1 53 | A = [kron([1:n(i)]',ones(rows(A),1)), repmat(A,n(i),1)]; 54 | end 55 | end 56 | endfunction 57 | 58 | %!demo 59 | %! ## Full factorial design with 3 binary variables 60 | %! fullfact (3) 61 | 62 | %!demo 63 | %! ## Full factorial design with 3 ordinal variables 64 | %! fullfact ([2, 3, 4]) 65 | 66 | %!error fullfact (); 67 | %!error fullfact (2, 5); 68 | %!error fullfact (2.5); 69 | %!error fullfact (0); 70 | %!error fullfact (-3); 71 | %!error fullfact (3+2i); 72 | %!error fullfact (Inf); 73 | %!error fullfact (NaN); 74 | %!error fullfact ([1, 2, -3]); 75 | %!error fullfact ([0, 1, 2]); 76 | %!error fullfact ([1, 2, NaN]); 77 | %!error fullfact ([1, 2, Inf]); 78 | %!test 79 | %! A = fullfact (2); 80 | %! assert (A, [0, 0; 0, 1; 1, 0; 1, 1]); 81 | %!test 82 | %! A = fullfact ([1, 2]); 83 | %! assert (A, [1, 1; 1, 2]); 84 | %!test 85 | %! A = fullfact ([1, 2, 4]); 86 | %! A_out = [1, 1, 1; 1, 1, 2; 1, 1, 3; 1, 1, 4; ... 87 | %! 1, 2, 1; 1, 2, 2; 1, 2, 3; 1, 2, 4]; 88 | %! assert (A, A_out); 89 | -------------------------------------------------------------------------------- /inst/loadmodel.m: -------------------------------------------------------------------------------- 1 | ## Copyright (C) 2024 Andreas Bertsatos 2 | ## 3 | ## This file is part of the statistics package for GNU Octave. 4 | ## 5 | ## This program is free software; you can redistribute it and/or modify it under 6 | ## the terms of the GNU General Public License as published by the Free Software 7 | ## Foundation; either version 3 of the License, or (at your option) any later 8 | ## version. 9 | ## 10 | ## This program is distributed in the hope that it will be useful, but WITHOUT 11 | ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 12 | ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 13 | ## details. 14 | ## 15 | ## You should have received a copy of the GNU General Public License along with 16 | ## this program; if not, see . 17 | 18 | ## -*- texinfo -*- 19 | ## @deftypefn {ClassificationSVM} {@var{obj} = } loadmodel (@var{filename}) 20 | ## 21 | ## Load a Classification or Regression model from a file. 22 | ## 23 | ## @code{@var{obj} = loadmodel (@var{filename})} loads a Classification or 24 | ## Regression object, @var{obj}, from a file defined in @var{filename}. 25 | ## 26 | ## @seealso{savemodel, ClassificationDiscriminant, ClassificationGAM, 27 | ## ClassificationKNN, ClassificationNeuralNetwork, 28 | ## ClassificationPartitionedModel, ClassificationSVM, RegressionGAM} 29 | ## @end deftypefn 30 | 31 | function obj = loadmodel (filename) 32 | 33 | ## Check input parameters 34 | if (nargin < 1) 35 | error ("loadmodel: too few arguments."); 36 | endif 37 | 38 | ## Supported Classification and Regression objects 39 | supported = {"ClassificationKNN"}; 40 | 41 | ## Read file into a structure 42 | data = load (filename); 43 | 44 | ## Check that 'classdef_name' variable exists and that it 45 | ## contains a valid Classification or Regression object 46 | if (! isfield (data, "classdef_name")) 47 | msg = " '%s' does not contain a Classification or Regression object."; 48 | error (strcat ("loadmodel:", msg), filename); 49 | endif 50 | 51 | ## Remove 'classdef_name' field from data structure 52 | classdef_name = data.classdef_name; 53 | data = rmfield (data, "classdef_name"); 54 | 55 | ## Parse data structure to the static load method of specified classdef 56 | switch (classdef_name) 57 | 58 | case "ClassificationDiscriminant" 59 | obj = ClassificationDiscriminant.load_model (filename, data); 60 | 61 | case "CompactClassificationDiscriminant" 62 | obj = CompactClassificationDiscriminant.load_model (filename, data); 63 | 64 | case "ClassificationGAM" 65 | obj = ClassificationGAM.load_model (filename, data); 66 | 67 | case "CompactClassificationGAM" 68 | obj = CompactClassificationGAM.load_model (filename, data); 69 | 70 | case "ClassificationKNN" 71 | obj = ClassificationKNN.load_model (filename, data); 72 | 73 | case "ClassificationNeuralNetwork" 74 | obj = ClassificationNeuralNetwork.load_model (filename, data); 75 | 76 | case "CompactClassificationNeuralNetwork" 77 | obj = CompactClassificationNeuralNetwork.load_model (filename, data); 78 | 79 | case "ClassificationSVM" 80 | obj = ClassificationSVM.load_model (filename, data); 81 | 82 | case "CompactClassificationSVM" 83 | obj = ClassificationSVM.load_model (filename, data); 84 | 85 | case "RegressionGAM" 86 | obj = RegressionGAM.load_model (filename, data); 87 | 88 | otherwise 89 | error ("loadmodel: '%s' is not supported.", classdef_name); 90 | 91 | endswitch 92 | 93 | endfunction 94 | 95 | ## Test input validation 96 | %!error loadmodel () 97 | %!error ... 98 | %! loadmodel ("fisheriris.mat") 99 | %!error ... 100 | %! loadmodel ("fail_loadmodel.mdl") 101 | %!error ... 102 | %! loadmodel ("fail_load_model.mdl") 103 | 104 | -------------------------------------------------------------------------------- /inst/logit.m: -------------------------------------------------------------------------------- 1 | ## Copyright (C) 1995-2017 Kurt Hornik 2 | ## 3 | ## This file is part of the statistics package for GNU Octave. 4 | ## 5 | ## This program is free software: you can redistribute it and/or 6 | ## modify it under the terms of the GNU General Public License as 7 | ## published by the Free Software Foundation, either version 3 of the 8 | ## License, or (at your option) any later version. 9 | ## 10 | ## This program is distributed in the hope that it will be useful, but 11 | ## WITHOUT ANY WARRANTY; without even the implied warranty of 12 | ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | ## General Public License for more details. 14 | ## 15 | ## You should have received a copy of the GNU General Public License 16 | ## along with this program; see the file COPYING. If not, see 17 | ## . 18 | 19 | ## -*- texinfo -*- 20 | ## @deftypefn {statistics} {@var{x} =} logit (@var{p}) 21 | ## 22 | ## Compute the logit for each value of @var{p} 23 | ## 24 | ## The logit is defined as 25 | ## @tex 26 | ## $$ {\rm logit}(p) = \log\Big({p \over 1-p}\Big) $$ 27 | ## @end tex 28 | ## @ifnottex 29 | ## 30 | ## @example 31 | ## logit (@var{p}) = log (@var{p} / (1-@var{p})) 32 | ## @end example 33 | ## 34 | ## @end ifnottex 35 | ## @seealso{probit, logicdf} 36 | ## @end deftypefn 37 | 38 | function x = logit (p) 39 | 40 | if (nargin != 1) 41 | print_usage (); 42 | endif 43 | 44 | x = logiinv (p, 0, 1); 45 | 46 | endfunction 47 | 48 | 49 | %!test 50 | %! p = [0.01:0.01:0.99]; 51 | %! assert (logit (p), log (p ./ (1-p)), 25*eps); 52 | 53 | %!assert (logit ([-1, 0, 0.5, 1, 2]), [NaN, -Inf, 0, +Inf, NaN]) 54 | 55 | ## Test input validation 56 | %!error logit () 57 | %!error logit (1, 2) 58 | -------------------------------------------------------------------------------- /inst/mahal.m: -------------------------------------------------------------------------------- 1 | ## Copyright (C) 2015 Lachlan Andrew 2 | ## 3 | ## This file is part of the statistics package for GNU Octave. 4 | ## 5 | ## This program is free software; you can redistribute it and/or modify it 6 | ## under the terms of the GNU General Public License as published by 7 | ## the Free Software Foundation; either version 3 of the License, or (at 8 | ## your option) any later version. 9 | ## 10 | ## This program, is distributed in the hope that it will be useful, but 11 | ## WITHOUT ANY WARRANTY; without even the implied warranty of 12 | ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | ## General Public License for more details. 14 | ## 15 | ## You should have received a copy of the GNU General Public License 16 | ## along with this program; see the file COPYING. If not, see 17 | ## . 18 | 19 | ## -*- texinfo -*- 20 | ## @deftypefn {statistics} {@var{d} =} mahal (@var{y}, @var{x}) 21 | ## 22 | ## Mahalanobis' D-square distance. 23 | ## 24 | ## Return the Mahalanobis' D-square distance of the points in 25 | ## @var{y} from the distribution implied by points @var{x}. 26 | ## 27 | ## Specifically, it uses a Cholesky decomposition to set 28 | ## 29 | ## @example 30 | ## answer(i) = (@var{y}(i,:) - mean (@var{x})) * inv (A) * (@var{y}(i,:)-mean (@var{x}))' 31 | ## @end example 32 | ## 33 | ## where A is the covariance of @var{x}. 34 | ## 35 | ## The data @var{x} and @var{y} must have the same number of components 36 | ## (columns), but may have a different number of observations (rows). 37 | ## 38 | ## @end deftypefn 39 | 40 | function retval = mahal (y, x) 41 | 42 | if (nargin != 2) 43 | print_usage (); 44 | endif 45 | 46 | if (! (isnumeric (x) || islogical (x)) || ! (isnumeric (y) || islogical (y))) 47 | error ("mahal: X and Y must be numeric matrices or vectors"); 48 | endif 49 | 50 | if (! ismatrix (x) || ! ismatrix (y)) 51 | error ("mahal: X and Y must be 2-D matrices or vectors"); 52 | endif 53 | 54 | [xr, xc] = size (x); 55 | [yr, yc] = size (y); 56 | 57 | if (xc != yc) 58 | error ("mahal: X and Y must have the same number of columns"); 59 | endif 60 | 61 | if (isinteger (x)) 62 | x = double (x); 63 | endif 64 | 65 | xm = mean (x, 1); 66 | 67 | ## Center data by subtracting mean of x 68 | x = bsxfun (@minus, x, xm); 69 | y = bsxfun (@minus, y, xm); 70 | 71 | w = (x' * x) / (xr - 1); 72 | 73 | retval = sumsq (y / chol (w), 2); 74 | 75 | endfunction 76 | 77 | 78 | ## Test input validation 79 | %!error mahal () 80 | %!error mahal (1, 2, 3) 81 | %!error mahal ("A", "B") 82 | %!error mahal ([1, 2], ["A", "B"]) 83 | %!error mahal (ones (2, 2, 2)) 84 | %!error mahal (ones (2, 2), ones (2, 2, 2)) 85 | %!error mahal (ones (2, 2), ones (2, 3)) 86 | 87 | %!test 88 | %! X = [1 0; 0 1; 1 1; 0 0]; 89 | %! assert (mahal (X, X), [1.5; 1.5; 1.5; 1.5], 10*eps) 90 | %! assert (mahal (X, X+1), [7.5; 7.5; 1.5; 13.5], 10*eps) 91 | 92 | %!assert (mahal ([true; true], [false; true]), [0.5; 0.5], eps) 93 | -------------------------------------------------------------------------------- /inst/manovacluster.m: -------------------------------------------------------------------------------- 1 | ## Copyright (C) 2022 Andreas Bertsatos 2 | ## 3 | ## This file is part of the statistics package for GNU Octave. 4 | ## 5 | ## This program is free software; you can redistribute it and/or modify it under 6 | ## the terms of the GNU General Public License as published by the Free Software 7 | ## Foundation; either version 3 of the License, or (at your option) any later 8 | ## version. 9 | ## 10 | ## This program is distributed in the hope that it will be useful, but WITHOUT 11 | ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 12 | ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 13 | ## details. 14 | ## 15 | ## You should have received a copy of the GNU General Public License along with 16 | ## this program; if not, see . 17 | 18 | ## -*- texinfo -*- 19 | ## @deftypefn {statistics} {} manovacluster (@var{stats}) 20 | ## @deftypefnx {statistics} {} manovacluster (@var{stats}, @var{method}) 21 | ## @deftypefnx {statistics} {@var{h} =} manovacluster (@var{stats}) 22 | ## @deftypefnx {statistics} {@var{h} =} manovacluster (@var{stats}, @var{method}) 23 | ## 24 | ## Cluster group means using manova1 output. 25 | ## 26 | ## @code{manovacluster (@var{stats})} draws a dendrogram showing the clustering 27 | ## of group means, calculated using the output STATS structure from 28 | ## @code{manova1} and applying the single linkage algorithm. See the 29 | ## @code{dendrogram} function for more information about the figure. 30 | ## 31 | ## @code{manovacluster (@var{stats}, @var{method})} uses the @var{method} 32 | ## algorithm in place of single linkage. The available methods are: 33 | ## 34 | ## @multitable @columnfractions 0.05 0.2 0.75 35 | ## @item @tab "single" @tab --- nearest distance 36 | ## @item @tab "complete" @tab --- furthest distance 37 | ## @item @tab "average" @tab --- average distance 38 | ## @item @tab "centroid" @tab --- center of mass distance 39 | ## @item @tab "ward" @tab --- inner squared distance 40 | ## @end multitable 41 | ## 42 | ## @code{@var{h} = manovacluster (@dots{})} returns a vector of line handles. 43 | ## 44 | ## @seealso{manova1} 45 | ## @end deftypefn 46 | 47 | function h = manovacluster (stats, method) 48 | 49 | ## Check for valid input arguments 50 | narginchk (1, 2); 51 | if nargin > 1 52 | valid_methods = {"single", "complete", "average", "centroid", "ward"}; 53 | if ! any (strcmpi (method, valid_methods)) 54 | error ("manovacluster: invalid method."); 55 | endif 56 | else 57 | method = "single"; 58 | end 59 | ## Get stats fields and create dendrogram 60 | dist = stats.gmdist; 61 | group_names = stats.gnames; 62 | [a, b] = meshgrid (1:length (dist)); 63 | hh = dendrogram (linkage (dist(a < b)', method), 0); 64 | ## Fix tick labels on x-axis 65 | oldlab = get (gca, "XTickLabel"); 66 | maxlen = max (cellfun ("length", group_names)); 67 | newlab = repmat(" ", size (oldlab, 1), maxlen); 68 | ng = size (group_names, 1); 69 | for j = 1:size (oldlab, 1) 70 | k = str2num (oldlab(j,:)); 71 | if (! isempty (k) & k > 0 & k <= ng) 72 | x = group_names{k,:}; 73 | newlab(j,1:length(x)) = x; 74 | endif 75 | endfor 76 | set(gca, "XtickLabel", newlab); 77 | ## Return plot handles if requested 78 | if nargout > 0 79 | h = hh; 80 | endif 81 | endfunction 82 | 83 | %!demo 84 | %! load carbig 85 | %! X = [MPG Acceleration Weight Displacement]; 86 | %! [d, p, stats] = manova1 (X, Origin); 87 | %! manovacluster (stats) 88 | 89 | ## Test plotting 90 | %!test 91 | %! hf = figure ("visible", "off"); 92 | %! unwind_protect 93 | %! load carbig 94 | %! X = [MPG Acceleration Weight Displacement]; 95 | %! [d, p, stats] = manova1 (X, Origin); 96 | %! manovacluster (stats); 97 | %! unwind_protect_cleanup 98 | %! close (hf); 99 | %! end_unwind_protect 100 | 101 | ## Test input validation 102 | %!error manovacluster (stats, "some"); 103 | -------------------------------------------------------------------------------- /inst/nansum.m: -------------------------------------------------------------------------------- 1 | ## Copyright (C) 2001 Paul Kienzle 2 | ## 3 | ## This file is part of the statistics package for GNU Octave. 4 | ## 5 | ## This program is free software; you can redistribute it and/or modify it under 6 | ## the terms of the GNU General Public License as published by the Free Software 7 | ## Foundation; either version 3 of the License, or (at your option) any later 8 | ## version. 9 | ## 10 | ## This program is distributed in the hope that it will be useful, but WITHOUT 11 | ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 12 | ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 13 | ## details. 14 | ## 15 | ## You should have received a copy of the GNU General Public License along with 16 | ## this program; if not, see . 17 | 18 | ## -*- texinfo -*- 19 | ## @deftypefn {statistics} {s =} nansum (@var{x}) 20 | ## @deftypefnx {statistics} {s =} nansum (@var{x}, @var{dim}) 21 | ## @deftypefnx {statistics} {s =} nansum (@dots{}, @qcode{"native"}) 22 | ## @deftypefnx {statistics} {s =} nansum (@dots{}, @qcode{"double"}) 23 | ## @deftypefnx {statistics} {s =} nansum (@dots{}, @qcode{"extra"}) 24 | ## Compute the sum while ignoring NaN values. 25 | ## 26 | ## @code{nansum} is identical to the @code{sum} function except that NaN 27 | ## values are treated as 0 and so ignored. If all values are NaN, the sum is 28 | ## returned as 0. 29 | ## 30 | ## See help text of @code{sum} for details on the options. 31 | ## 32 | ## @seealso{sum, nanmin, nanmax} 33 | ## @end deftypefn 34 | 35 | function v = nansum (X, varargin) 36 | if (nargin < 1) 37 | print_usage (); 38 | else 39 | X(isnan (X)) = 0; 40 | v = sum (X, varargin{:}); 41 | endif 42 | endfunction 43 | 44 | %!assert (nansum ([2 4 NaN 7]), 13) 45 | %!assert (nansum ([2 4 NaN Inf]), Inf) 46 | 47 | %!assert (nansum ([1 NaN 3; NaN 5 6; 7 8 NaN]), [8 13 9]) 48 | %!assert (nansum ([1 NaN 3; NaN 5 6; 7 8 NaN], 2), [4; 11; 15]) 49 | %!assert (nansum (single ([1 NaN 3; NaN 5 6; 7 8 NaN])), single ([8 13 9])) 50 | %!assert (nansum (single ([1 NaN 3; NaN 5 6; 7 8 NaN]), "double"), [8 13 9]) 51 | 52 | %!assert (nansum (uint8 ([2 4 1 7])), 14) 53 | %!assert (nansum (uint8 ([2 4 1 7]), "native"), uint8 (14)) 54 | %!assert (nansum (uint8 ([2 4 1 7])), 14) 55 | -------------------------------------------------------------------------------- /inst/ppplot.m: -------------------------------------------------------------------------------- 1 | ## Copyright (C) 1995-2017 Kurt Hornik 2 | ## Copyright (C) 2022-2023 Andreas Bertsatos 3 | ## 4 | ## This file is part of the statistics package for GNU Octave. 5 | ## 6 | ## This program is free software: you can redistribute it and/or 7 | ## modify it under the terms of the GNU General Public License as 8 | ## published by the Free Software Foundation, either version 3 of the 9 | ## License, or (at your option) any later version. 10 | ## 11 | ## This program is distributed in the hope that it will be useful, but 12 | ## WITHOUT ANY WARRANTY; without even the implied warranty of 13 | ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | ## General Public License for more details. 15 | ## 16 | ## You should have received a copy of the GNU General Public License 17 | ## along with this program; see the file COPYING. If not, see 18 | ## . 19 | 20 | ## -*- texinfo -*- 21 | ## @deftypefn {statistics} {} ppplot (@var{x}, @var{dist}) 22 | ## @deftypefnx {statistics} {} ppplot (@var{x}, @var{dist}, @var{params}) 23 | ## @deftypefnx {statistics} {[@var{p}, @var{y}] =} ppplot (@var{x}, @var{dist}, @var{params}) 24 | ## 25 | ## Perform a PP-plot (probability plot). 26 | ## 27 | ## If F is the CDF of the distribution @var{dist} with parameters 28 | ## @var{params} and @var{x} a sample vector of length @var{n}, the PP-plot 29 | ## graphs ordinate @var{y}(@var{i}) = F (@var{i}-th largest element of 30 | ## @var{x}) versus abscissa @var{p}(@var{i}) = (@var{i} - 0.5)/@var{n}. If 31 | ## the sample comes from F, the pairs will approximately follow a straight 32 | ## line. 33 | ## 34 | ## The default for @var{dist} is the standard normal distribution. 35 | ## 36 | ## The optional argument @var{params} contains a list of parameters of 37 | ## @var{dist}. 38 | ## 39 | ## For example, for a probability plot of the uniform distribution on [2,4] 40 | ## and @var{x}, use 41 | ## 42 | ## @example 43 | ## ppplot (x, "unif", 2, 4) 44 | ## @end example 45 | ## 46 | ## @noindent 47 | ## @var{dist} can be any string for which a function @var{distcdf} that 48 | ## calculates the CDF of distribution @var{dist} exists. 49 | ## 50 | ## If no output is requested then the data are plotted immediately. 51 | ## @seealso{qqplot} 52 | ## @end deftypefn 53 | 54 | function [p, y] = ppplot (x, dist, varargin) 55 | 56 | if (nargin < 1) 57 | print_usage (); 58 | endif 59 | 60 | if (! isnumeric (x) || ! isreal (x) || ! isvector (x) || isscalar (x)) 61 | error ("ppplot: X must be a numeric vector of real numbers"); 62 | endif 63 | 64 | s = sort (x); 65 | n = length (x); 66 | p = ((1 : n)' - 0.5) / n; 67 | if (nargin == 1) 68 | F = @stdnormal_cdf; 69 | elseif (! ischar (dist)) 70 | error ("ppplot: DIST must be a string"); 71 | else 72 | F = str2func ([dist "cdf"]); 73 | endif 74 | 75 | if (nargin <= 2) 76 | y = feval (F, s); 77 | else 78 | y = feval (F, s, varargin{:}); 79 | endif 80 | 81 | if (nargout == 0) 82 | plot (p, y); 83 | axis ([0, 1, 0, 1]); 84 | endif 85 | 86 | endfunction 87 | 88 | function p = stdnormal_cdf (x) 89 | p = 0.5 * erfc (x ./ sqrt (2)); 90 | endfunction 91 | 92 | 93 | ## Test plotting 94 | %!test 95 | %! hf = figure ("visible", "off"); 96 | %! unwind_protect 97 | %! ppplot ([2 3 3 4 4 5 6 5 6 7 8 9 8 7 8 9 0 8 7 6 5 4 6 13 8 15 9 9]); 98 | %! unwind_protect_cleanup 99 | %! close (hf); 100 | %! end_unwind_protect 101 | 102 | ## Test input validation 103 | %!error ppplot () 104 | %!error ppplot (ones (2,2)) 105 | %!error ppplot (1, 2) 106 | %!error ppplot ([1 2 3 4], 2) 107 | -------------------------------------------------------------------------------- /inst/probit.m: -------------------------------------------------------------------------------- 1 | ## Copyright (C) 1995-2017 Kurt Hornik 2 | ## Copyright (C) 2023 Andreas Bertsatos 3 | ## 4 | ## This file is part of the statistics package for GNU Octave. 5 | ## 6 | ## This program is free software: you can redistribute it and/or 7 | ## modify it under the terms of the GNU General Public License as 8 | ## published by the Free Software Foundation, either version 3 of the 9 | ## License, or (at your option) any later version. 10 | ## 11 | ## This program is distributed in the hope that it will be useful, but 12 | ## WITHOUT ANY WARRANTY; without even the implied warranty of 13 | ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | ## General Public License for more details. 15 | ## 16 | ## You should have received a copy of the GNU General Public License 17 | ## along with this program; see the file COPYING. If not, see 18 | ## . 19 | 20 | ## -*- texinfo -*- 21 | ## @deftypefn {statistics} {@var{x} =} probit (@var{p}) 22 | ## 23 | ## Probit transformation 24 | ## 25 | ## Return the probit (the quantile of the standard normal distribution) for 26 | ## each element of @var{p}. 27 | ## 28 | ## @seealso{logit} 29 | ## @end deftypefn 30 | 31 | function x = probit (p) 32 | 33 | if (nargin != 1) 34 | print_usage (); 35 | endif 36 | 37 | x = -sqrt (2) * erfcinv (2 * p); 38 | 39 | endfunction 40 | 41 | ## Test output 42 | %!assert (probit ([-1, 0, 0.5, 1, 2]), [NaN, -Inf, 0, Inf, NaN]) 43 | %!assert (probit ([0.2, 0.99]), norminv ([0.2, 0.99])) 44 | 45 | ## Test input validation 46 | %!error probit () 47 | %!error probit (1, 2) 48 | -------------------------------------------------------------------------------- /inst/qrandn.m: -------------------------------------------------------------------------------- 1 | ## Copyright (C) 2014 - Juan Pablo Carbajal 2 | ## 3 | ## This file is part of the statistics package for GNU Octave. 4 | ## 5 | ## This program is free software; you can redistribute it and/or modify 6 | ## it under the terms of the GNU General Public License as published by 7 | ## the Free Software Foundation; either version 3 of the License, or 8 | ## (at your option) any later version. 9 | ## 10 | ## This program is distributed in the hope that it will be useful, 11 | ## but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | ## GNU General Public License for more details. 14 | ## 15 | ## You should have received a copy of the GNU General Public License 16 | ## along with this program. If not, see . 17 | 18 | ## -*- texinfo -*- 19 | ## @deftypefn {statistics} {@var{z} =} qrandn (@var{q}, @var{r}, @var{c}) 20 | ## @deftypefnx {statistics} {@var{z} =} qrandn (@var{q}, [@var{r}, @var{c}]) 21 | ## 22 | ## Returns random deviates drawn from a q-Gaussian distribution. 23 | ## 24 | ## Parameter @var{q} charcterizes the q-Gaussian distribution. 25 | ## The result has the size indicated by @var{s}. 26 | ## 27 | ## Reference: 28 | ## W. Thistleton, J. A. Marsh, K. Nelson, C. Tsallis (2006) 29 | ## "Generalized Box-Muller method for generating q-Gaussian random deviates" 30 | ## arXiv:cond-mat/0605570 http://arxiv.org/abs/cond-mat/0605570 31 | ## 32 | ## @seealso{rand, randn} 33 | ## @end deftypefn 34 | 35 | function z = qrandn (q, R, C=[]) 36 | 37 | if (nargin < 2) 38 | print_usage; 39 | endif 40 | if (! isscalar (q)) 41 | error ("qrandn: the parameter q must be a scalar.")' 42 | endif 43 | 44 | ## Check that q < 3 45 | if (q >= 3) 46 | error ("qrandn: the parameter q must be lower than 3."); 47 | endif 48 | 49 | if (numel (R) > 1) 50 | S = R; 51 | elseif (numel (R) == 1 && isempty (C)) 52 | S = [R, 1]; 53 | elseif (numel (R) == 1 && ! isempty (C)) 54 | S = [R, C]; 55 | endif 56 | 57 | ## Calaulate the q to be used on the q-log 58 | qGen = (1 + q) / (3 - q); 59 | 60 | ## Initialize the output vector 61 | z = sqrt (-2 * log_q (rand (S), qGen)) .* sin (2 * pi * rand (S)); 62 | 63 | endfunction 64 | 65 | ## Returns the q-log of x, using q 66 | function a = log_q (x, q) 67 | 68 | dq = 1 - q; 69 | ## Check to see if q = 1 (to double precision) 70 | if (abs (dq) < 10 * eps) 71 | ## If q is 1, use the usual natural logarithm 72 | a = log (x); 73 | else 74 | ## If q differs from 1, use the definition of the q-log 75 | a = (x .^ dq - 1) ./ dq; 76 | endif 77 | 78 | endfunction 79 | 80 | %!demo 81 | %! z = qrandn (-5, 5e6); 82 | %! [c x] = hist (z,linspace(-1.5,1.5,200),1); 83 | %! figure(1) 84 | %! plot(x,c,"r."); axis tight; axis([-1.5,1.5]); 85 | %! 86 | %! z = qrandn (-0.14286, 5e6); 87 | %! [c x] = hist (z,linspace(-2,2,200),1); 88 | %! figure(2) 89 | %! plot(x,c,"r."); axis tight; axis([-2,2]); 90 | %! 91 | %! z = qrandn (2.75, 5e6); 92 | %! [c x] = hist (z,linspace(-1e3,1e3,1e3),1); 93 | %! figure(3) 94 | %! semilogy(x,c,"r."); axis tight; axis([-100,100]); 95 | %! 96 | %! # --------- 97 | %! # Figures from the reference paper. 98 | 99 | 100 | ## Tests for input validation 101 | %!error qrandn ([1 2], 1) 102 | %!error qrandn (4, 1) 103 | %!error qrandn (3, 1) 104 | %!error qrandn (2.5, 1, 2, 3) 105 | %!error qrandn (2.5) 106 | 107 | ## Tests for output validation 108 | %!test 109 | %! q = 1.5; 110 | %! s = [2, 3]; 111 | %! z = qrandn (q, s); 112 | %! assert (isnumeric (z) && isequal (size (z), s)); 113 | -------------------------------------------------------------------------------- /io.github.gnu_octave.statistics.metainfo.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 10 | 11 | io.github.gnu_octave.statistics 12 | org.octave.Octave 13 | FSFAP 14 | GPL-3.0-or-later 15 | Statistics 16 | The Statistics package for GNU Octave 17 | 18 | 19 | Octave 20 | Statistics 21 | 22 | 23 | 24 |

25 | The Statistics package for GNU Octave is a collection of functions 26 | for statistical analysis. 27 |

28 |
29 | 30 | https://github.com/gnu-octave/statistics/issues 31 | https://gnu-octave.github.io/statistics 32 | Octave Community 33 | octave-maintainers@gnu.org 34 |
35 | -------------------------------------------------------------------------------- /src/Makefile: -------------------------------------------------------------------------------- 1 | # Makefile for compiling required oct files (parallel version) 2 | 3 | # List all target oct files 4 | OCTFILES = editDistance.oct libsvmread.oct libsvmwrite.oct svmpredict.oct \ 5 | svmtrain.oct fcnntrain.oct fcnnpredict.oct 6 | 7 | # Default target - depends on all oct files (enables parallel build) 8 | all: $(OCTFILES) 9 | 10 | # Individual targets for each oct file with dependencies 11 | editDistance.oct: editDistance.cc 12 | $(MKOCTFILE) editDistance.cc 13 | 14 | libsvmread.oct: libsvmread.cc 15 | $(MKOCTFILE) libsvmread.cc 16 | 17 | libsvmwrite.oct: libsvmwrite.cc 18 | $(MKOCTFILE) libsvmwrite.cc 19 | 20 | svmpredict.oct: svmpredict.cc svm.cpp svm_model_octave.cc 21 | $(MKOCTFILE) svmpredict.cc svm.cpp svm_model_octave.cc 22 | 23 | svmtrain.oct: svmtrain.cc svm.cpp svm_model_octave.cc 24 | $(MKOCTFILE) svmtrain.cc svm.cpp svm_model_octave.cc 25 | 26 | fcnntrain.oct: fcnntrain.cc 27 | $(MKOCTFILE) fcnntrain.cc 28 | 29 | fcnnpredict.oct: fcnnpredict.cc 30 | $(MKOCTFILE) fcnnpredict.cc 31 | 32 | # Clean target 33 | clean: 34 | rm -f $(OCTFILES) 35 | 36 | # Declare targets that don't create files 37 | .PHONY: all clean 38 | 39 | -------------------------------------------------------------------------------- /src/svm_model_octave.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) 2022 Andreas Bertsatos 3 | Based on the Octave LIBSVM wrapper created by Alan Meeson (2014) based on an 4 | earlier version of the LIBSVM (3.18) library for MATLAB. Current implementation 5 | is based on LIBSVM 3.25 (2021) by Chih-Chung Chang and Chih-Jen Lin. 6 | 7 | This file is part of the statistics package for GNU Octave. 8 | 9 | This program is free software; you can redistribute it and/or modify it under 10 | the terms of the GNU General Public License as published by the Free Software 11 | Foundation; either version 3 of the License, or (at your option) any later 12 | version. 13 | 14 | This program is distributed in the hope that it will be useful, but WITHOUT 15 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 16 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 17 | details. 18 | 19 | You should have received a copy of the GNU General Public License along with 20 | this program; if not, see . 21 | */ 22 | 23 | #include 24 | #include 25 | #include "svm.h" 26 | 27 | const char *model_to_octave_structure(octave_value_list &plhs, int num_of_feature, struct svm_model *model); 28 | struct svm_model *octave_matrix_to_model(octave_scalar_map &octave_struct, const char **error_message); 29 | --------------------------------------------------------------------------------