Gaussian Probability Distribution

function pdf = normpdf(x,varargin)
% function pdf = normpdf(x)
% function pdf = normpdf(x,mu)
% function pdf = normpdf(x,mu,sigma)
% By default: mu = 0, sigma = 1.
% Calculate Gaussian probability distribution
%
% Binlin Wu, wub1@southernct.edu
% 04/02/2021
%

nn = [varargin{:}];
switch nargin
    case 1
        mu = 0;sigma = 1;
    case 2
        mu = nn(1);sigma = 1;
    case 3
        mu = nn(1);sigma = nn(2);
    otherwise
        error('Up to 3 inputs only!')
end
pdf = 1./(sigma*sqrt(2*pi))*exp(-(x-mu).^2/(2*sigma^2));

Binomial Probability Distribution

function P = binopdf(x,n,p)
% P = binopdf(x,n,p) 
% x: integer x, number of successes
% n: integer n, number of trials
% p: probability of success for each trial, must be between 0 and 1
% P: The probability for each of the values in x using the corresponding 
%    number of trials in n and probability of success for each trial in p.
% 
% Binlin Wu, wub1@southernct.edu, 04/07/2021
%
if sum(mod(x(:),1))~=0 || mod(n,1)~=0
    error('x and n must be integers');
end
if p<0 || p>1
    error('p must be between 0 and 1');
end

P = zeros(size(x));
for i = 1:numel(x)
    if x(i) < 0
        P(i) = 0;
    elseif x(i) == 0
        P(i) = (1-p).^n;
    elseif x(i) == n
        P(i) = p.^x(i);
    elseif x(i) > n
        P(i) = 0;
    else
        P(i) = exp(sum(log(1:n)))./exp(sum(log(1:x(i))))./exp(sum(log(1:n-x(i)))).*p.^x(i).*(1-p).^(n-x(i));
    end
end

Binomial Random Number Generator

function r = binornd(n,p,varargin)
% r = binornd(n,p)
% r = binornd(n,p,sz1)
% r = binornd(n,p,sz1,sz2,...)
% Generates random numbers from the binomial distribution specified by 
% the number of trials n and the probability of success for each trial p.
% sz1,sz2: size of the output matrix
% 
% Binlin Wu, wub1@southernct.edu
% 04/02/2021
%
nn = [varargin{:}];
r = zeros([nn,1]);
x = rand([n nn 1]);
for i = 1:numel(r(:))
    r(i) = nnz(x(:,i) <= p);
end