******************************************************************************
*                              Function PSIAP
*                             ~~~~~~~~~~~~~~~~
*  Purpose:
*
*     Compute the inverse standard normal distribution
*
*  Usage:
*
*     PSIAP ( X )
*
*  Input arguments:
*
*     X - argument, REAL*8;
*         must be in the range [0.8,0.99999999]
*
*  Returned value:
*
*               -1
*     Psi(X) = N (X;0,1), a quantile of the standard normal distribution N(0,1)
*     if X belongs to the specified interval or -1 otherwise, REAL*8
*
*  Called routines:
*
*     none
*
*  Method:
*
*     For X = 0.8, 0.9, 0.95, 0.975, 0.98, 0.99, 0.995, 0.9975, 0.999
*     the table value from Table 1.3 from L.N. Bol'shev and N.V. Smirnov,
*     Tables of mathematical statistics (Tablitsy matematicheskoi statistiki),
*     3rd Edition, Moscow: Nauka, 1983, is returned
*
*     For X = 0.9995, 0.9999 the table values from D.B. Owen, Handbook of
*     Statistical Tables, Russian Edition (Sbornik statisticheskih tablits),
*     Moscow: Academy of Sciences, 1973, is returned
*
*     For other X values author's approximation is used. Approximatioin error
*     does not exceed:
*          0.0024 for interval [0.8,0.99999999]
*          0.0012 for interval [0.8,0.999999]
*          0.0011 for interval [0.8,0.99999]
*          0.0007 for interval [0.8,0.9999]
*          0.0001 for interval [0.9,0.999]
*
*  Notes:
*
*        1. Psi(X) is a quantile of the standard normal distribution N(0,1)
*     with a significance level a=(1-X) (X=1-a) for a one-sided region and
*     a=2(1-X) (X=1-a/2) for a two-sided region.
*    
*
* (C) Z.Malkin, 1993-2026
*
* Distributed under the GNU General Public License.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
* REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
* LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
* OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*
******************************************************************************

      FUNCTION PSIAP ( X )

      IMPLICIT NONE

      INTEGER(4)  I, IND

      REAL(8)  X, PSIAP, T

* Table values for selected X.
      INTEGER(4), PARAMETER :: NTABL = 11
      REAL(8)  TABL(2,NTABL) /
     &   0.80000000D0, 0.841621D0,        ! Bol'shev and Smirnov
     &   0.90000000D0, 1.281552D0,        ! Bol'shev and Smirnov
     &   0.95000000D0, 1.644854D0,        ! Bol'shev and Smirnov
     &   0.97500000D0, 1.959964D0,        ! Bol'shev and Smirnov
     &   0.98000000D0, 2.053749D0,        ! Bol'shev and Smirnov
     &   0.99000000D0, 2.326348D0,        ! Bol'shev and Smirnov
     &   0.99500000D0, 2.575829D0,        ! Bol'shev and Smirnov
     &   0.99750000D0, 2.807034D0,        ! Bol'shev and Smirnov
     &   0.99900000D0, 3.090232D0,        ! Bol'shev and Smirnov
     &   0.99950000D0, 3.290530D0,        ! Owen
     &   0.99990000D0, 3.719016D0 /       ! Owen

* Intervals. Must be in increasing order.
      INTEGER(4), PARAMETER :: NINT = 5
* Boundaries.
      REAL(8)  BX(2,NINT)
     &  / 0.9D0, 0.999D0,
     &    0.8D0, 0.9999D0,
     &    0.8D0, 0.99999D0, 
     &    0.8D0, 0.999999D0,
     &    0.8D0, 0.99999999D0 /
* Approximation coefficients.
      REAL(8)  AC(4,NINT)
     &  / -0.92337495D0, -0.02522121D0, 1.64201371D0, -0.40330687D0,
     &    -0.95495887D0, -0.02695222D0, 1.65576265D0, -0.37514736D0,
     &    -0.92270803D0, -0.02326696D0, 1.63600922D0, -0.39742660D0,
     &    -0.88998754D0, -0.01991532D0, 1.61689621D0, -0.42100939D0,
     &    -0.84935143D0, -0.01629260D0, 1.59450774D0, -0.45174214D0 /
       
* ----------------------------------------------------------------------------

* Return table values for selected X.

      DO I=1,NTABL
         IF ( ABS(X-TABL(1,I)) < 1D-10 )  THEN
            PSIAP = TABL(2,I)
            RETURN
         END IF
      END DO

* Other X values.

      IND = 0

      DO I=1,NINT
         IF ( X >= BX(1,I) .AND. X <= BX(2,I) )   THEN
            IND = I
            EXIT
         END IF
      END DO

      IF ( IND /= 0 )  THEN
         T = - LOG(1D0-X)
         PSIAP = AC(1,IND) + AC(2,IND)*T + AC(3,IND)*SQRT(T+AC(4,IND))
      ELSE
         PSIAP = -1D0
         RETURN
      END IF

      END   ! PSIAP
