Chapter 11 - Getting Started01.doc

Getting Ready for
“Monte Carlo Modeling of Randomness”
by Alan Steele and Rob Douglas
Institute for National Measurement Standards
National Research Council Canada
You will be using both your computer and your brain in following this development, and
both will benefit from a bit of initial preparation.
 Your Computer
Recommended:
 >1GHz, >500MB RAM, >600MB disk space available
 Windows XP, DVD reader
 Excel 2003
Minimum:
 200MHz, 128MB RAM (if Windows 98 or 98SE),
 50MB disk space available, access to files on the book’s DVD
 Windows 98+
 Excel 97+
Many of the examples might run on an Apple computer with Excel (pre Office 2008), but
we have not tested them; and some examples are specific to Wintel or WinAMD
computer. Excel from Office 2007 has a new layout for the user interface which we have
not used in our documentation, although we have found no other problems (except more
layers of “security” that need to be set to permit us to run and to write Excel macros),
learning from our documentation is easiest with earlier versions of Excel.
 Your Brain
Recommended:
 standard metrologist’s brain
 working knowledge of ISO GUM uncertainty calculations
 working knowledge of some version of Excel, BASIC and C
Minimum:
 measurer’s brain with science/engineering experience
 familiarity with uncertainty as treated in ISO Guide to the
Expression of Uncertainty in Measurement (ISO GUM)
 comfort with the ideas of randomness as applying both to
measurements (and what values might have been reported, as the
measurement, instead of the reported value) and to measurands (and where
the idealized value might be relative to the reported value )
 familiarity with some version of Excel and BASIC
Excel can be useful for many aspects of metrology, but is particularly powerful for
addressing the tasks associated with calculating uncertainties. Formula-based standard
approximations given in the ISO GUM, such as the “Law of Propagation of Uncertainty”
or the “Welch-Satterthwaite approximation” are easily handled in Excel by entering
formulae into the cells of an Excel worksheet.
For uncertainty analysis, “advanced methods” are needed to go beyond this level, or to
address questions about how good these approximations are. Some approaches rely on
propagating distributions. A simpler approach propagates simulated measurements:
Monte Carlo simulation. It can be handled in Excel by writing programs Excel’s
embedded programming language, Visual Basic for Applications (VBA), supplemented
very occasionally with programs written in the C language and compiled into DLLs.
 Setting Up Your Computer
(1) Backup your system disk (or disks). Since you will be encouraged to try new
ways of organizing your copy of Excel, path variables etc., you will also want an
easy and complete method for undoing all these changes if you don’t like them.
With the added confidence of this safety net, we hope that you will be happier to
make changes and to try our way of doing things.
(2) In Windows XP, it may also be convenient to create a new “user” account, with
administrator privileges, for you to employ as you work on the seminar material,
so that your new setup for Excel can co-exist with your old setup.
Start | Control Panel | User Accounts | Create a new account | enter account
name | select Computer Administrator | click Create Account |
 Setting Up Excel
Here the objective is to prepare your copy of Excel so that it will allow you to load
and use workbooks with macros, and allow you full access to the Visual Basic for
Applications programming environment. This is a powerful environment, and
denying you access to its power is sometimes done in the name of “security”.
For Windows XP/Excel 2003: earlier variants need different (and less) setting up.
Some of the changes below are suggestions, others are required to load and use our
Macros.
(1) For learning, we suggest using full menus for Excel, since in this way unfamiliar
menu choices are easier to find. Otherwise, truncated menus (below left are
presented first) – with varying content. To get the full menus (below right) right
away…
(2)
we simply change an item in Tools | Customize | Options tab | select Always show
full menus | Close:
(2) We also suggest turning off the “Task pane”, which we think gets in the way more
than it helps:
You can, of course, turn off the Task Pane each time it appears, (at left)
but this task can become annoying, and the Task Pane can be turned off
quite easily:
(a) by Tools | Customize | Toolbars tab | deselect Task Pane | Close
also, you may wish to stop the Task Pane from appearing each time you
start Excel
(b) by Tools | Options | View tab | scroll down and deselect Show
startup task pane | OK.
N.B.!
(3) To be able to load our seminar’s example Excel workbooks with their Macros, you
must change the default Security setting on your copy of Excel. Because Excel Macros
can be so powerful, and malicious Excel Macros can be executed so easily from an e-mail
attachment or from a website hotlink, Excel now installs with a high level of isolation.
We think that a reasonable compromise is to us the medium level of isolation. With this
level, each time you try to load an Excel workbook which has Excel Macros, you will be
reminded of the hazards and asked for permission to load and use the workbook.
To set Excel’s security level to Medium, use
Tools | Options | Security tab | click on Macro Security | select Medium | OK | OK
(4) We strongly recommend that you load the toolbar for Excel VBA (Visual Basic for
Applications) and Macros. The buttons on this toolbar will significantly simplify your
tasks of writing, debugging and running of Excel Macros in this seminar.
To do this,
Tools | Customize | Toolbars tab | scroll down and select Visual Basic | drag and drop
the Visual Basic toolbar where you want it | Close
The Visual Basic toolbar will stay
visible with the other toolbars,
although it may be compressed and
need a click on its rightmost tab
to display all its buttons.
As shown here, the buttons are:
Run Macro (used a lot!)
Record Macro (quite useful)
Security
Visual Basic Editor (used a lot!)
Control toolbox
Design Mode
Microsoft Script Editor
 Setting Up the Borland C Compiler
If you wish to try the examples that demonstrate how to use C language DLLs as part of
an Excel Macro, you must first download and set up the free Borland command-line C++
compiler. In our experience, C programming is rarely needed for uncertainty analysis. Its
main advantages are speed (often > 10x faster), and access to standard scientific libraries
and perhaps to your own validated special-purpose programs. Because of the licence
restrictions on the free C compiler, each of you must register with Borland and download
your own copy of this compiler.
On our website http://inms-ienm.nrc-cnrc.gc.ca/qde/montecarlo/choosedownloads.html
we have nine Videos of the steps to download, install and use a free compiler (Borland's
C++) to compile and run a C language DLL from Excel.
The 9 video files have a total running time of about 40 minutes. They show and describe
the steps that can be used to install C and invoke C routines from Excel. They are
intended to be used as help files when more compact documentation is not clear.
The files are .wmv files that can play from recent versions of the Windows Media player.
They were prepared with the Windows Media Encoder and we tried to replicate our
“Here, let me show you how I do that...” style.
 Setting Up your Brain
About Uncertainties
Our seminar will focus on advanced methods of uncertainty analysis, using Excel. The
methods are based on our National Metrology Institute perspective for analyzing and
communicating uncertainty. The framework and vocabulary that we will use are
described in the ISO Guide to the Expression of Uncertainty in Measurement – using
uncertainties in
input quantities, with a
measurement equation, aiming at calculating the
standard uncertainty (and
expanded uncertainties) of the output quantity.
Gaussian (or “normal”) distributions,
uniform distributions,
Student-t distributions;
degrees of freedom,
standard deviation,
variance,
covariance,
correlation coefficient,
“law of propagation of uncertainty”.
It would be helpful to review the italicized words above, and to at least have enough
familiarity with these concepts to be able to articulate what you know (and don’t know)
about each. We suggest that your preparation should focus on your current working
knowledge of these concepts, and that little preparation should go into extending your
working knowledge before the seminar. Extending your working knowledge about
uncertainties may well be substantially easier after the seminar, using some of the
approaches we will present there.
 Setting Up your Brain
About Excel
Our seminar is aimed at participants who are regular users of the Excel for calculations
concerning measurements. The advanced methods will be prepared and used as Excel
Macros, using Excel’s embedded programming language: Visual Basic for Applications
(VBA).
If substantial parts of the following program look unfamiliar to you, we recommend that
you review the BASIC programming language in preparation for the seminar. The
program below is for use as a Macro that is attached to an Excel workbook.
Name of Macro, name of subroutine.
Declare variables (a good practice!)
Zero the sum-of-squares variable.
Loop for 3 components.
Read standard uncertainty from active
worksheet cells - squaring and adding.
End of loop.
Take square root for combined uncertainty.
Write combined uncertainty to cell A5.
End of subroutine (Macro) code.
Excel’s version of BASIC, VBA, has a richness and many extensions (such as the
Cells(i,j).Value in the example above) that we will want to use, but do not all need to be
remembered: Excel’s Macro recorder will write the Macro as a VBA program that you
can run, view, copy as templates and edit in your own VBA programs.
Therefore, we strongly recommend that you review running Excel’s Macro recorder,
viewing your recorded Macro, and running your recorded Macro.
Also, we recommend a book about VBA programming for Excel as a really handy
accessory when you are applying the methods of the seminar. There is quite a choice, and
any book on VBA programming for Excel – whether for Excel 97, Excel 2000, Excel XP
or Excel 2003 – can be a big help, since all these versions share a core VBA language.
We have used different books, with different styles, that emphasize different parts of
VBA for Excel. Of these books, our most-thumbed copy is one of Excel 2000 Power
Programming with VBA by John Walkenbach. He has updated this book for the later
versions of Excel, but has kept much the same approach. He has also written quite a few
other books on Excel and VBA, as have many other authors. Some of these other books
may well be more appealing to you. By far the best technique we have found for learning
VBA is simply to write VBA programs to do the things we want done. The Excel/VBA
programming environment provides quite a bit of support for the learning-by-doing
approach.
We recommend that you not try to use Excel from Office 2007 in the hands-on part of the
seminar, unless you will need no help in using its completely revised menus of
commands and options: we will not be able to provide help about these differences with
previous versions of Excel. If you wish to use Excel from Office 2007 after the Seminar,
we recommend that you keep as much compatibility as possible with previous versions of
Excel, by saving your workbooks in the “Excel 97-2003” format option of the Excel 2007
“Save As” command.
 Setting Up your Brain
About C
Our seminar may touch on the interface between C language programming and how to
make C language DLL’s (Microsoft’s Dynamic Link Libraries) that can be called from
VBA programs so they can easily be invoked as Excel Macros.
Although VBA may be one of the most widely distributed computer programming
languages, for serious numerical calculations it is very desirable to be able to use the
extensive libraries that have been written (and debugged) in languages such as C and
FORTRAN. In this tutorial, we will benefit from Borland’s generosity in freely providing
a command-line C compiler that you may download, and discuss the creation Dynamic
Link Libraries from C language code, as well as their invocation from Excel Macros
written in VBA. Thus we will have access to the vide variety of C language libraries, as
well as to the efficiencies of a fully compiled language. For most of the calculations that
we are interested in, the computational efficiency of C over VBA (which is often a factor
of 10 or more) is not essential, and the Seminar is planned to stress VBA programming.
If you are a C language programmer, you will likely have your own favourite C-language
programming environment. Initially, we advise using the free Borland command-line
compiler: the first objective is to master the DLL to Excel link with one standard C
compiler. Some help is available for the gcc compiler. Frank Liebmann, who attended
our day-long seminar at the 2007 Measurement Science Conference, has posted a “Howto” file for using the Dev-C++ environment for writing DLLs to use with Excel: see
http://www.geocities.com/texliebmann/devcpp/devcpp-vb.html .
If you are not a C language programmer, but have access to one or more such experts, we
hope that you will be able to pick up enough of the jargon so that with our examples and
detailed write-ups you may be able to persuade them to help you. The main new thing
that your C language expert may have to learn is how to use Excel’s “safe arrays”, and
this is fully illustrated in our supporting material about C language programming.
 Setting Up your Brain
About Randomness, Measurement Science and Statistics
In this chapter, our approach follows randomness through measurements, claims about
measurements, measurement equations and validation through measurement science. Our
methods aim to facilitate the prediction / experiment / comparison cycle of the classical
scientific method. We deliberately minimize the probabilistic exercises used in formal
statistics. We employ the simplest formal relationships between the possible randomness
of measurement and the randomness of the measurand. Using the fiducual argument, we
treat them either as equivalent, or as equivalent after flipping about the fiducial value
used to report both the measurement and measurand.
In measurement science, the final arbiter about the adequacy of any method for predicting
the distribution of the measurand is to be the experiments done in the real world – the
domain of physicists, physical chemists and engineers – rather than just using the
assumptions and theorems that are the usual domain of mathematicians and statisticians.
Experiments, rather than purely statistical arguments, are needed to judge whether the
methods used in this chapter must be supplemented by any approach to “Modeling
Randomness” that uses more complicated methods of probabilistic induction.
Excel’s Wintel Versions
Excel 2007
– Version 12 – released with Office 2007 and Windows Vista in 2007. It
adds new “security” tools that can lock out VBA programming. It has a
new and quite different user interface than we have documented. It has a
new workbook file format, but can still use Excel 97-2003 .xls format.
Excel 2003
– Version 11 – our preferred version, released in 2003.
Excel XP
– Version 10 – released in 2001 with Office XP, and also known as
Excel 2002. As-released version has a broken random number
function Rnd() that can be fixed by downloading its updates.
Excel 2000
– Version 9 – released in 1999.
Excel 97
– Version 8 – released in 1997.
Excel 95
– Version 7 – released in 1995. Uses a different workbook file structure
(Biff7) than the above versions (which use Biff8). Introduced current
VBA editor.
– there is no Version 6 of Excel.
Excel 5
Excel 4
Excel 3
Excel 2
– Version 5, released in 1993, was the first version of Excel to support
VBA as a Macro language.
– Version 4, released in 1992, but for our purposes Versions 2-4 have been
orphaned because of their inability to use VBA Macros.
– Version 3, released in 1990.
– Version 2, released in 1987. This was the first Windows version of
Excel (for Windows 2.0, but could also run under DOS).
The installed base of Excel installations now stands at around the half-billion mark.
While many of these installations may be on deceased computers, many others will be
serving more than one user. Almost all of these installations have the full VBA
programming environment loaded, although it is usually unappreciated and underused –
and increasingly, defaults to being locked in the name of security. Excel 97 and all later
versions interoperate gracefully, and the huge installed base of Excel workbooks and
VBA Macros means that this likely will continue for the foreseeable future.
In this light, Excel with VBA seems to be the most ubiquitous of all programming
environments that could be seriously considered for co-developing and sharing programs
for calculations concerning measurements, metrology and uncertainties. We have used it
for these purposes for the better part of a decade, and although we have sometimes
reached its limits, we have not been seriously inconvenienced by these limits. For any
application such as Monte Carlo simulation, where the user may wish to “tweak” a
program, we think that Excel is unsurpassed as a programming environment for reaching
other metrologists.