1 00:00:00,040 --> 00:00:02,460 The following content is provided under a Creative 2 00:00:02,460 --> 00:00:03,870 Commons license. 3 00:00:03,870 --> 00:00:06,910 Your support will help MIT OpenCourseWare continue to 4 00:00:06,910 --> 00:00:10,560 offer high quality educational resources for free. 5 00:00:10,560 --> 00:00:13,460 To make a donation, or view additional materials from 6 00:00:13,460 --> 00:00:19,290 hundreds of MIT courses, visit MIT OpenCourseWare at 7 00:00:19,290 --> 00:00:21,271 ocw.mit.edu. 8 00:00:21,271 --> 00:00:24,270 PROFESSOR: OK. 9 00:00:24,270 --> 00:00:27,820 You can probably figure out, I'm not Professor Guttag. 10 00:00:27,820 --> 00:00:28,850 He's away today. 11 00:00:28,850 --> 00:00:30,860 And so he's asked me to cover this lecture, which 12 00:00:30,860 --> 00:00:32,530 I'm happy to do. 13 00:00:32,530 --> 00:00:35,150 I'm asked to remind you that there is no 14 00:00:35,150 --> 00:00:37,860 recitation on Friday. 15 00:00:37,860 --> 00:00:40,440 I was expecting at least a smile or a small cheer. 16 00:00:40,440 --> 00:00:41,420 Early lead into spring break. 17 00:00:41,420 --> 00:00:43,870 This ought to be good. 18 00:00:43,870 --> 00:00:46,660 So my understanding is that at the end of last class, 19 00:00:46,660 --> 00:00:50,480 Professor Guttag was walking you through a set of classes. 20 00:00:50,480 --> 00:00:52,250 And I want to remind you of where you were. 21 00:00:52,250 --> 00:00:54,710 I want to look at one of those classes in a bit more detail, 22 00:00:54,710 --> 00:00:55,820 because we're going to show you one new 23 00:00:55,820 --> 00:00:57,410 piece of the language. 24 00:00:57,410 --> 00:00:59,800 And then we're going to turn to a different topic, which is 25 00:00:59,800 --> 00:01:02,640 going to be the subject for the next several lectures. 26 00:01:02,640 --> 00:01:05,850 So just to remind you where you were, Professor Guttag 27 00:01:05,850 --> 00:01:07,910 showed you a set of classes which started off with a 28 00:01:07,910 --> 00:01:09,580 person class. 29 00:01:09,580 --> 00:01:14,210 There's a definition I'll put up on the screen. 30 00:01:14,210 --> 00:01:17,060 This was just something that created a kind of object. 31 00:01:17,060 --> 00:01:18,560 I'm not going to go through the details, but you had the 32 00:01:18,560 --> 00:01:19,830 standard things you'd expect. 33 00:01:19,830 --> 00:01:21,740 You had a way of initializing it. 34 00:01:21,740 --> 00:01:22,960 So you'd make each instance. 35 00:01:22,960 --> 00:01:25,155 And it had a way of pulling out the name, so you could 36 00:01:25,155 --> 00:01:27,620 have a full name and a last name. 37 00:01:27,620 --> 00:01:30,580 You could get out pieces of it, it set birthday, it had 38 00:01:30,580 --> 00:01:32,470 age, it had a bunch of stuff. 39 00:01:32,470 --> 00:01:33,665 And that's fine. 40 00:01:33,665 --> 00:01:38,310 The second thing that he did that we do in a lot of dealing 41 00:01:38,310 --> 00:01:41,850 with classes is he created specialization of that, a 42 00:01:41,850 --> 00:01:43,100 thing called an MIT person. 43 00:01:50,130 --> 00:01:52,880 And you all know that MIT people are 44 00:01:52,880 --> 00:01:55,750 special people, right? 45 00:01:55,750 --> 00:01:57,520 And what did an MIT person do? 46 00:01:57,520 --> 00:01:59,110 Well, again, you can look at it. 47 00:01:59,110 --> 00:02:02,190 It had, again, an init method. 48 00:02:02,190 --> 00:02:04,120 But one of the things that was a little different here-- you 49 00:02:04,120 --> 00:02:06,030 may have seen it in some other places-- is that we can 50 00:02:06,030 --> 00:02:07,920 create, inside of that class 51 00:02:07,920 --> 00:02:10,580 definition, an internal variable. 52 00:02:10,580 --> 00:02:13,430 In this case, it was the ID number. 53 00:02:13,430 --> 00:02:17,120 So that every time we created an instance of this class, we 54 00:02:17,120 --> 00:02:18,840 could give that instance a special number 55 00:02:18,840 --> 00:02:19,610 and a unique number. 56 00:02:19,610 --> 00:02:21,740 And that was exactly what we did here, right? 57 00:02:21,740 --> 00:02:24,030 If you're going to create one of these things, you set the 58 00:02:24,030 --> 00:02:29,800 ID number to be the next number. 59 00:02:29,800 --> 00:02:31,840 And then you would increment that, so that you'd be able 60 00:02:31,840 --> 00:02:33,310 to, next time you created an instance, 61 00:02:33,310 --> 00:02:34,620 give it a unique number. 62 00:02:34,620 --> 00:02:37,590 And that let you do things like put things out in order, 63 00:02:37,590 --> 00:02:39,830 and things of that sort. 64 00:02:39,830 --> 00:02:41,690 There was also a method in this class that we're going to 65 00:02:41,690 --> 00:02:44,360 use in a second, which is something that would say, is 66 00:02:44,360 --> 00:02:46,210 this person a student? 67 00:02:46,210 --> 00:02:48,780 I'll just highlight for you there the definition of it, 68 00:02:48,780 --> 00:02:52,730 which said you'd look at the type of the instance and say, 69 00:02:52,730 --> 00:02:54,850 is it undergrad or grad? 70 00:02:54,850 --> 00:02:58,670 And that was the other two specializations we had here. 71 00:02:58,670 --> 00:03:06,580 We had undergraduates and we had graduates. 72 00:03:06,580 --> 00:03:08,020 And again, I'm not going to worry about the details. 73 00:03:08,020 --> 00:03:08,950 You saw this last time. 74 00:03:08,950 --> 00:03:11,640 Undergrads, they have some methods like you could create 75 00:03:11,640 --> 00:03:13,090 an instance of it -- you'd have its year. 76 00:03:13,090 --> 00:03:15,330 You could give back what the year was. 77 00:03:15,330 --> 00:03:17,440 And for the graduate students, we didn't even bother with 78 00:03:17,440 --> 00:03:18,120 putting details in. 79 00:03:18,120 --> 00:03:21,350 It could be anything you want. 80 00:03:21,350 --> 00:03:23,270 Now these are things that you ought to be getting used to 81 00:03:23,270 --> 00:03:23,700 looking at. 82 00:03:23,700 --> 00:03:25,610 So this is creating some class definitions. 83 00:03:25,610 --> 00:03:26,820 I can make some instances. 84 00:03:26,820 --> 00:03:29,945 There are a couple of nice variations like, for the MIT 85 00:03:29,945 --> 00:03:32,790 people, each one has a unique identifier, which lets us keep 86 00:03:32,790 --> 00:03:34,390 track of them. 87 00:03:34,390 --> 00:03:36,650 I think where Professor Guttag ended up last time was 88 00:03:36,650 --> 00:03:39,810 actually creating a course list. 89 00:03:39,810 --> 00:03:43,670 So a list of students in a class, like 6.00. 90 00:03:43,670 --> 00:03:45,700 So I want to show you that, and I want to show you some 91 00:03:45,700 --> 00:03:46,590 highlights of that. 92 00:03:46,590 --> 00:03:48,830 So there's the definition. 93 00:03:48,830 --> 00:03:51,620 It's on your handout as well. 94 00:03:51,620 --> 00:03:52,900 So what does a course list do? 95 00:03:55,430 --> 00:03:57,490 Well, the idea should be, it's a way of collecting together 96 00:03:57,490 --> 00:03:59,370 all the students in 6.00. 97 00:03:59,370 --> 00:04:03,910 So if you look at our init function up here, when we 98 00:04:03,910 --> 00:04:07,320 create an instance of a course list, what does it do? 99 00:04:07,320 --> 00:04:09,960 Well, we give it a number, like 6.00. 100 00:04:09,960 --> 00:04:12,260 And it's going to bind that locally inside of a 101 00:04:12,260 --> 00:04:12,820 definition. 102 00:04:12,820 --> 00:04:17,230 And it's going to create a collection, a list. 103 00:04:17,230 --> 00:04:19,899 It's going to be a list of all the students. 104 00:04:19,899 --> 00:04:22,070 Now if I'm going to do that, I need to have a way of putting 105 00:04:22,070 --> 00:04:23,230 students into the class. 106 00:04:23,230 --> 00:04:26,110 And I know nobody drops 6.00, but just in case-- drop date's 107 00:04:26,110 --> 00:04:28,630 coming up-- we have to have some way of removing a student 108 00:04:28,630 --> 00:04:29,820 from a class. 109 00:04:29,820 --> 00:04:33,450 So I'm going to have a method right here-- 110 00:04:33,450 --> 00:04:35,080 oops, sorry, I'm going to get it from the right 111 00:04:35,080 --> 00:04:37,120 place, right there-- 112 00:04:37,120 --> 00:04:40,210 that would add a student in. 113 00:04:40,210 --> 00:04:43,210 Now take a look at that for a second. 114 00:04:43,210 --> 00:04:46,760 It's not just putting the student into the list. 115 00:04:46,760 --> 00:04:48,880 The very bottom down here, that last line, 116 00:04:48,880 --> 00:04:50,960 self.students.append(). 117 00:04:50,960 --> 00:04:51,710 What's that doing? 118 00:04:51,710 --> 00:04:54,400 Well, self, remember, refers to the instance. 119 00:04:54,400 --> 00:04:57,990 .students will get me that list for that instance. 120 00:04:57,990 --> 00:05:01,030 And then .append says, add on to the end of that list the 121 00:05:01,030 --> 00:05:03,380 person that I just added in, the person 122 00:05:03,380 --> 00:05:05,390 that is bound to who. 123 00:05:05,390 --> 00:05:07,940 The things above it are being careful. 124 00:05:07,940 --> 00:05:09,690 They're defensive programming. 125 00:05:09,690 --> 00:05:13,330 They're ways of saying, look, make sure that the person is 126 00:05:13,330 --> 00:05:15,172 actually a student. 127 00:05:15,172 --> 00:05:15,660 Huh. 128 00:05:15,660 --> 00:05:18,120 Wait a minute, where'd that come from? 129 00:05:18,120 --> 00:05:19,770 isStudent. 130 00:05:19,770 --> 00:05:24,280 Oh, yeah, that was up here in MIT person. 131 00:05:24,280 --> 00:05:26,680 So we're making an assumption here that says, we're only 132 00:05:26,680 --> 00:05:27,520 adding in students. 133 00:05:27,520 --> 00:05:29,840 No post-docs are welcome. 134 00:05:29,840 --> 00:05:31,100 We're going to only add in students. 135 00:05:31,100 --> 00:05:33,680 So we're taking instances of one of these two classes, and 136 00:05:33,680 --> 00:05:36,750 checking using the inherited method to make sure that this 137 00:05:36,750 --> 00:05:38,430 person is actually a student. 138 00:05:38,430 --> 00:05:40,150 And we're doing this in a careful way, which is we're 139 00:05:40,150 --> 00:05:43,570 going to raise an error if, in fact, it's not a student. 140 00:05:43,570 --> 00:05:45,320 And then the second thing, notice, that we're going to do 141 00:05:45,320 --> 00:05:46,870 here is we're going to say, gee, make sure that we're not 142 00:05:46,870 --> 00:05:48,590 adding you twice. 143 00:05:48,590 --> 00:05:49,730 So we're going to do what? 144 00:05:49,730 --> 00:05:51,880 We're going to say, get out the list of 145 00:05:51,880 --> 00:05:55,270 students right there. 146 00:05:55,270 --> 00:05:56,160 and I begin. 147 00:05:56,160 --> 00:05:58,930 self is pointing to the instance.students gives me the 148 00:05:58,930 --> 00:06:00,550 list of students currently in that instance. 149 00:06:00,550 --> 00:06:04,580 And I'm basically saying, is this person in that list? 150 00:06:04,580 --> 00:06:06,410 And if they are, again, I'm going to raise an error 151 00:06:06,410 --> 00:06:08,990 saying, wait a minute, you're already in the class. 152 00:06:08,990 --> 00:06:11,120 So these are things I'm sure you've seen before. 153 00:06:11,120 --> 00:06:12,820 But they're ways of being a little careful about the 154 00:06:12,820 --> 00:06:14,080 programming. 155 00:06:14,080 --> 00:06:15,940 The removeStudent is roughly the same. 156 00:06:15,940 --> 00:06:18,690 So let's just look at some examples here. 157 00:06:18,690 --> 00:06:21,680 I've created a set of people. 158 00:06:21,680 --> 00:06:25,100 And just to show you what they look like, let's go down here. 159 00:06:25,100 --> 00:06:26,350 So I've created m1. 160 00:06:29,170 --> 00:06:31,470 Let me do that again, sorry about that. 161 00:06:31,470 --> 00:06:33,406 I'm going to run this. 162 00:06:33,406 --> 00:06:34,210 All right. 163 00:06:34,210 --> 00:06:35,520 So if I look at m1-- 164 00:06:39,560 --> 00:06:40,620 it's not liking me, is it? 165 00:06:40,620 --> 00:06:41,830 We'll try it one more time. 166 00:06:41,830 --> 00:06:44,250 I'm going to look at m1. 167 00:06:44,250 --> 00:06:47,650 It gives me back an instance of an object. 168 00:06:47,650 --> 00:06:48,230 [UNINTELLIGIBLE] 169 00:06:48,230 --> 00:06:54,140 if I actually say a print of m1, it'll print out the name. 170 00:06:54,140 --> 00:06:55,740 And the same thing for the undergraduates here. 171 00:06:55,740 --> 00:06:57,490 So I've made a couple of undergraduates, I've made a 172 00:06:57,490 --> 00:06:58,750 couple of graduate students. 173 00:06:58,750 --> 00:07:00,400 And notice what I've done down here. 174 00:07:00,400 --> 00:07:05,910 I've created a course, SixHundred, by calling 175 00:07:05,910 --> 00:07:06,350 CourseList. 176 00:07:06,350 --> 00:07:08,380 So CourseList is going to make an instance of that. 177 00:07:08,380 --> 00:07:09,630 I gave it the name. 178 00:07:09,630 --> 00:07:10,640 So we can go back over here. 179 00:07:10,640 --> 00:07:12,055 Let's look at what SixHundred says. 180 00:07:16,010 --> 00:07:17,245 OK. 181 00:07:17,245 --> 00:07:19,200 I'm sure you're used to reading that gobbledygook. 182 00:07:19,200 --> 00:07:23,070 It says, ah, it's an instance, and it's an instance of a 183 00:07:23,070 --> 00:07:24,170 CourseList. 184 00:07:24,170 --> 00:07:26,960 And it gives me a pointer to where it is. 185 00:07:26,960 --> 00:07:32,550 Now what if I want to see the students inside this course? 186 00:07:32,550 --> 00:07:35,510 I'd like to see, who are all the students there? 187 00:07:35,510 --> 00:07:38,045 So printing SixHundred-- 188 00:07:38,045 --> 00:07:39,860 let's see if that does the right thing. 189 00:07:42,390 --> 00:07:43,960 Oops, no. 190 00:07:43,960 --> 00:07:44,930 That kind of makes sense, right? 191 00:07:44,930 --> 00:07:46,390 SixHundred is just pointing to an instance. 192 00:07:46,390 --> 00:07:48,110 So where are the students? 193 00:07:48,110 --> 00:07:51,960 They're sitting down inside of an internal variable, which 194 00:07:51,960 --> 00:07:53,940 is, in fact, in this case, the list of students. 195 00:07:53,940 --> 00:08:02,690 So one way I could do this is I could say, take SixHundred, 196 00:08:02,690 --> 00:08:05,950 which is an instance, and go into that frame that 197 00:08:05,950 --> 00:08:08,250 corresponds to that and look up the 198 00:08:08,250 --> 00:08:10,050 local variable, students. 199 00:08:10,050 --> 00:08:13,670 And if I do that, oh, I've got a bunch of 200 00:08:13,670 --> 00:08:16,790 students named Main. 201 00:08:16,790 --> 00:08:18,320 Now this shouldn't surprise you, right? 202 00:08:18,320 --> 00:08:19,110 This is doing what? 203 00:08:19,110 --> 00:08:22,540 It's giving me back that instance of the list. 204 00:08:22,540 --> 00:08:25,190 If I actually want to print out the names of the students, 205 00:08:25,190 --> 00:08:26,920 I have to do something a little more careful. 206 00:08:26,920 --> 00:08:28,870 So here's one way I could do it. 207 00:08:28,870 --> 00:08:39,047 I could say, for every student in that list, let's print out 208 00:08:39,047 --> 00:08:40,297 their name. 209 00:08:42,179 --> 00:08:43,429 Oh, good. 210 00:08:45,890 --> 00:08:47,300 You're wondering, why am I doing this? 211 00:08:47,300 --> 00:08:49,050 Well, I want to show you what's going on in this class. 212 00:08:49,050 --> 00:08:51,100 And then I want to highlight something for you. 213 00:08:51,100 --> 00:08:52,175 So let's think about what I just said. 214 00:08:52,175 --> 00:08:55,130 I want to get the names of all the students in the class. 215 00:08:55,130 --> 00:08:58,350 So I took the instance, I went in and pulled out an internal 216 00:08:58,350 --> 00:09:01,020 variable, and then I did a for loop that walked along it, 217 00:09:01,020 --> 00:09:02,190 printing those out. 218 00:09:02,190 --> 00:09:04,810 Is that a good idea? 219 00:09:04,810 --> 00:09:07,770 Is that a not-so-good idea? 220 00:09:07,770 --> 00:09:10,590 Is that a really bad idea? 221 00:09:10,590 --> 00:09:12,990 Don't you hate professors who ask questions at 10 o'clock in 222 00:09:12,990 --> 00:09:14,840 the morning? 223 00:09:14,840 --> 00:09:17,710 Well, I'm going to suggest that it's not a great idea. 224 00:09:17,710 --> 00:09:19,490 And here's why. 225 00:09:19,490 --> 00:09:23,870 I'm reaching into an instance and pulling out an internal 226 00:09:23,870 --> 00:09:26,715 variable and doing something with it. 227 00:09:26,715 --> 00:09:29,980 A much cleaner way of dealing with that is to build an 228 00:09:29,980 --> 00:09:32,925 interface, to build a function, a method, that 229 00:09:32,925 --> 00:09:35,410 belongs to that instance, that would give me back 230 00:09:35,410 --> 00:09:37,110 the pieces I want. 231 00:09:37,110 --> 00:09:39,500 And the reason I'd like that is then I don't have to worry 232 00:09:39,500 --> 00:09:42,500 about, if I change my mind in terms of how I represent 233 00:09:42,500 --> 00:09:44,970 things internally, all I have to do is think about that 234 00:09:44,970 --> 00:09:45,585 interface function. 235 00:09:45,585 --> 00:09:47,630 Right 236 00:09:47,630 --> 00:09:49,730 So the last thing I want to show you here in this example 237 00:09:49,730 --> 00:09:52,960 is let's look, in fact, right here. 238 00:09:52,960 --> 00:09:57,130 I've added a method called allStudents. 239 00:09:57,130 --> 00:09:58,960 But it's a little different, I think, than things you've 240 00:09:58,960 --> 00:10:01,120 probably seen before. 241 00:10:01,120 --> 00:10:01,740 So what's the idea? 242 00:10:01,740 --> 00:10:03,420 I want allStudents. 243 00:10:03,420 --> 00:10:04,650 To be a function, or a method. 244 00:10:04,650 --> 00:10:06,350 When I call that, I want it to give me 245 00:10:06,350 --> 00:10:09,730 back all of the students. 246 00:10:09,730 --> 00:10:12,210 Now, I could have had it just give me back the list, but I 247 00:10:12,210 --> 00:10:15,730 might want to have the method give me back not just all the 248 00:10:15,730 --> 00:10:17,520 students, but maybe some students, like only the 249 00:10:17,520 --> 00:10:19,690 undergraduates in the class. 250 00:10:19,690 --> 00:10:22,490 And in fact, right below there, I've got a 251 00:10:22,490 --> 00:10:25,100 method that does that. 252 00:10:25,100 --> 00:10:25,770 Let's look at this one. 253 00:10:25,770 --> 00:10:27,040 It says, I want to get back the 254 00:10:27,040 --> 00:10:29,010 undergraduates in the class. 255 00:10:29,010 --> 00:10:30,340 And what's it do? 256 00:10:30,340 --> 00:10:31,560 It's going to do a little counting. 257 00:10:31,560 --> 00:10:33,880 It's going to set index to 0. 258 00:10:33,880 --> 00:10:35,830 And then it's going to run through a while loop where it 259 00:10:35,830 --> 00:10:40,910 basically takes each student and checks its type. 260 00:10:40,910 --> 00:10:43,575 This is something you've seen before, taking a loop and 261 00:10:43,575 --> 00:10:46,120 walking along, taking each element of that list and 262 00:10:46,120 --> 00:10:47,760 saying, if it's an undergraduate, I'm going to 263 00:10:47,760 --> 00:10:50,360 give you back that student, and I'm incrementing index at 264 00:10:50,360 --> 00:10:52,830 the bottom. 265 00:10:52,830 --> 00:10:54,680 Here's the funky thing. 266 00:10:54,680 --> 00:10:56,940 I've used a word called yield. 267 00:10:56,940 --> 00:10:59,640 And that's different than you've seen before. 268 00:10:59,640 --> 00:11:02,160 I'm not directly accumulating a list. 269 00:11:02,160 --> 00:11:04,550 You don't see anywhere where I'm building up a list here. 270 00:11:04,550 --> 00:11:09,630 So yield is an example of a form that you're going to want 271 00:11:09,630 --> 00:11:15,630 to use called a generator. 272 00:11:20,780 --> 00:11:26,560 Now a generator is a little bit like a return, but with 273 00:11:26,560 --> 00:11:27,410 one big exception. 274 00:11:27,410 --> 00:11:30,520 If I had a return in that method somewhere, when it 275 00:11:30,520 --> 00:11:32,200 found the first thing, it's going to give me back the 276 00:11:32,200 --> 00:11:33,710 value and it's done. 277 00:11:33,710 --> 00:11:36,080 And so any information it stored about where it was in 278 00:11:36,080 --> 00:11:37,225 that computation-- 279 00:11:37,225 --> 00:11:40,680 the phrase we use is it gets popped off the stack frame. 280 00:11:40,680 --> 00:11:42,040 Yield is a little different. 281 00:11:42,040 --> 00:11:44,415 And it's different because a generator-- 282 00:11:44,415 --> 00:11:46,620 I'm going to write this down carefully. 283 00:11:46,620 --> 00:11:59,180 A generator is a function that remembers the point in the 284 00:11:59,180 --> 00:12:01,280 body where it was. 285 00:12:06,417 --> 00:12:10,460 It remembers the point in the function body where it last 286 00:12:10,460 --> 00:12:22,410 returned, plus all the local variables. 287 00:12:27,450 --> 00:12:29,220 So what does this mean? 288 00:12:29,220 --> 00:12:33,240 It says, it's going to keep track of what was the state 289 00:12:33,240 --> 00:12:36,720 when I did the computation, so that if I call it a second 290 00:12:36,720 --> 00:12:40,100 time, it goes right back to that state and continues the 291 00:12:40,100 --> 00:12:40,740 computation. 292 00:12:40,740 --> 00:12:42,860 If I called it a third time, it goes right back to that 293 00:12:42,860 --> 00:12:45,430 state and gives you a continuation on the 294 00:12:45,430 --> 00:12:46,840 computation. 295 00:12:46,840 --> 00:12:50,000 So in fact, what yield does, what a generator does, is what 296 00:12:50,000 --> 00:12:52,650 you would do what you normally walk through a list. 297 00:12:52,650 --> 00:12:54,660 But it gives you some control. 298 00:12:54,660 --> 00:12:57,940 So it gives you a way of actually deciding, how do I 299 00:12:57,940 --> 00:13:02,160 want to create something I could use in a for list. 300 00:13:02,160 --> 00:13:04,580 And if we go back and look at it, you can 301 00:13:04,580 --> 00:13:05,830 see it right there. 302 00:13:08,260 --> 00:13:12,300 The method definition for UGs, undergraduates-- 303 00:13:12,300 --> 00:13:16,510 if I call it, it is basically going to walk through, one at 304 00:13:16,510 --> 00:13:20,110 a time, each element in the underlying representation and 305 00:13:20,110 --> 00:13:22,280 generate for me the next element of the 306 00:13:22,280 --> 00:13:24,070 thing I want to use. 307 00:13:24,070 --> 00:13:25,910 So let me show you a couple of examples of using it. 308 00:13:25,910 --> 00:13:28,550 In this case, I've buried inside of a for or a while 309 00:13:28,550 --> 00:13:33,360 loop, but if I go over here, I should be able to do things 310 00:13:33,360 --> 00:13:38,725 like, are all the students in SixHundred? 311 00:13:44,550 --> 00:13:46,010 And notice the open/close paren, 312 00:13:46,010 --> 00:13:46,800 because that's a method. 313 00:13:46,800 --> 00:13:48,050 I need to call it. 314 00:13:53,190 --> 00:13:54,510 Now you're going, whoop-de-do. 315 00:13:54,510 --> 00:13:56,570 That's just like the print thing I did before. 316 00:13:56,570 --> 00:13:59,320 But the difference here is I'm controlling now how I get 317 00:13:59,320 --> 00:14:00,480 access to those. 318 00:14:00,480 --> 00:14:02,290 And to show you just a slightly different way of 319 00:14:02,290 --> 00:14:05,290 doing this, I could now do things like say the following. 320 00:14:05,290 --> 00:14:15,060 For all the students in SixHundred, and I'm going to 321 00:14:15,060 --> 00:14:17,460 take all the students again in SixHundred. 322 00:14:22,290 --> 00:14:24,730 So notice what I'm doing, two loops. 323 00:14:24,730 --> 00:14:27,130 And I'm going to create something that basically keeps 324 00:14:27,130 --> 00:14:30,030 track of where I am in each one of those loops using that 325 00:14:30,030 --> 00:14:31,280 yield function. 326 00:14:37,270 --> 00:14:39,212 So take a look at what it printed out. 327 00:14:39,212 --> 00:14:42,690 That outer loop is using the generator to generate each 328 00:14:42,690 --> 00:14:43,610 element of the list. 329 00:14:43,610 --> 00:14:46,940 It hangs onto that, so that the inner loop can walk 330 00:14:46,940 --> 00:14:49,020 through again, a generator going through each 331 00:14:49,020 --> 00:14:50,510 element of that list. 332 00:14:50,510 --> 00:14:52,660 And I'm printing out students squared -- 333 00:14:52,660 --> 00:14:54,820 first student times all other students, second student times 334 00:14:54,820 --> 00:14:56,080 all other students, third student 335 00:14:56,080 --> 00:14:58,050 times all other students. 336 00:14:58,050 --> 00:15:00,870 And of course the other thing I can do is I 337 00:15:00,870 --> 00:15:03,670 can do the same thing-- 338 00:15:03,670 --> 00:15:09,300 sorry about that-- for s in SixHundred undergrads. 339 00:15:14,760 --> 00:15:16,970 And in this case, it's walking through that internal 340 00:15:16,970 --> 00:15:20,310 representation, generating each element that satisfies 341 00:15:20,310 --> 00:15:21,560 the constraints. 342 00:15:23,560 --> 00:15:25,590 OK. 343 00:15:25,590 --> 00:15:27,590 It's a little esoteric, but you're going to 344 00:15:27,590 --> 00:15:29,500 find it really useful. 345 00:15:29,500 --> 00:15:31,330 And it's the last piece, I think, of syntax you're going 346 00:15:31,330 --> 00:15:34,360 to see for a while in terms of the language. 347 00:15:34,360 --> 00:15:35,800 You could do this by just 348 00:15:35,800 --> 00:15:38,060 manipulating the list directly. 349 00:15:38,060 --> 00:15:40,580 But the idea of a generator you're going to find really 350 00:15:40,580 --> 00:15:43,680 useful when you want to control access to a 351 00:15:43,680 --> 00:15:44,930 collection. 352 00:15:44,930 --> 00:15:49,410 Really think of the generator as something that keeps track 353 00:15:49,410 --> 00:15:52,040 of the state of the computation, so it can go back 354 00:15:52,040 --> 00:15:55,540 and pick it up whenever it needs it. 355 00:15:55,540 --> 00:15:57,090 OK. 356 00:15:57,090 --> 00:15:58,830 That wraps up that piece of what 357 00:15:58,830 --> 00:16:00,240 Professor Guttag was doing. 358 00:16:00,240 --> 00:16:04,520 So I want to now switch to a completely different topic, 359 00:16:04,520 --> 00:16:06,210 which we're going to do today, we're going to do Thursday, 360 00:16:06,210 --> 00:16:09,990 and it may well carry into after spring break. 361 00:16:09,990 --> 00:16:13,440 The second topic deals with, how do we go about building 362 00:16:13,440 --> 00:16:18,530 computational models to solve real problems. 363 00:16:18,530 --> 00:16:20,995 And I want to start by putting up here a comparison. 364 00:16:23,790 --> 00:16:33,570 For much of the history of science, people focused on 365 00:16:33,570 --> 00:16:36,720 trying to find analytic methods. 366 00:16:36,720 --> 00:16:39,060 Big word, what does it mean? 367 00:16:39,060 --> 00:16:41,860 Well, an analytic model is something that lets you 368 00:16:41,860 --> 00:16:46,050 precisely predict the behavior of a system, just based on 369 00:16:46,050 --> 00:16:49,420 some initial conditions and a set of parameters. 370 00:16:49,420 --> 00:16:53,600 So this is a mathematical function, if you like. 371 00:16:53,600 --> 00:17:07,760 You can predict behavior given some initial conditions and 372 00:17:07,760 --> 00:17:09,010 some parameters. 373 00:17:13,390 --> 00:17:16,599 Newtonian physics, 8.01-- 374 00:17:16,599 --> 00:17:17,980 analytic models. 375 00:17:17,980 --> 00:17:19,579 Spring constants-- analytic models. 376 00:17:19,579 --> 00:17:20,880 They're things that let you predict what a 377 00:17:20,880 --> 00:17:24,170 system's going to do. 378 00:17:24,170 --> 00:17:26,859 For, I don't know, several centuries, this was basically 379 00:17:26,859 --> 00:17:27,640 what science did. 380 00:17:27,640 --> 00:17:28,510 It was really useful. 381 00:17:28,510 --> 00:17:31,960 It lead to things like calculus and to things like 382 00:17:31,960 --> 00:17:33,710 probability theory. 383 00:17:33,710 --> 00:17:39,540 It lead to basically understanding the macroscopic 384 00:17:39,540 --> 00:17:40,370 physical world. 385 00:17:40,370 --> 00:17:43,720 So it's the stuff you saw in high school. 386 00:17:43,720 --> 00:17:44,970 That's nice. 387 00:17:44,970 --> 00:17:48,030 But it doesn't always work. 388 00:17:48,030 --> 00:17:51,090 And indeed, as the 20th century progressed, one of the 389 00:17:51,090 --> 00:17:54,860 things we saw was that there are places where this doesn't 390 00:17:54,860 --> 00:18:01,750 really satisfy what you need, and you're better off with 391 00:18:01,750 --> 00:18:06,190 simulation methods, which is what we're going 392 00:18:06,190 --> 00:18:07,020 to talk about here. 393 00:18:07,020 --> 00:18:08,460 So what do I mean by simulation methods? 394 00:18:08,460 --> 00:18:10,420 First of all, let me talk about why do we want to get a 395 00:18:10,420 --> 00:18:11,260 simulation method. 396 00:18:11,260 --> 00:18:13,930 The idea was that there are going to be some places where 397 00:18:13,930 --> 00:18:15,610 it's really hard to build a model. 398 00:18:15,610 --> 00:18:17,850 So in fact, places where this comes up-- 399 00:18:22,950 --> 00:18:24,890 sometimes we have systems that are not 400 00:18:24,890 --> 00:18:26,140 mathematically tractable. 401 00:18:34,120 --> 00:18:35,180 Again, a highfalutin word. 402 00:18:35,180 --> 00:18:35,650 What does it mean? 403 00:18:35,650 --> 00:18:37,620 It says, there are some systems where it's really hard 404 00:18:37,620 --> 00:18:40,810 to build a physical model that exactly predicts 405 00:18:40,810 --> 00:18:42,370 what's going to happen. 406 00:18:42,370 --> 00:18:45,130 Think weather forecasting. 407 00:18:45,130 --> 00:18:45,810 It doesn't work well. 408 00:18:45,810 --> 00:18:47,050 Actually, in Boston, it's easy. 409 00:18:47,050 --> 00:18:48,690 Just assume it's going to snow, and you're fine. 410 00:18:51,522 --> 00:18:51,800 All right. 411 00:18:51,800 --> 00:18:53,020 That's one reason why we want to move 412 00:18:53,020 --> 00:18:53,960 towards simulation models. 413 00:18:53,960 --> 00:18:55,600 Here's a second reason why we'd like to move towards 414 00:18:55,600 --> 00:18:56,320 simulation models. 415 00:18:56,320 --> 00:18:59,540 There's times when, in fact, as things get more complex, 416 00:18:59,540 --> 00:19:08,870 we're better off just successively refining a series 417 00:19:08,870 --> 00:19:10,120 of simulations. 418 00:19:13,720 --> 00:19:15,670 In other words, rather than spending the effort trying to 419 00:19:15,670 --> 00:19:18,700 build a very detailed predictive model, analytic 420 00:19:18,700 --> 00:19:21,480 model, just run a simulation that gives us an insight to 421 00:19:21,480 --> 00:19:22,270 what the problem's doing. 422 00:19:22,270 --> 00:19:24,080 We'll refine the simulation, and we can keep 423 00:19:24,080 --> 00:19:25,445 going on like that. 424 00:19:25,445 --> 00:19:27,810 The third reason you might like to do it is it's often 425 00:19:27,810 --> 00:19:36,210 easier to extract useful intermediate results from a 426 00:19:36,210 --> 00:19:38,030 simulation than it is to try and build a 427 00:19:38,030 --> 00:19:39,310 detailed analytic model. 428 00:19:42,750 --> 00:19:45,320 And of course, the last one is pretty obvious. 429 00:19:48,680 --> 00:19:52,020 The advent of computers made this possible. 430 00:19:52,020 --> 00:19:54,720 I want to just give you an example of this. 431 00:19:54,720 --> 00:19:58,150 Put yourself back in the 1700s and think about how you would 432 00:19:58,150 --> 00:20:00,560 do a computation. 433 00:20:00,560 --> 00:20:01,490 You have two choices. 434 00:20:01,490 --> 00:20:04,140 You can crank it out by hand. 435 00:20:04,140 --> 00:20:05,550 Good luck. 436 00:20:05,550 --> 00:20:07,200 Imagine how detailed a model you could 437 00:20:07,200 --> 00:20:08,700 actually do that way. 438 00:20:08,700 --> 00:20:11,240 Or you could build really precise, 439 00:20:11,240 --> 00:20:13,700 detailed mechanical models. 440 00:20:13,700 --> 00:20:15,450 And in fact, there are wonderful collections of these 441 00:20:15,450 --> 00:20:17,030 from people like Leonardo. 442 00:20:17,030 --> 00:20:18,980 For example, people that wanted to understand the solar 443 00:20:18,980 --> 00:20:23,350 system built what were called mechanical orreries, which 444 00:20:23,350 --> 00:20:26,360 were these very complex pieces of clockwork that allowed you 445 00:20:26,360 --> 00:20:29,530 to try and predict the motions of the planets. 446 00:20:29,530 --> 00:20:32,680 That's a really expensive way to try and do a simulation. 447 00:20:32,680 --> 00:20:35,620 So computation suddenly makes things change a lot. 448 00:20:35,620 --> 00:20:39,650 Now just to give you a sense of this, I did the following 449 00:20:39,650 --> 00:20:43,030 experiment this morning, to see how 450 00:20:43,030 --> 00:20:44,080 important simulation is. 451 00:20:44,080 --> 00:20:45,940 And I haven't really said what simulation means. 452 00:20:45,940 --> 00:20:50,750 So simulation means giving me an estimate, rather than a 453 00:20:50,750 --> 00:20:51,160 prediction. 454 00:20:51,160 --> 00:20:53,700 Giving me a sense of what might happen to a system under 455 00:20:53,700 --> 00:20:55,720 certain conditions and doing that multiple times, actually 456 00:20:55,720 --> 00:20:58,830 running, if you like, a model of the system rather than 457 00:20:58,830 --> 00:21:01,130 trying to predict exactly what's going to happen. 458 00:21:01,130 --> 00:21:02,590 So I did the following experiment this morning. 459 00:21:02,590 --> 00:21:03,586 I went to Google-- 460 00:21:03,586 --> 00:21:05,560 I could have gone to Bing-- 461 00:21:05,560 --> 00:21:10,480 and I typed in "finance simulation." I got 5 million 462 00:21:10,480 --> 00:21:13,690 hits, most of them probably wrong by the 463 00:21:13,690 --> 00:21:15,790 way, but that's OK. 464 00:21:15,790 --> 00:21:19,150 I typed in "biology simulation." I 465 00:21:19,150 --> 00:21:21,900 got 11 million hits. 466 00:21:21,900 --> 00:21:25,110 I typed in "physics simulation." I 467 00:21:25,110 --> 00:21:27,750 got 15 million hits. 468 00:21:27,750 --> 00:21:31,880 And then finally, I typed in "game simulation." How many 469 00:21:31,880 --> 00:21:34,030 hits do you figure I got? 470 00:21:34,030 --> 00:21:37,420 North or south of 15 million? 471 00:21:37,420 --> 00:21:37,990 North. 472 00:21:37,990 --> 00:21:38,340 Yeah. 473 00:21:38,340 --> 00:21:41,090 50 million hits. 474 00:21:41,090 --> 00:21:42,550 OK, so why should you care? 475 00:21:42,550 --> 00:21:43,840 A, it's fun to do. 476 00:21:43,840 --> 00:21:48,550 B, it's really a common tool that we want to use here. 477 00:21:48,550 --> 00:21:50,880 So let's think about what a simulation would look like, 478 00:21:50,880 --> 00:21:52,160 and then we're going to start building one. 479 00:21:52,160 --> 00:21:55,170 So the idea of a simulation, then, is-- what we're trying 480 00:21:55,170 --> 00:22:02,730 to do is we want to build a model with 481 00:22:02,730 --> 00:22:03,980 the following property. 482 00:22:07,160 --> 00:22:20,156 It's going to give useful information about the behavior 483 00:22:20,156 --> 00:22:21,640 of a system. 484 00:22:25,150 --> 00:22:27,980 Boy, there's a statement that has no content to it, right? 485 00:22:27,980 --> 00:22:30,940 Let me tell you what I mean by that. 486 00:22:30,940 --> 00:22:32,690 I'm going to compare this to an analytic model. 487 00:22:32,690 --> 00:22:35,220 An analytic model would exactly predict what this 488 00:22:35,220 --> 00:22:37,160 system's going to do. 489 00:22:37,160 --> 00:22:39,850 With a simulation, we want to build a model that says, if I 490 00:22:39,850 --> 00:22:42,270 give you some sense of the state of the system, it will 491 00:22:42,270 --> 00:22:44,760 give me some information about how that 492 00:22:44,760 --> 00:22:46,400 system is going to behave. 493 00:22:46,400 --> 00:22:51,100 It may not be exactly right, But it's going to give me some 494 00:22:51,100 --> 00:22:51,860 simulation of it. 495 00:22:51,860 --> 00:22:54,540 Another way of thinking about it is it's going to give me an 496 00:22:54,540 --> 00:23:05,105 approximation to reality. 497 00:23:09,230 --> 00:23:22,220 And another way of saying it is simulation models are 498 00:23:22,220 --> 00:23:24,095 descriptive, not prescriptive. 499 00:23:36,200 --> 00:23:38,070 So what does that mean? 500 00:23:38,070 --> 00:23:41,132 An analytic model is prescriptive. 501 00:23:41,132 --> 00:23:42,680 You're doing 8.01 problems. 502 00:23:42,680 --> 00:23:45,420 You type in the definition of the parameters of the problem, 503 00:23:45,420 --> 00:23:47,810 and it would tell you, at least to the accuracy of the 504 00:23:47,810 --> 00:23:50,090 computer, exactly what's going to happen. 505 00:23:50,090 --> 00:23:53,580 With the simulation, it says, given a particular scenario, I 506 00:23:53,580 --> 00:23:55,890 can give you a good guess of what's going to happen. 507 00:23:55,890 --> 00:23:57,910 But it might actually be the case, by the way, that for 508 00:23:57,910 --> 00:24:00,590 exactly the same scenario, I run the simulation multiple 509 00:24:00,590 --> 00:24:03,050 times and I might get slightly different answers. 510 00:24:03,050 --> 00:24:06,270 Because maybe the world, as we said over here, is something 511 00:24:06,270 --> 00:24:09,160 that we can't model exactly mathematically. 512 00:24:09,160 --> 00:24:11,100 So we want to be able to have that ability, to sort of go 513 00:24:11,100 --> 00:24:14,090 back and forth. 514 00:24:14,090 --> 00:24:18,010 Now probably the easiest way to think about this is let's 515 00:24:18,010 --> 00:24:19,860 look at a model and a simulation. 516 00:24:19,860 --> 00:24:22,330 So the idea here is I want to build simulations. 517 00:24:22,330 --> 00:24:25,350 I want to control the fact then I may not 518 00:24:25,350 --> 00:24:26,560 get a precise answer. 519 00:24:26,560 --> 00:24:28,300 I might not get the same answer every time. 520 00:24:28,300 --> 00:24:31,750 But if I do enough simulations of a circumstance, I can get 521 00:24:31,750 --> 00:24:35,730 some good sense which I can refine of how this object's 522 00:24:35,730 --> 00:24:37,680 actually going to behave. 523 00:24:37,680 --> 00:24:41,360 So here's the example I'm going to start with. 524 00:24:41,360 --> 00:24:43,680 I'm going to go back, again, a couple hundred years. 525 00:24:43,680 --> 00:24:49,750 In 1827, a Scottish botanist named Robert Brown observed 526 00:24:49,750 --> 00:24:52,870 that a pollen particle suspended in water seemed to 527 00:24:52,870 --> 00:24:55,470 just float at random. 528 00:24:55,470 --> 00:24:56,420 You've probably heard this term. 529 00:24:56,420 --> 00:24:59,490 It's called Brownian motion, named after Robert Brown. 530 00:24:59,490 --> 00:25:01,210 He had no plausible explanation for 531 00:25:01,210 --> 00:25:01,840 this, by the way. 532 00:25:01,840 --> 00:25:03,040 He just observed it. 533 00:25:03,040 --> 00:25:05,820 And he made no attempt to model it mathematically, which 534 00:25:05,820 --> 00:25:07,620 kind of makes sense. 535 00:25:07,620 --> 00:25:09,781 That's 1827. 536 00:25:09,781 --> 00:25:12,880 The first really clear mathematical model of Brownian 537 00:25:12,880 --> 00:25:15,900 motion didn't come around until 1900. 538 00:25:15,900 --> 00:25:19,020 A guy named Louis Bachelier had a doctoral thesis called 539 00:25:19,020 --> 00:25:22,130 "The Theory of Speculation." Part of the problem for 540 00:25:22,130 --> 00:25:26,470 Bachelier, though, was that he didn't model Brownian in 541 00:25:26,470 --> 00:25:27,210 pollen particles. 542 00:25:27,210 --> 00:25:29,370 He did it in finance markets. 543 00:25:29,370 --> 00:25:31,580 And that was considered, at least at the time, completely 544 00:25:31,580 --> 00:25:32,540 disreputable. 545 00:25:32,540 --> 00:25:35,280 And so nobody paid any attention to his thesis. 546 00:25:35,280 --> 00:25:36,330 That's not going to happen to your thesis. 547 00:25:36,330 --> 00:25:38,190 Your thesis is going to be reputable when you get done 548 00:25:38,190 --> 00:25:39,820 with this place. 549 00:25:39,820 --> 00:25:42,220 So it took eighty years to get to that point. 550 00:25:42,220 --> 00:25:47,310 Unfortunately for poor Bachelier, five years later, 551 00:25:47,310 --> 00:25:51,310 another person came along and actually built the kind of 552 00:25:51,310 --> 00:25:54,970 model that introduced this sort of-- what's called 553 00:25:54,970 --> 00:25:56,790 stochastic thinking into the world of physics. 554 00:25:56,790 --> 00:25:59,580 This was a model that was almost exactly the same as 555 00:25:59,580 --> 00:26:02,570 Bachelier's, but it was used to confirm the 556 00:26:02,570 --> 00:26:04,620 existence of atoms. 557 00:26:04,620 --> 00:26:09,910 Anybody know who did that model in 1905? 558 00:26:09,910 --> 00:26:12,820 No physics buffs here. 559 00:26:12,820 --> 00:26:16,470 If I told you he was born in Switzerland, would that help? 560 00:26:16,470 --> 00:26:19,210 A minor guy named Albert Einstein. 561 00:26:19,210 --> 00:26:21,040 So that was one of the first things Einstein did, was he 562 00:26:21,040 --> 00:26:22,630 actually built the first really good 563 00:26:22,630 --> 00:26:24,360 model of Brownian motion. 564 00:26:24,360 --> 00:26:26,450 And that allowed, in fact, this kind of thinking to go 565 00:26:26,450 --> 00:26:28,840 into real-world problems. 566 00:26:28,840 --> 00:26:46,000 So Brownian motion is an example of a tool we're going 567 00:26:46,000 --> 00:26:47,250 to use a lot. 568 00:26:49,832 --> 00:26:52,090 I'm probably hiding that below the screen where 569 00:26:52,090 --> 00:26:53,100 you can't see it. 570 00:26:53,100 --> 00:26:56,070 It's an example of what we call a random walk. 571 00:26:59,530 --> 00:27:01,600 Random walks you're going to see all over the place. 572 00:27:01,600 --> 00:27:04,900 They're an incredibly useful way of building a simulation. 573 00:27:04,900 --> 00:27:08,860 And the essential idea of a random walk is, if I've got a 574 00:27:08,860 --> 00:27:10,820 system of interacting objects-- 575 00:27:10,820 --> 00:27:12,960 could be pollen particles-- 576 00:27:12,960 --> 00:27:15,640 I want to model what happens in that system under the 577 00:27:15,640 --> 00:27:18,930 assumption that each one of those things is going to move 578 00:27:18,930 --> 00:27:22,590 at each time step under some random distribution. 579 00:27:22,590 --> 00:27:24,050 It's going to move in a particular direction. 580 00:27:24,050 --> 00:27:27,190 I want to model what the overall system does. 581 00:27:27,190 --> 00:27:29,140 OK, let me give you some examples of where this is 582 00:27:29,140 --> 00:27:31,000 really useful. 583 00:27:31,000 --> 00:27:35,540 It's really useful in modeling physical processes. 584 00:27:35,540 --> 00:27:38,280 Well, we're going to start with pollen particles in air. 585 00:27:38,280 --> 00:27:40,950 But you could think about any kind of particle in water. 586 00:27:40,950 --> 00:27:42,980 You could think about any kind of air 587 00:27:42,980 --> 00:27:45,610 particle in a larger fluid. 588 00:27:45,610 --> 00:27:48,740 Ah, weather. 589 00:27:48,740 --> 00:27:50,900 Modeling weather-- if we could really model the motion of all 590 00:27:50,900 --> 00:27:54,820 those molecules-- is just a really large random walk. 591 00:27:54,820 --> 00:27:56,490 Random walks are really useful in 592 00:27:56,490 --> 00:27:58,990 understanding biological processes. 593 00:27:58,990 --> 00:28:02,950 For example, the kinetics of displacement of RNA from 594 00:28:02,950 --> 00:28:06,140 heteroduplexes of DNA is a great 595 00:28:06,140 --> 00:28:08,790 example of a random walk. 596 00:28:08,790 --> 00:28:11,480 And in fact, people interested in bioinformatics or 597 00:28:11,480 --> 00:28:14,810 computational biology will see random walks used all the time 598 00:28:14,810 --> 00:28:18,800 to try and to understand the displacements of things. 599 00:28:18,800 --> 00:28:21,420 It's really useful in social processes. 600 00:28:21,420 --> 00:28:25,500 Movement of the stock market is definitely a random walk, 601 00:28:25,500 --> 00:28:28,340 except for the day when the markets are all crashing for 602 00:28:28,340 --> 00:28:29,920 unfortunate reasons. 603 00:28:29,920 --> 00:28:32,410 So it's something we want to use a lot. 604 00:28:32,410 --> 00:28:34,430 In the example I'm going to use to motivate a random walk, 605 00:28:34,430 --> 00:28:35,590 we're going to build a simulation. 606 00:28:35,590 --> 00:28:37,870 Here is what was the traditional 607 00:28:37,870 --> 00:28:39,460 view of a random walk. 608 00:28:39,460 --> 00:28:40,740 So here's the motivation. 609 00:28:40,740 --> 00:28:43,410 Excuse me a second. 610 00:28:43,410 --> 00:28:46,580 Let's take a drunken university student. 611 00:28:46,580 --> 00:28:48,770 Not an Institute student, a University student. 612 00:28:48,770 --> 00:28:51,270 So this is a Harvard student, not an MIT student, because I 613 00:28:51,270 --> 00:28:53,910 know you're all well-behaved. 614 00:28:53,910 --> 00:28:56,180 At least smile at me when I make these bad jokes. 615 00:28:56,180 --> 00:28:57,160 Thank you. 616 00:28:57,160 --> 00:28:57,370 All right. 617 00:28:57,370 --> 00:28:59,660 You've got a drunken student. 618 00:28:59,660 --> 00:29:02,190 They're out on a big field. 619 00:29:02,190 --> 00:29:04,170 And they start off in the middle of the field, and every 620 00:29:04,170 --> 00:29:07,830 second, this student can take one step in one of the four 621 00:29:07,830 --> 00:29:08,900 cardinal directions. 622 00:29:08,900 --> 00:29:12,670 So north, south, east, west. 623 00:29:12,670 --> 00:29:15,450 After 1,000 seconds-- after 1,000 steps-- 624 00:29:15,450 --> 00:29:17,630 how far away is that student from where he 625 00:29:17,630 --> 00:29:18,880 or she started out? 626 00:29:21,410 --> 00:29:23,740 So I need some help here. 627 00:29:23,740 --> 00:29:24,880 Guesses. 628 00:29:24,880 --> 00:29:25,820 1,000 steps. 629 00:29:25,820 --> 00:29:28,990 How far away after 1,000 steps is that student-- 630 00:29:28,990 --> 00:29:30,200 we'll make it a he. 631 00:29:30,200 --> 00:29:31,450 From where he started out. 632 00:29:35,148 --> 00:29:36,561 Yeah. 633 00:29:36,561 --> 00:29:38,916 AUDIENCE: It depends on the probability of what direction 634 00:29:38,916 --> 00:29:39,890 you're going. 635 00:29:39,890 --> 00:29:41,670 PROFESSOR: OK, so let's assume that the steps 636 00:29:41,670 --> 00:29:42,290 were equally likely. 637 00:29:42,290 --> 00:29:43,975 The four steps-- north, south, east, west-- they're all 638 00:29:43,975 --> 00:29:45,665 equally probable. 639 00:29:45,665 --> 00:29:48,455 AUDIENCE: [INTERPOSING VOICES] 640 00:29:48,455 --> 00:29:49,100 PROFESSOR: OK. 641 00:29:49,100 --> 00:29:51,240 So the suggestion over here is, where he started. 642 00:29:51,240 --> 00:29:53,200 So 0 distance away. 643 00:29:53,200 --> 00:29:54,270 That's not a bad guess, right? 644 00:29:54,270 --> 00:29:56,930 You're just going in different directions. 645 00:29:56,930 --> 00:29:58,830 It's equally likely you end up where you started. 646 00:29:58,830 --> 00:30:01,020 So one possibility is 0. 647 00:30:01,020 --> 00:30:03,060 You end up back where you started. 648 00:30:03,060 --> 00:30:04,454 Yeah. 649 00:30:04,454 --> 00:30:05,900 AUDIENCE: Maybe 150 or so? 650 00:30:05,900 --> 00:30:08,310 PROFESSOR: Well, I'm going to take bets here. 651 00:30:08,310 --> 00:30:09,610 I've got 150. 652 00:30:09,610 --> 00:30:10,670 It's an interesting number. 653 00:30:10,670 --> 00:30:13,190 Not bad. 654 00:30:13,190 --> 00:30:14,030 Any other guesses. 655 00:30:14,030 --> 00:30:14,930 This side of the crowd. 656 00:30:14,930 --> 00:30:16,280 I don't want to just do the right side. 657 00:30:16,280 --> 00:30:16,960 I'm very liberal. 658 00:30:16,960 --> 00:30:18,560 I want the left side of the crowd over here. 659 00:30:18,560 --> 00:30:19,925 Somebody over on this side. 660 00:30:19,925 --> 00:30:21,350 Help me out. 661 00:30:21,350 --> 00:30:22,170 Any other guesses? 662 00:30:22,170 --> 00:30:25,491 I've got 0, and I've got 150 steps. 663 00:30:25,491 --> 00:30:27,630 Boy, I get no eye contact when I do this. 664 00:30:27,630 --> 00:30:28,760 This is great. 665 00:30:28,760 --> 00:30:30,010 Yes, please. 666 00:30:32,473 --> 00:30:34,126 AUDIENCE: 500 root 2 steps? 667 00:30:34,126 --> 00:30:35,172 PROFESSOR: 500 root 2. 668 00:30:35,172 --> 00:30:37,110 Can I drop the root 2 for second? 669 00:30:37,110 --> 00:30:39,400 The 500 is not a bad guess, right? 670 00:30:39,400 --> 00:30:43,750 Because you're about half the distance away. 671 00:30:43,750 --> 00:30:45,590 The root 2 we'll come back to in a second. 672 00:30:45,590 --> 00:30:46,300 Keep that in mind. 673 00:30:46,300 --> 00:30:46,360 All right. 674 00:30:46,360 --> 00:30:46,830 500. 675 00:30:46,830 --> 00:30:52,380 So we've got 0, we've got 150, we've got 500. 676 00:30:52,380 --> 00:30:54,120 Well, gee, that's the whole point of building the 677 00:30:54,120 --> 00:30:54,750 simulation. 678 00:30:54,750 --> 00:30:56,820 Let's see if we can figure out what might happen here. 679 00:30:56,820 --> 00:30:57,750 Ok. 680 00:30:57,750 --> 00:30:59,960 And I'm going to build the simulation in a second. 681 00:30:59,960 --> 00:31:04,970 But before I do that, one of the good things to do is to 682 00:31:04,970 --> 00:31:09,400 try and build a simple model that we could use to get an 683 00:31:09,400 --> 00:31:12,000 intuition about what's going to happen. 684 00:31:12,000 --> 00:31:14,770 So I'm going to start with a very simple model here. 685 00:31:22,280 --> 00:31:23,530 There's my field. 686 00:31:25,900 --> 00:31:28,570 The guys who mow the lawn at Fenway have been over and very 687 00:31:28,570 --> 00:31:32,680 nicely put x- and y-axes onto this field. 688 00:31:32,680 --> 00:31:37,870 And let's assume the students starts off there. 689 00:31:37,870 --> 00:31:38,990 We'll just call that the origin. 690 00:31:38,990 --> 00:31:40,720 It doesn't matter what it is, but that's where the students 691 00:31:40,720 --> 00:31:42,220 starts off. 692 00:31:42,220 --> 00:31:46,550 And what I want to do is I want to get a very rough sense 693 00:31:46,550 --> 00:31:49,220 of how far away the student might go after a few steps. 694 00:31:49,220 --> 00:31:51,360 We won't be able to go very far, but enough steps that we 695 00:31:51,360 --> 00:31:52,430 can start estimating this. 696 00:31:52,430 --> 00:31:54,730 So they start there. 697 00:31:54,730 --> 00:32:00,400 After one step, there are the places the 698 00:32:00,400 --> 00:32:02,070 student could end up. 699 00:32:02,070 --> 00:32:03,490 Equally likely. 700 00:32:03,490 --> 00:32:11,900 So after one step, if I want to build a distance associated 701 00:32:11,900 --> 00:32:16,630 probability, there's probability 1 they're 1 unit 702 00:32:16,630 --> 00:32:20,170 away, no matter which direction they went. 703 00:32:20,170 --> 00:32:22,090 OK, let's take this one here. 704 00:32:25,240 --> 00:32:27,510 Let's assume the student went east. 705 00:32:27,510 --> 00:32:29,210 In fact, they're all going to be roughly the same. 706 00:32:29,210 --> 00:32:32,170 And the second step, where can the student go? 707 00:32:32,170 --> 00:32:37,780 He can go there, he can go there, he can go there, or he 708 00:32:37,780 --> 00:32:38,560 could go there. 709 00:32:38,560 --> 00:32:40,410 Right? 710 00:32:40,410 --> 00:32:41,860 So what are the distances here? 711 00:32:41,860 --> 00:32:51,400 Well, in one out of four cases, he's at a distance 0, 712 00:32:51,400 --> 00:32:53,590 which would be back to where we started, which is that 713 00:32:53,590 --> 00:32:56,230 original pretty good guess. 714 00:32:56,230 --> 00:32:59,220 If he goes there or he goes there, those are both what 715 00:32:59,220 --> 00:33:00,090 distance away? 716 00:33:00,090 --> 00:33:01,890 Root 2, right? 717 00:33:01,890 --> 00:33:07,550 So it could be root 2 away, 2 out of 4 times. 718 00:33:07,550 --> 00:33:10,300 And if we went east again, we're 2 units away. 719 00:33:15,120 --> 00:33:18,300 Again, with probability 1 in 4. 720 00:33:18,300 --> 00:33:20,680 I'm cheating slightly here, in the sense that I should do the 721 00:33:20,680 --> 00:33:23,530 same things if the first step was north, south, or west, but 722 00:33:23,530 --> 00:33:24,610 they're all symmetric to this. 723 00:33:24,610 --> 00:33:25,630 So it'll all come out the same way. 724 00:33:25,630 --> 00:33:27,645 So there is what happens after two steps. 725 00:33:30,150 --> 00:33:31,490 Now let's do one more step. 726 00:33:31,490 --> 00:33:32,560 It's going to get a little messy here. 727 00:33:32,560 --> 00:33:34,920 But suppose I want to go three steps. 728 00:33:40,630 --> 00:33:41,440 About three possibilities. 729 00:33:41,440 --> 00:33:42,130 Well, I've got three possibilities. 730 00:33:42,130 --> 00:33:46,690 If I'm in this case, which is the 0 case, I'm here, in which 731 00:33:46,690 --> 00:33:50,750 case no matter which step I take, I'm 1 unit away. 732 00:33:50,750 --> 00:33:53,780 So this will say, distance 1. 733 00:33:53,780 --> 00:33:56,810 And the probability of being here was 1/4, so the 734 00:33:56,810 --> 00:33:59,760 probability again is still 1/4 that one unit away. 735 00:34:02,670 --> 00:34:04,410 Let's take one of these, the root 2 ones. 736 00:34:04,410 --> 00:34:06,210 That says I'm either here or I'm there. 737 00:34:06,210 --> 00:34:06,980 They're going to be symmetric. 738 00:34:06,980 --> 00:34:10,080 If I'm here, where can I go? 739 00:34:10,080 --> 00:34:14,139 I've got possibilities going to those. 740 00:34:14,139 --> 00:34:18,310 So these two are 1 unit away. 741 00:34:18,310 --> 00:34:20,850 So half the time I'm 1 unit away, and it was 742 00:34:20,850 --> 00:34:22,510 1/2 that got me there. 743 00:34:25,690 --> 00:34:27,260 Did I do that right? 744 00:34:27,260 --> 00:34:29,510 Yes. 745 00:34:29,510 --> 00:34:31,980 That's also with 1/4. 746 00:34:31,980 --> 00:34:34,690 That was getting if I went here or I went there. 747 00:34:34,690 --> 00:34:41,749 If I'm up here or I'm over there, that's root 5 away. 748 00:34:41,749 --> 00:34:45,139 So let me put that over here. 749 00:34:45,139 --> 00:34:47,290 So I've got a probability of 1/2 getting there, and a 750 00:34:47,290 --> 00:34:50,139 probability of 1/2 from here of getting that, so that's 751 00:34:50,139 --> 00:34:53,139 also with probability of 1/4. 752 00:34:53,139 --> 00:34:54,960 And then what's the one case I have left? 753 00:34:54,960 --> 00:34:58,800 The case I have left is I went all the way over to here, two 754 00:34:58,800 --> 00:35:01,420 steps over, in which case I'm going to go what? 755 00:35:01,420 --> 00:35:03,310 There, there, there, or there. 756 00:35:03,310 --> 00:35:04,330 If I just pull these together-- 757 00:35:04,330 --> 00:35:06,140 I'm just going to do them for you. 758 00:35:06,140 --> 00:35:10,240 That says, I have a probability of being 1 away 759 00:35:10,240 --> 00:35:16,490 with probability 1/16, root 5 away with probability 1/8 -- 760 00:35:16,490 --> 00:35:19,710 and I'm running out of room here -- and 3 units away with 761 00:35:19,710 --> 00:35:21,515 probability of 1/16. 762 00:35:24,500 --> 00:35:25,965 Don't sweat the details. 763 00:35:25,965 --> 00:35:27,910 What you can see is I'm just building this up. 764 00:35:27,910 --> 00:35:31,450 What I want you to see is what do the distances look like? 765 00:35:31,450 --> 00:35:37,640 After one step, I'm one unit away. 766 00:35:37,640 --> 00:35:41,710 After two steps, that was this. 767 00:35:41,710 --> 00:35:42,940 You can kind of do the math in your head. 768 00:35:42,940 --> 00:35:44,880 What's the expected distance away? 769 00:35:44,880 --> 00:35:47,600 What's the average distance I'd be away? 770 00:35:47,600 --> 00:35:52,060 Well, it's 0 times 1/4, plus root 2 times 1/2, 771 00:35:52,060 --> 00:35:54,290 plus 2 times 1/4. 772 00:35:54,290 --> 00:35:58,040 So that's 1.4, that's 2.8, and that's 4.8 divided by 4. 773 00:35:58,040 --> 00:36:07,550 So it's about 1.2. 774 00:36:07,550 --> 00:36:09,300 Now I'm sure you can do the math for three steps in your 775 00:36:09,300 --> 00:36:10,990 head, right? 776 00:36:10,990 --> 00:36:12,330 Yeah, right. 777 00:36:12,330 --> 00:36:14,330 But if I do that, if I add those things up-- you can go 778 00:36:14,330 --> 00:36:16,680 do this yourself-- what you'll find is that this is 779 00:36:16,680 --> 00:36:23,870 about 1.4 or 1.5. 780 00:36:23,870 --> 00:36:25,010 Let me make it 1.4. 781 00:36:25,010 --> 00:36:26,300 It's probably a little bit closer. 782 00:36:31,080 --> 00:36:31,560 All right. 783 00:36:31,560 --> 00:36:34,280 Why was I doing that? 784 00:36:34,280 --> 00:36:36,150 This is something you want to do as you start building a 785 00:36:36,150 --> 00:36:39,530 simulation, which is do a little bit of a calculation to 786 00:36:39,530 --> 00:36:43,360 get a sense of what you expect to happen here. 787 00:36:43,360 --> 00:36:47,500 So what conclusion could I draw from that? 788 00:36:47,500 --> 00:36:50,580 Admittedly on very small numbers, it looks like the 789 00:36:50,580 --> 00:36:55,060 more steps the Harvard student takes, on average the further 790 00:36:55,060 --> 00:36:58,450 away they are from the starting point. 791 00:36:58,450 --> 00:37:00,670 So your 0 was a great guess, but it's probably not 792 00:37:00,670 --> 00:37:01,720 going to work here. 793 00:37:01,720 --> 00:37:03,890 And we want to see why. 794 00:37:03,890 --> 00:37:08,150 On the other hand, 500 that looks-- well, 795 00:37:08,150 --> 00:37:09,430 maybe we're still OK. 796 00:37:09,430 --> 00:37:11,460 Three steps, half of 3 would be 1 and 1/2. 797 00:37:11,460 --> 00:37:13,960 So maybe the 500's OK. 798 00:37:13,960 --> 00:37:16,120 Let's see what happens if we do this. 799 00:37:16,120 --> 00:37:21,320 So what I want to do is see how I could build a set of 800 00:37:21,320 --> 00:37:25,180 classes that would let me build this simulation. 801 00:37:25,180 --> 00:37:27,760 And part of the design process here is I want to try and 802 00:37:27,760 --> 00:37:30,930 invent classes that correspond to the types of things I 803 00:37:30,930 --> 00:37:32,890 expect to see happening. 804 00:37:32,890 --> 00:37:35,430 So in this case, what do I have? 805 00:37:35,430 --> 00:37:40,080 I need to model a drunk, I need to model a field, which 806 00:37:40,080 --> 00:37:42,120 is where the drunk's wandering around in. 807 00:37:42,120 --> 00:37:44,290 And I'll need a third thing, which may not be obvious 808 00:37:44,290 --> 00:37:44,890 immediately. 809 00:37:44,890 --> 00:37:46,840 But the third thing I'm going to need-- so let me just put 810 00:37:46,840 --> 00:37:47,540 the pieces up. 811 00:37:47,540 --> 00:37:54,330 I need a drunk, I need a field, and I need to keep 812 00:37:54,330 --> 00:37:57,080 track of where the drunk is in the field. 813 00:37:57,080 --> 00:37:59,080 So I'm going to pull out another class I'm going to 814 00:37:59,080 --> 00:38:02,210 call a location, that's going to tell me where the drunk 815 00:38:02,210 --> 00:38:04,870 actually is. 816 00:38:04,870 --> 00:38:05,830 OK. 817 00:38:05,830 --> 00:38:08,880 With that in mind, I'm going to show you some code. 818 00:38:08,880 --> 00:38:11,340 I'm going to walk through it reasonably carefully, just to 819 00:38:11,340 --> 00:38:12,940 let you see what it looks like. 820 00:38:12,940 --> 00:38:18,660 So there is my class definition for a location. 821 00:38:18,660 --> 00:38:20,610 Now at this stage in the course, hopefully you can look 822 00:38:20,610 --> 00:38:22,530 at that and already see the main pieces of it. 823 00:38:22,530 --> 00:38:23,510 So what do we have in here? 824 00:38:23,510 --> 00:38:25,820 Well, we going to have an initialization. 825 00:38:25,820 --> 00:38:26,930 You can kind of see that right there. 826 00:38:26,930 --> 00:38:29,670 When I create a Location, I'm giving it an x and y. 827 00:38:29,670 --> 00:38:30,850 I'm going to make an assumption, 828 00:38:30,850 --> 00:38:32,030 which is they're floats. 829 00:38:32,030 --> 00:38:33,700 I'll come back to that in a second. 830 00:38:33,700 --> 00:38:35,040 And that's just going to store them away in 831 00:38:35,040 --> 00:38:36,040 some internal variables. 832 00:38:36,040 --> 00:38:37,090 That's things you've done before. 833 00:38:37,090 --> 00:38:40,590 So self.x gets that value, self.y gets that value. 834 00:38:40,590 --> 00:38:46,530 And of course, I could get out the values right there. 835 00:38:46,530 --> 00:38:48,840 I need to know where the drunk goes. 836 00:38:48,840 --> 00:38:52,540 So one of the things I'd like to do is to say, given the 837 00:38:52,540 --> 00:38:54,680 current location of the drunk, I might like to know how far 838 00:38:54,680 --> 00:38:57,280 away is that from some other location, like the place the 839 00:38:57,280 --> 00:38:58,700 drunk started. 840 00:38:58,700 --> 00:39:01,270 So that last method down here, distance from-- 841 00:39:01,270 --> 00:39:03,920 notice what it's going to do. 842 00:39:03,920 --> 00:39:06,720 It says, if you give me another, 843 00:39:06,720 --> 00:39:08,860 which is another location-- 844 00:39:08,860 --> 00:39:10,960 remember, self will point to my instance-- 845 00:39:10,960 --> 00:39:11,440 what can I do? 846 00:39:11,440 --> 00:39:13,930 Well, I can just do Pythagoras's theorem to figure 847 00:39:13,930 --> 00:39:18,470 out how far away am I. I get the x- and y-coordinates of 848 00:39:18,470 --> 00:39:19,370 the other location. 849 00:39:19,370 --> 00:39:21,410 I get my own x- and y-coordinates. 850 00:39:21,410 --> 00:39:24,290 And then I just take those distances squared and take the 851 00:39:24,290 --> 00:39:24,880 square root of them. 852 00:39:24,880 --> 00:39:26,330 So it's just giving me distance away, 853 00:39:26,330 --> 00:39:28,680 which is pretty nice. 854 00:39:28,680 --> 00:39:30,620 There's one other method here which is a little funky. 855 00:39:30,620 --> 00:39:31,830 This is a design choice. 856 00:39:31,830 --> 00:39:36,530 And that method is the one up here called move. 857 00:39:36,530 --> 00:39:38,960 So the background to this is going to have 858 00:39:38,960 --> 00:39:41,210 a drunk at a location. 859 00:39:41,210 --> 00:39:43,620 one of the things I will want is to be 860 00:39:43,620 --> 00:39:44,900 able to change location. 861 00:39:44,900 --> 00:39:47,600 So move says, if you give me a change in x 862 00:39:47,600 --> 00:39:48,770 and a change in y-- 863 00:39:48,770 --> 00:39:50,510 I'm going to assume they're floats-- 864 00:39:50,510 --> 00:39:53,960 I will give you back the new location, which is to go from 865 00:39:53,960 --> 00:39:56,240 my x and y by some amount. 866 00:39:56,240 --> 00:39:57,700 It could be in the east direction. 867 00:39:57,700 --> 00:39:58,990 It could be in the north direction. 868 00:39:58,990 --> 00:40:00,150 But notice what it returns. 869 00:40:00,150 --> 00:40:04,190 It returns a new instance of a Location. 870 00:40:04,190 --> 00:40:05,440 OK. 871 00:40:07,600 --> 00:40:10,170 I made a couple of assumptions which are worth 872 00:40:10,170 --> 00:40:12,200 thinking about here. 873 00:40:12,200 --> 00:40:13,960 So one assumption is I'm assuming that 874 00:40:13,960 --> 00:40:16,330 this is a 2D world. 875 00:40:16,330 --> 00:40:17,580 You can't elevate. 876 00:40:17,580 --> 00:40:18,780 You can't rise up. 877 00:40:18,780 --> 00:40:20,330 There's no change in altitude. 878 00:40:20,330 --> 00:40:22,810 That kind of makes sense. 879 00:40:22,810 --> 00:40:25,640 The second assumption I made was I said, I want to build it 880 00:40:25,640 --> 00:40:30,220 in so that things like delta-x and delta-y are floats. 881 00:40:30,220 --> 00:40:31,370 What is that doing? 882 00:40:31,370 --> 00:40:33,950 Well, that's saying, I don't want to restrict myself to 883 00:40:33,950 --> 00:40:37,440 just moves that are only in the cardinal directions. 884 00:40:37,440 --> 00:40:39,790 I'm going to start there, but initially, I'd like to have 885 00:40:39,790 --> 00:40:42,720 the ability that the move could be along a diagonal, or 886 00:40:42,720 --> 00:40:44,090 it could be some partial step. 887 00:40:44,090 --> 00:40:47,980 So notice I'm making a choice there that is kind of nice. 888 00:40:47,980 --> 00:40:48,210 All right. 889 00:40:48,210 --> 00:40:51,380 That gives me locations. 890 00:40:51,380 --> 00:40:54,880 And that's a pretty straightforward class. 891 00:40:54,880 --> 00:40:56,000 So I've got locations. 892 00:40:56,000 --> 00:40:57,600 That's always a good start. 893 00:40:57,600 --> 00:41:00,730 I have this. 894 00:41:00,730 --> 00:41:02,140 Now again, I want to model a drunk 895 00:41:02,140 --> 00:41:03,330 wandering around in a field. 896 00:41:03,330 --> 00:41:06,090 So the second thing I need is I need to say, what's a field 897 00:41:06,090 --> 00:41:07,680 going to be? 898 00:41:07,680 --> 00:41:11,390 And I want to show you that definition, 899 00:41:11,390 --> 00:41:12,640 which is right here. 900 00:41:16,430 --> 00:41:26,840 And a field is basically going to be something that maps 901 00:41:26,840 --> 00:41:30,315 drunks to locations. 902 00:41:34,190 --> 00:41:36,970 So a field is just going to be a collection of drunks, 903 00:41:36,970 --> 00:41:38,500 keeping track of where they are. 904 00:41:38,500 --> 00:41:40,870 So let's look at what that definition says. 905 00:41:40,870 --> 00:41:46,620 When I create a field, right up here at the top it sets up 906 00:41:46,620 --> 00:41:50,490 a variable inside of that instance, just called drunks. 907 00:41:50,490 --> 00:41:52,631 And we're going to use a dictionary. 908 00:41:52,631 --> 00:41:54,560 We could use other choices, but the dictionary is going to 909 00:41:54,560 --> 00:41:55,820 be nice, because it's going to let us keep 910 00:41:55,820 --> 00:41:57,270 track of the drunks. 911 00:41:57,270 --> 00:41:59,330 I can add a drunk to the field. 912 00:42:02,250 --> 00:42:03,360 So notice what it says. 913 00:42:03,360 --> 00:42:06,360 It says, given I've got a field, I'm assuming I have a 914 00:42:06,360 --> 00:42:09,570 drunk and a location, because the drunk's got to be in a 915 00:42:09,570 --> 00:42:10,630 particular location. 916 00:42:10,630 --> 00:42:12,850 Notice what it does. 917 00:42:12,850 --> 00:42:15,490 First of all, it checks to make sure that I don't already 918 00:42:15,490 --> 00:42:17,720 have this person in my collection. 919 00:42:17,720 --> 00:42:20,620 So that first check there says, if this particular drunk 920 00:42:20,620 --> 00:42:24,260 is already in my dictionary, I complain. 921 00:42:24,260 --> 00:42:28,010 You can be drunk enough to see double, but you can't be drunk 922 00:42:28,010 --> 00:42:30,380 enough to be double. 923 00:42:30,380 --> 00:42:32,630 Another bad joke right off the back wall. 924 00:42:32,630 --> 00:42:33,940 Great. 925 00:42:33,940 --> 00:42:35,180 OK, so what else do I want? 926 00:42:35,180 --> 00:42:38,700 Otherwise, notice what I'm going to do here. 927 00:42:38,700 --> 00:42:40,170 I want to make sure you can see these 928 00:42:40,170 --> 00:42:41,780 things fairly cleanly. 929 00:42:41,780 --> 00:42:44,170 If I'm going to add a drunk, what do I do? 930 00:42:44,170 --> 00:42:49,320 Into the dictionary, under that label of drunk, I'm going 931 00:42:49,320 --> 00:42:51,820 to insert a value corresponding with that label. 932 00:42:51,820 --> 00:42:53,950 And the values going to be the location. 933 00:42:53,950 --> 00:42:56,890 So my representation here is now a dictionary of drunks and 934 00:42:56,890 --> 00:42:58,520 where they are. 935 00:42:58,520 --> 00:43:01,270 And that's going to be handy as I want to use this. 936 00:43:01,270 --> 00:43:04,120 And again, if I want to get out the location, well, I just 937 00:43:04,120 --> 00:43:05,600 return exactly that. 938 00:43:05,600 --> 00:43:09,910 The final piece is, when I go to move the drunk, I've got 939 00:43:09,910 --> 00:43:11,950 some tests here to make sure that I actually have a drunk. 940 00:43:11,950 --> 00:43:15,380 But otherwise, I'm going to ask the drunk to take a step 941 00:43:15,380 --> 00:43:18,350 right there. 942 00:43:18,350 --> 00:43:19,630 We have to say what a drunk does. 943 00:43:19,630 --> 00:43:23,000 That's going to give me back a change in x and a change in y. 944 00:43:23,000 --> 00:43:26,870 And then notice the funky little call below here. 945 00:43:26,870 --> 00:43:27,620 This says what? 946 00:43:27,620 --> 00:43:31,670 The right-hand side says, I get the dictionary of drunks. 947 00:43:31,670 --> 00:43:35,170 I go in and index into it to get out the actual drunk. 948 00:43:35,170 --> 00:43:36,820 That's this first part right here. 949 00:43:39,900 --> 00:43:40,920 That gives me back what? 950 00:43:40,920 --> 00:43:42,450 It gives me an instance. 951 00:43:42,450 --> 00:43:46,570 So the dot says, for that particular drunk, get its move 952 00:43:46,570 --> 00:43:50,040 method, which was that call, and have it 953 00:43:50,040 --> 00:43:53,150 move by that amount. 954 00:43:53,150 --> 00:43:56,470 And that returns for me then a location which I can store 955 00:43:56,470 --> 00:43:59,610 back into the list. 956 00:43:59,610 --> 00:44:01,210 All right. 957 00:44:01,210 --> 00:44:02,930 I made some assumptions about Field. 958 00:44:02,930 --> 00:44:05,900 They're worth pointing out. 959 00:44:05,900 --> 00:44:08,270 No constraints on locations. 960 00:44:08,270 --> 00:44:10,470 How big is this field? 961 00:44:10,470 --> 00:44:11,590 As big as you want. 962 00:44:11,590 --> 00:44:14,960 So I didn't build in a real physical representation of the 963 00:44:14,960 --> 00:44:17,410 limits of the field with every location represented. 964 00:44:17,410 --> 00:44:19,230 I just said, it's just a collection of drunks. 965 00:44:19,230 --> 00:44:21,760 So there's no limits on how far the drunk can go. 966 00:44:21,760 --> 00:44:24,385 The second assumption I made here is I can 967 00:44:24,385 --> 00:44:25,350 have multiple drunks. 968 00:44:25,350 --> 00:44:27,320 And we're going to have a simulation of that in a while. 969 00:44:27,320 --> 00:44:29,630 You can imagine it's going to be fun watching them collide 970 00:44:29,630 --> 00:44:30,060 with each other. 971 00:44:30,060 --> 00:44:31,210 All right. 972 00:44:31,210 --> 00:44:33,860 And the third thing is, it says nothing about the 973 00:44:33,860 --> 00:44:36,450 patterns in which the drunk moves. 974 00:44:36,450 --> 00:44:38,370 So this is a design to keep this clean. 975 00:44:38,370 --> 00:44:40,390 It says nothing about how the drunks move. 976 00:44:40,390 --> 00:44:42,430 It simply says, give me a change in x and y. 977 00:44:42,430 --> 00:44:45,090 I'll change the location. 978 00:44:45,090 --> 00:44:47,630 The last piece is we're going to capture how the drunk moves 979 00:44:47,630 --> 00:44:49,460 in a drunk. 980 00:44:49,460 --> 00:44:51,200 And there's the definition of the drunk. 981 00:44:54,740 --> 00:44:57,285 What does that say to do? 982 00:44:57,285 --> 00:45:00,090 Well, we're going to give it a name, to start it off. 983 00:45:00,090 --> 00:45:02,730 And the only method it has is that thing that's going to do 984 00:45:02,730 --> 00:45:04,390 a takeStep. 985 00:45:04,390 --> 00:45:07,850 And here's where we're going to build in the choices about 986 00:45:07,850 --> 00:45:10,810 how they can move, what you asked about earlier. 987 00:45:10,810 --> 00:45:12,250 Notice what I've done there. 988 00:45:12,250 --> 00:45:16,830 In this case, I've said, I'm going to pick at random from 989 00:45:16,830 --> 00:45:22,360 this set, stepChoices. 990 00:45:22,360 --> 00:45:24,450 And that's where I'm building in the idea that it's only 991 00:45:24,450 --> 00:45:27,010 going to move plus or minus 1 in the x and y direction-- 992 00:45:27,010 --> 00:45:28,540 north, south, east, west. 993 00:45:28,540 --> 00:45:30,500 I could clearly change that. 994 00:45:30,500 --> 00:45:33,380 I'm also building in here that it's equally likely, because 995 00:45:33,380 --> 00:45:35,500 random picks one of those equally likely. 996 00:45:35,500 --> 00:45:37,120 I might decide to change that. 997 00:45:37,120 --> 00:45:38,860 And I can certainly do that if I want to move to other 998 00:45:38,860 --> 00:45:39,340 directions. 999 00:45:39,340 --> 00:45:42,870 But right now, that's where I'm going to build it in. 1000 00:45:42,870 --> 00:45:43,780 OK. 1001 00:45:43,780 --> 00:45:46,340 So now we're ready to try the simulation. 1002 00:45:46,340 --> 00:45:48,270 We've got what? 1003 00:45:48,270 --> 00:45:48,900 We've got location. 1004 00:45:48,900 --> 00:45:50,820 That's just representing a place. 1005 00:45:50,820 --> 00:45:51,840 We've got fields. 1006 00:45:51,840 --> 00:45:54,680 That just lets us map drunks to locations. 1007 00:45:54,680 --> 00:45:56,990 And then we've got the ability to move the drunk around. 1008 00:45:56,990 --> 00:46:02,350 So I can now think about making this 1009 00:46:02,350 --> 00:46:04,480 actually move around. 1010 00:46:04,480 --> 00:46:06,970 So let me show you a quick example. 1011 00:46:06,970 --> 00:46:08,220 Let's create a drunk. 1012 00:46:17,170 --> 00:46:19,630 I'm picking the obvious person. 1013 00:46:19,630 --> 00:46:23,160 I'm going to create a location. 1014 00:46:23,160 --> 00:46:27,320 We'll call the origin just the spot at 0, 0. 1015 00:46:30,270 --> 00:46:34,320 And I need to create a field which I can do. 1016 00:46:34,320 --> 00:46:38,810 And then I'm going to add Homer to this 1017 00:46:38,810 --> 00:46:43,420 field at the origin. 1018 00:46:43,420 --> 00:46:44,970 And now the last thing I want to do is I want to have Homer 1019 00:46:44,970 --> 00:46:45,460 wander around. 1020 00:46:45,460 --> 00:46:46,370 I want to see how far he goes. 1021 00:46:46,370 --> 00:46:48,470 So I'm going to build a little function over here-- you can 1022 00:46:48,470 --> 00:46:52,900 see right there-- called walk, which takes the field, takes a 1023 00:46:52,900 --> 00:46:55,810 drunk in the field, and says how many steps do I want him 1024 00:46:55,810 --> 00:46:56,790 to stagger around with? 1025 00:46:56,790 --> 00:46:57,560 And what's it going to do? 1026 00:46:57,560 --> 00:46:59,910 I'm going to set the start location to be where the drunk 1027 00:46:59,910 --> 00:47:01,190 was initially. 1028 00:47:01,190 --> 00:47:04,020 It might be at the origin or it might be somewhere else. 1029 00:47:04,020 --> 00:47:06,180 And then I'm just going to run through that number of steps. 1030 00:47:06,180 --> 00:47:08,380 So range is just saying, take the number of steps, just 1031 00:47:08,380 --> 00:47:09,150 moving the drunk. 1032 00:47:09,150 --> 00:47:11,480 And then I'll return how far away he is. 1033 00:47:11,480 --> 00:47:17,580 So if I do that here, I can do walk with that field, with my 1034 00:47:17,580 --> 00:47:18,350 drunk, Homer. 1035 00:47:18,350 --> 00:47:20,890 And let's take 10 steps. 1036 00:47:20,890 --> 00:47:22,090 Hmm. 1037 00:47:22,090 --> 00:47:23,200 That's interesting. 1038 00:47:23,200 --> 00:47:24,450 Let's try it again. 1039 00:47:30,830 --> 00:47:32,205 I was getting worried there for second. 1040 00:47:35,780 --> 00:47:40,640 So we're getting an interesting range of values. 1041 00:47:40,640 --> 00:47:42,760 So here's the last thing I'd like to do. 1042 00:47:42,760 --> 00:47:45,360 Let's actually do a simulation of this. 1043 00:47:45,360 --> 00:47:47,350 And I'm going to do this reasonably quickly. simWalks 1044 00:47:47,350 --> 00:47:51,370 right here is simply going to do what I just did, but 1045 00:47:51,370 --> 00:47:52,680 multiple times. 1046 00:47:52,680 --> 00:47:55,030 That is, it's going to take a drunk, a field, some number of 1047 00:47:55,030 --> 00:47:59,370 steps, and it's going to run a bunch of trials, however many 1048 00:47:59,370 --> 00:48:00,190 trials I want to get. 1049 00:48:00,190 --> 00:48:01,920 So I do what I just did over here, and 1050 00:48:01,920 --> 00:48:03,610 collect those together. 1051 00:48:03,610 --> 00:48:07,370 And then I can just print out some statistics about how far 1052 00:48:07,370 --> 00:48:09,820 away did the drunk get as I do that. 1053 00:48:09,820 --> 00:48:11,350 And that's what drunk test is going to do. 1054 00:48:11,350 --> 00:48:13,490 We're going to do it for 10 steps, 100 steps, 1,000 1055 00:48:13,490 --> 00:48:14,350 steps, and so on. 1056 00:48:14,350 --> 00:48:16,420 So let me simply run that. 1057 00:48:16,420 --> 00:48:19,780 I run drunkTest. 1058 00:48:19,780 --> 00:48:21,480 And we'll do it with 10 trials each. 1059 00:48:26,160 --> 00:48:27,470 OK. 1060 00:48:27,470 --> 00:48:29,700 Does that look right? 1061 00:48:29,700 --> 00:48:32,550 Does that look good? 1062 00:48:32,550 --> 00:48:35,170 You can tell this is a trick question. 1063 00:48:35,170 --> 00:48:36,590 So what should you be looking for there? 1064 00:48:36,590 --> 00:48:36,970 Look at it. 1065 00:48:36,970 --> 00:48:39,780 It says, if I take 10 steps, on average I'm about 2 and 1/2 1066 00:48:39,780 --> 00:48:40,760 steps away. 1067 00:48:40,760 --> 00:48:43,270 If I take 100 steps, on average I'm less than two 1068 00:48:43,270 --> 00:48:44,300 steps away. 1069 00:48:44,300 --> 00:48:46,790 1,000 steps, I'm about two steps away. 1070 00:48:46,790 --> 00:48:49,150 10,000 steps, I'm about two steps away. 1071 00:48:49,150 --> 00:48:52,540 100,000 steps, on average I'm about two steps away. 1072 00:48:52,540 --> 00:48:53,970 Wait a minute. 1073 00:48:53,970 --> 00:48:58,170 Didn't we say that it looked like, over on that board 1074 00:48:58,170 --> 00:49:00,280 there, that as I increased the number of steps, I got a 1075 00:49:00,280 --> 00:49:02,885 little bit further away? 1076 00:49:02,885 --> 00:49:04,870 Hmm. 1077 00:49:04,870 --> 00:49:06,030 So I've done this deliberately. 1078 00:49:06,030 --> 00:49:07,210 This suggests what? 1079 00:49:07,210 --> 00:49:10,830 The first key point, think about what your simulation 1080 00:49:10,830 --> 00:49:11,550 should return. 1081 00:49:11,550 --> 00:49:13,650 What are good examples to think about? 1082 00:49:13,650 --> 00:49:15,940 Because this doesn't look right. 1083 00:49:15,940 --> 00:49:16,950 OK. 1084 00:49:16,950 --> 00:49:19,850 So here's how I could test it. 1085 00:49:19,850 --> 00:49:23,110 Why don't I try a simulation on values for 1086 00:49:23,110 --> 00:49:25,580 which I know the answer? 1087 00:49:25,580 --> 00:49:29,050 So I'm going to go back in here, and right here, I'm 1088 00:49:29,050 --> 00:49:30,555 going to replace this with-- 1089 00:49:33,270 --> 00:49:40,240 we're either going to take zero steps or one step. 1090 00:49:40,240 --> 00:49:41,490 Let's run that. 1091 00:49:44,000 --> 00:49:45,750 Oh, don't do that to me. 1092 00:49:45,750 --> 00:49:47,200 Oh, yes, I do have an error. 1093 00:49:47,200 --> 00:49:48,736 Thank you. 1094 00:49:48,736 --> 00:49:51,580 Get rid of it right there. 1095 00:49:51,580 --> 00:49:54,730 Almost out of time, so this is perfect. 1096 00:49:54,730 --> 00:49:56,590 And now let's try drunkStep again. 1097 00:50:00,150 --> 00:50:01,790 Wow. 1098 00:50:01,790 --> 00:50:05,390 If I take a random walk taking no steps, on average I end up 1099 00:50:05,390 --> 00:50:08,360 2 and 1/2 units away. 1100 00:50:08,360 --> 00:50:10,670 I teleported. 1101 00:50:10,670 --> 00:50:12,890 That's pretty amazing. 1102 00:50:12,890 --> 00:50:16,430 And with perfect timing, we'll figure out what's wrong with 1103 00:50:16,430 --> 00:50:19,140 my simulation next time. 1104 00:50:19,140 --> 00:50:20,390 Thanks.