Chapter 20: Simulation Re-Visited Spreadsheet-Based Decision Support Systems Prof. Name Position (123) 456-7890 University Name.

Slides:



Advertisements
Similar presentations
Exercise 7.5 (p. 343) Consider the hotel occupancy data in Table 6.4 of Chapter 6 (p. 297)
Advertisements

Sta220 - Statistics Mr. Smith Room 310 Class #14.
Week 10.  Protecting Code  Protecting Worksheets  Error Handling  Charts.
Templates and Styles Excel Advanced. Templates are pre- designed and formatted spreadsheets –They provide consistency of layout/structure –They.
Tutorial 12: Enhancing Excel with Visual Basic for Applications
Simulation Operations -- Prof. Juran.
Session 7a. Decision Models -- Prof. Juran2 Overview Monte Carlo Simulation –Basic concepts and history Excel Tricks –RAND(), IF, Boolean Crystal Ball.
Overview Reference parameters Documenting functions A game of craps. Design, code, test and document.
1 A Balanced Introduction to Computer Science, 2/E David Reed, Creighton University ©2008 Pearson Prentice Hall ISBN Chapter 17 JavaScript.
Chapter 20: Simulation Re-Visited Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University Name.
Chapter 6: Pivot Tables Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University Name.
Probability Distributions Finite Random Variables.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 12 – Craps Game Application: Introducing Random.
 The Law of Large Numbers – Read the preface to Chapter 7 on page 388 and be prepared to summarize the Law of Large Numbers.
Monté Carlo Simulation MGS 3100 – Chapter 9. Simulation Defined A computer-based model used to run experiments on a real system.  Typically done on a.
Chapter 12: Recording Macros Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University Name.
Introduction to Excel 2007 Part 3: Bar Graphs and Histograms Psych 209.
Stevenson and Ozgur First Edition Introduction to Management Science with Spreadsheets McGraw-Hill/Irwin Copyright © 2007 by The McGraw-Hill Companies,
Chapter 6: Pivot Tables Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University Name.
Simulation.
11 Chapter 4 LOOPS AND FILES. 22 THE INCREMENT AND DECREMENT OPERATORS To increment a variable means to increase its value by one. To decrement a variable.
Exploring Microsoft Excel 2002 Chapter 8 Chapter 8 Automating Repetitive Tasks: Macros and Visual Basic for Applications By Robert T. Grauer Maryann Barber.
Spreadsheet-Based Decision Support Systems
Chapter 9: Simulation Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University Name.
Chapter 7: Statistical Analysis Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University Name.
Chapter 10: Working with Large Data Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University Name.
IE 212: Computational Methods for Industrial Engineering
Spreadsheet-Based Decision Support Systems Chapter 22:
Chapter 11: Introduction to the Visual Basic Environment Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University.
Chapter 17: Arrays Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University Name.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 6 Value- Returning Functions and Modules.
Chapter 19: Solver Re-Visited Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University Name.
Chapter 4 Probability Distributions
Stephen P. Carl - CS 2421 Recursion Reading : Chapter 4.
Contemporary Engineering Economics, 6 th edition Park Copyright © 2016 by Pearson Education, Inc. All Rights Reserved Risk Simulation Lecture No. 40 Chapter.
Chapter 12: Recording Macros Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University Name.
Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 7 The Game Loop and Animation Starting Out with Games & Graphics.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 4 Loops.
Chapter 19: The Solver Re-Visited Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University Name.
Review for Exam2 Key Ideas 1. Key Ideas: Boolean Operators (2 > 3) || (3 < 29.3) A.True B.False C.Impossible to determine (22 > 3) && (3 > 29.3) A.True.
30/10/ Iteration Loops Do While (condition is true) … Loop.
ESD.70J Engineering Economy Module - Session 21 ESD.70J Engineering Economy Fall 2006 Session Two Alex Fadeev - Link for this PPT:
Chapter 16: Programming Structures Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University Name.
Chapter 3 MATLAB Fundamentals Introduction to MATLAB Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
McGraw-Hill/Irwin © 2006 The McGraw-Hill Companies, Inc., All Rights Reserved. 1.
Copyright ©2011 Pearson Education, Inc. publishing as Prentice Hall 5-1 Business Statistics: A Decision-Making Approach 8 th Edition Chapter 5 Discrete.
A Balanced Introduction to Computer Science, 3/E David Reed, Creighton University ©2011 Pearson Prentice Hall ISBN Chapter 17 JavaScript.
Chapter 16: Programming Structures Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University Name.
Risk Analysis Simulate a scenario of possible input values that could occur and observe key impacts Pick many input scenarios according to their likelihood.
Chapter 3: Referencing and Names Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University Name.
Computer Simulation. The Essence of Computer Simulation A stochastic system is a system that evolves over time according to one or more probability distributions.
Chapter 11: Introduction to VBA Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University Name.
Copyright © 2002 W. A. Tucker1 Chapter 9 Lecture Notes Bill Tucker Austin Community College COSC 1315.
Risk Analysis Simulate a scenario of possible input values that could occur and observe key financial impacts Pick many different input scenarios according.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Craps Game Application Introducing Random-Number Generation and Enum.
Visual Basic Objects / Properties / Methods PropertyAdjective ObjectNoun Part of the application Attribute MethodVerb Action to do something.
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
Chapter 3: Referencing and Names Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University Name.
Chapter 10: Working with Large Data Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University Name.
Chapter 15: Sub Procedures and Function Procedures Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University.
Probability and Statistics 12/11/2015. Statistics Review/ Excel: Objectives Be able to find the mean, median, mode and standard deviation for a set of.
Introduction Imagine the process for testing a new design for a propulsion system on the International Space Station. The project engineers wouldn’t perform.
Chapter 9: Value-Returning Functions
Spreadsheet-Based Decision Support Systems
Exploring Microsoft Excel
Iteration: Beyond the Basic PERFORM
Loops CIS 40 – Introduction to Programming in Python
Topics Introduction to Value-returning Functions: Generating Random Numbers Writing Your Own Value-Returning Functions The math Module Storing Functions.
Spreadsheet-Based Decision Support Systems
Spreadsheet-Based Decision Support Systems
Presentation transcript:

Chapter 20: Simulation Re-Visited Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University Name

2 Overview  20.1 Introduction  20.2 Review of Chapter 9  20.3 Simulation with VBA  20.4 Applications  20.5 Summary

3 Introduction  Random numbers and distributions in VBA  Performing dynamic simulation runs using VBA  Creating simulation animation using VBA  Dynamic analysis using histograms and VBA  A full dynamic simulation application with animation

4 Review of Chapter 9  Defining Simulation  Working with Distributions

5 Review of Chapter 9  In Chapter 9 we defined simulation and discussed how to apply it in an Excel spreadsheet.  In this chapter we will show some parallel functionality that can be accomplished using VBA.  First let us review how simulation is defined and how to work with distributions when using simulation in Excel.

6 Defining Simulation  Simulation is a software-generated display of a process which one wants to observe.  Simulation is a useful tool for analyzing how a set of actions functions under different parameters or settings.  It benefits from trial and error without the cost of materials, labor and time that would be necessary to observe the same results on the original process.  Simulation differs from optimization in that it does not aim to find the best result for some given inputs.  Rather, it seeks to find the best inputs given a desired result by running several trials of a particular scenario.

7 Working with Distributions  Simulation is useful because it can handle variability of parameters in a model.  That is, we cannot know many settings in a process with full certainty; there is usually some range of numbers into which values fall but we can not know exact numbers.  Thus, we use distributions and random numbers.

8 Random Numbers and Distributions  We use the RAND function in Excel to generate random numbers.  The general distribution functions in Excel have the following format =NORMDIST(x_value, mean, std_dev, cumulative_value)  The x-value is the number for which we are calculating the distribution.  The cumulative value is TRUE if we use the cumulative distribution function (cdf), or FALSE if we use the probability mass function (pmf).

9 Rand Num and Dist (cont)  To generate a random number within a given distribution, we must use the inverse distribution functions.  The format of these inverse distribution functions is the following –=NORMINV(probability, mean, std_dev)  The probability parameter is a number between 0 and 1 associated with the given distribution.

10 Rand Num and Dist (cont)  We will use the RAND function as our value for this parameter to generate some numbers between 0 and 1.  For example, to generate ten random numbers in the Normal distribution, we would follow the format. =NORMINV(RAND(), mean, std dev)

11 Simulation with VBA  Random Numbers and Distributions  Making Runs and Collecting Data  Animation  Analysis

12 Simulation with VBA  There is no particular “simulation code” in VBA.  We will instead be defining a set of functions we have already learned, and some new ones, which we can use when programming a simulation application.

13 VBA Rand Num and Dist  To generate random numbers in VBA, we will use the Rnd function as discussed in Chapter 14.  This function can be manipulated to generate a random number between a lower bound and upper bound using the following formula. (upperbound - lowerbound + 1) * Rnd + lowerbound  You can use this equation with the Int function to ensure this random number is an integer.

14 VBA Rand Num and Dist (cont)  To generate a random number in a particular distribution in VBA, we will usually have to use the distribution formulas from Excel. –To do this, we can use the same techniques from Chapter 9 along with the Formula or FormulaR1C1 methods in VBA. –We can also use some of the Worksheet Functions available with the Application object to use some of Excel’s inverse distribution functions.  Since the Log function is also a defined VBA function, we can use it with the Rnd function to generate random numbers from the Exponential distribution. –To do this we just take the logarithm of the random number. Log(Rnd())

15 VBA Rand Num and Dist (cont)  To specify a particular Exponential distribution from which the random number is being generated, we simply multiply the mean by the previous function. Mean * Log(Rnd())  For example, the below code generates 10 random numbers from an Exponential distribution with a user-specified mean. Dim mean As Double mean = InputBox(“Please enter mean of your Exponential distribution.”) For i = 1 to 10 Range(“A1”).Offset(i,0).Value = mean * Log(Rnd()) Next i

16 Making Runs and Collecting Data  As seen in the code above, we can use For, Next loops to generate multiple data values.  We may ask the user to specify how much data they want to use for the simulation.  We may also perform some calculations with the random data we generate.  To simulate a system, we first prompt the user for the number of runs they want to perform. –A run is a single execution of a series of actions which model the system.

17 Runs and Data (cont)  We then need to determine if we will be storing this data and the results of the specified calculations in arrays or in a spreadsheet.  When multiple calculations of data are needed, which do not require Excel functions, arrays can be easier structures to work with. –They are easier in the since that range names do not need to be defined and extra worksheet space does not need to be used.  If however, some other Excel functions, such as distribution functions, will need to be used with the generated data, it may be better to store this data in a worksheet.

18 Runs and Data (cont)  For example, say there is a system which takes as input a number from an Exponential distribution with lambda 5, and outputs the square of that number. Dim runs As Integer, Input() As Double, Output() As Double runs = InputBox(“Please enter the number of runs for this simulation”) ReDim Input(runs) ReDim Output(runs) For i = 1 to runs Input(i) = 5 * Log(Rnd()) Output(i) = (Input(i)) ^ 2 Range(“A1”).Offset(i,0).Value = Output(i) Next i

19 Runs and Data (cont)  The input and/or output of a simulation can then be stored in a spreadsheet for the user to see.  For some simulation models, it will be unnecessary to store or show the user the input values. –Analysis of the output is usually of most importance to the user.

20 Animation  Two other VBA methods, both of the Application object, should be used when creating a simulation program: –ScreenUpdating method –Wait method  The ScreenUpdating method should be set to False before the simulation runs begin. –This reduces screen flickering while the program runs and increase efficiency (decrease running time) if the screen is not updated after every action in the code.  It should be set to True when the runs have been completed.

21 Animation (cont)  The Wait method can be used to create some animation of the simulation.  For example, if we are simulating a production line, we may have several parts being received and shipped from different work stations.  Each of these work stations may have a different distribution for processing time  We can use the functions discussed above to create a list of generated time values at which a part is finished at each station.

22 Animation (cont)  If Work Station 1 processes parts at an Exponential rate of 2, then we could use the following code to generate times at which parts would leave Work Station 1. ReDim WorkStation1(runs) As Double For i = 1 to runs WorkStation1(i) = 2 * Log(Rnd()) Next i  We can then create a cumulative time array using the following. ReDim CumWork1(runs) As Double WorkStation(0) = 0 For i = 1 to runs CumWork1(i) = WorkStation1(i) + WorkStation1(i – 1) Next i

23 Animation (cont)  Now we can run a loop to show that a product leaves Work Station 1 at each time value.  To do this, we may highlight some cell which changes position, or disappears and reappears, every time this action occurs.  To create this action we would pick some range to highlight (by changing the color with the Interior property) and each time we loop through a run, we un-highlight this cell and highlight the next cell (using the Offset property).  However, to ensure that the user sees a delay between these actions, we use the Wait method.

24 Animation (cont) For i = 1 to runs Application.ScreenUpdating = False Range(“Time”).Value = CumWork1(i) Range(“A1”).Offset(i-1, 0).Interior.ColorIndex = 0 Range(“A1”).Offset(i, 0).Interior.ColorIndex = 5 Application.Wait(Now + Time(0:0:03)) Application.ScreenUpdating = True Next i  Running this code will create an animated simulation which appears to move a product from one cell to the next at each iteration.  This idea can be modified to create an animation that better reflects any particular system.

25 Analysis  The motivation for using simulation is of course to perform some analysis on a system.  There are several ways to analyze the results of a simulation.  For the output produced, a graph can be created, the maximum or minimum can be found, etc.  We can use Chart objects to accomplish these analsyis.  We can also use the Analysis Toolpack with features such as Histograms.

26 Histograms  Histograms calculate the number of occurrences of values in a particular interval.  There are four main parts to creating a histogram. –Input = range of data on the worksheet. –Bins = the intervals into which values can be counted; they can be defined by the user or can be evenly distributed among the data. –Output = range where the frequency calculations for each bin will be printed. –Charts options = simple chart, cumulative percentages for each bin value, and/or a Pareto organization of the chart.

27 Histogram (cont)

28 Histogram (cont)  To create the histogram using VBA, we will use the Application object and the Run method. –The Run method is used to specify the Add-In file from which we will be running the analysis. –The arguments for this method include all of the parameters we have previously seen. Application.Run "ATPVBAEN.XLA!Histogram", Worksheets("Histograms").Range("Input"), _ Worksheets("Histograms").Range("Output"),, True, True, True, False

29 Histogram (cont)  Note that if you run this code multiple times, you will have multiple charts created.  For better code efficiency, we recommend creating a histogram in Excel first (as we have seen with some Chart applications) and then modifying your code to have the chart option parameters all set to False.  You will also see a message warning you that some data will be overwritten if you repeatedly print the output of the histogram to the same cell.  We therefore recommend clearing this output range of cells before calling the histogram function each time.

30 Histogram (cont)  Other analysis from the Analysis Toolpack may be useful to your application.  To discover the particular code and parameters for each analysis tool, record a macro first in Excel (most analysis tools will still run an extension of the ATPVBAEN.XLA file).

31 Applications  Game of Craps Revisited

32 Description  In the Craps game simulation in Chapter 9, we determined how often a player wins or loses in up to 5 rolls of the dice.  In the game of Craps, a player rolls two dice.  If the first roll yields a sum of 2, 3, or 12, the player loses.  If the first roll yields a sum of 7 or 11, the player wins.  Otherwise, the player continues rolling the dice until she matches the value thrown on the first roll or rolls a sum of 7.  Rolling a match for the first roll wins the game, rolling a value of 7 before a match loses the game.

33 VBA Simulation  VBA allows us to perform more runs and include some animation.  We will show the user the value of the dice they roll on each run.  Each run is defined to be one play of the game with one to five rolls of the dice.  After each roll of the dice, we evaluate the value of the sum to determine if they continue rolling or stop the game by winning or losing.

34 Figure 20.1  The spreadsheet

35 Figure 20.2  Simulation runs and statistical analysis

36 Figure 20.3  The initial code

37 Figure 20.4  The simulation code

38 Figure 20.5  Two functions used in each roll

39 Figure 20.6  Stored dice images

40 Figure 20.7  The statistical analysis and create histogram code

41 Application Conclusion  The application is now complete.  We can start the code by assigning the Start procedure to the “START” button.

42 Summary  Simulation is a modeling tool which we use to imitate a real-world process in order to understand system behavior.  To generate random numbers in VBA, we use the Rnd function.  We can use the Log function with the Rnd function in VBA to generate random numbers from the exponential distribution.  The Application.WorksheetFunction method contains several inverse functions which we can use directly in VBA.  We can use the Wait method to create some animation of the simulation.  We can use the Analysis Toolpack to perform many types of analysis, including histograms.  Histograms calculate the number of occurrences of values in a particular interval.

43 Additional Links  (place links here)