28
Object-oriented Software for Uncertainty Propagation Keith D. McCroan US EPA National Air and Radiation Environmental Laboratory

Object-oriented Software for Uncertainty Propagation Keith D. McCroan US EPA National Air and Radiation Environmental Laboratory

Embed Size (px)

Citation preview

Page 1: Object-oriented Software for Uncertainty Propagation Keith D. McCroan US EPA National Air and Radiation Environmental Laboratory

Object-oriented Software forUncertainty Propagation

Keith D. McCroan

US EPA

National Air and Radiation Environmental Laboratory

Page 2: Object-oriented Software for Uncertainty Propagation Keith D. McCroan US EPA National Air and Radiation Environmental Laboratory

Disclaimers

The views and opinions of the author expressed here do not necessarily reflect those of the Environmental Protection Agency

Reference here to any commercial product, process, or service does not imply its endorsement by the Environmental Protection Agency

Page 3: Object-oriented Software for Uncertainty Propagation Keith D. McCroan US EPA National Air and Radiation Environmental Laboratory

Motivation

Most of us recognize the importance of good uncertainty evaluation

Uncertainty propagation involves calculus, and many people forget their calculus after graduation (or before)

At best the math is usually tedious

So, sometimes uncertainty evaluation may be done incorrectly, incompletely, or not at all

Page 4: Object-oriented Software for Uncertainty Propagation Keith D. McCroan US EPA National Air and Radiation Environmental Laboratory

Believe It or Not

The most straightforward aspect of uncertainty evaluation is uncertainty propagation

The complexity of uncertainty propagation arises mostly through the repeated application of simple rules

In fact, uncertainty propagation is so “easy” that it can be done automatically in a shared software module, such as a Windows® DLL

Page 5: Object-oriented Software for Uncertainty Propagation Keith D. McCroan US EPA National Air and Radiation Environmental Laboratory

In a Nutshell

What follows is an approach for implementing automatic uncertainty propagation in a language, like C++, that allows the definition of new data types, with function and operator overloading

It permits the application programmer to focus on calculating results, while a software library propagates uncertainties in the background

Page 6: Object-oriented Software for Uncertainty Propagation Keith D. McCroan US EPA National Air and Radiation Environmental Laboratory

Terminology

For terminology and symbols, we follow the “GUM” (Guide to the Expression of Uncertainty in Measurement)

In particular, we use the terms standard uncertainty and combined standard uncertainty to mean “1-sigma uncertainty” and “total propagated (1-sigma) uncertainty”

Page 7: Object-oriented Software for Uncertainty Propagation Keith D. McCroan US EPA National Air and Radiation Environmental Laboratory

Mathematical Model

Uncertainty propagation begins with a mathematical model of the measurement

The model is written abstractly as Y=f(X1,X2,…,XN) where X1,X2,…,XN are input quantities and Y is the output quantity

A simple radiochemistry example might be: A=(NS/tS NB/tB) / (EVRD)

Page 8: Object-oriented Software for Uncertainty Propagation Keith D. McCroan US EPA National Air and Radiation Environmental Laboratory

Input & Output Estimates

For each measurement, particular values x1,x2,…,xN, called input estimates, are plugged into the model and the output estimate, y, is calculated as y=f(x1,x2,…,xN)

Input estimates are often the “raw data”

Output estimates are the results of calculations

Page 9: Object-oriented Software for Uncertainty Propagation Keith D. McCroan US EPA National Air and Radiation Environmental Laboratory

Uncertainty PropagationFormula

The combined standard uncertainty of y is obtained from the equation:

This equation may be intimidating to anyone who is uncomfortable with calculusBut it is actually straightforward

u yf

xu x

f

x

f

xu x xc

ii

i

N

i jj i

N

i

N

i j( ) ( ) ( , )

2

2

1 11

1

2

Page 10: Object-oriented Software for Uncertainty Propagation Keith D. McCroan US EPA National Air and Radiation Environmental Laboratory

Propagating Uncertainty

The uncertainty propagation formula may be straightforward, but applying it can be tedious…

…especially if there are many input estimates and some of them are correlated

The biggest difficulty is in the calculation of the partial derivatives, f / xi (also called sensitivity coefficients)

Page 11: Object-oriented Software for Uncertainty Propagation Keith D. McCroan US EPA National Air and Radiation Environmental Laboratory

Differentiation

The rules for calculating derivatives of the functions typically used in laboratory measurements are well known and can be implemented in software

The uncertainty-propagation library is primarily a derivative calculator (with a few other functions thrown in)

Page 12: Object-oriented Software for Uncertainty Propagation Keith D. McCroan US EPA National Air and Radiation Environmental Laboratory

Some Differentiation Rules (Examples)

( )F G

x

F

x

G

xi i i

( )FG

x

F

xG F

G

xi i i

(exp ( ))exp ( )

F

xF

F

xi i

Page 13: Object-oriented Software for Uncertainty Propagation Keith D. McCroan US EPA National Air and Radiation Environmental Laboratory

New Data Types

The library exports 2 data types, which may be used in an application program: Input estimateOutput estimate

In C++, these data types are implemented as classes (called InpEst and OutEst)

Page 14: Object-oriented Software for Uncertainty Propagation Keith D. McCroan US EPA National Air and Radiation Environmental Laboratory

Syntax

The syntax for calculating with input estimates and output estimates is the same as for ordinary floating-point numbersE.g. one may write the following C++ code: A=(NS/tSNB/tB) / (E*V*R*D);The syntax is the same regardless of whether the variables on the right are “floats”, input estimates, or output estimatesThe difference is in the semantics

Page 15: Object-oriented Software for Uncertainty Propagation Keith D. McCroan US EPA National Air and Radiation Environmental Laboratory

The Client Application:

Declares variables of type “input estimate” and “output estimate” as necessary

Assigns values and standard uncertainties to the input estimates

Specifies the covariance for each pair of correlated input estimates

Calculates intermediate and final results (output estimates) using these variables

Page 16: Object-oriented Software for Uncertainty Propagation Keith D. McCroan US EPA National Air and Radiation Environmental Laboratory

The Payoff

The uncertainties and covariances of the calculated results are then available almost for free (i.e., with little effort)The client application calls a library function to return the uncertainty of an output estimateIt can call another function to evaluate covariances (if needed)

Page 17: Object-oriented Software for Uncertainty Propagation Keith D. McCroan US EPA National Air and Radiation Environmental Laboratory

C++ Examples

InpEst x1,x2,x3,x4; // Declare input estimates

// Here are some of the ways to assign values// & uncertainties to input estimates

x1 = InpEst(10, 2); // Value uncertaintyx2 = Poi(240); // Poisson distributionx3 = Rect(100, 3); // Rectangular dist.x4 = Tri(300, 5); // Triangular dist.

Page 18: Object-oriented Software for Uncertainty Propagation Keith D. McCroan US EPA National Air and Radiation Environmental Laboratory

Covariances

The covariance of any pair of input estimates may be specified. For example:

Set_u(x1, x2) = 40;

Alternatively, the correlation coefficient may be specified:

Set_r(x1, x2) = 0.92;

Page 19: Object-oriented Software for Uncertainty Propagation Keith D. McCroan US EPA National Air and Radiation Environmental Laboratory

Output Estimates

When the application performs a calculation involving input estimates and/or output estimates, the result is an output estimate

Both intermediate results and final results are output estimates

Page 20: Object-oriented Software for Uncertainty Propagation Keith D. McCroan US EPA National Air and Radiation Environmental Laboratory

Sensitivity Coefficients

Each output estimate has a value and an array of sensitivity coefficientsThere is 1 sensitivity coefficient for each input estimate on which the value of the output estimate dependsThe library propagates sensitivity coefficients in the background, without help from the programmer

Page 21: Object-oriented Software for Uncertainty Propagation Keith D. McCroan US EPA National Air and Radiation Environmental Laboratory

Combined Standard Uncertainty

The library propagates sensitivity coefficients automatically, but it calculates uncertainties only upon request

When an output estimate is calculated and stored in a variable, the application can obtain its combined standard uncertainty with a function call

Page 22: Object-oriented Software for Uncertainty Propagation Keith D. McCroan US EPA National Air and Radiation Environmental Laboratory

Example

Assume input estimates Ns, Nb, ts, and tb have been given values, and R is a variable of type “output estimate”. Calculate:

R = Ns / ts Nb / tb;

The variable R acquires the value indicated

And it automatically acquires 4 sensitivity coefficients: one for each of the input estimates from which it was calculated

Page 23: Object-oriented Software for Uncertainty Propagation Keith D. McCroan US EPA National Air and Radiation Environmental Laboratory

Example: Continued

When the result is calculated and stored in the variable R, the application can obtain its combined standard uncertainty using the expression u(R)

The library applies the uncertainty propagation formula to evaluate u(R) for the application

Page 24: Object-oriented Software for Uncertainty Propagation Keith D. McCroan US EPA National Air and Radiation Environmental Laboratory

Simplistic Exampleint main() { InpEst Ns, Nb, Eff, V, Y; // Declare variables: input estimates OutEst A; // Declare variable: output estimate float ts, tb; // Declare variables: floating-point numbers ts = tb = 6000; // Count times Ns = Poi(240); // Gross count (Poisson) Nb = Poi(86); // Blank count (Poisson) Eff = InpEst(0.364, 0.022); // Efficiency V = InpEst(1, 0.004); // Aliquant size Y = InpEst(0.84, 0.02); // Yield A = (Ns/ts - Nb/tb) / (Eff*V*Y); // Final result cout<<“The answer is “<<m(A)<<“+”<<u(A)<<endl; // Show results return 0;}

Page 25: Object-oriented Software for Uncertainty Propagation Keith D. McCroan US EPA National Air and Radiation Environmental Laboratory

The program prints:

The answer is 0.0839438+-0.0112566

Output

Page 26: Object-oriented Software for Uncertainty Propagation Keith D. McCroan US EPA National Air and Radiation Environmental Laboratory

Summary

The right software makes uncertainty propagation easy -- for arbitrary measurement modelsThe propagation can be done automatically in a shared library moduleYou (and your programmer) can focus on calculating results and let the library propagate uncertainties for you

Page 27: Object-oriented Software for Uncertainty Propagation Keith D. McCroan US EPA National Air and Radiation Environmental Laboratory

For More Information

A handout is available here for more details of the implementation

All code is in the public domain: available at www.mccroan.com

Page 28: Object-oriented Software for Uncertainty Propagation Keith D. McCroan US EPA National Air and Radiation Environmental Laboratory

Questions?