X-Git-Url: https://git.creatis.insa-lyon.fr/pubgit/?p=CreaPhase.git;a=blobdiff_plain;f=octave_packages%2Fstatistics-1.1.3%2Fhistfit.m;fp=octave_packages%2Fstatistics-1.1.3%2Fhistfit.m;h=ff716682b5f371c5675fbe98d6e70a854e9722fc;hp=0000000000000000000000000000000000000000;hb=c880e8788dfc484bf23ce13fa2787f2c6bca4863;hpb=1705066eceaaea976f010f669ce8e972f3734b05 diff --git a/octave_packages/statistics-1.1.3/histfit.m b/octave_packages/statistics-1.1.3/histfit.m new file mode 100644 index 0000000..ff71668 --- /dev/null +++ b/octave_packages/statistics-1.1.3/histfit.m @@ -0,0 +1,70 @@ +## Copyright (C) 2003 Alberto Terruzzi +## +## This program is free software; you can redistribute it and/or modify it under +## the terms of the GNU General Public License as published by the Free Software +## Foundation; either version 3 of the License, or (at your option) any later +## version. +## +## This program is distributed in the hope that it will be useful, but WITHOUT +## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more +## details. +## +## You should have received a copy of the GNU General Public License along with +## this program; if not, see . + +## -*- texinfo -*- +## @deftypefn {Function File} histfit (@var{data}, @var{nbins}) +## +## Plot histogram with superimposed fitted normal density. +## +## @code{histfit (@var{data}, @var{nbins})} plots a histogram of the values in +## the vector @var{data} using @var{nbins} bars in the histogram. With one input +## argument, @var{nbins} is set to the square root of the number of elements in +## data. +## +## Example +## +## @example +## histfit (randn (100, 1)) +## @end example +## +## @seealso{bar,hist, pareto} +## @end deftypefn + +## Author: Alberto Terruzzi +## Version: 1.0 +## Created: 3 March 2004 + +function histfit (data,nbins) + + if nargin < 1 || nargin > 2 + print_usage; + endif + + if isvector (data) != 1 + error ("data must be a vector."); + endif + + row = sum(~isnan(data)); + + if nargin < 2 + nbins = ceil(sqrt(row)); + endif + + [n,xbin]=hist(data,nbins); + if any(abs(diff(xbin,2)) > 10*max(abs(xbin))*eps) + error("histfit bins must be uniform width"); + endif + + mr = nanmean(data); ## Estimates the parameter, MU, of the normal distribution. + sr = nanstd(data); ## Estimates the parameter, SIGMA, of the normal distribution. + x=(-3*sr+mr:0.1*sr:3*sr+mr)';## Evenly spaced samples of the expected data range. + [xb,yb] = bar(xbin,n); + y = normal_pdf(x,mr,sr.^2); + binwidth = xbin(2)-xbin(1); + y = row*y*binwidth; ## Normalization necessary to overplot the histogram. + plot(xb,yb,";;b",x,y,";;r-"); ## Plots density line over histogram. + +endfunction +