text stringlengths 0 2.15k |
|---|
Modeling and Simulation in Python Version 3. 4. 3 Allen B. Downey Green Tea Press Needham, Massachusetts |
Copyright ©2017 Allen B. Downey. Green Tea Press 9 Washburn Ave Needham MA 02492 Permission is granted to copy, distribute, transmit and adapt this work under a Creative Commons Attribution-Non Commercial-Share Alike 4. 0 International License: http://modsimpy. com/license. If you are interested in distributing a comme... |
Contents Preface xi 0. 1 Can modeling be taught?.................... xi 0. 2 How much programming do I need?.............. xii 0. 3 How much math and science do I need?............ xiii 0. 4 Getting started......................... xiv 0. 5 Installing Python........................ xiv 0. 6 Copying my files................ |
iv CONTENTS 2. 7 Time Series............................ 15 2. 8 Plotting............................. 16 3 Iterative modeling 19 3. 1 Iterative modeling........................ 19 3. 2 More than one State object................... 20 3. 3 Documentation......................... 22 3. 4 Negative bikes...................... |
CONTENTS v 6. 4 Combining birth and death................... 48 7 Quadratic growth 51 7. 1 Quadratic growth........................ 51 7. 2 Equilibrium........................... 54 7. 3 Dysfunctions........................... 55 8 Prediction 59 8. 1 Generating projections..................... 59 8. 2 Comparing project... |
vi CONTENTS 11 Epidemiology 85 11. 1 The Freshman Plague...................... 85 11. 2 The SIR model......................... 86 11. 3 The SIR equations....................... 87 11. 4 Implementation......................... 88 11. 5 The update function...................... 89 11. 6 Running the simulation............... |
CONTENTS vii 15 Heat 123 15. 1 The coffee cooling problem................... 123 15. 2 Temperature and heat..................... 124 15. 3 Heat transfer.......................... 125 15. 4 Newton's law of cooling..................... 126 15. 5 Implementation......................... 127 16 Mixing 131 16. 1 Finding roots... |
viii CONTENTS 19. 4 Thermal behavior of a wall................... 163 20 Projectiles 165 20. 1 Newton's second law of motion................. 165 20. 2 Dropping pennies........................ 167 20. 3 Events.............................. 170 21 Air resistance 171 21. 1 Drag force............................ 171 21. 2... |
CONTENTS ix 25. 2 Moment of inertia........................ 202 25. 3 Teapots and turntables..................... 202 25. 4 Estimating friction....................... 207 26 Case studies 211 26. 1 Computational tools...................... 211 26. 2 Bungee jumping......................... 213 26. 3 Bungee dunk revisited... |
x CONTENTS |
Preface 0. 1 Can modeling be taught? The essential skills of modeling — abstraction, analysis, simulation, and vali-dation — are central in engineering, natural sciences, social sciences, medicine, and many other fields. Some students learn these skills implicitly, but in most schools they are not taught explicitly, and... |
xii Chapter 0 Preface 0. 2 How much programming do I need? If you have never programmed before, you should be able to read this book, understand it, and do the exercises. I will do my best to explain everything you need to know; in particular, I have chosen carefully the vocabulary I introduce, and I try to define each ... |
0. 3 How much math and science do I need? xiii Jupyter for reading, running, and developing code (see https://jupyter. org). These tools let you work on more interesting programs sooner, but there are some drawbacks: they can be hard to use, and it can be challenging to keep track of which library does what and how th... |
xiv Chapter 0 Preface When we get to mechanics, I assume you understand the relationship between position, velocity, and acceleration, and that you are familiar with Newton's laws of motion, especially the second law, which is often expressed as F=ma (force equals mass times acceleration). I think that's everything you... |
0. 6 Copying my files xv to install it, and if you have a version of Python already, Anaconda will not remove or modify it. Start at https://www. anaconda. com/download. Download the installer for your system and run it. You don't need administrative privileges to install Anaconda, so I recommend you run the installer a... |
xvi Chapter 0 Preface If you download the Zip file, you can skip the rest of this section, which explains how to use Git. The code for this book is available from https://github. com/Allen Downey/ Mod Sim Py, which is a Git repository. Git is a software tool that helps you keep track of the programs and other files that ... |
0. 7 Running Jupyter xvii The next step is to copy files from your repository on Git Hub to your computer; in Git vocabulary, this process is called cloning. Run this command: git clone https://github. com/Your Git Hub User Name/Mod Sim Py Of course, you should replace Your Git Hub User Name with your Git Hub user name.... |
xviii Chapter 0 Preface Contributor List If you have a suggestion or correction, send it to downey@allendowney. com. Or if you are a Git user, send me a pull request! If I make a change based on your feedback, I will add you to the contributor list, unless you ask to be omitted. If you include at least part of the sent... |
Chapter 1 Modeling This book is about modeling and simulation of physical systems. The following diagram shows what I mean by “modeling”: Abstraction Analysis Simulation Validation Measurement Model System Prediction Data Starting in the lower left, the system is something in the real world we are interested in. Often,... |
2 Chapter 1 Modeling in the form of diagrams and equations, which can be used for mathematical analysis. It can also be implemented in the form of a computer program, which can run simulations. The result of analysis and simulation might be a prediction about what the system will do, an explanation of why it behaves th... |
1. 1 The falling penny myth 3 Choosing the best model depends on what the model is for. It is usually a good idea to start with a simple model, even if it is likely to be too simple, and test whether it is good enough for its purpose. Then you can add features gradually, starting with the ones you expect to be most ess... |
4 Chapter 1 Modeling we get a velocity on impact of 86 m /s, which is about 190 miles per hour. That sounds like it could hurt. Of course, these results are not exact because the model is based on simplifi-cations. For example, we assume that gravity is constant. In fact, the force of gravity is different on different par... |
1. 2 Computation 5 As a first example, I'll show you how I computed the results from the previous section using Python. First I create a variable to represent acceleration. a = 9. 8 * meter / second**2 A variable is a name that corresponds to a value. In this example, the name is aand the value is the number 9. 8multipl... |
6 Chapter 1 Modeling This example demonstrates analysis and computation using Python. In the next chapter, we'll see an example of simulation. Before you go on, you might want to read the notebook for this chapter, chap01. ipynb, and work on the exercises. For instructions on downloading and running the code, see Secti... |
Chapter 2 Bike share This chapter presents a simple model of a bike share system and demonstrates the features of Python we'll use to develop simulations of real-world systems. Along the way, we'll make decisions about how to model the system. In the next chapter we'll review these decisions and gradually improve the m... |
8 Chapter 2 Bike share This line of code is an import statement that tells Python to read the file modsim. py and make the functions it defines available. Functions in the modsim. py library include sqrt, which we used in the previous section, and State, which we are using now. State creates a State object, which is a co... |
2. 2 Defining functions 9 bikeshare. olin = 9 bikeshare. wellesley = 3 Or we can use update operators,-=and +=, to subtract 1 from olin and add 1 to wellesley : bikeshare. olin-= 1 bikeshare. wellesley += 1 The result is the same either way, but the second version is more versatile. 2. 2 Defining functions So far we have... |
10 Chapter 2 Bike share When you define a function, it has no immediate effect. The body of the func-tion doesn't run until you callthe function. Here's how to call this function: bike_to_wellesley() When you call the function, it runs the statements in the body, which update the variables of the bikeshare object; you ca... |
2. 3 Print statements 11 2. 3 Print statements As you write more complicated programs, it is easy to lose track of what is going on. One of the most useful tools for debugging is the print statement, which displays text in the Jupyter notebook. Normally when Jupyter runs the code in a cell, it displays the value of the... |
12 Chapter 2 Bike share 2. 4 If statements The Mod Sim library provides a function called flip ; when you call it, you provide a value between 0 and 1; in this example, it's 0. 7: flip(0. 7) The result is one of two values: True with probability 0. 7 or False with probability 0. 3. If you run flip like this 100 times, ... |
2. 5 Parameters 13 Now we can use flip to simulate the arrival of students who want to borrow a bike. Suppose students arrive at the Olin station every 2 minutes, on average. In that case, the chance of an arrival during any one-minute period is 50%, and we can simulate it like this: if flip(0. 5): bike_to_wellesley() ... |
14 Chapter 2 Bike share So instead of putting the constant values 0. 5 and 0. 33 in step we can replace them with parameters. Parameters are variables whose values are set when a function is called. Here's a version of step that takes two parameters, p1andp2: def step(p1, p2): if flip(p1): bike_to_wellesley() if flip(p... |
2. 6 For loops 15 2. 6 For loops At some point you will get sick of running cells over and over. Fortunately, there is an easy way to repeat a chunk of code, the for loop. Here's an example: for i in range(4): bike_to_wellesley() The punctuation here should look familiar; the first line ends with a colon, and the lines ... |
16 Chapter 2 Bike share And we can add a value to a Time Series like this: results[0] = bikeshare. olin The number in brackets is the time stamp, also called a label. We can use a Time Series inside a for loop to store the results of the simulation: for i in range(10): step(0. 3, 0. 2) results[i] = bikeshare. olin Each... |
2. 8 Plotting 17 0 2 4 6 8 Time step (min)9. 09. 510. 010. 511. 0Number of bikes Olin-Wellesley Bikeshare Olin Figure 2. 1: Simulation of a bikeshare system showing number of bikes at Olin over time. label is an example of a keyword argument, so called because we provide a “keyword”, which is label in this case, along ... |
18 Chapter 2 Bike share and running the code, see Section 0. 4. |
Chapter 3 Iterative modeling To paraphrase two Georges, “All models are wrong, but some models are more wrong than others. ” In this chapter, I demonstrate the process we use to make models less wrong. As an example, we'll review the bikeshare model from the previous chapter, consider its strengths and weaknesses, and ... |
20 Chapter 3 Iterative modeling The model does not account for travel time from one bike station to another. The model does not check whether a bike is available, so it's possible for the number of bikes to be negative (as you might have noticed in some of your simulations). Some of these modeling decisions are bette... |
3. 2 More than one State object 21 When this function is called, it modifies bikeshare. As long as there is only one State object, that's fine, but what if there is more than one bike share system in the world? Or what if we want to run more than one simulation? This function would be more flexible if it took a State obje... |
22 Chapter 3 Iterative modeling 3. 3 Documentation Another problem with the code we have so far is that it contains no documen-tation. Documentation is text we add to a program to help other programmers read and understand it. It has no effect on the program when it runs. There are two forms of documentation, docstrings... |
3. 4 Negative bikes 23 For example, here is a version of bike_to_olin with a docstring and a com-ment. def bike_to_olin(state): """Move one bike from Wellesley to Olin. state: State object """ # We decrease one state variable and increase the # other, so the total number of bikes is unchanged. state. wellesley-= 1 stat... |
24 Chapter 3 Iterative modeling 3. 5 Comparison operators The version of bike_to_olin in the previous section uses the equals operator, ==, which compares two values and returns True if they are equal and False otherwise. It is easy to confuse the equals operators with the assignment operator, =, which assigns a value ... |
3. 6 Metrics 25 Operation Symbol Less than < Greater than > Less than or equal <= Greater than or equal >= Equal == Not equal != 3. 6 Metrics Getting back to the bike share system, at this point we have the ability to simulate the behavior of the system. Since the arrival of customers is random, the state of the system... |
26 Chapter 3 Iterative modeling def bike_to_olin(state): if state. wellesley == 0: state. wellesley_empty += 1 return state. wellesley-= 1 state. olin += 1 If a customer arrives at the Wellesley station and finds no bike available, bike_to_olin updates wellesley_empty which counts the number of un-happy customers. This ... |
Chapter 4 Sweeping parameters In the previous chapter we defined metrics that quantify the performance of bike sharing this system. In this chapter we see how those metrics depend on the parameters of the system, like the arrival rate of customers at bike stations. We also discuss a program development strategy, called ... |
28 Chapter 4 Sweeping parameters To write functions that return values, we can use a second form of the return statement, like this: def add_five(x): return x + 5 add_five takes a parameter, x, which could be any number. It computes x + 5 and returns the result. So if we run it like this, the result is 8: add_five(3) A... |
4. 2 Two kinds of parameters 29 4. 2 Two kinds of parameters This version of run_simulation always starts with the same initial condition, 10 bikes at Olin and 2 bikes at Wellesley, and the same values of p1,p2, and num_steps. Taken together, these five values are the parameters of the model, which are values that deter... |
30 Chapter 4 Sweeping parameters for i in range(4): bike_to_wellesley() range(4) creates a sequence of numbers from 0 to 3. Each time through the loop, the next number in the sequence gets assigned to the loop variable, i. range only works with integers; to get a sequence of non-integer values, we can use linspace, whi... |
4. 4 Sweeping parameters 31 4. 4 Sweeping parameters If we know the actual values of parameters like p1andp2, we can use them to make specific predictions, like how many bikes will be at Olin after one hour. But prediction is not the only goal; models like this are also used to explain why systems behave as they do and ... |
32 Chapter 4 Sweeping parameters And add values like this: for p1 in p1_array: state = run_simulation(p1, p2, num_steps) sweep[p1] = state. olin_empty The result is a Sweep Series that maps from each value of p1to the resulting number of unhappy customers. Then we can plot the results: plot(sweep, label= 'Olin ') 4. 5 ... |
4. 5 Incremental development 33 Sometimes you have to write extra code to generate visible output that you can check. This extra code is called scaffolding because you use it to build the program and then remove it when you are done. That might seem like a waste, but time you spend on scaffolding is almost always time y... |
34 Chapter 4 Sweeping parameters |
Chapter 5 World population In 1968 Paul Erlich published The Population Bomb, in which he predicted that world population would grow quickly during the 1970s, that agricultural production could not keep up, and that mass starvation in the next two decades was inevitable (see http://modsimpy. com/popbomb ). As someone w... |
36 Chapter 5 World population To read this data, we will use Pandas, which provides functions for working with data. The function we'll use is read_html, which can read a web page and extract data from any tables it contains. Before we can use it, we have to import it. You have already seen this import statement: from ... |
5. 1 World Population Data 37 The result, which is assigned to tables, is a sequence that contains one Data Frame for each table. A Data Frame is an object, defined by Pandas, that represents tabular data. To select a Data Frame from tables, we can use the bracket operator like this: table2 = tables[2] This line selects... |
38 Chapter 5 World population 1950 1960 1970 1980 1990 2000 2010 Year34567World population (billion)US Census UN DESA Figure 5. 1: Estimates of world population, 1950-2016. 5. 2 Plotting Now we can plot the estimates like this: plot(census, ': ', label= 'US Census ') plot(un, '--', label= 'UN DESA ') The next two lines... |
5. 3 Constant growth model 39 grow, fitting the model to the data we have so far, and then using the model to generate predictions. In the next few sections I demonstrate this process starting with simple models and gradually improving them. Although there is some curvature in the plotted estimates, it looks like world ... |
40 Chapter 5 World population The Mod Sim library also defines get_first_value and get_last_value, which we can use to compute total_growth : p_0 = get_first_value(census) p_end = get_last_value(census) total_growth = p_end-p_0 Finally, we can compute average annual growth. annual_growth = total_growth / elapsed_time Th... |
5. 4 Simulation 41 1950 1960 1970 1980 1990 2000 2010 Year34567World population (billion)Constant growth US Census UN DESA model Figure 5. 2: Estimates of world population, 1950-2016, and a constant growth model. the loop, the value of tis 2015, so the last label in results is 2016, which is what we want. Figure 5. 2 s... |
42 Chapter 5 World population |
Chapter 6 Modeling growth In the previous chapter we simulated a model of world population with con-stant growth. In this chapter we see if we can make a better model with growth proportional to the population. But first, we can improve the code from the previous chapter by encapsulating it in a function and using Syste... |
44 Chapter 6 Modeling growth In the population model, the only state variable is the population. System variables include the annual growth rate, the initial time and population, and the end time. Suppose we have the following variables, as computed in the previous chapter (assuming that census is a Series object): t_0... |
6. 2 Proportional growth model 45 When run_simulation1 runs, it stores the results in a Time Series and re-turns it. The following function plots the results along with the estimates census and un: def plot_results(census, un, timeseries, title): plot(census, ': ', label= 'US Census ') plot(un, '--', label= 'UN DESA ')... |
46 Chapter 6 Modeling growth On the other hand, if some fraction of the population dies each year, and some fraction gives birth, we can compute the net change in the population like this: def run_simulation2(system): results = Time Series() results[system. t_0] = system. p_0 for t in linrange(system. t_0, system. t_en... |
6. 3 Factoring out the update function 47 1950 1960 1970 1980 1990 2000 2010 Year345678World population (billion)Proportional model US Census UN DESA model Figure 6. 1: Estimates of world population, 1950-2016, and a proportional model. Rather than repeat identical code, we can separate the things that change from the ... |
48 Chapter 6 Modeling growth def run_simulation(system, update_func): results = Time Series() results[system. t_0] = system. p_0 for t in linrange(system. t_0, system. t_end): results[t+1] = update_func(results[t], t, system) return results This function demonstrates a feature we have not seen before: it takes a functi... |
6. 4 Combining birth and death 49 Here's the modified version of update_func1 : def update_func2(pop, t, system): net_growth = system. alpha * pop return pop + net_growth And here's how we run it: results = run_simulation(system, update_func2) Again, the result is the same as Figure 6. 1. Before you go on, you might wan... |
50 Chapter 6 Modeling growth |
Chapter 7 Quadratic growth In the previous chapter we developed a population model where net growth during each time step is proportional to the current population. This model seems more realistic than the constant growth model, but it does not fit the data as well. There are a few things we could try to improve the mod... |
52 Chapter 7 Quadratic growth 1950 1960 1970 1980 1990 2000 2010 Year34567World population (billion)Quadratic model US Census UN DESA model Figure 7. 1: Estimates of world population, 1950-2016, and a quadratic model. net_growth = system. alpha * pop + system. beta * pop**2 We can test that conjecture with a new update... |
7. 1 Quadratic growth 53 Of course, we should expect the quadratic model to fit better than the constant and proportional models because it has two parameters we can choose, where the other models have only one. In general, the more parameters you have to play with, the better you should expect the model to fit. But fitti... |
54 Chapter 7 Quadratic growth 0. 0 2. 5 5. 0 7. 5 10. 0 12. 5 15. 0 Population (billions)0. 02 0. 000. 020. 040. 060. 08Net growth (billions) Figure 7. 2: Net growth as a function of population. Above 14 billion, resources are so limited that the death rate exceeds the birth rate and net growth becomes negative. Just ... |
7. 3 Dysfunctions 55 In the context of population modeling, the quadratic model is more conven-tionally written like this: ∆p=rp(1-p/K) This is the same model; it's just a different way to parameterize it. Given αandβ, we can compute r=αand K=-α/β. In this version, it is easier to interpret the parameters: ris the maxim... |
56 Chapter 7 Quadratic growth def carrying_capacity(): K =-sys1. alpha / sys1. beta return K sys1 = System(alpha=0. 025, beta=-0. 0018) pop = carrying_capacity() print(pop) This version actually works, but it is not as versatile as it could be. If there are several System objects, this function can only work with one o... |
7. 3 Dysfunctions 57 # WRONG def carrying_capacity(system): K =-system. alpha / system. beta sys1 = System(alpha=0. 025, beta=-0. 0018) pop = carrying_capacity(sys1) print(pop) A function that doesn't have a return statement always returns a special value called None, so in this example the value of popis None. If you ... |
58 Chapter 7 Quadratic growth Before you go on, you might want to read the notebook for this chapter, chap07. ipynb, and work on the exercises. For instructions on downloading and running the code, see Section 0. 4. |
Chapter 8 Prediction In the previous chapter we developed a quadratic model of world population growth from 1950 to 2016. It is a simple model, but it fits the data well and the mechanisms it's based on are plausible. In this chapter we'll use the quadratic model to generate projections of future growth, and compare our... |
60 Chapter 8 Prediction t_0 = get_first_label(census) t_end = get_last_label(census) p_0 = census[t_0] Now we can create a System object: system = System(t_0=t_0, t_end=t_end, p_0=p_0, alpha=0. 025, beta=-0. 0018) And run the model: results = run_simulation(system, update_func_quad) We have already seen the results in ... |
8. 1 Generating projections 61 1950 2000 2050 2100 2150 2200 2250 Year2468101214World population (billion)World population projection US Census UN DESA model Figure 8. 1: Quadratic model of world population growth, with projection from 2016 to 2250. The quadratic model we've been working with is based on the assumption... |
62 Chapter 8 Prediction We should not take too seriously the idea that this model can estimate carrying capacity. But the predictions of a model can be credible even if the assump-tions of the model are not strictly true. For example, population growth might behave as if it is resource limited, even if the actual mecha... |
8. 2 Comparing projections 63 1960 1980 2000 2020 2040 2060 2080 2100 Year4681012World population (billion)World population projections US Census UN DESA model Figure 8. 2: Projections of world population generated by the U. S. Census Bureau, the United Nations, and our quadratic model. And compare our projections to t... |
64 Chapter 8 Prediction |
Chapter 9 Analysis In this chapter we express the models from previous chapters as difference equations and differential equations, solve the equations, and derive the func-tional forms of the solutions. We also discuss the complementary roles of mathematical analysis and simulation. 9. 1 Recurrence relations The populat... |
66 Chapter 9 Analysis In the case of constant growth we can see that x1=x0+c, andx2=x1+c. Combining these, we get x2=x0+ 2c, thenx3=x0+ 3c, and it is not hard to conclude that in general xn=x0+nc So if we want to know x100and we don't care about the other values, we can compute it with one multiplication and one additi... |
9. 2 Differential equations 67 If we define ∆ xto be the change in xfrom one time step to the next, we can write: ∆x=xn+1-xn=c If we define ∆ tto be the time step, which is one year in the example, we can write the rate of change per unit of time like this: ∆x ∆t=c This model is discrete, which means it is only defined at ... |
68 Chapter 9 Analysis Now we integrate both sides, yielding: lnx=αt+K where ln is the natural logarithm and Kis the constant of integration. Expo-nentiating both sides1, we have exp(ln(x)) = exp(αt+K) which we can rewrite x= exp(αt) exp(K) Since Kis an arbitrary constant, exp( K) is also an arbitrary constant, so we ca... |
9. 3 Analysis and simulation 69 But there are several things we can do with analysis that are harder or impos-sible with simulations: With analysis we can sometimes compute, exactly and efficiently, a value that we could only approximate, less efficiently, with simulation. For example, in Figure 7. 2, we can see that net ... |
70 Chapter 9 Analysis 9. 4 Analysis with Wolfram Alpha Until recently, most analysis was done by rubbing graphite on wood pulp3, a process that is laborious and error-prone. A useful alternative is symbolic computation. If you have used services like Wolfram Alpha, you have used symbolic computation. For example, if yo... |
9. 5 Analysis with Sym Py 71 Before we can use Sym Py, we have to import it: from sympy import * Sym Py defines many functions, and some of them conflict with functions de-fined in modsim and the other libraries we're using. To avoid these conflicts, I suggest that you do symbolic computation with Sym Py in a separate note... |
72 Chapter 9 Analysis 9. 6 Differential equations in Sym Py Sym Py provides a function, diff, that can differentiate a function. We can apply it to f(t) like this: dfdt = diff(f(t), t) The result is a Symbol that represents the derivative of fwith respect to t. But again, Sym Py doesn't try to compute the derivative yet.... |
9. 7 Solving the quadratic growth model 73 9. 7 Solving the quadratic growth model In the notebook for this chapter, you will see how to use the same tools to solve the quadratic growth model with parameters rand K. The general solution is f(t) =Kexp(C1K+rt) exp(C1K+rt)-1 To get the particular solution where f(0) =p0, ... |
74 Chapter 9 Analysis Continuous (differential equation) Constant linear:x(t) =p0+αt Proportional exponential: x(t) =p0exp(αt) Quadratic logistic:x(t) =K/(1 +Aexp(-rt)) What I've been calling the constant growth model is more commonly called “linear growth” because the solution is a line. Similarly, what I've called pro... |
Chapter 10 Case studies This chapter reviews the computational patterns we have seen so far and presents exercises where you can apply them. 10. 1 Computational tools In Chapter 1 we used Pint to define units and perform calculations with units: meter = UNITS. meter second = UNITS. second a = 9. 8 * meter / second**2 In... |
76 Chapter 10 Case studies def step(p1, p2): if flip(p1): bike_to_wellesley() if flip(p2): bike_to_olin() We used a forloop with the range function to execute the body of the loop a specified number of times. for i in range(4): step(p1, p2) We learned to create a Time Series object and use it to store the value of a sta... |
10. 1 Computational tools 77 def run_simulation(p1, p2, num_steps): state = State(olin=10, wellesley=2, olin_empty=0, wellesley_empty=0) for i in range(num_steps): step(state, p1, p2) return state This version of run_simulation returns the final value of state, which con-tains metrics we can use to measure the performan... |
78 Chapter 10 Case studies This version takes a System object as a parameter, and an update function. Instead of returning the final state of the system, it returns a Time Series that contains the state as it changes over time. The update function takes the current state of the system, the time, and the System object as... |
10. 2 Under the hood 79 indexcolumns census 1950 20162557628654... prbun Data Frame name = 'census' 1950 20162557628654... Series valuesindex Figure 10. 1: The elements that make up a Data Frame and a Series. Each Data Frame contains three objects: index is a sequence of labels for the rows, columns is a sequence of la... |
80 Chapter 10 Case studies The type of table2. columns is Index, which might seem strange, because “the” index is the sequence of row labels. But the sequence of column labels is also a kind of index. The type of table2. values isndarray, which is the primary array type pro-vided by Num Py; the name indicates that the ... |
10. 3 One queue or two? 81 λμλμ μλμ μ Figure 10. 2: One queue, one server (left), one queue, two servers (middle), two queues, two servers (right). the study of systems that involve waiting in lines, also known as “queues”. Suppose you are designing the checkout area for a new store. There is enough room in the store f... |
82 Chapter 10 Case studies 5 minutes, but a few take substantially longer, which is probably not a bad model of the distribution in real stores. Now we're ready to get started. In the repository for this book, you'll find a notebook called queue. ipynb that contains some code to get you started and instructions. As alwa... |
End of preview. Expand in Data Studio
README.md exists but content is empty.
- Downloads last month
- 6