SlideShare a Scribd company logo
Fast Inversion of Normal CDF
John D. Cook
What we’re computing
Basic assumptions
  Five or six significant figures is
  adequate for many applications
  Memory is fast and plentiful
  Low-level bit operations are faster than
  arithmetic
Basic Approach
  Tabulate values at sample points and
  use low-order approximations to fill in
  Take advantage of binary
  representation of floating point
  numbers to decide where to sample
  Change variables to avoid arithmetic
Big clever idea
  Extract sample points based on the
  exponent and mantissa
  The extraction is extremely fast
  It is biased to sample more frequently
  near zero, exactly where cPhi-1 needs
  more sampling
IEEE Floating Point
Representation (conceptual)

  x = +/- 2e m, 1 <= m < 2
  Bit 1: sign bit +/-
  Bits 2 through 9: exponent e
  Bits 10 through 32: mantissa
IEEE Floating Point
Representation (details)

  Bit 1: 0 for positive, 1 for negative
  Bits 2 through 9: exponent of 2 biased
  by 127
  (values 0 through 255 correspond to
  actual exponents -127 through 128)
  Bits 10 through 32: mantissa minus 1
  (leading bit always 1, so don’t store)
Starting Point for Marsaglia’s
algorithm
  Represent numbers by
  u = 2-k (2-1 + 2-6 j + 2-24 m)
  0 <= k < 32, 0 <= j < 32
  0 <= m < 224
  k = 126 – e, where ‘e’ is the exponent
  representation bits
  j = first five mantissa representation bits
  m = last 18 mantissa representation bits
Sample Points
  Tabulate cPhi-1 at points corresponding
  to m = 0, i.e. at 32 possible values of i
  and j, a total of 1024 points.
  Use quadratic Taylor approximation
  based at these points
  A fixed number samples per exponent
  samples more finely near zero, just
  where cPhi-1 needs more samples
Clever indexing
  Conceptually, we have a matrix A[i][j] of
  tabulated values
  This requires two calculations to find indices –
  one for i and one for j – and two operations
  to lookup values
  Combine into a single index
  n = 992-32k + j
  that can be extracted directly by one bit
  manipulation: bits 2 through 14 minus 3040
Polynomial evaluation
  Taylor approximation:
  t = h B(k,j)
  x = A(k,j) -0.5 t + 0.125 A(k,j)t2
  Rescale B’s by square root of 8:
  t = h B’
  x = A – c t – A t2 [ c = sqrt(2) ]
  Horner’s method:
  x = A – t(c – A t)
C++ Implementation
double NormalCCDFInverse(double x)
{
      float f1 = (float) x;
      unsigned int ui;
      memcpy(&ui, &f1, 4);
      int n = (ui >> 18) - 3008;
      ui &= 0xFFFC0000;
      float f2;
      memcpy(&f2, &ui, 4);
      double v = (f1-f2)*B[n];

     return A[n] - v*(sqrt2 - A[n]*v);
}
Fine Print
  This algorithm only valid for p <= 0.5
  For p > 0.5, use cPhi-1(p) = -cPhi-1(1-p)
  Phi-1(p) = cPhi-1(1-p)
  Algorithm not valid for p < 2-33
  The maximum error is 0.000004,
  which occurs near p = 0.25
Implementation
double PrivateNormalCCDFInverse(double x) …

double NormalCDFInverse(double x) {
    return (x > 0.5)
      ? PrivateNormalCCDFInverse(1.0-x)
      : PrivateNormalCCDFInverse(x);
}

double NormalCCDFInverse(double x) {
    return (x > 0.5)
      ? -PrivateNormalCCDFInverse(1.0-x)
      : PrivateNormalCCDFInverse(x);
}
References
  Rapid evaluation of the inverse of the
  normal distribution function
  by Marsaglia, Zaman, and Marsaglia
  Statistics and Probability Letters
  19 (1994) 259 – 266
Notes
  This talk presented June 6, 2001
  http://www.JohnDCook.com

More Related Content

PPTX
2 Combinational Logic Circuit 01
PPT
Addition and subtraction with signed magnitude data (mano
PPT
adder and subtractor
PPS
Arithmetic Operations
PPT
Lecture filling algorithms
PDF
Chapter 05 computer arithmetic
PPTX
Computer arithmetic
PDF
Combinatorial optimization CO-2
2 Combinational Logic Circuit 01
Addition and subtraction with signed magnitude data (mano
adder and subtractor
Arithmetic Operations
Lecture filling algorithms
Chapter 05 computer arithmetic
Computer arithmetic
Combinatorial optimization CO-2

What's hot (19)

PPTX
C++ lab -4
PPTX
C++:Lab 2
PPTX
Subtractor (1)
PDF
Lab 4 Three-Bit Binary Adder
PPT
Jmet Ppt3 Higher Maths
PPTX
Polygons - Computer Graphics - Notes
PPTX
BOOTH ALGO, DIVISION(RESTORING _ NON RESTORING) etc etc
PPTX
Design half ,full Adder and Subtractor
PPT
Calculus Sections 4.1 and 4.3
PPTX
Presentation(intermediate code generation)
PDF
Matrices, Arrays and Vectors in MATLAB
PPTX
Three Address code
PDF
A lab report on modeling and simulation with python code
PPTX
Multiplication algorithm, hardware and flowchart
PPTX
Newton cotes integration method
PDF
AP Calculus Slides February 14, 2008
PPTX
computer operating system:Greedy algorithm
DOC
Complex Number From Jayant for TV
C++ lab -4
C++:Lab 2
Subtractor (1)
Lab 4 Three-Bit Binary Adder
Jmet Ppt3 Higher Maths
Polygons - Computer Graphics - Notes
BOOTH ALGO, DIVISION(RESTORING _ NON RESTORING) etc etc
Design half ,full Adder and Subtractor
Calculus Sections 4.1 and 4.3
Presentation(intermediate code generation)
Matrices, Arrays and Vectors in MATLAB
Three Address code
A lab report on modeling and simulation with python code
Multiplication algorithm, hardware and flowchart
Newton cotes integration method
AP Calculus Slides February 14, 2008
computer operating system:Greedy algorithm
Complex Number From Jayant for TV
Ad

Similar to Fast coputation of Phi(x) inverse (20)

PPT
Number theory
PPTX
Logic Circuits Design - "Chapter 1: Digital Systems and Information"
PPTX
01 - DAA - PPT.pptx
PDF
Computer graphics 2
PDF
Efficient Volume and Edge-Skeleton Computation for Polytopes Given by Oracles
PPTX
Optimization tutorial
PPT
tutorial5.ppt
PDF
Oracle-based algorithms for high-dimensional polytopes.
PPTX
dynamic programming complete by Mumtaz Ali (03154103173)
PPT
C++ Language
PPT
04-Induction and Recursion.ppt ppt bai tap
PPTX
pseudocodes for interview preparation 2.pptx
PPT
algo_vc_lecture8.ppt
PPTX
Episode 50 : Simulation Problem Solution Approaches Convergence Techniques S...
PPTX
01 EC 7311-Module IV.pptx
PDF
SKuehn_MachineLearningAndOptimization_2015
PPTX
Regression
PPTX
Binu Siva Singh Final.pptx
PPT
Design and Analysis of Algorithm Brute Force 1.ppt
PPTX
3 analysis.gtm
Number theory
Logic Circuits Design - "Chapter 1: Digital Systems and Information"
01 - DAA - PPT.pptx
Computer graphics 2
Efficient Volume and Edge-Skeleton Computation for Polytopes Given by Oracles
Optimization tutorial
tutorial5.ppt
Oracle-based algorithms for high-dimensional polytopes.
dynamic programming complete by Mumtaz Ali (03154103173)
C++ Language
04-Induction and Recursion.ppt ppt bai tap
pseudocodes for interview preparation 2.pptx
algo_vc_lecture8.ppt
Episode 50 : Simulation Problem Solution Approaches Convergence Techniques S...
01 EC 7311-Module IV.pptx
SKuehn_MachineLearningAndOptimization_2015
Regression
Binu Siva Singh Final.pptx
Design and Analysis of Algorithm Brute Force 1.ppt
3 analysis.gtm
Ad

More from John Cook (6)

PDF
Bayesian adaptive clinical trials: Promises and pitfalls
PDF
Erasure Coding Costs and Benefits
PDF
Combining Intuition and Data
PDF
Monte Carlo and quasi-Monte Carlo integration
PDF
Bayesian hypothesis testing
PDF
Bayesian clinical trials: software and logistics
Bayesian adaptive clinical trials: Promises and pitfalls
Erasure Coding Costs and Benefits
Combining Intuition and Data
Monte Carlo and quasi-Monte Carlo integration
Bayesian hypothesis testing
Bayesian clinical trials: software and logistics

Recently uploaded (20)

PPTX
SOPHOS-XG Firewall Administrator PPT.pptx
PPTX
Programs and apps: productivity, graphics, security and other tools
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Univ-Connecticut-ChatGPT-Presentaion.pdf
PDF
Assigned Numbers - 2025 - Bluetooth® Document
PDF
Unlocking AI with Model Context Protocol (MCP)
PPTX
TechTalks-8-2019-Service-Management-ITIL-Refresh-ITIL-4-Framework-Supports-Ou...
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PPTX
Group 1 Presentation -Planning and Decision Making .pptx
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PPTX
cloud_computing_Infrastucture_as_cloud_p
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Approach and Philosophy of On baking technology
PDF
Mushroom cultivation and it's methods.pdf
PPTX
Spectroscopy.pptx food analysis technology
PPTX
OMC Textile Division Presentation 2021.pptx
SOPHOS-XG Firewall Administrator PPT.pptx
Programs and apps: productivity, graphics, security and other tools
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Reach Out and Touch Someone: Haptics and Empathic Computing
Univ-Connecticut-ChatGPT-Presentaion.pdf
Assigned Numbers - 2025 - Bluetooth® Document
Unlocking AI with Model Context Protocol (MCP)
TechTalks-8-2019-Service-Management-ITIL-Refresh-ITIL-4-Framework-Supports-Ou...
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Group 1 Presentation -Planning and Decision Making .pptx
Building Integrated photovoltaic BIPV_UPV.pdf
cloud_computing_Infrastucture_as_cloud_p
Diabetes mellitus diagnosis method based random forest with bat algorithm
Digital-Transformation-Roadmap-for-Companies.pptx
Advanced methodologies resolving dimensionality complications for autism neur...
Approach and Philosophy of On baking technology
Mushroom cultivation and it's methods.pdf
Spectroscopy.pptx food analysis technology
OMC Textile Division Presentation 2021.pptx

Fast coputation of Phi(x) inverse

  • 1. Fast Inversion of Normal CDF John D. Cook
  • 3. Basic assumptions Five or six significant figures is adequate for many applications Memory is fast and plentiful Low-level bit operations are faster than arithmetic
  • 4. Basic Approach Tabulate values at sample points and use low-order approximations to fill in Take advantage of binary representation of floating point numbers to decide where to sample Change variables to avoid arithmetic
  • 5. Big clever idea Extract sample points based on the exponent and mantissa The extraction is extremely fast It is biased to sample more frequently near zero, exactly where cPhi-1 needs more sampling
  • 6. IEEE Floating Point Representation (conceptual) x = +/- 2e m, 1 <= m < 2 Bit 1: sign bit +/- Bits 2 through 9: exponent e Bits 10 through 32: mantissa
  • 7. IEEE Floating Point Representation (details) Bit 1: 0 for positive, 1 for negative Bits 2 through 9: exponent of 2 biased by 127 (values 0 through 255 correspond to actual exponents -127 through 128) Bits 10 through 32: mantissa minus 1 (leading bit always 1, so don’t store)
  • 8. Starting Point for Marsaglia’s algorithm Represent numbers by u = 2-k (2-1 + 2-6 j + 2-24 m) 0 <= k < 32, 0 <= j < 32 0 <= m < 224 k = 126 – e, where ‘e’ is the exponent representation bits j = first five mantissa representation bits m = last 18 mantissa representation bits
  • 9. Sample Points Tabulate cPhi-1 at points corresponding to m = 0, i.e. at 32 possible values of i and j, a total of 1024 points. Use quadratic Taylor approximation based at these points A fixed number samples per exponent samples more finely near zero, just where cPhi-1 needs more samples
  • 10. Clever indexing Conceptually, we have a matrix A[i][j] of tabulated values This requires two calculations to find indices – one for i and one for j – and two operations to lookup values Combine into a single index n = 992-32k + j that can be extracted directly by one bit manipulation: bits 2 through 14 minus 3040
  • 11. Polynomial evaluation Taylor approximation: t = h B(k,j) x = A(k,j) -0.5 t + 0.125 A(k,j)t2 Rescale B’s by square root of 8: t = h B’ x = A – c t – A t2 [ c = sqrt(2) ] Horner’s method: x = A – t(c – A t)
  • 12. C++ Implementation double NormalCCDFInverse(double x) { float f1 = (float) x; unsigned int ui; memcpy(&ui, &f1, 4); int n = (ui >> 18) - 3008; ui &= 0xFFFC0000; float f2; memcpy(&f2, &ui, 4); double v = (f1-f2)*B[n]; return A[n] - v*(sqrt2 - A[n]*v); }
  • 13. Fine Print This algorithm only valid for p <= 0.5 For p > 0.5, use cPhi-1(p) = -cPhi-1(1-p) Phi-1(p) = cPhi-1(1-p) Algorithm not valid for p < 2-33 The maximum error is 0.000004, which occurs near p = 0.25
  • 14. Implementation double PrivateNormalCCDFInverse(double x) … double NormalCDFInverse(double x) { return (x > 0.5) ? PrivateNormalCCDFInverse(1.0-x) : PrivateNormalCCDFInverse(x); } double NormalCCDFInverse(double x) { return (x > 0.5) ? -PrivateNormalCCDFInverse(1.0-x) : PrivateNormalCCDFInverse(x); }
  • 15. References Rapid evaluation of the inverse of the normal distribution function by Marsaglia, Zaman, and Marsaglia Statistics and Probability Letters 19 (1994) 259 – 266
  • 16. Notes This talk presented June 6, 2001 http://www.JohnDCook.com