This is a static copy of a profile report

Home

Function details for binopdfThis is a static copy of a profile report

Home

binopdf (Calls: 4, Time: 0.038 s)
Generated 03-Jul-2019 20:30:06 using performance time.
function in file C:\Program Files\MATLAB\R2019a\toolbox\stats\stats\binopdf.m
Copy to new window for comparing multiple runs

Parents (calling functions)

Function NameFunction TypeCalls
fft_disc_rand_var_statsfunction4
Lines where the most time was spent

Line NumberCodeCallsTotal Time% TimeTime Plot
98
-binodeviance(x(K),n(K).*p(K))...
40.012 s31.6%
97
lc = stirlerr(n(K))-stirlerr(x...
40.012 s30.8%
24
[errorcode, x, n, p] = distchc...
40.006 s15.2%
39
y = zeros(size(x),'like',inter...
40.003 s7.6%
99
-binodeviance(n(K)-x(K),n(K).*...
40.002 s4.4%
All other lines  0.004 s10.3%
Totals  0.038 s100% 
Children (called functions)

Function NameFunction TypeCallsTotal Time% TimeTime Plot
stats\private\binodeviancefunction80.010 s26.8%
stats\private\stirlerrfunction120.007 s19.6%
distchckfunction40.003 s8.8%
dominantTypefunction40.001 s2.8%
Self time (built-ins, overhead, etc.)  0.016 s42.1%
Totals  0.038 s100% 
Code Analyzer results
Line numberMessage
20Extra comma is unnecessary in IF statement before newline.
Coverage results
Show coverage for parent directory
Total lines in function107
Non-code lines (comments, blank lines)42
Code lines (lines that can run)65
Code lines that did run42
Code lines that did not run23
Coverage (did run/can run)64.62 %
Function listing
time 
Calls 
 line
   1 
function y = binopdf(x,n,p)
   2 
% BINOPDF Binomial probability density function.
   3 
%   Y = BINOPDF(X,N,P) returns the binomial probability density 
   4 
%   function with parameters N and P at the values in X.
   5 
%   Note that the density function is zero unless X is an integer.
   6 
%
   7 
%   The size of Y is the common size of the input arguments. A scalar input  
   8 
%   functions as a constant matrix of the same size as the other inputs.    
   9 
%
  10 
%   See also BINOCDF, BINOFIT, BINOINV, BINORND, BINOSTAT, PDF.
  11 

  12 
%   References:
  13 
%      [1]  M. Abramowitz and I. A. Stegun, "Handbook of Mathematical
  14 
%      Functions", Government Printing Office, 1964, 26.1.20.
  15 
%      [2]  C. Loader, "Fast and Accurate Calculations
  16 
%      of Binomial Probabilities", July 9, 2000.
  17 

  18 
%   Copyright 1993-2015 The MathWorks, Inc.
  19 

< 0.001 
      4 
  20
if nargin < 3,  
  21 
    error(message('stats:binopdf:TooFewInputs'));
  22 
end
  23 

  0.006 
      4 
  24
[errorcode, x, n, p] = distchck(3,x,n,p); 
  25 

< 0.001 
      4 
  26
if errorcode > 0 
  27 
    error(message('stats:binopdf:InputSizeMismatch'));
  28 
end
  29 

  30 
% Binomial distribution is defined on positive integers less than N.
< 0.001 
      4 
  31
if ~isfloat(x) 
  32 
   x = double(x);
  33 
end
< 0.001 
      4 
  34
if ~isfloat(n) 
  35 
   n = double(n);
  36 
end
  37 

  38 
% Initialize Y to zero.
  0.003 
      4 
  39
y = zeros(size(x),'like',internal.stats.dominantType(x,n,p)); % Single if x, n or p is. 
< 0.001 
      4 
  40
y(isnan(x) | isnan(n) | isnan(p)) = NaN; 
  41 

< 0.001 
      4 
  42
k = find(x >= 0  &  x==round(x)  &  x <= n & n==round(n) & p>=0 & p<=1); 
< 0.001 
      4 
  43
if any(k) 
  44 
   % First deal with borderline cases
< 0.001 
      4 
  45
   t = (p(k)==0); 
< 0.001 
      4 
  46
   if any(t) 
  47 
      kt = k(t);
  48 
      y(kt) = (x(kt)==0);
  49 
      k(t) = [];
  50 
   end
< 0.001 
      4 
  51
   t = (p(k)==1); 
< 0.001 
      4 
  52
   if any(t) 
  53 
      kt = k(t);
  54 
      y(kt) = (x(kt)==n(kt));
  55 
      k(t) = [];
  56 
   end
< 0.001 
      4 
  57
end 
  58 

  59 
% More borderline cases: x=0 and x=n
< 0.001 
      4 
  60
if any(k) 
< 0.001 
      4 
  61
    t = (x(k)==0); 
< 0.001 
      4 
  62
    if any(t) 
< 0.001 
      4 
  63
        kt = k(t); 
< 0.001 
      4 
  64
        y(kt) = exp(n(kt).*log(1-p(kt))); 
< 0.001 
      4 
  65
        k(t) = []; 
< 0.001 
      4 
  66
    end 
< 0.001 
      4 
  67
    t = (x(k)==n(k)); 
< 0.001 
      4 
  68
    if any(t) 
< 0.001 
      4 
  69
        kt = k(t); 
< 0.001 
      4 
  70
        y(kt) = exp(n(kt).*log(p(kt))); 
< 0.001 
      4 
  71
        k(t) = []; 
< 0.001 
      4 
  72
    end 
< 0.001 
      4 
  73
end 
< 0.001 
      4 
  74
if any(k) 
< 0.001 
      4 
  75
    t = (n(k)<10); 
< 0.001 
      4 
  76
    if any(t) 
  77 
        % Faster method that is not accurate for large n
  78 
        K = k(t);
  79 
        nk = gammaln(n(K) + 1) - gammaln(x(K) + 1) - gammaln(n(K) - x(K) + 1);
  80 
        lny = nk + x(K).*log( p(K)) + (n(K) - x(K)).*log1p(-p(K));
  81 
        y(K) = exp(lny);
  82 
    end
< 0.001 
      4 
  83
    if any(~t) 
  84 
        % Slower method
< 0.001 
      4 
  85
        K = k(~t); 
  86 
        
  87 
        % Notes:
  88 
        % 1- Equation 3 in reference [2] is used to calculate the pdf.
  89 
        % 2- The second term on the RHS of Equation 3 is calculated using
  90 
        % Equation 5.
  91 
        % 3- The deviance, bd0(x,np), is equivalent to npD0(x/np) in the
  92 
        % reference, which is calculated using Equation 5.2.
  93 
        % 4- The function stirlerr(n) is the error term (delta(n)) in the
  94 
        % Stirling-De Moivre series in Equation 4.2 of the reference.
  95 
        
  96 
        % Calculate the pdf
  0.025 
      4 
  97
        lc = stirlerr(n(K))-stirlerr(x(K))-stirlerr(n(K)-x(K)) ... 
      4 
  98
             -binodeviance(x(K),n(K).*p(K)) ... 
      4 
  99
             -binodeviance(n(K)-x(K),n(K).*(1.0-p(K))); 
< 0.001 
      4 
 100
        y(K) = exp(lc).*sqrt(n(K)./(2*pi*x(K).*(n(K)-x(K)))); 
< 0.001 
      4 
 101
    end 
< 0.001 
      4 
 102
end 
< 0.001 
      4 
 103
k1 = find(n < 0 | p < 0 | p > 1 | round(n) ~= n);  
< 0.001 
      4 
 104
if any(k1) 
 105 
   y(k1) = NaN;
 106 
end
< 0.001 
      4 
 107
end 

Other subfunctions in this file are not included in this listing.