1 00:00:00 --> 00:00:00 2 00:00:00 --> 00:00:02 The following content is provided under a 3 00:00:02 --> 00:00:03 Creative Commons license. 4 00:00:03 --> 00:00:06 Your support helps MIT OpenCourseWare continue to 5 00:00:06 --> 00:00:10 offer high quality educational resources for free. 6 00:00:10 --> 00:00:13 To make a donation or view additional materials from 7 00:00:13 --> 00:00:17 hundreds of MIT courses, visit MIT OpenCourseWare 8 00:00:17 --> 00:00:19 at ocw.mit.edu. 9 00:00:19 --> 00:00:24 PROFESSOR: You'll recall, at least some of you will recall, 10 00:00:24 --> 00:00:28 that last time we ended up looking at a simulation of a 11 00:00:28 --> 00:00:33 drunken university student wandering randomly in a field. 12 00:00:33 --> 00:00:37 I'm going to return to that. 13 00:00:37 --> 00:00:41 Before I get to the interesting part, I do want to call your 14 00:00:41 --> 00:00:45 attention to something that some people seemed a little 15 00:00:45 --> 00:00:47 bit confused by last time. 16 00:00:47 --> 00:00:51 So you remember that we had this thing called 17 00:00:51 --> 00:00:53 perform trial. 18 00:00:53 --> 00:00:59 And the way we basically tested the drunk, what would happen, 19 00:00:59 --> 00:01:05 is we would call this, passing in a time, the number of steps, 20 00:01:05 --> 00:01:07 the amount of time that the drunk was wondering, 21 00:01:07 --> 00:01:09 and the field. 22 00:01:09 --> 00:01:13 And there was just 1 line, that I blazed past, 23 00:01:13 --> 00:01:16 probably too quickly. 24 00:01:16 --> 00:01:23 So what that line is saying, is we, from the field, we're going 25 00:01:23 --> 00:01:26 to call the method get drunk, which is going 26 00:01:26 --> 00:01:30 to return us a drunk. 27 00:01:30 --> 00:01:33 And then we're going to call the move method for 28 00:01:33 --> 00:01:38 that drunk, passing the field as an argument. 29 00:01:38 --> 00:01:42 Now the question is, what would have happened if I had left 30 00:01:42 --> 00:01:47 off these parentheses? 31 00:01:47 --> 00:01:52 There. 32 00:01:52 --> 00:01:54 A volunteer? 33 00:01:54 --> 00:01:59 What would, we will do the experiment in a minute. 34 00:01:59 --> 00:02:02 And by the way, I want to emphasize the importance 35 00:02:02 --> 00:02:04 of experimentation. 36 00:02:04 --> 00:02:07 I was kind of surprised, for example, to get email from 37 00:02:07 --> 00:02:09 people saying, well, what was the correct answer to 38 00:02:09 --> 00:02:12 question 3 on the quiz? 39 00:02:12 --> 00:02:14 And my sort of response was, why don't you 40 00:02:14 --> 00:02:18 type it and find out? 41 00:02:18 --> 00:02:20 When in doubt, run the experiment. 42 00:02:20 --> 00:02:23 So, what's going to happen, what would have happened, 43 00:02:23 --> 00:02:29 had I failed to type that pair of parentheses? 44 00:02:29 --> 00:02:34 Anybody? 45 00:02:34 --> 00:02:43 Well, what is, get drunk? 46 00:02:43 --> 00:02:45 It is a method. 47 00:02:45 --> 00:02:50 And remember, in Python, methods, like classes, 48 00:02:50 --> 00:02:55 are themselves objects. 49 00:02:55 --> 00:02:58 So get drunk would have a returned an object which 50 00:02:58 --> 00:03:10 was a method, and then, well, let's try it. 51 00:03:10 --> 00:03:14 And the error message will tell us everything we need to know. 52 00:03:14 --> 00:03:20 Function has no attribute move. 53 00:03:20 --> 00:03:24 Sure enough, the method does not have an attribute move. 54 00:03:24 --> 00:03:28 The instance of the class has the attribute. 55 00:03:28 --> 00:03:37 And so what those parentheses did, was tell me to invoke the 56 00:03:37 --> 00:03:40 method get drunk, so now instead of that being the 57 00:03:40 --> 00:03:45 method itself, it's the result returned by the method. 58 00:03:45 --> 00:03:51 Which is an instance of class drunk, and that instance has 59 00:03:51 --> 00:03:57 an attribute move, which is itself a method. 60 00:03:57 --> 00:04:00 This is a very common programming paradigm, and it's 61 00:04:00 --> 00:04:04 important to sort of lock into your heads, the distinction 62 00:04:04 --> 00:04:10 between the method and an invocation of the method. 63 00:04:10 --> 00:04:13 Because you can write either, and sometimes you won't get an 64 00:04:13 --> 00:04:17 error message, you'll just get the wrong answer. 65 00:04:17 --> 00:04:19 And that's kind of bad. 66 00:04:19 --> 00:04:22 That make sense to everybody? 67 00:04:22 --> 00:04:25 Does it not make sense to anybody, maybe is the 68 00:04:25 --> 00:04:29 question I should ask? 69 00:04:29 --> 00:04:32 All right, we'll go on, and we'll see more examples 70 00:04:32 --> 00:04:34 of this kind of thing. 71 00:04:34 --> 00:04:39 Now last week, I ran this several times to see what would 72 00:04:39 --> 00:04:43 happen, and we saw that in fact contrary to everyone's, most 73 00:04:43 --> 00:04:47 everyone's, expectation, the longer we ran the simulation, 74 00:04:47 --> 00:04:50 the further the drunk was from the starting point. 75 00:04:50 --> 00:04:54 And we saw that by plotting how far the drunk was 76 00:04:54 --> 00:04:57 after each time unit. 77 00:04:57 --> 00:05:01 We ran it several times, we got different answers. 78 00:05:01 --> 00:05:05 And at that point we should ask ourselves, is that really the 79 00:05:05 --> 00:05:10 way to go about answering the original question? 80 00:05:10 --> 00:05:15 Which was, how far should we expect the drunk to be? 81 00:05:15 --> 00:05:17 The answer is no. 82 00:05:17 --> 00:05:21 I don't want to sit there at the keyboard typing in 400 83 00:05:21 --> 00:05:25 examples and then, in my head, trying to figure out what's 84 00:05:25 --> 00:05:29 quote, typical, unquote. 85 00:05:29 --> 00:05:35 Instead, I need to organize my simulation so that it runs a 86 00:05:35 --> 00:05:42 number of trials for me and summarizes the results. 87 00:05:42 --> 00:05:46 All the simulations we're going to look at it, in fact almost 88 00:05:46 --> 00:05:51 all the simulations anyone will ever write, sort of, have 89 00:05:51 --> 00:05:55 the same kind of structure. 90 00:05:55 --> 00:06:14 We start with an inner loop that simulates one trial. 91 00:06:14 --> 00:06:16 That's what we have here, right, we have happen 92 00:06:16 --> 00:06:18 to have a function. 93 00:06:18 --> 00:06:22 So when I say inner loop, what I mean is, not that I'll write 94 00:06:22 --> 00:06:26 a program a bunch of nested loops, but that I'll write a 95 00:06:26 --> 00:06:31 program with some function calls, and down at sort of 96 00:06:31 --> 00:06:36 the bottom of the stack will be perform trial. 97 00:06:36 --> 00:06:39 Which stimulates one trial of some number of 98 00:06:39 --> 00:06:46 seconds, in this case. 99 00:06:46 --> 00:07:04 And then I'll quote, enclose, unquote the inner loop in 100 00:07:04 --> 00:07:25 another loop that conducts an appropriate number of trials. 101 00:07:25 --> 00:07:29 Now a little bit later in the term, we'll get to the question 102 00:07:29 --> 00:07:33 of, how do we know what an appropriate number is? 103 00:07:33 --> 00:07:39 For today, we'll just say, a lot. 104 00:07:39 --> 00:07:42 And we'll talk about that a little bit more. 105 00:07:42 --> 00:08:00 And then finally, what we want to do, is calculate and present 106 00:08:00 --> 00:08:07 some relevant statistics about the trials. 107 00:08:07 --> 00:08:11 And we'll talk about what's relevant. 108 00:08:11 --> 00:08:17 I want to emphasize today the presentation of them. 109 00:08:17 --> 00:08:21 Last time we looked at a graph, which I think you'll all agree, 110 00:08:21 --> 00:08:33 was a lot prettier to look at an array of, say, 1000 numbers. 111 00:08:33 --> 00:08:34 All right. 112 00:08:34 --> 00:08:41 So now, on your handout and on the screen, you'll see 113 00:08:41 --> 00:08:43 the way we've done this. 114 00:08:43 --> 00:08:46 So perform trial, we've already seen, so we know what the 115 00:08:46 --> 00:08:49 inner loop looks like. 116 00:08:49 --> 00:08:53 We can ignore first test, that was just my taking the code 117 00:08:53 --> 00:08:58 that I had in line last time and putting it in a function. 118 00:08:58 --> 00:09:02 And now what you'll see in the handout is perform 119 00:09:02 --> 00:09:07 sim and answer question. 120 00:09:07 --> 00:09:11 So, let's look it those. 121 00:09:11 --> 00:09:17 So perform sim, sim for simulation, takes the amount of 122 00:09:17 --> 00:09:21 time, the number of steps for each trial, plus the 123 00:09:21 --> 00:09:26 number of trials. 124 00:09:26 --> 00:09:32 It starts with an empty list, dist short for distances here, 125 00:09:32 --> 00:09:35 saying so far we haven't run any trials, we don't know what 126 00:09:35 --> 00:09:37 any of the distances are. 127 00:09:37 --> 00:09:42 And then for trial in range number of trials, it creates a 128 00:09:42 --> 00:09:50 drunk, and I'm putting the trial as part of the drunk's 129 00:09:50 --> 00:09:53 name, just so we can make sure that the drunks are 130 00:09:53 --> 00:09:56 all different. 131 00:09:56 --> 00:09:59 Then I'm going to create a field with that drunk 132 00:09:59 --> 00:10:03 in it at location 0,0. 133 00:10:03 --> 00:10:08 And then I'm going to call perform trial with the 134 00:10:08 --> 00:10:14 time in that field to get the distances. 135 00:10:14 --> 00:10:22 What does perform trial return? 136 00:10:22 --> 00:10:25 We looked at it earlier. 137 00:10:25 --> 00:10:30 What is perform trial returning? 138 00:10:30 --> 00:10:35 Somebody? 139 00:10:35 --> 00:10:39 Surely someone can figure this one out. 140 00:10:39 --> 00:10:45 I have a whole new bag of candy here, post-Halloween. 141 00:10:45 --> 00:10:47 What kind of thing is it returning? 142 00:10:47 --> 00:10:49 Is it returning a number? 143 00:10:49 --> 00:10:51 If you think it's returning a number, oh, ok, you gonna 144 00:10:51 --> 00:10:55 tell me what kind of thing it's returning, please? 145 00:10:55 --> 00:10:57 A list, thank you. 146 00:10:57 --> 00:11:02 And it's a list of what? 147 00:11:02 --> 00:11:04 I'm giving you the candy on spec, assuming you'll 148 00:11:04 --> 00:11:07 give me the right answer. 149 00:11:07 --> 00:11:10 List of distances, exactly. 150 00:11:10 --> 00:11:13 So how far way it is after each time step? 151 00:11:13 --> 00:11:19 This is exactly the list that we graphed last time. 152 00:11:19 --> 00:11:27 So perform sim will get that list and append it to the list. 153 00:11:27 --> 00:11:34 So dist list will be a list of lists, each element will be 154 00:11:34 --> 00:11:38 a list of distances, right? 155 00:11:38 --> 00:11:42 OK, so that's good. 156 00:11:42 --> 00:11:50 And that's now step 2. 157 00:11:50 --> 00:11:54 In some sense, all of this is irrelevant to the 158 00:11:54 --> 00:11:58 actual question we started with, in some sense. 159 00:11:58 --> 00:12:00 This is just the structure. 160 00:12:00 --> 00:12:03 Then I'm going to write a function called answer 161 00:12:03 --> 00:12:08 question, or ans quest, designed to actually address 162 00:12:08 --> 00:12:12 the original question. 163 00:12:12 --> 00:12:14 So it, too, is going to create a list. 164 00:12:14 --> 00:12:19 This is a list called means. 165 00:12:19 --> 00:12:29 Then it's going to call perform sim, to get this list of lists, 166 00:12:29 --> 00:12:36 then it will go through that and calculate the means and 167 00:12:36 --> 00:12:40 create a list of means. 168 00:12:40 --> 00:12:43 And then it will plot it, and we'll get to, before this 169 00:12:43 --> 00:12:45 lecture's over, how the plotting works. 170 00:12:45 --> 00:12:49 So I'm following exactly that structure here. 171 00:12:49 --> 00:12:55 It calls this function, which runs an appropriate number of 172 00:12:55 --> 00:12:58 trials by calling that function, and then we'll 173 00:12:58 --> 00:13:01 calculate and present some statistics. 174 00:13:01 --> 00:13:11 So now let's run it. 175 00:13:11 --> 00:13:12 All right, so what have I done? 176 00:13:12 --> 00:13:15 I typed an inadvertent chara -- ah, yes, I typed an s which 177 00:13:15 --> 00:13:30 I didn't intend to type. 178 00:13:30 --> 00:13:33 It's going to take a little while, it's loading Pylab. 179 00:13:33 --> 00:13:37 Now it's running the simulation. 180 00:13:37 --> 00:13:43 All right, and here's a picture. 181 00:13:43 --> 00:13:50 So, when we ran it, let's look at the code for a minute here, 182 00:13:50 --> 00:13:55 what we can see is at the bottom, I called ans quest, 183 00:13:55 --> 00:14:04 saying each trial should be 500 steps, and run 100 trials. 184 00:14:04 --> 00:14:20 And then we'll plot this graph. 185 00:14:20 --> 00:14:23 Graph is lurking somewhere in there, it is. 186 00:14:23 --> 00:14:26 And one of the nice things we'll see is, it's kind of 187 00:14:26 --> 00:14:31 smooth, And we'll come back to this, but the fact that it's 188 00:14:31 --> 00:14:38 sort of smooth makes me feel that running 100 trials might 189 00:14:38 --> 00:14:43 actually be enough to give me a consistent answer. 190 00:14:43 --> 00:14:45 You know, if it had been bouncing up and down as 191 00:14:45 --> 00:14:51 we went, then we'd say, jeez, no trend here. 192 00:14:51 --> 00:14:56 Seeing a relatively smooth trend makes me feel somewhat 193 00:14:56 --> 00:15:01 comfortable that we're actually getting an appropriate answer. 194 00:15:01 --> 00:15:04 And that if I were to run 500 trials, the line would be 195 00:15:04 --> 00:15:08 smoother, but it would look kind of the same. 196 00:15:08 --> 00:15:12 Because it doesn't look like it's moving here, in arbitrary 197 00:15:12 --> 00:15:14 directions large amounts. 198 00:15:14 --> 00:15:19 It's not like the stock market. 199 00:15:19 --> 00:15:23 Should I be happy? 200 00:15:23 --> 00:15:25 I've sort of done what I wanted, I kind of I think I 201 00:15:25 --> 00:15:30 have an answer now, which is 500 steps, it should be four 202 00:15:30 --> 00:15:37 and a half units away from the origin. 203 00:15:37 --> 00:15:39 What do you think? 204 00:15:39 --> 00:15:48 Who think this is the right answer? 205 00:15:48 --> 00:15:55 So who thinks it's a wrong answer, raise your hand? 206 00:15:55 --> 00:15:57 All right, TAs, what do you guys think? 207 00:15:57 --> 00:15:59 Putting you on the spot. 208 00:15:59 --> 00:16:03 Right answer or wrong answer? 209 00:16:03 --> 00:16:06 They think it's right. 210 00:16:06 --> 00:16:11 Well, shame on them. 211 00:16:11 --> 00:16:17 Let's remember, rack our brains to a week ago, when we ran a 212 00:16:17 --> 00:16:19 bunch of individual tests. 213 00:16:19 --> 00:16:22 And let's see what we get if we do that. 214 00:16:22 --> 00:16:27 And the point here is, it's always good to check. 215 00:16:27 --> 00:16:30 My recollection, when I looked at this, was that 216 00:16:30 --> 00:16:32 something was amiss. 217 00:16:32 --> 00:16:36 Because I kind of remember, when I ran the test last 218 00:16:36 --> 00:16:41 time, we were more like 40 away than four away. 219 00:16:41 --> 00:16:44 Well all right, let's try it. 220 00:16:44 --> 00:16:58 We'll, sometimes happens, all right, I'm going to have to 221 00:16:58 --> 00:17:03 restart Idol here, just, as you all, at least all who use 222 00:17:03 --> 00:17:06 Macintoshes know, this happens sometimes, 223 00:17:06 --> 00:17:18 it's not catastrophic. 224 00:17:18 --> 00:17:22 Sigh. 225 00:17:22 --> 00:17:26 So this reminds me of the old joke. 226 00:17:26 --> 00:17:31 That a computer scientist, a mechanical engineer, were 227 00:17:31 --> 00:17:36 riding in a car and the car stalled, stopped running. 228 00:17:36 --> 00:17:39 And the mechanical engineer said I know what to do, 229 00:17:39 --> 00:17:42 let's go out and check the carburetor, and look 230 00:17:42 --> 00:17:43 at the engine. 231 00:17:43 --> 00:17:47 The computer scientist said, no that's the wrong thing to do. 232 00:17:47 --> 00:17:50 What you ought to do is, let's turn off the key, get out of 233 00:17:50 --> 00:17:53 the car, shut the doors, open the doors, get back 234 00:17:53 --> 00:17:56 in and restart it. 235 00:17:56 --> 00:17:58 And sure enough, it worked. 236 00:17:58 --> 00:18:03 So when in doubt, reboot. 237 00:18:03 --> 00:18:09 So, we'll come down, we'll do that, and we're going to call 238 00:18:09 --> 00:18:18 first test here, and see what that gives us. 239 00:18:18 --> 00:18:33 And we'll, for the moment, ignore that. 240 00:18:33 --> 00:18:35 Well, look at this. 241 00:18:35 --> 00:18:38 We ran a bunch of Homer's random walks, and maybe it 242 00:18:38 --> 00:18:45 isn't 40, but not even one of them was four. 243 00:18:45 --> 00:18:50 So now we see is, we've run two tests, and we've gotten 244 00:18:50 --> 00:18:54 inconsistent answers. 245 00:18:54 --> 00:18:57 Well, we don't know which one is wrong. 246 00:18:57 --> 00:18:59 We know that one of them is wrong. 247 00:18:59 --> 00:19:01 We don't even know that, maybe we just got unlucky 248 00:19:01 --> 00:19:04 with these five tests. 249 00:19:04 --> 00:19:10 But odds are, something is wrong, that there's a bug here. 250 00:19:10 --> 00:19:15 And, we have to figure out which one. 251 00:19:15 --> 00:19:20 So how would we go about doing that? 252 00:19:20 --> 00:19:28 Well, I'm going to do what I always recommend people do. 253 00:19:28 --> 00:19:34 Which was, find a really simple example, One for which I 254 00:19:34 --> 00:19:38 actually know the answer. 255 00:19:38 --> 00:19:40 So what would be a good example for which I 256 00:19:40 --> 00:19:49 might know the answer? 257 00:19:49 --> 00:19:54 Give me the simplest example of a simulation of the random walk 258 00:19:54 --> 00:19:57 I could run, where you're confident you know 259 00:19:57 --> 00:19:59 what the answer is. 260 00:19:59 --> 00:20:04 Yeah? one step, exactly, and what's the answer 261 00:20:04 --> 00:20:07 after one step? 262 00:20:07 --> 00:20:08 One. 263 00:20:08 --> 00:20:12 She can't catch and talk at the same time. 264 00:20:12 --> 00:20:12 Exactly. 265 00:20:12 --> 00:20:17 So we know if we simulate it, one, the drunk has moved in 266 00:20:17 --> 00:20:19 some direction, and is going to be exactly one 267 00:20:19 --> 00:20:21 step from the origin. 268 00:20:21 --> 00:20:25 So now we can go and see what we get. 269 00:20:25 --> 00:20:27 So let's do that. 270 00:20:27 --> 00:20:36 And we'll change this to be one, and we'll 271 00:20:36 --> 00:20:45 change this to be one. 272 00:20:45 --> 00:20:56 We'll see what the answer is. 273 00:20:56 --> 00:20:59 Well. 274 00:20:59 --> 00:21:02 50? 275 00:21:02 --> 00:21:06 Well, kind of makes me worry. 276 00:21:06 --> 00:21:08 1. 277 00:21:08 --> 00:21:14 All right, so we see that the simple test of Homer gives 278 00:21:14 --> 00:21:15 me the right answer. 279 00:21:15 --> 00:21:17 We don't know it's always the right answer, we know 280 00:21:17 --> 00:21:18 it's, at least for this 1. 281 00:21:18 --> 00:21:22 But we know the other 1 was just way off. 282 00:21:22 --> 00:21:25 Interestingly, unlike the previous time, instead of being 283 00:21:25 --> 00:21:29 way too low, it's way too high. 284 00:21:29 --> 00:21:33 So that gives us some pause for thought. 285 00:21:33 --> 00:21:37 And now we need to go in and debug it. 286 00:21:37 --> 00:21:40 So let's go and debug it. 287 00:21:40 --> 00:21:51 And seems to me the right thing to do is to go here. 288 00:21:51 --> 00:21:56 Oh, boy, I'm going to have to restart it again, not good. 289 00:21:56 --> 00:22:00 And we'll put an intermediate value. 290 00:22:00 --> 00:22:07 Actually, maybe we'll do it. 291 00:22:07 --> 00:22:16 What would be a nice thing to do here? 292 00:22:16 --> 00:22:18 We're going to come here. 293 00:22:18 --> 00:22:21 Well, let's, you know, we want to go somewhere halfway through 294 00:22:21 --> 00:22:24 the program, print some intermediate value that will 295 00:22:24 --> 00:22:27 give us some information. 296 00:22:27 --> 00:22:35 So, this might be a good place. 297 00:22:35 --> 00:22:43 And, what should we print? 298 00:22:43 --> 00:22:55 Well, what values do you think you should get here? 299 00:22:55 --> 00:22:55 Pardon? 300 00:22:55 --> 00:22:57 STUDENT: The total distance so far. 301 00:22:57 --> 00:23:02 PROFESSOR: The total distance so far. 302 00:23:02 --> 00:23:07 So that would be a good thing to print. 303 00:23:07 --> 00:23:24 And what do we think it should be? 304 00:23:24 --> 00:23:29 We'll comment this 1 out since we think that works, and just 305 00:23:29 --> 00:23:33 to be safe, let's not even run 100 trials let's, run one 306 00:23:33 --> 00:23:38 trial, or two trials maybe. 307 00:23:38 --> 00:23:45 See we get. 308 00:23:45 --> 00:23:49 0 and then 2. 309 00:23:49 --> 00:23:51 W, 0 was sort of what we expected the first 310 00:23:51 --> 00:23:54 time around, but 2? 311 00:23:54 --> 00:24:00 How did you get to be 2? 312 00:24:00 --> 00:24:03 Anyone want to see what's going on here? 313 00:24:03 --> 00:24:08 So we see, right here we have the wrong answer. 314 00:24:08 --> 00:24:18 Well, maybe we should see what things looked like before this? 315 00:24:18 --> 00:24:19 Is it the lists are wrong? 316 00:24:19 --> 00:24:23 What am I doing wrong here? 317 00:24:23 --> 00:24:32 I'll bet someone can figure this out. 318 00:24:32 --> 00:24:32 Pardon? 319 00:24:32 --> 00:24:35 STUDENT: [INAUDIBLE] 320 00:24:35 --> 00:24:39 PROFESSOR: Well, I'm adding them up, fair enough. 321 00:24:39 --> 00:24:42 But so tot looks OK. 322 00:24:42 --> 00:24:48 So, all right, maybe we should take a look at means. 323 00:24:48 --> 00:24:49 Right? 324 00:24:49 --> 00:25:03 Let's take a look at what that looks like. 325 00:25:03 --> 00:25:06 Not bad. 326 00:25:06 --> 00:25:08 All right, so maybe my example's too simple. 327 00:25:08 --> 00:25:19 Let's try a little bit bigger. 328 00:25:19 --> 00:25:26 Hmmm -- 2.5? 329 00:25:26 --> 00:25:30 All right, so now I know what's going wrong is, somehow not 330 00:25:30 --> 00:25:34 that I'm messing up tot, but that I'm computing the 331 00:25:34 --> 00:25:37 mean incorrectly. 332 00:25:37 --> 00:25:39 Where am I computing the mean? 333 00:25:39 --> 00:25:41 They're only two expressions here. 334 00:25:41 --> 00:25:44 There's tot, we've checked that. 335 00:25:44 --> 00:25:48 So there must be a problem with the divisor, that's 336 00:25:48 --> 00:25:51 the only thing that's left. 337 00:25:51 --> 00:25:54 Yeah? 338 00:25:54 --> 00:25:55 STUDENT: [INAUDIBLE] 339 00:25:55 --> 00:25:56 PROFESSOR: Exactly right. 340 00:25:56 --> 00:26:01 I should be dividing by the length of the list. 341 00:26:01 --> 00:26:06 The number of things I'm adding to tot. 342 00:26:06 --> 00:26:19 So I just, inadvertently, divided by, I have 343 00:26:19 --> 00:26:21 a list of lists. 344 00:26:21 --> 00:26:23 And what I really wanted to do is, divide by 345 00:26:23 --> 00:26:25 the number of lists. 346 00:26:25 --> 00:26:28 Because I'm computing the mean for each list, adding it to 347 00:26:28 --> 00:26:31 total, and then at the end I need to divide by the number of 348 00:26:31 --> 00:26:36 lists who's means I computed, not by the length of 1 349 00:26:36 --> 00:26:41 of the lists, right? 350 00:26:41 --> 00:26:49 So now, let's see what happens if we run it. 351 00:26:49 --> 00:26:52 Now, we get some output printed, which I really 352 00:26:52 --> 00:26:57 didn't want, but it happens. 353 00:26:57 --> 00:27:02 Well this looks a lot better. 354 00:27:02 --> 00:27:02 Right? 355 00:27:02 --> 00:27:08 Sure enough, it's 1. 356 00:27:08 --> 00:27:10 All right, so now I'm feeling better. 357 00:27:10 --> 00:27:13 I'm going to get rid of this print statement, if we're gonna 358 00:27:13 --> 00:27:16 run a more extensive test. 359 00:27:16 --> 00:27:25 And now we can go back to our original question. 360 00:27:25 --> 00:27:35 And run it. 361 00:27:35 --> 00:27:37 Well, this looks a lot more consistent with 362 00:27:37 --> 00:27:41 what we saw before. 363 00:27:41 --> 00:27:49 It says that on average, you should be around 20. 364 00:27:49 --> 00:27:52 So we feel pretty good about that. 365 00:27:52 --> 00:27:55 Now, just to feel even better, I'm going to double the 366 00:27:55 --> 00:28:10 number of trials and see what that tells us. 367 00:28:10 --> 00:28:12 And it's still around 20. 368 00:28:12 --> 00:28:15 Line a little smoother. 369 00:28:15 --> 00:28:18 And if I where do 1000 trials will get a little smoother, and 370 00:28:18 --> 00:28:21 it would still be around 20. 371 00:28:21 --> 00:28:26 Maybe slightly different each time, but consistent with what 372 00:28:26 --> 00:28:29 we saw before, when we ran the other program. 373 00:28:29 --> 00:28:34 We can feel that we're actually doing something useful. 374 00:28:34 --> 00:28:38 And so now we can conclude, and would actually be the correct 375 00:28:38 --> 00:28:43 conclusion, that we know about how far this random drunk is 376 00:28:43 --> 00:28:47 going to move in 500 steps. 377 00:28:47 --> 00:28:50 And if you want to know how far he would move in 1000 steps, 378 00:28:50 --> 00:28:54 we could try that, too. 379 00:28:54 --> 00:28:55 All right. 380 00:28:55 --> 00:28:59 What are the lessons here? 381 00:28:59 --> 00:29:05 One lesson is to look at the labels on the axes. 382 00:29:05 --> 00:29:08 Because if we just looked at it without noticing these 383 00:29:08 --> 00:29:13 numbers, it looks the same. 384 00:29:13 --> 00:29:13 Right? 385 00:29:13 --> 00:29:15 This doesn't look any different, in some sense, than 386 00:29:15 --> 00:29:18 when the numbers were four. 387 00:29:18 --> 00:29:20 So you can't just look at the shape of the curve, you 388 00:29:20 --> 00:29:22 have to look at the values. 389 00:29:22 --> 00:29:25 So what does that tell me? 390 00:29:25 --> 00:29:30 It tells me that a responsible person will always 391 00:29:30 --> 00:29:32 label the axes. 392 00:29:32 --> 00:29:39 As I have done here, not only giving you the numbers, but 393 00:29:39 --> 00:29:42 telling you it's the distance. 394 00:29:42 --> 00:29:46 I hate it when I look at graphs and I have to guess what 395 00:29:46 --> 00:29:48 the x- and y- axes are. 396 00:29:48 --> 00:29:52 Here it says time versus distance, and you also 397 00:29:52 --> 00:29:55 notice I put a title on it. 398 00:29:55 --> 00:30:00 So there's a point there. 399 00:30:00 --> 00:30:08 And look, when you're doing it. 400 00:30:08 --> 00:30:16 Ask if the answer make sense. 401 00:30:16 --> 00:30:20 One of the things we'll see as we go on, is you can get all 402 00:30:20 --> 00:30:24 your statistics right, and still get the wrong answer 403 00:30:24 --> 00:30:29 because of a consistent bug. 404 00:30:29 --> 00:30:33 And so always just say, do I believe it, or is this so 405 00:30:33 --> 00:30:37 counterintuitive that I'm suspicious? 406 00:30:37 --> 00:30:51 And as part of that ask, is it consistent with other evidence? 407 00:30:51 --> 00:30:54 In this case we had the evidence of watching 408 00:30:54 --> 00:30:56 an individual walk. 409 00:30:56 --> 00:30:59 Now those two things were not consistent, don't know 410 00:30:59 --> 00:31:03 which is wrong, but it must be one of them. 411 00:31:03 --> 00:31:07 And then the final point I wanted to make, is that you can 412 00:31:07 --> 00:31:20 be pretty systematic about debugging, And in particular, 413 00:31:20 --> 00:31:28 debug with a simple example. 414 00:31:28 --> 00:31:32 Right, instead of trying to debug 500 steps and 100 trials, 415 00:31:32 --> 00:31:35 I said, all right, let's look at one step and four 416 00:31:35 --> 00:31:38 trials, five trials. 417 00:31:38 --> 00:31:41 OK, where in my head I knew what it should look like, 418 00:31:41 --> 00:31:44 and then I could check it. 419 00:31:44 --> 00:31:47 All right. 420 00:31:47 --> 00:31:52 Jumping up a level or three of abstraction now. 421 00:31:52 --> 00:31:54 What we've done, is we've introduced the notion of a 422 00:31:54 --> 00:32:00 random walk in the context of a pretty contrived example. 423 00:32:00 --> 00:32:06 But in fact, it's worth knowing that random walks are used all 424 00:32:06 --> 00:32:11 over the place to solve real problems, deal with 425 00:32:11 --> 00:32:13 real phenomena. 426 00:32:13 --> 00:32:28 So for example, if you look at something like Brownian motion, 427 00:32:28 --> 00:32:32 which can be used to model the path traced by a molecule as it 428 00:32:32 --> 00:32:35 travels in a liquid or a gas. 429 00:32:35 --> 00:32:41 Typically, people who do that model it using a random walk. 430 00:32:41 --> 00:32:45 And, depending upon, say the density of the gas or the 431 00:32:45 --> 00:32:49 liquid, the size of the molecules, they change 432 00:32:49 --> 00:32:53 parameters in the simulation, how far it, say, goes in each 433 00:32:53 --> 00:32:55 unit time and things like that. 434 00:32:55 --> 00:32:58 But they use a random walk to try and model what 435 00:32:58 --> 00:33:02 will really happened. 436 00:33:02 --> 00:33:06 People have attempted, for several hundred years now, to 437 00:33:06 --> 00:33:11 use, well, maybe a 150 years, to use random walks to 438 00:33:11 --> 00:33:12 model the stock market. 439 00:33:12 --> 00:33:17 There was a very famous book called A Random Walk Down Wall 440 00:33:17 --> 00:33:24 Street, that argued that things happened as a, random walk was 441 00:33:24 --> 00:33:29 a good way to model things. 442 00:33:29 --> 00:33:32 There's a lot of evidence that says that's wrong, but people 443 00:33:32 --> 00:33:36 continue to attempt to do it. 444 00:33:36 --> 00:33:44 They use it a lot in biology to do things like model kinetics. 445 00:33:44 --> 00:33:50 So, the kinetics of a protein, DNA strand exchange, 446 00:33:50 --> 00:33:52 things of that nature. 447 00:33:52 --> 00:33:55 A separation of macro-molecules, the movement 448 00:33:55 --> 00:34:02 of microorganisms all of those things are done in biology. 449 00:34:02 --> 00:34:04 And do that. 450 00:34:04 --> 00:34:10 People use it to model evolution. 451 00:34:10 --> 00:34:15 They look at mutations as kind of a random event. 452 00:34:15 --> 00:34:20 So, we'll come back to this, but random walks are used over 453 00:34:20 --> 00:34:24 and over and over again in the sciences, the social sciences 454 00:34:24 --> 00:34:29 and therefore a very useful thing to notice about. 455 00:34:29 --> 00:34:30 All right, we're going to come back to that. 456 00:34:30 --> 00:34:34 We're going to even come back to our drunken student and look 457 00:34:34 --> 00:34:37 at other kinds of random walks other than the kind 458 00:34:37 --> 00:34:38 we just looked at. 459 00:34:38 --> 00:34:42 Before I do that, though, I wanted back up and take the 460 00:34:42 --> 00:34:45 magic out of plotting. 461 00:34:45 --> 00:34:50 So we've gone from the sublime, of what random walks are good 462 00:34:50 --> 00:34:54 for, to in some sense the ridiculous, the actual 463 00:34:54 --> 00:34:56 syntax for plotting things. 464 00:34:56 --> 00:35:01 And maybe it's not ridiculous, but it's boring. 465 00:35:01 --> 00:35:05 But you need it, so let's look at it. 466 00:35:05 --> 00:35:12 So we're doing this using a package called Pylab, which is 467 00:35:12 --> 00:35:28 in itself built on a package called Pylab, either pronounced 468 00:35:28 --> 00:35:32 num p or num pi, you can choose your pronunciation 469 00:35:32 --> 00:35:34 as you prefer. 470 00:35:34 --> 00:35:38 This basically gives you a lot of operations on numbers, 471 00:35:38 --> 00:35:43 numbered things, and on top of that, someone bill Pylab which 472 00:35:43 --> 00:35:47 is designed to provide a Python interface to a lot of the 473 00:35:47 --> 00:35:50 functionality you get in Matlab. 474 00:35:50 --> 00:35:55 And in particular, we're going to be using today the plotting 475 00:35:55 --> 00:36:00 functionality that comes with Matlab, or the version of it. 476 00:36:00 --> 00:36:05 So we're going to say, from Pylab import star, that's 477 00:36:05 --> 00:36:09 just so I don't have to type Pylab dot plot every time. 478 00:36:09 --> 00:36:12 And I'm going import random which we're going to use later. 479 00:36:12 --> 00:36:15 So let's look at it now. 480 00:36:15 --> 00:36:19 First thing we're going to do is plot 1, 2, 3, 4, and then 481 00:36:19 --> 00:36:24 1, 2, 3, and then 5, 6, 7, 8. 482 00:36:24 --> 00:36:28 And then at the very bottom, you'll see this line show. 483 00:36:28 --> 00:36:31 That's going to annoy the heck out of you throughout the 484 00:36:31 --> 00:36:34 semester, the rest of the semester. 485 00:36:34 --> 00:36:37 Because what happens is, Pylab produces all these beautiful 486 00:36:37 --> 00:36:40 plots, and then does not display them until 487 00:36:40 --> 00:36:43 you type show. 488 00:36:43 --> 00:36:47 So remember, at the end of every program, kind of, 489 00:36:47 --> 00:36:50 the last thing you should execute should be show. 490 00:36:50 --> 00:36:54 You don't want to execute it in the middle, because what 491 00:36:54 --> 00:36:57 happens in the middle is it, in an interactive mode at 492 00:36:57 --> 00:37:00 least, it just stops. 493 00:37:00 --> 00:37:03 And displays the graphs, and until you make the plots go 494 00:37:03 --> 00:37:07 away, it won't execute the next line. 495 00:37:07 --> 00:37:09 Which is why I've tucked the show at the very bottom 496 00:37:09 --> 00:37:11 of my script here. 497 00:37:11 --> 00:37:14 Inevitably, you will forget to type show. 498 00:37:14 --> 00:37:17 You will ask a TA, how come my graphs aren't appearing in the 499 00:37:17 --> 00:37:21 screen, and the TA will say, did you do show? 500 00:37:21 --> 00:37:25 And you'll go -- but it happens to all of us. 501 00:37:25 --> 00:37:27 All right, so let's try it. 502 00:37:27 --> 00:37:34 See what we get. 503 00:37:34 --> 00:37:41 So sure enough, it's plotted the values 1, 2, 3, 4, and 5, 504 00:37:41 --> 00:37:44 6, 7, 8, on the x- and y- axis. 505 00:37:44 --> 00:37:49 Two things I want you to notice here. 506 00:37:49 --> 00:37:56 One Is, that both plots showed up on the same figure. 507 00:37:56 --> 00:37:58 Which is sometimes what you want, and sometimes 508 00:37:58 --> 00:37:59 not what you want. 509 00:37:59 --> 00:38:02 You'll notice that also happened with the random walk 510 00:38:02 --> 00:38:05 we looked at, where when I plotted five different walks 511 00:38:05 --> 00:38:08 for Homer they all showed up superimposed on top 512 00:38:08 --> 00:38:12 of one another. 513 00:38:12 --> 00:38:16 The other thing I want you to notice, is the x-axis 514 00:38:16 --> 00:38:19 runs from 0 to 3. 515 00:38:19 --> 00:38:22 So you might have kind of thought, that what we 516 00:38:22 --> 00:38:27 would see is a 45 degree angle on these things. 517 00:38:27 --> 00:38:32 But of course, Python, when not instructed otherwise, 518 00:38:32 --> 00:38:35 always starts at zero. 519 00:38:35 --> 00:38:43 Since when we called plot, I gave it only the y-values, it 520 00:38:43 --> 00:38:48 used default values for x. 521 00:38:48 --> 00:38:53 It was smart enough to say, since we have four y-values, we 522 00:38:53 --> 00:38:57 should need four x-values, and I'll choose the integers 0, 523 00:38:57 --> 00:39:04 1, 2, 3 as those values. 524 00:39:04 --> 00:39:08 Now you don't have to do that. 525 00:39:08 --> 00:39:18 We could do this instead. 526 00:39:18 --> 00:39:20 Let's try this one. 527 00:39:20 --> 00:39:26 What did I just do? 528 00:39:26 --> 00:39:31 Let's comment these two out, if we could only get there. 529 00:39:31 --> 00:39:42 This is highly annoying. 530 00:39:42 --> 00:39:46 Let's hope it doesn't tell me that I have to -- All 531 00:39:46 --> 00:39:50 right, so let's go here. 532 00:39:50 --> 00:39:58 We'll get rid of those guys, and we'll try this one. 533 00:39:58 --> 00:40:17 We'll plot 1, 2, 3, 4 against 1, 4, 9, 16. 534 00:40:17 --> 00:40:19 OK? 535 00:40:19 --> 00:40:25 So now, it's using 1, 2, 3, 4 as the x-axis, and 536 00:40:25 --> 00:40:28 the y-axis I gave it. 537 00:40:28 --> 00:40:31 First x then y. 538 00:40:31 --> 00:40:34 Now it looks a little funny, right, you might have not 539 00:40:34 --> 00:40:36 expected it to look like this. 540 00:40:36 --> 00:40:40 You'll notice they're these little inflection points here. 541 00:40:40 --> 00:40:43 Well, because what it's really doing is, I gave it a small 542 00:40:43 --> 00:40:46 number of points, only four. 543 00:40:46 --> 00:40:51 It's found those four points, and it's connected them, each 544 00:40:51 --> 00:40:55 point by a straight line. 545 00:40:55 --> 00:40:59 And since the points are kind of spread out, the line 546 00:40:59 --> 00:41:03 has little bumps in it. 547 00:41:03 --> 00:41:05 That makes sense to everyone? 548 00:41:05 --> 00:41:09 Now, it's often deceptive to plot things this way, where you 549 00:41:09 --> 00:41:12 think you have a continuous function when in fact 550 00:41:12 --> 00:41:17 you just have a few miscellaneous points. 551 00:41:17 --> 00:41:22 So let's look at another example. 552 00:41:22 --> 00:41:27 Here, what I'm going to do, is I've called figure, and 553 00:41:27 --> 00:41:34 remember, this is Pylab dot figure, which says, 554 00:41:34 --> 00:41:36 create a new figure. 555 00:41:36 --> 00:41:40 So instead of putting this new curve on the same figure as the 556 00:41:40 --> 00:41:44 old curve, start a new one. 557 00:41:44 --> 00:41:48 And furthermore, I've got this obscure little 558 00:41:48 --> 00:41:50 thing at the end of it. 559 00:41:50 --> 00:41:54 After you give it the x- and y- values, you can give it some 560 00:41:54 --> 00:41:59 instructions about how you want to plot points, or 561 00:41:59 --> 00:42:00 anything else. 562 00:42:00 --> 00:42:05 In this case, what this little string says is, each point 563 00:42:05 --> 00:42:12 should be represented as a red o. r for red, o for o. 564 00:42:12 --> 00:42:16 I'm not asking you to remember this, what you will discover, 565 00:42:16 --> 00:42:21 the good news is there's very good documentation on this. 566 00:42:21 --> 00:42:27 And so you'll find in the reading of pointer to plots, 567 00:42:27 --> 00:42:30 and it will tell you everything you need to know, all of the 568 00:42:30 --> 00:42:34 wizardry and the magic you can put in these strings that 569 00:42:34 --> 00:42:36 tell you how to do things. 570 00:42:36 --> 00:42:41 These are basically the same strings borrowed from Matlab. 571 00:42:41 --> 00:42:48 And now if we run it. 572 00:42:48 --> 00:42:53 Figure one is the same figure we saw before. 573 00:42:53 --> 00:42:58 But figure two has not connected the dots, not drawn a 574 00:42:58 --> 00:43:05 line, it's actually planted each, or plotted, excuse me, 575 00:43:05 --> 00:43:11 each point as a red circle. 576 00:43:11 --> 00:43:15 Now when I look at this, there's something that's not 577 00:43:15 --> 00:43:18 very pleasing about this. 578 00:43:18 --> 00:43:23 That in particular, I know I plotted four points, but 579 00:43:23 --> 00:43:30 it a quick glance it looks like they're only three. 580 00:43:30 --> 00:43:36 And that's because it's taking this fourth point and stuck it 581 00:43:36 --> 00:43:38 way up there in the corner where I missed it. 582 00:43:38 --> 00:43:40 It's there. 583 00:43:40 --> 00:43:44 But it's so close to the edge of the graph that 584 00:43:44 --> 00:43:49 it's kind of hard to see. 585 00:43:49 --> 00:44:09 So I can fix that by executing the command axis, which tells 586 00:44:09 --> 00:44:13 it how far I want it to be. 587 00:44:13 --> 00:44:17 And this says, I want 1 axis to go from 0 to 6, and 588 00:44:17 --> 00:44:22 the other 0 to 20. 589 00:44:22 --> 00:44:26 We'll do that, and also to avoid boring you, we'll do 590 00:44:26 --> 00:44:29 more at the same time. 591 00:44:29 --> 00:44:36 We'll put some labels on these things. 592 00:44:36 --> 00:44:38 I'm going to put that the title of the graph is going to be 593 00:44:38 --> 00:44:45 earnings, and that the x-axis will be labelled days, and 594 00:44:45 --> 00:44:48 the y-axis will be labelled dollars. 595 00:44:48 --> 00:44:55 So earnings dollars against days. 596 00:44:55 --> 00:45:03 OK, now let's see what happens when we do this. 597 00:45:03 --> 00:45:07 Well, we get the same ugly figure one as before, and now 598 00:45:07 --> 00:45:12 you can see figure two I've moved the axes so that my graph 599 00:45:12 --> 00:45:15 will show up in the middle rather than at the edges, and 600 00:45:15 --> 00:45:17 therefore easier to read. 601 00:45:17 --> 00:45:22 I put a title in the top, and I put labels on the axes. 602 00:45:22 --> 00:45:26 Every graph that I ask you to do this course, I want you to 603 00:45:26 --> 00:45:29 put a title on it and to label your axes so we know 604 00:45:29 --> 00:45:32 what we're reading. 605 00:45:32 --> 00:45:36 Again, nothing very deep here, this is really just syntax, 606 00:45:36 --> 00:45:45 just to give you an idea of the sorts of things you can do. 607 00:45:45 --> 00:45:47 All right. now we get to something a little 608 00:45:47 --> 00:45:52 bit more interesting. 609 00:45:52 --> 00:46:02 Let's look at this code here. 610 00:46:02 --> 00:46:07 So far, what I've been passing to the plot function for the 611 00:46:07 --> 00:46:13 x- and y- values are lists. 612 00:46:13 --> 00:46:22 In fact, what Pylab uses is something it gets from NumPy 613 00:46:22 --> 00:46:33 which are not lists really, but what it calls arrays. 614 00:46:33 --> 00:46:39 Now, truth be told, most programming languages use 615 00:46:39 --> 00:46:42 array to mean something quite different. 616 00:46:42 --> 00:46:53 But, in NumPy an array is basically a matrix. 617 00:46:53 --> 00:46:56 On which we can do some interesting things. 618 00:46:56 --> 00:47:05 So for example, when I say x-axes equals array 1, 2, 3, 4. 619 00:47:05 --> 00:47:09 Array is a type, so array applied to the list is just 620 00:47:09 --> 00:47:11 like applying float to an int. 621 00:47:11 --> 00:47:14 If I apply float to an int, it turns it into a 622 00:47:14 --> 00:47:15 floating point number. 623 00:47:15 --> 00:47:22 If I apply array to a list, it turns it into an array. 624 00:47:22 --> 00:47:25 Once it's an array, as we'll see, we can do some very 625 00:47:25 --> 00:47:28 interesting things with it. 626 00:47:28 --> 00:47:34 Now, in addition to getting an array by coercing a the list, 627 00:47:34 --> 00:47:36 which is probably the most common way to get 628 00:47:36 --> 00:47:37 it, by the way. 629 00:47:37 --> 00:47:40 Because you build up a list in simulations of the sort we 630 00:47:40 --> 00:47:43 looked at, and then you might want to change it to an 631 00:47:43 --> 00:47:46 array to perform some operations on it. 632 00:47:46 --> 00:47:50 You can get an array directly with aRange. 633 00:47:50 --> 00:47:52 This is just like the range function we've been using all 634 00:47:52 --> 00:47:56 term, but whereas the range function gives you a list of 635 00:47:56 --> 00:48:04 ints, this gives you an array of ints. 636 00:48:04 --> 00:48:08 But the nice thing about an array is, I can perform 637 00:48:08 --> 00:48:13 operations on it like this. 638 00:48:13 --> 00:48:17 So if I say y-axis equals x-axis raised to the third 639 00:48:17 --> 00:48:22 power, that's something I can't do with a list. 640 00:48:22 --> 00:48:24 I get an error message if I try that with a list. 641 00:48:24 --> 00:48:29 What that will do is, will point-wise, take each element 642 00:48:29 --> 00:48:34 in the array and cube it. 643 00:48:34 --> 00:48:38 So the nice thing about arrays is, you can use them to do the 644 00:48:38 --> 00:48:41 kinds of things you, if you ever took a linear algebra 645 00:48:41 --> 00:48:43 course, you learned about doing. 646 00:48:43 --> 00:48:46 You can multiply an array times an array, You can multiply 647 00:48:46 --> 00:48:49 an array times an integer. 648 00:48:49 --> 00:48:53 And sometimes that's a very convenient thing to do. 649 00:48:53 --> 00:48:55 It's not what this course is about, I don't 650 00:48:55 --> 00:48:56 want to emphasize it. 651 00:48:56 --> 00:49:00 I just want you to know it's there so if in some subsequent 652 00:49:00 --> 00:49:06 life you want to do more complicated manipulations, 653 00:49:06 --> 00:49:09 you'll know that that's possible. 654 00:49:09 --> 00:49:27 So let's run this and see what we get. 655 00:49:27 --> 00:49:31 So the first thing to look at, is we'll ignore the 656 00:49:31 --> 00:49:36 figure for the moment. 657 00:49:36 --> 00:49:42 And we've seen that when I printed test and I printed 658 00:49:42 --> 00:49:48 x-axis, they look the same, they are the same. 659 00:49:48 --> 00:49:51 And in fact, I can do this interesting thing now. 660 00:49:51 --> 00:49:54 Print test double equals x-axis. 661 00:49:54 --> 00:49:56 You might have thought that would return a 662 00:49:56 --> 00:50:00 single value, true. 663 00:50:00 --> 00:50:04 Instead it returns a list, where it's done a point-wise 664 00:50:04 --> 00:50:08 comparison of each element. 665 00:50:08 --> 00:50:13 So when we deal with arrays, they don't behave like lists. 666 00:50:13 --> 00:50:15 And you can imagine that it might be very convenient 667 00:50:15 --> 00:50:17 to be able to do this. 668 00:50:17 --> 00:50:21 Answers the question, are all the elements the same? 669 00:50:21 --> 00:50:23 Or which ones are the same? 670 00:50:23 --> 00:50:27 So you can imagine doing some very clever things with these. 671 00:50:27 --> 00:50:32 And certainly, if you can convert problems to vectors of 672 00:50:32 --> 00:50:37 this sort, you can really perform what's almost magical. 673 00:50:37 --> 00:50:44 And then when we look at the figure, which should be tucked 674 00:50:44 --> 00:50:51 away somewhere here, what did I do with the figure? 675 00:50:51 --> 00:50:57 Did I make it go away? 676 00:50:57 --> 00:51:04 Well, I think I did one of those ugly things and 677 00:51:04 --> 00:51:07 made it go away again. 678 00:51:07 --> 00:51:08 Oh, no, there it is. 679 00:51:08 --> 00:51:10 All right. 680 00:51:10 --> 00:51:14 And sure enough here, we're plotting a cubic. 681 00:51:14 --> 00:51:16 All right. 682 00:51:16 --> 00:51:21 Nothing very important to observe about any of that, 683 00:51:21 --> 00:51:23 other than that arrays are really quite interesting 684 00:51:23 --> 00:51:26 and can be very valuable. 685 00:51:26 --> 00:51:31 Finally, the thing I want to show you is that there are a 686 00:51:31 --> 00:51:39 lot of things we can do that are more interesting 687 00:51:39 --> 00:51:46 than what we've done. 688 00:51:46 --> 00:51:49 So now I'm going to use that random, which I brought in 689 00:51:49 --> 00:51:57 before, and show you that we can plot things other 690 00:51:57 --> 00:52:01 than simply curves. 691 00:52:01 --> 00:52:04 In this case, I'm going to plot a histogram. 692 00:52:04 --> 00:52:07 And what this histogram is going to do is, I'm going to 693 00:52:07 --> 00:52:12 throw a pair of dice a large number of times, and add up 694 00:52:12 --> 00:52:16 the sum, and see what I get. 695 00:52:16 --> 00:52:20 So, for die values equals 1 through 6, for i in range 696 00:52:20 --> 00:52:22 10,000, a lot of dice. 697 00:52:22 --> 00:52:26 I'm just going to append random choice, we've seen this before, 698 00:52:26 --> 00:52:30 of the two dice, and their sum. 699 00:52:30 --> 00:52:33 And then I'm going to plot a histogram, Pylab dot hist 700 00:52:33 --> 00:52:42 instead of plot, and we'll get something quite different. 701 00:52:42 --> 00:52:47 A nice little histogram showing me the values I get, and we 702 00:52:47 --> 00:52:50 will come back to this later and talk about why this is 703 00:52:50 --> 00:52:52 called a normal distribution. 704 00:52:52 --> 00:52:53