[Contents] [TitleIndex] [WordIndex

Source code of Hypergeometric macros

Introduction

This is a report by Prateek Papriwal for the GSOC 2012 on the project "Distribution functions". This page gathers proposals for the Hypergeometric distribution.

A word of caution

These function are just temporary work and may contain bugs, which may have been fixed in the distfun project at http://forge.scilab.org/index.php/p/distfun/source/.

distfun_hygepdf.sci

function y = distfun_hygepdf(varargin)
    
    [lhs,rhs] = argn()
    apifun_checkrhs("distfun_hygepdf",rhs,4)
    apifun_checklhs("distfun_hygepdf",lhs,0:1)
    x = varargin(1)
    M = varargin(2)
    N = varargin(3)
    k = varargin(4)
    //
    // Check type
    apifun_checktype("distfun_hygepdf",x,"x",1,"constant")
    apifun_checktype("distfun_hygepdf",M,"M",2,"constant")
    apifun_checktype("distfun_hygepdf",N,"N",3,"constant")
    apifun_checktype("distfun_hygepdf",k,"k",4,"constant")
    
    //
    // Check content
    apifun_checkgreq("distfun_hygepdf",x,"x",1,0)
    apifun_checkgreq("distfun_hygepdf",M,"M",2,0)
    apifun_checkgreq("distfun_hygepdf",N,"N",3,0)
    apifun_checkgreq("distfun_hygepdf",k,"k",4,0)
    
    [x,M,N,k] = apifun_expandvar(x,M,N,k)
    
    if (x == []) then
        y=[]
        return
    end
    
    ok = k < x | N < x | k - x > M - N
    
    
    p = k ./ M
    
    for i = 1:size(x,'c')
        if (ok(i)) then
            y(1,i) = 0
            
        else
            y1 = distfun_binopdf(x(i),N(i),p(i))
            y2 = distfun_binopdf(k(i)-x(i),M(i)-N(i),p(i))
            y3 = distfun_binopdf(k(i),M(i),p(i))
            y(1,i) = y1 .* y2 ./ y3
        end
    end
endfunction 

distfun_hygecdf.sci

function p = distfun_hygecdf(varargin)
    [lhs,rhs] = argn()
    apifun_checkrhs("distfun_hygepdf",rhs,4:5)
    apifun_checklhs("distfun_hygepdf",lhs,0:1)
    x = varargin(1)
    M = varargin(2)
    N = varargin(3)
    k = varargin(4)
    lowertail = apifun_argindefault(varargin,5,%t)
    //
    // Check type
    apifun_checktype("distfun_hygepdf",x,"x",1,"constant")
    apifun_checktype("distfun_hygepdf",M,"M",2,"constant")
    apifun_checktype("distfun_hygepdf",N,"N",3,"constant")
    apifun_checktype("distfun_hygepdf",k,"k",4,"constant")
    apifun_checktype("distfun_hygecdf",lowertail,"lowertail",5,"boolean")
    
    apifun_checkscalar("distfun_hygecdf",lowertail,"lowertail",5)
    
    
    //
    // Check content
    apifun_checkgreq("distfun_hygepdf",x,"x",1,0)
    apifun_checkgreq("distfun_hygepdf",M,"M",2,0)
    apifun_checkgreq("distfun_hygepdf",N,"N",3,0)
    apifun_checkgreq("distfun_hygepdf",k,"k",4,0)
    
    [x,M,N,k] = apifun_expandvar(x,M,N,k)
    
    if (x == []) then
        p=[]
        return
    end
    
    
    temp = zeros(1,size(x,'c'))
    for i = 1:size(x,'c')
        for j = 0:x(i)
            temp(i) = temp(i) + distfun_hygepdf(j,M(i),N(i),k(i)) 
        end
    end
    if (lowertail) then
        p = temp
    else
        path = distfun_getpath()
        internallib = lib(fullfile(path,"macros","internals"))
        
        p = distfun_p2q(temp)
    end
    
endfunction

distfun_hygeinv.sci

function x = distfun_hygeinv(varargin)
    [lhs,rhs] = argn()
    apifun_checkrhs("distfun_hygeinv",rhs,4:5)
    apifun_checklhs("distfun_hygeinv",lhs,0:1)
    
    p = varargin(1)
    M = varargin(2)
    N = varargin(3)
    k = varargin(4)
    lowertail = apifun_argindefault(varargin,5,%t)
    
    //
    // Check type
    apifun_checktype("distfun_hygeinv",p,"p",1,"constant")
    apifun_checktype("distfun_hygeinv",M,"M",2,"constant")
    apifun_checktype("distfun_hygeinv",N,"N",3,"constant")
    apifun_checktype("distfun_hygeinv",k,"k",4,"constant")
    
    apifun_checktype("distfun_hygeinv",lowertail,"lowertail",5,"boolean")
    
    apifun_checkscalar("distfun_hygeinv",lowertail,"lowertail",5)
    
    
    //
    // Check content
    apifun_checkrange("distfun_hygeinv",p,"p",1,0,1)
    apifun_checkgreq("distfun_hygeinv",M,"M",2,0)
    apifun_checkgreq("distfun_hygeinv",N,"N",3,0)
    apifun_checkgreq("distfun_hygepdf",k,"k",4,0)
    
    [p,M,N,k] = apifun_expandvar(p,M,N,k)
    
    if (p == []) then
        x = []
        return
    end

    
    i = zeros(1,size(p,'c'))
    for j = 1:size(p,'c')
        if (lowertail) then
            while(%t)
                if(distfun_hygecdf(i(j),M(j),N(j),k(j)) >= p(j) ) then
                    break
                end
            i(j) = i(j) + 1
            end
            x(1,j) = i(j)
        
        else
            path = distfun_getpath()
            internallib = lib(fullfile(path,"macros","internals"))
            q(j) = distfun_p2q(p(j))
            while(%t)
                if(distfun_hygecdf(i(j),M(j),N(j),k(j)) >= q(j) ) then
                    break
                end
            i(j) = i(j) + 1
            end
            x = i(1,j) + 1
        end
    end
    
endfunction

distfun_hygernd.sci

function R = distfun_hygernd(varargin)
    
    path = distfun_getpath()
    internallib = lib(fullfile(path,"macros","internals"))

    [lhs,rhs] = argn()

    apifun_checkrhs("distfun_hygernd",rhs,3:5)
    apifun_checklhs("distfun_hygernd",lhs,0:1)
    
    M = varargin(1)
    N = varargin(2)
    k = varargin(3)
    //
    // Check type
    apifun_checktype ( "distfun_hygernd" , M , "M" , 1 , "constant" )
    apifun_checktype ( "distfun_hygernd" , N , "N" , 2 , "constant" )
    apifun_checktype ( "distfun_hygernd" , k , "k" , 3 , "constant" )
    
    [M,N,k] = apifun_expandvar(M,N,k)
    
    if (rhs == 3) then
        m = size(M,'r')
        n = size(M,'c')
        b = zeros(m,n)
        for i = 1:m
            for j = 1:n
                count = 0
                t = 0:k(i,j)
                p = distfun_hygepdf(t,M(i,j),N(i,j),k(i,j))
                a = cumsum(p(1:k(i,j))) ./ sum(p)
                for l = 1:k(i,j)
                    if (rand(1,1) >= a(l) ) then
                        count = count + 1
                    end
                end
                b(i,j) = count
                b(i,j) = t(b(i,j)+1)
            end 
        end
        R = b
        return
    
    end
    
    if ( rhs == 4 ) then
        v = varargin(4)
    end
    if ( rhs == 5 ) then
        m = varargin(4)
        n = varargin(5)
    end
    
    distfun_checkvmn ( "distfun_hygernd" , 4 , varargin(4:$) )
    
    
    if (M == []) then
        R = []
        return
    end
    
    
    //
    // Check content
    apifun_checkgreq("distfun_hygernd",M,"M",1,0)
    apifun_checkgreq("distfun_hygernd",N,"N",2,0)
    apifun_checkgreq("distfun_hygernd",k,"k",3,0)
    
    t = 0:k
    p = distfun_hygepdf(t,M,N,k)
    
    a = cumsum(p(1:k)) ./ sum(p)
    b = rand(m,n)
    
    for i = 1:m
        for j = 1:n
            count = 0
            for l = 1:k
                if (b(i,j) >= a(l)) then
                    count = count + 1 
                end
            end
        b(i,j) = count
        end
    end
    
    R = t(b + 1)
    R = matrix(R,m,n)
    
endfunction

distfun_hygestat.sci

function [M,V] = distfun_hygestat(M,N,k)
    
    [lhs,rhs] = argn()
    apifun_checkrhs("distfun_hygestat",rhs,3)
    apifun_checklhs("distfun_hygestat",lhs,1:2)
    
    // Check type
    //
    apifun_checktype("distfun_hygestat",M,"M",1,"constant")
    apifun_checktype("distfun_hygestat",N,"M",2,"constant")
    apifun_checktype("distfun_hygestat",k,"M",3,"constant")
    
    [M,N,k] = apifun_expandvar(M,N,k)
    //Check content
    // 
    apifun_checkgreq("distfun_hygestat",M,"M",1,N)
    apifun_checkrange("distfun_hygestat",N,"N",2,0,M)
    apifun_checkrange("distfun_hygestat",k,"k",3,0,M)
    
    //
    
    
    M = k .* N ./ M
    V = (k .* N .* (M-N) .* (M -k)) ./ (M .* M .* (M - 1))
    
endfunction

2022-09-08 09:26