Mar 21, 1997 - ISSN 1500-6050. Oslo Scienti c Computing Archive. Report 1998-5. Stochastic methods in Di pack. Hans Petter Langtangen. Harald Osnes.
ISSN 1500-6050
Oslo Scienti c Computing Archive Report 1998-5
Stochastic methods in Dipack Hans Petter Langtangen March 21, 1997
Harald Osnes
Aims and scope: Traditionally, scienti c documentation of many of the activities in modern scienti c computing, like e.g. code design and development, software guides and results of extensive computer experiments, have received minor attention, at least in journals, books and preprint series, although the the results of such activites are of fundamental importance for further progress in the eld. The Oslo Scienti c Computing Archive is a forum for documenting advances in scienti c computing, with a particular emphasis on topics that are not yet covered in the established literature. These topics include design of computer codes, utilization of modern programming techniques, like object-oriented and object-based programming, user's guide to software packages, veri cation and reliability of computer codes, visualization techniques and examples, concurrent computing, technical discussions of computational eciency, problem solving environments, description of mathematical or numerical methods along with a guide to software implementing the methods, results of extensive computer experiments, and review, comparison and/or evaluation of software tools for scienti c computing. The archive may also contain the software along with its documentation. More traditional development and analysis of mathematical models and numerical methods are welcome, and the archive may then act as a preprint series. There is no copyright, and the authors are always free to publish the material elsewhere. All contributions are subject to a quality control.
Oslo Scienti c Computing Archive 1998-5
Revised March 21, 1997
Title
Stochastic methods in Dipack Contributed by
Hans Petter Langtangen Harald Osnes Communicated by
Are Magnus Bruaset
Oslo Scienti c Computing Archive is available on the World Wide Web. The format of the contributions is chosen by the authors, but is restricted to PostScript les, generated from LaTeX, and HTML les for documents with movies and text, and compressed tar- les for software. There is a special LaTeX style le and instructions for the authors. There is also a standard for the use of HTML. All documents must easily be printed in their complete form.
Contents
1 Introduction 2 Overview of the functionality 3 Stochastic variables
3.1 Random number generation . 3.2 Probability distributions . . . 3.2.1 Basic Dipack classes 3.2.2 Example . . . . . . . . 3.3 Estimation . . . . . . . . . . 3.3.1 Basic Dipack classes 3.3.2 Example . . . . . . . . 3.4 Monte Carlo simulation . . . 3.4.1 Basic Dipack classes 3.4.2 Example . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
4 Stochastic processes
1 2 3 3 3 3 4 5 5 6 7 7 9
12
4.1 Generation of stochastic processes . . . . . . . . . . . . . . . 12 4.2 Statistical properties . . . . . . . . . . . . . . . . . . . . . . . 12 4.3 Stochastic ordinary dierential equations . . . . . . . . . . . . 13
5 Random elds 5.1 5.2 5.3 5.4
Basic design issues . . . . . . . . . . . . Generation of random elds . . . . . . . Statistical properties . . . . . . . . . . . Stochastic partial dierential equations .
i
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
17
17 18 19 20
This report should be referenced as shown in the following
Bib
TEX entry:
@techreport{OSCA1998-5, author = "Hans Petter Langtangen and Harald Osnes", title = "Stochastic methods in Diffpack", type = "Oslo Scientific Computing Archive ", note = "URL: http://www.math.uio.no/OSCA ; ISSN 1500-6050", number = "\#{}1998-5", year = "March 21, 1997", }
ii
Stochastic methods in Dipack Hans Petter Langtangen
Harald Osnes
Abstract
This note gives an overview of various functionality in Dipack for dealing with stochastic variables, stochastic processes, random elds and stochastic dierential equations.
1 Introduction This report describes various utilities in Dipack for dealing with stochastic problems. There are functionality for stochastic variables, stochastic processes, random elds and stochastic dierential equations. These stochastic tools make it possible to investigate the impact of uncertain input parameters in mathematical models. It is a well known fact that uncertainties are most consistently modeled in a stochastic framework, leading to stochastic ordinary dierential equations (SODEs) and stochastic partial dierential equations (SPDEs), where the mathematical model otherwise contain deterministic dierential equations. Therefore, the attention in this report has been paid to problems related to stochastic dierential equations, rather than statistics in general. The aim of stochastic methods in Dipack has always been to assist algorithms for ecient solution of stochastic dierential equations. Statistical computations in general should be carried out using well-known problem solving environments in statistics, such as S-Plus or SAS. We start by presenting the basic functionality for random variables. Thereafter, stochastic processes and random elds are explained. We discuss how to choose dierent probability distributions for the stochastic quantities, how to generate realizations of random variables and how to estimate statistical properties. Also introductions to SODEs and SPDEs with emphasis on the object-oriented implementation, virtual functions and inheritance are given. For example, the close relation between stochastic and deterministic solution methods will be clear. Several program examples are provided. 1
Page 2
Stochastic methods in Dipack
The main purpose of the present note is to give an overview on the stochastic functionality in Dipack. Details are documented in the man pages.
2 Overview of the functionality Here we list the stochastic features of Dipack at the time of this writing. Random number generation:
{ three generation methods for uniform (0,1) variables { transformation to arbitrarily distributed realizations by use of
the inverse cumulative distribution function (normal, lognormal, uniform and Dirac delta distributions are available) { Box-Muller method for Gaussian variates { Gaussian white noise processes { exact Cholesky-factor transformation from uncorrelated to correlated random eld { two approximative Markov based methods for random eld generation { conditional Gaussian random elds (kriging) Estimation:
{ expectation, standard deviation, variance, skewness and kurto{ { { {
sis for single random variables, stochastic (vector) processes and random nite element elds covariance of a stochastic vector process covariance along lines in random elds histogram test of normality
Stochastic ordinary dierential equations: { problem class for de nition of the equations to be solved, { ergodic time series simulation { rst passage time simulation (with xed initial state) { integration of ODEs Stochastic partial dierential equations: { random eld generation and estimation
Page 3
{ programming framework for Monte Carlo simulation
summary reports from multiple loops can be exported directly to SPlus for regression analysis etc.1
3 Stochastic variables Methods that deal with stochastic variables include random number generation, evaluation of probability distributions and estimation of statistical properties of random variables. Also applications of random variables as coecients in dierential equations will be considered along with discussions of solution methods for such problems.
3.1 Random number generation
The fundamental class for generating realizations of random variables is the RandGen class. It contains a handle to an object of class UnifRandGen, which is an abstract base class for generating uniformly distributed ([0,1]) variables. There are three such generation methods, implemented in the classes UnifRandGenSimple UnifRandGenOpSystem UnifRandGenCongruential
which are all derived from the base class UnifRandGen. RandGen contains the important function generate with an argument of type ProbDistr (see below). This function generates a realization of the random variable by drawing a number from the uniform generator in the UnifRandGen hierarchy and transforming it with respect to the probability distribution de ned by the ProbDistr object. In class RandGen there are also some simple functions for generating uniformly and normally distributed variables.
3.2 Probability distributions
3.2.1 Basic Dipack classes
Some of the most common probability distributions are implemented in an hierarchy with class ProbDistr as an abstract base class. All relevant code appears in the les $BTR/src/libs/bt2/sstat/statdistr.[hC]
There are subclasses for the uniform distribution (UniformDistr), the normal (Gaussian) distribution (NormalDistr), 1
This subject is not treated further in this report.
Page 4
Stochastic methods in Dipack
the log-normal distribution (LogNormalDistr), and the delta-function distribution (DetermDistr), which in fact means that
deterministic numbers can be treated as a special case of stochastic variables. Each subclass oers methods for evaluation of the probability density function, the cumulative distribution function and the inverse of the cumulative distribution. The constructors of the probability distribution classes require an argument of type prm(ProbDistr). This parameter object contains the name of the desired distribution class and an array of parameters for the distribution. For example, in the case of a normal distribution, there are two such parameters. The parameter class has nice functionality for interpreting menu input on the form Normal(2.1,0.4)
for specifying a normal distribution with mean 2.1 and standard deviation 0.4. See the man page of class prm(ProbDistr) for more details.
3.2.2 Example Here, we give an example on a simple program generating realizations of a random variable. #include main (int nargs, const char** args) { initDIFFPACK (nargs, args); RandGen rv_generator("UnifRandGenCongruential", 0.12345); prm(ProbDistr) rv_prm; Handle(ProbDistr) rv_distr;
// generator // seed
String prob_distr_descr; // user's description of probability distribution initFromCommandLineArg ("-pd", prob_distr_descr, "NormalDistr(0,1)"); rv_prm.scan (prob_distr_descr); // init the prm(ProbDistr) object rv_distr.rebind (createProbDistr(rv_prm)); // create new ProbDistr object
}
for (int i=1; i