(15C)(DM15) - PDF/CDF and Inverse of a Normal Distribution - Printable Version +- HP Forums (https://www.hpmuseum.org/forum) +-- Forum: HP Software Libraries (/forum-10.html) +--- Forum: General Software Library (/forum-13.html) +--- Thread: (15C)(DM15) - PDF/CDF and Inverse of a Normal Distribution (/thread-17744.html) (15C)(DM15) - PDF/CDF and Inverse of a Normal Distribution - deetee - 11-24-2021 03:37 PM Hello! This is my second program I wrote for my new DM15. It calculates the probability density function (PDF), the cumulative distribution function (CDF) and their inverse functions (!) of a standardized normal function. Call it with the argument (Y) and the desired function number (X). The function number is 1 for the PDF, 2 for its inverse, 3 for the CDF and 4 for its inverse. To calculate the CDF I used some approximating function rather than integrating the PDF, which offers a accuracy of at least 2 digits (error < 1%) and is much faster. The label to call is D (distribution). Examples: * 0 (Y) and 1 (X) calculates PDF(0)=0.39 * 0.1 (Y) and 2 (X) calculates 1.66 (the positive solution) where the PDF is 0.1 * 1 (Y) and 3 (X) calculates CDF(1)=0.84 * 0.95 (Y) and 4 (X) calculates 1.64 where CDF(1.64)=0.95 The Code: Code: ``` # ------------------------------------------------------------------------------ #T:Normal Distribution #D:Calculates PDF, PDFinverse, CDF and CDFinverse of a standardized normal distribution. #D: #D:INPUT: #D:Y: Argument #D:X: Function #D: #D:FUNCTION: #D:1: PDF #D:2: PDFinverse, Argument = 0 ... 1/sqrt(2*pi) #D:3: CDF #D:4: CDFinverse, Argument = 0 ...1 #D: #D:PDF(x) = 1/sqrt(2*PI) * exp(-x*x/2) #D: #D:CDF(x) = (integral, [-inf;x]) PDF(z) * dz =(approx)= 1/(1 + exp(-0.07*x*x*x-1.6*x)) #L-4:Distribution (Start Program) #L10:PDF #L11:CDF #L12:Menu entry 2 #L13:Menu entry 3 #L14:Menu entra 4 #RI:Target for SOLVE # ------------------------------------------------------------------------------    000 {             }     001 {    42 21 14 } f LBL D    002 {           1 } 1    003 {    43 30  8 } g TEST x 0 Also, cdf/icdf using tanh/atanh is equivalent to original exp/ln (1 + tanh(x))/2 = (sinh(x)+cosh(x)) / (2*cosh(x)) = e^x / (e^x + e^-x) = 1 / (1 + e^(-2x)) CDF ≈ 1/(1+exp(-0.07056*z^3-1.5976*z))﻿ = (1 + tanh(0.03528*z^3+0.7988*z))/2 Similarly, for the inverse, ln(x) = 2*atanh((x-1)/(x+1)) z ≈ 5.494 * sinh(asinh(-0.3418*ln(1/CDF-1))/3) = 5.494 * sinh(asinh(-0.6836*atanh(1-2*CDF))/3) RE: (15C)(DM15) - PDF/CDF and Inverse of a Normal Distribution - deetee - 11-30-2021 02:46 PM (11-30-2021 01:28 PM)Albert Chan Wrote:  With 0 < CDF < 1, 1/CDF - 1 > 0 Hi Albert! Sorry, that was my fault. I read 1/CDF-1 as 1/(CDF-1) Thanks for solving my comprehension problem. Regards deetee