HP Forums
PYTHON program not work - Printable Version

+- HP Forums (https://www.hpmuseum.org/forum)
+-- Forum: HP Calculators (and very old HP Computers) (/forum-3.html)
+--- Forum: HP Prime (/forum-5.html)
+--- Thread: PYTHON program not work (/thread-17432.html)



PYTHON program not work - robmio - 09-05-2021 05:59 AM

Hello, below is a program to calculate the incomplete regularized beta function. The program is written with PYTHON (this program is available on the Web). When I launch the program, I only get a zero on the screen. Where am I doing wrong?
Thanks for your cooperation, Roberto.

Code:
#PYTHON EXPORT nome
from math import *

def contfractbeta(a,b,x, ITMAX = 200):
     
    """ contfractbeta() evaluates the continued fraction form of the incomplete Beta function; incompbeta().  
    (Code translated from: Numerical Recipes in C.)"""
     
    EPS = 3.0e-7
    bm = az = am = 1.0
    qab = a+b
    qap = a+1.0
    qam = a-1.0
    bz = 1.0-qab*x/qap
     
    for i in range(ITMAX+1):
        em = float(i+1)
        tem = em + em
        d = em*(b-em)*x/((qam+tem)*(a+tem))
        ap = az + d*am
        bp = bz+d*bm
        d = -(a+em)*(qab+em)*x/((qap+tem)*(a+tem))
        app = ap+d*az
        bpp = bp+d*bz
        aold = az
        am = ap/bpp
        bm = bp/bpp
        az = app/bpp
        bz = 1.0
        if (abs(az-aold)<(EPS*abs(az))):
            return az
            
def incompbeta(a, b, x):
     
    ''' incompbeta(a,b,x) evaluates incomplete beta function, here a, b > 0 and 0 <= x <= 1. This function requires contfractbeta(a,b,x, ITMAX = 200) 
    (Code translated from: Numerical Recipes in C.)'''
     
    if (x == 0):
        return 0;
    elif (x == 1):
        return 1;
    else:
        lbeta = lgamma(a+b) - lgamma(a) - lgamma(b) + a * log(x) + b * log(1-x)
        if (x < (a+1) / (a+b+2)):
            return exp(lbeta) * contfractbeta(a, b, x) / a;
        else:
            return 1 - exp(lbeta) * contfractbeta(b, a, 1-x) / b;
#end

EXPORT BetaRegInc(a,b,x)
BEGIN
PYTHON(nome,a,b,x)
END;



RE: PYTHON program not work - robmio - 09-05-2021 08:54 AM

I figured out how to run the program: see the code
Code:
#PYTHON EXPORT nome
from math import *
from sys import *
a=argv[0];
b=argv[1];
x=argv[2];
a=float(a);
b=float(b);
x=float(x);

def contfractbeta(a,b,x, ITMAX = 200):
     
    """ contfractbeta() evaluates the continued fraction form of the incomplete Beta function; incompbeta().  
    (Code translated from: Numerical Recipes in C.)"""
     
    EPS = 3.0e-7
    bm = az = am = 1.0
    qab = a+b
    qap = a+1.0
    qam = a-1.0
    bz = 1.0-qab*x/qap
     
    for i in range(ITMAX+1):
        em = float(i+1)
        tem = em + em
        d = em*(b-em)*x/((qam+tem)*(a+tem))
        ap = az + d*am
        bp = bz+d*bm
        d = -(a+em)*(qab+em)*x/((qap+tem)*(a+tem))
        app = ap+d*az
        bpp = bp+d*bz
        aold = az
        am = ap/bpp
        bm = bp/bpp
        az = app/bpp
        bz = 1.0
        if (abs(az-aold)<(EPS*abs(az))):
            return az
            
def incompbeta(a, b, x):
     
    ''' incompbeta(a,b,x) evaluates incomplete beta function, here a, b > 0 and 0 <= x <= 1. This function requires contfractbeta(a,b,x, ITMAX = 200) 
    (Code translated from: Numerical Recipes in C.)'''
     
    if (x == 0):
        return 0;
    elif (x == 1):
        return 1;
    else:
        lbeta = lgamma(a+b) - lgamma(a) - lgamma(b) + a * log(x) + b * log(1-x)
        if (x < (a+1) / (a+b+2)):
            return exp(lbeta) * contfractbeta(a, b, x) / a;
        else:
            return 1 - exp(lbeta) * contfractbeta(b, a, 1-x) / b;
print(incompbeta(a,b,x));
#end

EXPORT BetaRegInc(a,b,x)
BEGIN
PYTHON(nome,a,b,x);
END;



RE: PYTHON program not work - robmio - 09-05-2021 11:06 AM

Hello everyone: I ask a question: how can you use a function, programmed with the PYTHON language, in another program? For example, if I program a function in HPPPL language, it becomes immediately usable in another program, since the result does not go through the "Terminal".
For instance:
Code:
EXPORT Rad_q(b)
BEGIN
RETURN sqrt(b);
END;
Now I use the Rad_q function in another program:
Code:
EXPORT Rad_q_Det(a)
BEGIN
LOCAL uu;
uu:=DET(a);
uu:=Rad_q(uu);
RETURN uu;
END;
In the BetaRegInc(a, b, x) program, shown at the beginning of this post, the final line of "#PYTHON EXPORT nome" contains a "print(incomplete(a, b, x));" to view the result on the terminal; even using “EXPR (TERMINAL (1))”, the result of the function always passes through the “Terminal”, before being displayed on the “main screen”. How to prevent the result of the function from passing through the terminal?
Thanks a lot, Roberto


RE: PYTHON program not work - Albert Chan - 09-05-2021 03:58 PM

(09-05-2021 05:59 AM)robmio Wrote:  Hello, below is a program to calculate the incomplete regularized beta function ...

This answered another thread of yours, regarding log of incomplete beta

(08-23-2021 04:17 AM)robmio Wrote:  There remains the problem of the incomplete Beta function, which cannot be "transformed" into other functions (at least I think).

Code:
def log_beta(a, b, x=1):
    'return Log[Beta[x,a,b]], assumed a>0, b>0, 0<=x<=1'
    if x==0: return float('-inf')

    b0 = a * log(x) + b * log(1-x)
    if x*(a+b+2) < (a+1): return b0 + log(contfractbeta(a, b, x) / a)

    b1 = -lgamma(a+b) + lgamma(a) + lgamma(b)
    if x < 1: b1 += log1p(-exp(b0-b1) * contfractbeta(b, a, 1-x) / b)
    return b1



RE: PYTHON program not work - robmio - 09-05-2021 04:54 PM

Thanks so much for your reply, Albert. Reflecting on the algorithm for the calculation of CDF_Roy, the obstacle is in the calculation of the determinant. The result varies according to the precision with which the values of the matrix “A” are calculated. For example: to obtain the true result of “CDF_Roy (8,15,5,0.959)”, it would take a calculation precision comprising several hundred values after the decimal point. In short, the values that make up the matrix "A" should have at least 100 or more decimal precision, so that the right result is obtained.
Even using your suggestion, which consists in calculating the beta function with continuous fractions and with “loggamma”, the problem arises again in the calculation of the determinant of the matrix “A” (see "RETURN √(DET(A));" at the bottom of the algorithm).
Code:
#cas
CDF_Roy(s,m,n,theta):=
BEGIN
LOCAL A, ii, j, b, a, adzero, aa, cc;
A:=MAKEMAT(0,s,s);
b:=MAKELIST(0,x,1,s);
cc(x):=sqrt(π)*Gamma((2*m+2*n+s+x+2)/
2)/(Gamma((2*m+x+1)/2)*Gamma((2*n+x+
1)/2)*Gamma(x/2));
FOR ii FROM 1 TO s DO
b:=REPLACE(b,ii,{(Beta(m+ii,n+1,theta)
^2)/2});
FOR j FROM ii TO s-1 DO
b:=REPLACE(b,j+1,{(m+j)/(m+j+n+1)*
b(j)-Beta(2*m+ii+j,2*n+2,theta)/
(m+j+n+1)});
a:=(Beta(m+ii,n+1,theta)*Beta(m+j+1,
n+1,theta)-2*b(j+1))*cc(ii)*
cc(j+1);
A:=REPLACE(A,{ii,j+1},[[a]]);
END;
END;
aa:={};
FOR ii FROM 1 TO s DO
aa:=CONCAT(aa,{Beta(m+ii,n+1,theta)*
cc(ii)});
END;
aa:=ListToMat(aa);
adzero:=MAKELIST(0,x,1,s+1,1);
adzero:=ListToMat(adzero);
IF odd(s)==1 THEN
A:=ADDCOL(A,aa,s+1);
A:=ADDROW(A,adzero,s+1);
END;
A:=A-TRN(A);
RETURN √(DET(A));
END;
#end

However, in this post, my question is: “how can I get the result of a program written with PYTHON in an HPPPL environment without going through 'the terminal'?”.
Best regards, Roberto