22
Introduction to Scientific Modeling CS 365 Fall, 2012 Modeling and Simulation Stephanie Forrest [email protected] 505-277-7104

Introduction to Scientific Modelingforrest/classes/cs365/lectures/simulation.pdf– Stan Ulam and Nicholas Metropolis "• Use random numbers and probability to solve problems" –

  • Upload
    others

  • View
    2

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Introduction to Scientific Modelingforrest/classes/cs365/lectures/simulation.pdf– Stan Ulam and Nicholas Metropolis "• Use random numbers and probability to solve problems" –

Introduction to Scientific Modeling CS 365 Fall, 2012

Modeling and Simulation

Stephanie Forrest

[email protected]

"

Page 2: Introduction to Scientific Modelingforrest/classes/cs365/lectures/simulation.pdf– Stan Ulam and Nicholas Metropolis "• Use random numbers and probability to solve problems" –

Modeling and Simulation"

•  The activities associated with constructing models of real world systems and simulating them on a computer."

•  Three elements:"–  The real system"–  The model"–  The computer"

•  Modeling refers to the relationship between the real system and the model."

•  Simulation refers to the relationship between the computer and the model."

Page 3: Introduction to Scientific Modelingforrest/classes/cs365/lectures/simulation.pdf– Stan Ulam and Nicholas Metropolis "• Use random numbers and probability to solve problems" –

Model Validation "

•  Comparing the behavior of the model to that of the real system"•  Replicative validity"

–  Model matches data already acquired from real system"•  Predictive validity"

–  Model matches data before they are acquired from real system"•  Structural validity"

–  Model reflects the “way” in which the real system operates to produce behavior"

•  Homomorphic maps"

Page 4: Introduction to Scientific Modelingforrest/classes/cs365/lectures/simulation.pdf– Stan Ulam and Nicholas Metropolis "• Use random numbers and probability to solve problems" –

Simulation Relation (Verification)"

•  The faithfulness with which the computer realizes the model."–  Verifying the correctness of the program realization of the model"

•  Does the computer faithfully generate model behavior?"•  Or, does it generate misleading outputs, artifacts of the

computer implementation rather than characteristics of the model itself?"

•  Potential confusion between faults of the model with faults of the program."

Page 5: Introduction to Scientific Modelingforrest/classes/cs365/lectures/simulation.pdf– Stan Ulam and Nicholas Metropolis "• Use random numbers and probability to solve problems" –

The Simulation Process"

•  Model description"•  Simplification"

–  Drop components, descriptive variables and/or interaction terms"–  Replace deterministically controlled variables by random variables"–  Group components together in aggregate blocks"

•  Validation"•  Simulation"

–  Strategies (various approaches taken in sequencing events and activities)"–  Mechanics (how random variables are generated, how differential equations

are integrated)"•  Exploration"

Page 6: Introduction to Scientific Modelingforrest/classes/cs365/lectures/simulation.pdf– Stan Ulam and Nicholas Metropolis "• Use random numbers and probability to solve problems" –

Simulation Strategies Discrete Event Simulation"

•  System is represented as a chronological sequence of events. "•  Each event occurs at an instant in time and marks a change of

state in the system"•  Example:"

–  Elevator simulation"–  Event: "level 6 button pressed" "–  Resulting system state: "lift moving" and eventually (unless one chooses to

simulate the failure of the lift) "lift at level 6"."•  Common implementation:"

–  Queue of pending events"–  Simulation loop:"

•  Remove first event from queue"•  Process event, possibly adding new events to end of queue"•  Example: A set of bank tellers and a customer queue"

Page 7: Introduction to Scientific Modelingforrest/classes/cs365/lectures/simulation.pdf– Stan Ulam and Nicholas Metropolis "• Use random numbers and probability to solve problems" –

Components of a Discrete Event Simulation"

•  Clock"–  Keeps track of current simulation

time"–  Needs to be synchronized with

event processing (e.g., in bank example, not all customers are necessarily processed in unit time)"

•  Event lists (queue)"•  Random number generator"•  Statistics"•  Stopping condition"•  Simulation engine logic"

Initialize Ending Condition to FALSE"Initialize system state variables."Initialize Clock"Schedule an initial event (i.e., put some

initial event into the Events List)."

While (~StoppingCondition){""Set clock to next event time.""Do next event and remove from the EventList.""Update statistics."

}"Generate statistical report""

Page 8: Introduction to Scientific Modelingforrest/classes/cs365/lectures/simulation.pdf– Stan Ulam and Nicholas Metropolis "• Use random numbers and probability to solve problems" –

Discrete Time Simulation"

•  Outer loop of simulation is one (n) clock ticks."

•  All events for that time period are processed before clock is advanced."

•  Example: Cellular Automata simulation"

Initialize StoppingCondition"Initialize system state variables."Initialize Clock""For (T = 1 to StoppingCondition){""Run simulation logic""Update statistics""Advance Clock"

}"Generate statistical report"

Page 9: Introduction to Scientific Modelingforrest/classes/cs365/lectures/simulation.pdf– Stan Ulam and Nicholas Metropolis "• Use random numbers and probability to solve problems" –

How would a discrete event cellular automata simulation be organized?"

Page 10: Introduction to Scientific Modelingforrest/classes/cs365/lectures/simulation.pdf– Stan Ulam and Nicholas Metropolis "• Use random numbers and probability to solve problems" –

Monte Carlo Simulation"

•  Invented in 1940s at Los Alamos"–  Stan Ulam and Nicholas Metropolis "

•  Use random numbers and probability to solve problems"–  Iteratively evaluate a deterministic model using sets of random numbers as

inputs. "–  A sampling method because the inputs are randomly generated from

probability distributions to simulate the process of sampling from an actual population. "

–  Choose a distribution for the inputs that most closely matches data we already have, or best represents our current state of knowledge. "

–  The data generated from the simulation can be represented as probability distributions (or histograms) or converted to error bars, reliability predictions, tolerance zones, and confidence intervals. "

•  Used when the model is complex, nonlinear, or involves more than just a couple of uncertain parameters. "

“After examining the possible histories of only a few thousand, one will have a good sample and an approximate answer to the problem.”"

Page 11: Introduction to Scientific Modelingforrest/classes/cs365/lectures/simulation.pdf– Stan Ulam and Nicholas Metropolis "• Use random numbers and probability to solve problems" –

Stan Ulam"Ulam, who had been advised, during his convalescence, to avoid strenuous mental activity, amused himself by playing solitaire. He could not resist a question: What were the chances that a Canfield solitaire with fifty-two cards will play out successfully? After spending a lot of time trying to estimate them by pure combinatorial calculations, he recalls, “I wondered whether a more practical method than ‘abstract thinking’ might not be to lay it out say one hundred times and simply observe and count the number of successful plays.” ""This is intellectually surprising, and if not exactly humiliating, it gives one a feeling of modesty about the limits of rational or traditional thinking,” he added. ""“It occurred to me then that this could be equally true of processes involving branching of events, as in the production and further multiplication of neutrons in some kind of material containing uranium or other fissile elements,”""At each stage of the process, there are many possibilities determining the fate of the neutron … The elementary probabilities for each of these possibilities are individually known … but the problem is to know what a succession and branching of perhaps hundreds of thousands or millions will do.” """

G. Dyson Turing’s Cathedral, pp. 190 - 191"

Page 12: Introduction to Scientific Modelingforrest/classes/cs365/lectures/simulation.pdf– Stan Ulam and Nicholas Metropolis "• Use random numbers and probability to solve problems" –

Stanislaw Ulam (1909 – 1984)"

Page 13: Introduction to Scientific Modelingforrest/classes/cs365/lectures/simulation.pdf– Stan Ulam and Nicholas Metropolis "• Use random numbers and probability to solve problems" –

Principal of stochastic uncertainty propagation (the basic principle behind Monte Carlo simulation)"

•  Create a parametric model, y = f(x1, x2, ..., xq)."

•  Generate a set of random inputs, xi1, xi2, ..., xiq."

•  Evaluate the model and store the results as yi."

•  Repeat steps 2 and 3 for i = 1 to n."

•  Analyze the results using histograms, summary statistics, confidence intervals, etc."

http://www.vertex42.com/ExcelArticles/mc/MonteCarloSimulation.html

Page 14: Introduction to Scientific Modelingforrest/classes/cs365/lectures/simulation.pdf– Stan Ulam and Nicholas Metropolis "• Use random numbers and probability to solve problems" –

Examples of Monte Carlo Simulation"

•  Graphics, particularly for ray tracing; "•  Modeling light transport in multi-layered tissues (MCML)"•  Finance (pricing derivatives)"•  Reliability engineering"•  Simulated annealing for protein structure prediction"•  Semiconductor device research, to model the transport of

current carriers"•  Environmental science (modeling contaminant behavior)"•  Statistical physics"•  Molecular modeling as an alternative for computational

molecular dynamics."•  Nuclear and particle physics codes"

Page 15: Introduction to Scientific Modelingforrest/classes/cs365/lectures/simulation.pdf– Stan Ulam and Nicholas Metropolis "• Use random numbers and probability to solve problems" –

Generating Random Numbers"

•  “Anyone who considers arithmetical methods of producing random digits is, of course, in a state of sin.” (John von Neumann “Various techniques used in connection with random digits", Applied Mathematics Series, no. 12, 36-38 1951)."

•  Psuedo-random number generator (PRNG)"–  An algorithm to generate a sequence of numbers that

approximates the properties of random numbers. "–  The sequence is not truly random in that it is completely

determined by a relatively small set of initial values, called the PRNG's state."

–  The algorithm is initialized with an a SEED."–  Advantage: replicatability"–  Convention: Produce sequences that are uniformly distributed by

any of several tests"

Page 16: Introduction to Scientific Modelingforrest/classes/cs365/lectures/simulation.pdf– Stan Ulam and Nicholas Metropolis "• Use random numbers and probability to solve problems" –

Issues"

•  Periodicity:"–  How long before the sequence repeats?"–  Can I predict the periodicity?"–  If PRNG state contains n bits, then in principle periodicity

can’t be more than 2n-1"–  Does the period of the sequence depend on the initial seed?"

•  How uniform is the distribution of numbers, e.g., in [0,1)?"•  Are successive values correlated?"

–  E.g., 1 1 1 2 2 2 3 3 3 …"•  Distances between where certain values occur are distributed

differently than for a random sequence"•  Choosing seeds"

Page 17: Introduction to Scientific Modelingforrest/classes/cs365/lectures/simulation.pdf– Stan Ulam and Nicholas Metropolis "• Use random numbers and probability to solve problems" –

Types of Pseudo-Random Number Generators"

•  Linear congruential generators"•  Lagged Fibonacci generators"•  Linear feedback shift registers"

Page 18: Introduction to Scientific Modelingforrest/classes/cs365/lectures/simulation.pdf– Stan Ulam and Nicholas Metropolis "• Use random numbers and probability to solve problems" –

A Linear Congruential Method"

•  r0 is the initial seed"•  Produces a sequence of 10 pseudorandom numbers before

repeating: 10, 5, 3, 0, 1, 8, 2, 4, 7, 6, 10"•  To generate numbers in [0,1), simply divide by modulus:"€

r0 =10rn = (7rn−1 +1) mod 11, for n > 0

1011, 311, 011, 111, 811, 211, 411, 711, 611,1011

Page 19: Introduction to Scientific Modelingforrest/classes/cs365/lectures/simulation.pdf– Stan Ulam and Nicholas Metropolis "• Use random numbers and probability to solve problems" –

More generally Linear Congruential Method"

•  Consider:"–  Multiplier = 5"–  Modulus = 11"–  Increment = 1"–  Seed = 10"

r0 = seedrn = (multiplier × rn−1 + increment) modulus, for n > 0

Page 20: Introduction to Scientific Modelingforrest/classes/cs365/lectures/simulation.pdf– Stan Ulam and Nicholas Metropolis "• Use random numbers and probability to solve problems" –

More generally The Linear Congruential Method"

•  Consider:"–  Multiplier = 5"–  Modulus = 11"–  Increment = 1"–  Seed = 10"

•  Sequence repeats after 5 numbers:"–  10, 7, 3, 5, 4, 10"

•  A better choice: "–  Modulus = 231-1, Multiplier = 16,807, Increment = 0"–  Produces a sequence of 231 - 2 numbers"

r0 = seedrn = (multiplier × rn−1 + increment) mod modulus, for n > 0

Page 21: Introduction to Scientific Modelingforrest/classes/cs365/lectures/simulation.pdf– Stan Ulam and Nicholas Metropolis "• Use random numbers and probability to solve problems" –

Example of a Poor PSRG"

•  3-dimensional plot of 100,000 points generated with this PSRG."

•  All points fall in 15 2-dimensional planes"

•  RANDU "–  Widespread in 1970s"–  Non-prime modulus and multiplier

chosen to make calculations easy"–  Many simulations were based on

RANDU"

V j+1 ≡ 65539 ×V j mod231,with V0 odd

Page 22: Introduction to Scientific Modelingforrest/classes/cs365/lectures/simulation.pdf– Stan Ulam and Nicholas Metropolis "• Use random numbers and probability to solve problems" –

What PNRG Does MatLab Use?"

•  Lagged Fibonacci generator"–  Tricky to initialize, not as well grounded theoretically (statistical arguments)"

•  Fibonacci series:"

•  Operators:"–  OP = binary operators (+,-,*,/,XOR)"–  << (left shift)"–  ^ (bitwise exclusive OR)"

sn = sn−1 + sn−2

sn = sn− j OP sn−k mod modulus, 0 < j < k