-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathGradientDescent.nb
547 lines (538 loc) · 24.2 KB
/
GradientDescent.nb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
(* Content-type: application/vnd.wolfram.mathematica *)
(*** Wolfram Notebook File ***)
(* http://www.wolfram.com/nb *)
(* CreatedBy='Mathematica 11.1' *)
(*CacheID: 234*)
(* Internal cache information:
NotebookFileLineBreakTest
NotebookFileLineBreakTest
NotebookDataPosition[ 158, 7]
NotebookDataLength[ 24598, 537]
NotebookOptionsPosition[ 24073, 518]
NotebookOutlinePosition[ 24427, 534]
CellTagsIndexPosition[ 24384, 531]
WindowFrame->Normal*)
(* Beginning of Notebook Content *)
Notebook[{
Cell[BoxData[
RowBox[{
RowBox[{
RowBox[{"stepGradient", "[",
RowBox[{
"func_", ",", "start_", ",", "\[Lambda]_", ",", "steps_", ",", "vars_"}],
"]"}], ":=",
RowBox[{
RowBox[{"stepGradient", "[",
RowBox[{
"func", ",", "start", ",", "\[Lambda]", ",", "steps", ",", "vars"}],
"]"}], "=",
RowBox[{"Module", "[",
RowBox[{
RowBox[{"{", "}"}], ",", "\[IndentingNewLine]",
RowBox[{
RowBox[{"vals", "=",
RowBox[{"{", "start", "}"}]}], ";", "\[IndentingNewLine]",
RowBox[{
RowBox[{
RowBox[{"grd", "[",
RowBox[{"f_", ",", "v_"}], "]"}], "[", "n_", "]"}], ":=",
RowBox[{
RowBox[{"Grad", "[",
RowBox[{"f", ",", "v"}], "]"}], "/.",
RowBox[{"Thread", "[",
RowBox[{"v", "\[Rule]", "n"}], "]"}]}]}], ";",
"\[IndentingNewLine]",
RowBox[{"grad", "=",
RowBox[{"grd", "[",
RowBox[{"func", ",", "vars"}], "]"}]}], ";", "\[IndentingNewLine]",
RowBox[{"Print", "[",
RowBox[{"grad", "[",
RowBox[{"{",
RowBox[{"m", ",", "b"}], "}"}], "]"}], "]"}], ";",
"\[IndentingNewLine]",
RowBox[{"For", "[",
RowBox[{
RowBox[{"i", "=", "0"}], ",",
RowBox[{"i", "<", "steps"}], ",",
RowBox[{"i", "++"}], ",", "\[IndentingNewLine]",
RowBox[{"vals", "=",
RowBox[{"{",
RowBox[{"vals", ",",
RowBox[{
RowBox[{"(",
RowBox[{"Last", "@", "vals"}], ")"}], "-",
RowBox[{"\[Lambda]", "*",
RowBox[{"grad", "[",
RowBox[{"Last", "@", "vals"}], "]"}]}]}]}], "}"}]}]}],
"\[IndentingNewLine]", "]"}], ";", "\[IndentingNewLine]",
RowBox[{"Cases", "[",
RowBox[{"vals", ",",
RowBox[{"{", "__", "}"}], ",",
RowBox[{"{",
RowBox[{"-", "2"}], "}"}]}], "]"}]}]}], "\[IndentingNewLine]",
"]"}]}]}], ";"}]], "Input",
CellChangeTimes->{{3.718621658297077*^9, 3.7186216686540537`*^9}, {
3.7186217105538883`*^9, 3.718621711919973*^9}, {3.7186220446703587`*^9,
3.718622078677733*^9}, {3.7186221362103233`*^9, 3.7186222015362577`*^9}, {
3.718622446657549*^9, 3.7186227174886227`*^9}, {3.718622899706777*^9,
3.7186229121285048`*^9}, {3.718622952375741*^9, 3.7186229669955597`*^9}, {
3.71862622864426*^9, 3.718626274954872*^9}, 3.718626342017943*^9, {
3.718626389376546*^9, 3.718626400121395*^9}, {3.718626434188376*^9,
3.7186264689891853`*^9}, {3.7186265018777533`*^9, 3.718626573691004*^9}, {
3.718626713708095*^9, 3.7186267207903767`*^9}, {3.718626766057271*^9,
3.71862684399109*^9}, {3.718626973020793*^9, 3.718627003692083*^9}, {
3.7186271905530853`*^9, 3.718627242844858*^9}, {3.718627279409709*^9,
3.7186273388545027`*^9}, {3.718627512392046*^9, 3.718627563729879*^9}, {
3.7186276202290163`*^9, 3.7186276210580683`*^9}, {3.718627688634799*^9,
3.718627714327981*^9}, {3.7186278582623167`*^9, 3.718627869244907*^9},
3.718628978563342*^9, 3.718629016507867*^9, {3.7186290800948563`*^9,
3.718629100789154*^9}, {3.718629208954197*^9, 3.718629301187652*^9}, {
3.7186293685911303`*^9, 3.7186294046360703`*^9}, {3.7186299431773443`*^9,
3.718629952735681*^9}, {3.718637408248844*^9, 3.718637430624119*^9}, {
3.718703902044339*^9, 3.7187039026162558`*^9}, {3.718704026729581*^9,
3.718704040682145*^9}, {3.718704289230756*^9, 3.7187042899652767`*^9}, {
3.71870433161106*^9, 3.718704342169716*^9}, {3.718704483105143*^9,
3.718704485071224*^9}, {3.7187053857491207`*^9, 3.718705406350339*^9}, {
3.718705462376897*^9, 3.718705502137866*^9}, {3.718705642848012*^9,
3.7187056452862053`*^9}, {3.718705695137155*^9, 3.7187057012814207`*^9}, {
3.718705843350316*^9, 3.718705903057852*^9}, {3.718730906895591*^9,
3.718730918125392*^9}, {3.718730976591416*^9, 3.718730976989505*^9}, {
3.718731010712644*^9, 3.7187310156626053`*^9}, {3.718731060601488*^9,
3.718731108010228*^9}, {3.718731747637165*^9, 3.71873175435466*^9}, {
3.718731793940878*^9, 3.71873181989209*^9}, {3.7187320134666862`*^9,
3.7187320140018053`*^9}, {3.718732050132414*^9, 3.718732054258564*^9}, {
3.718732091597485*^9, 3.718732120660841*^9}, {3.71873215812759*^9,
3.718732216014977*^9}, {3.718732487415925*^9, 3.718732488470386*^9}, {
3.718965342702701*^9, 3.718965347124527*^9}, {3.7189656459931927`*^9,
3.7189656690668087`*^9}, {3.718965731901113*^9, 3.718965746420471*^9}, {
3.718970316631033*^9, 3.7189704600698757`*^9}, {3.7189705012203217`*^9,
3.718970510773596*^9}, {3.718970554949584*^9, 3.7189705582676077`*^9}, {
3.718970707333726*^9, 3.7189707278812304`*^9}, {3.7189838092298527`*^9,
3.718983836422491*^9}, {3.7189839599145184`*^9, 3.718984017946722*^9},
3.7189840646136923`*^9, {3.718984251298897*^9, 3.71898426294769*^9}, {
3.7189842930685587`*^9, 3.718984297763266*^9}, {3.7189843532690372`*^9,
3.718984360429139*^9}, {3.718984399956791*^9, 3.718984403176359*^9}, {
3.718984731026917*^9, 3.718984753938504*^9}, {3.718984802948419*^9,
3.718984859140726*^9}, {3.718984939255373*^9, 3.718984958712489*^9}, {
3.7189849930349503`*^9,
3.718985045290514*^9}},ExpressionUUID->"dd3a8ac6-ae65-4c57-9565-\
40a69eca9642"],
Cell[BoxData[{
RowBox[{
RowBox[{
RowBox[{"f", "[",
RowBox[{"x_", ",", "y_"}], "]"}], ":=",
RowBox[{
RowBox[{"Sin", "[",
RowBox[{
RowBox[{
RowBox[{"1", "/", "2"}],
RowBox[{"x", "^", "2"}]}], "-",
RowBox[{
RowBox[{"1", "/", "4"}],
RowBox[{"y", "^", "2"}]}], "+", "3"}], "]"}],
RowBox[{"Cos", "[",
RowBox[{
RowBox[{"2", "x"}], "+", "1", "-",
RowBox[{"Exp", "[", "y", "]"}]}], "]"}]}]}],
"\[IndentingNewLine]"}], "\[IndentingNewLine]",
RowBox[{
RowBox[{"width", "=", "5"}], ";"}], "\[IndentingNewLine]",
RowBox[{
RowBox[{
RowBox[{"height", "=", "5"}], ";"}],
"\[IndentingNewLine]"}], "\[IndentingNewLine]",
RowBox[{"Manipulate", "[", "\[IndentingNewLine]",
RowBox[{
RowBox[{"DynamicModule", "[",
RowBox[{
RowBox[{"{", "}"}], ",", "\[IndentingNewLine]",
RowBox[{
RowBox[{"points2D", "=",
RowBox[{"stepGradient", "[",
RowBox[{
RowBox[{"g", "[",
RowBox[{"x", ",", "y"}], "]"}], ",",
RowBox[{"Setting", "@",
RowBox[{"Dynamic", "@", "start"}]}], ",", "\[Lambda]", ",",
"numPoints", ",",
RowBox[{"{",
RowBox[{"x", ",", "y"}], "}"}]}], "]"}]}], ";",
"\[IndentingNewLine]",
RowBox[{"points3D", "=",
RowBox[{
RowBox[{"(",
RowBox[{
RowBox[{"Append", "[",
RowBox[{"#", ",",
RowBox[{"g", "@@", "#"}]}], "]"}], "&"}], ")"}], "/@",
"points2D"}]}], ";", "\[IndentingNewLine]",
RowBox[{"p1", "=",
RowBox[{"Show", "[", "\[IndentingNewLine]",
RowBox[{
RowBox[{"Plot3D", "[",
RowBox[{
RowBox[{"g", "[",
RowBox[{"x", ",", "y"}], "]"}], ",",
RowBox[{"{",
RowBox[{"x", ",",
RowBox[{
RowBox[{"-", "width"}], "/", "2"}], ",",
RowBox[{"width", "/", "2"}]}], "}"}], ",",
RowBox[{"{",
RowBox[{"y", ",",
RowBox[{
RowBox[{"-", "height"}], "/", "2"}], ",",
RowBox[{"height", "/", "2"}]}], "}"}], ",",
RowBox[{"ColorFunction", "\[Rule]", "\"\<Rainbow\>\""}]}], "]"}],
",", "\[IndentingNewLine]",
RowBox[{"Graphics3D", "[",
RowBox[{"{",
RowBox[{
RowBox[{"Thickness", "[", "0.005", "]"}], ",", "Red", ",",
RowBox[{"Line", "[", "points3D", "]"}]}], "}"}], "]"}], ",",
"\[IndentingNewLine]",
RowBox[{"Graphics3D", "[",
RowBox[{"{",
RowBox[{
RowBox[{"PointSize", "[", "0.02", "]"}], ",", "Red", ",",
RowBox[{"Point", "[", "points3D", "]"}]}], "}"}], "]"}], ",",
"\[IndentingNewLine]",
RowBox[{"ImageSize", "\[Rule]", "Large"}]}], "\[IndentingNewLine]",
"]"}]}], ";", "\[IndentingNewLine]",
RowBox[{"p2", "=",
RowBox[{"LocatorPane", "[",
RowBox[{
RowBox[{"Dynamic", "@", "start"}], ",",
RowBox[{"Show", "[",
RowBox[{
RowBox[{"{",
RowBox[{
RowBox[{"ContourPlot", "[",
RowBox[{
RowBox[{"g", "[",
RowBox[{"x", ",", "y"}], "]"}], ",",
RowBox[{"{",
RowBox[{"x", ",",
RowBox[{
RowBox[{"-", "width"}], "/", "2"}], ",",
RowBox[{"width", "/", "2"}]}], "}"}], ",",
RowBox[{"{",
RowBox[{"y", ",",
RowBox[{
RowBox[{"-", "height"}], "/", "2"}], ",",
RowBox[{"height", "/", "2"}]}], "}"}], ",",
RowBox[{"ColorFunction", "\[Rule]", "\"\<Rainbow\>\""}]}],
"]"}], ",",
RowBox[{"Graphics", "[",
RowBox[{"{",
RowBox[{"Thick", ",", "Red", ",",
RowBox[{"Line", "[", "points2D", "]"}]}], "}"}], "]"}], ",",
"\[IndentingNewLine]",
RowBox[{"Graphics", "[",
RowBox[{"{",
RowBox[{
RowBox[{"PointSize", "[", "0.02", "]"}], ",", "Red", ",",
RowBox[{"Point", "[", "points2D", "]"}]}], "}"}], "]"}]}],
"}"}], ",",
RowBox[{"ImageSize", "\[Rule]", "Medium"}]}], "]"}], ",", " ",
RowBox[{"LocatorAutoCreate", "\[Rule]", "False"}], ",",
RowBox[{"ContinuousAction", "\[Rule]", "False"}]}], "]"}]}], ";",
"\[IndentingNewLine]",
RowBox[{"Row", "[",
RowBox[{"{",
RowBox[{"p1", ",",
RowBox[{"Dynamic", "@", "p2"}]}], "}"}], "]"}]}]}],
"\[IndentingNewLine]", "]"}], ",", "\[IndentingNewLine]",
RowBox[{"{",
RowBox[{
RowBox[{"{",
RowBox[{"g", ",", "f", ",", "\"\<Function\>\""}], "}"}], ",",
RowBox[{"{",
RowBox[{
RowBox[{"f", "\[Rule]", "\"\<Terrain\>\""}], ",",
RowBox[{
RowBox[{"(",
RowBox[{
RowBox[{
RowBox[{"Sin", "[", "#1", "]"}],
RowBox[{"Cos", "[", "#2", "]"}]}], "&"}], ")"}], "\[Rule]",
"\"\<Hills\>\""}], ",",
RowBox[{
RowBox[{"(",
RowBox[{
RowBox[{
RowBox[{"#1", "^", "2"}], "+",
RowBox[{"#2", "^", "2"}]}], "&"}], ")"}], "\[Rule]",
"\"\<Paraboloid\>\""}], ",", " ",
RowBox[{
RowBox[{"(",
RowBox[{
RowBox[{
RowBox[{"error", "/.",
RowBox[{"m", "\[Rule]", "#1"}]}], "/.",
RowBox[{"b", "\[Rule]", "#2"}]}], "&"}], ")"}], "\[Rule]",
"\"\<Error\>\""}]}], "}"}]}], "}"}], ",", "\[IndentingNewLine]",
RowBox[{"{",
RowBox[{
RowBox[{"{",
RowBox[{"numPoints", ",", "2", ",", "\"\<Number of Iterations\>\""}],
"}"}], ",", "1", ",", "25", ",", "1"}], "}"}], ",",
"\[IndentingNewLine]",
RowBox[{"{",
RowBox[{
RowBox[{"{",
RowBox[{"\[Lambda]", ",", "0.2", ",", "\"\<Weight\>\""}], "}"}], ",",
"0.01", ",", "1", ",", "0.01"}], "}"}], ",", "\[IndentingNewLine]",
RowBox[{"{",
RowBox[{
RowBox[{"{",
RowBox[{"start", ",",
RowBox[{"{",
RowBox[{
RowBox[{"-", "0.34"}], ",",
RowBox[{"-", "2"}]}], "}"}]}], "}"}], ",", "None"}], "}"}], ",",
"\[IndentingNewLine]",
RowBox[{"ContinuousAction", "\[Rule]", "False"}]}], "\[IndentingNewLine]",
"]"}], "\[IndentingNewLine]"}], "Input",
CellChangeTimes->{{3.718620625362838*^9, 3.718620628402626*^9}, {
3.718620724076364*^9, 3.718620894231141*^9}, {3.718620948650828*^9,
3.7186209530329847`*^9}, {3.718621051773834*^9, 3.718621052500537*^9}, {
3.7186210850655003`*^9, 3.718621288275591*^9}, {3.718621332294962*^9,
3.718621338995668*^9}, {3.7186213740483637`*^9, 3.718621407920711*^9}, {
3.718621452266864*^9, 3.718621566875441*^9}, {3.718621599783967*^9,
3.718621648720087*^9}, {3.7186221122601023`*^9, 3.718622131826696*^9}, {
3.718622723080687*^9, 3.71862284431459*^9}, {3.718626846408836*^9,
3.718626873685411*^9}, {3.7186273729989033`*^9, 3.7186273882453127`*^9}, {
3.718627435748845*^9, 3.718627462901271*^9}, {3.718627776667673*^9,
3.718627790708009*^9}, {3.7186278287991056`*^9, 3.718627843126073*^9}, {
3.718627887876789*^9, 3.718628058398171*^9}, {3.718628135429603*^9,
3.7186282780841923`*^9}, {3.7186283165285463`*^9,
3.7186284975520773`*^9}, {3.718628732084072*^9, 3.718628758511445*^9},
3.718628823825041*^9, {3.7186288650606527`*^9, 3.718628933805275*^9}, {
3.718628989931963*^9, 3.718628999921926*^9}, {3.7186293049721317`*^9,
3.71862930726011*^9}, {3.71862940715621*^9, 3.718629456223192*^9}, {
3.7186296157732773`*^9, 3.718629717366349*^9}, {3.718629888692745*^9,
3.718629928111326*^9}, {3.718629968875428*^9, 3.718629970552354*^9}, {
3.7186300375363626`*^9, 3.718630049313654*^9}, {3.718630118127907*^9,
3.718630144350863*^9}, {3.71863022845126*^9, 3.718630228873926*^9}, {
3.718630261673565*^9, 3.718630480821497*^9}, {3.718630838923675*^9,
3.7186308417454147`*^9}, {3.718630960223764*^9, 3.718630961038883*^9}, {
3.718631002159809*^9, 3.718631002309968*^9}, {3.718631163404912*^9,
3.718631238012267*^9}, {3.718631272165419*^9, 3.7186312831019278`*^9},
3.718631351967557*^9, {3.718631388192088*^9, 3.718631392639182*^9}, {
3.7186314263479633`*^9, 3.718631447138464*^9}, {3.718631519088818*^9,
3.7186316195399923`*^9}, {3.7186316509718027`*^9, 3.718631662940094*^9},
3.7186316954980087`*^9, 3.718631761985188*^9, {3.7186318615573874`*^9,
3.718631889380137*^9}, {3.718631933999518*^9, 3.7186320546599693`*^9}, {
3.718632206249839*^9, 3.718632251222685*^9}, {3.7186323359455748`*^9,
3.718632441131679*^9}, {3.7186324796415*^9, 3.718632485199547*^9}, {
3.7186325421690683`*^9, 3.7186325468234797`*^9}, {3.718632818807362*^9,
3.718633057672388*^9}, {3.718633169475028*^9, 3.718633196158821*^9},
3.718635182459193*^9, {3.718635397213066*^9, 3.718635403149289*^9}, {
3.71863591444477*^9, 3.718635918575713*^9}, {3.7186374631939783`*^9,
3.718637542619362*^9}, {3.7186376876654243`*^9, 3.718637704963203*^9}, {
3.718637781243421*^9, 3.718637846157175*^9}, {3.71863788739828*^9,
3.718637891829813*^9}, {3.718637925536373*^9, 3.7186379256764603`*^9}, {
3.718637997710984*^9, 3.718638000240532*^9}, {3.7186383309588346`*^9,
3.718638404678874*^9}, {3.7186384461045427`*^9, 3.718638448864315*^9}, {
3.7186385951762466`*^9, 3.718638628454486*^9}, {3.718638659749567*^9,
3.718638679040296*^9}, {3.7186387459529963`*^9, 3.718638775770838*^9}, {
3.71863881425959*^9, 3.7186388239307623`*^9}, {3.718638855203973*^9,
3.718638941023629*^9}, {3.718639008205018*^9, 3.7186390156909447`*^9}, {
3.7186465043695307`*^9, 3.7186465390634127`*^9}, {3.718646847353249*^9,
3.718646880599955*^9}, {3.718647027893852*^9, 3.718647032075573*^9}, {
3.718647190384325*^9, 3.718647386515169*^9}, {3.718648083427381*^9,
3.718648085990952*^9}, {3.718648137263163*^9, 3.718648150799656*^9}, {
3.718648903032716*^9, 3.718648951424608*^9}, {3.718649186797927*^9,
3.7186492140157824`*^9}, {3.718649323986709*^9, 3.718649372113493*^9}, {
3.7186494770141363`*^9, 3.71864948039493*^9}, {3.718649581296941*^9,
3.7186496056077137`*^9}, {3.718649798734744*^9, 3.718649826437416*^9}, {
3.718650314644826*^9, 3.718650330002803*^9}, {3.718650393461955*^9,
3.718650398454114*^9}, {3.7186504347993517`*^9, 3.718650507656117*^9}, {
3.718651069935691*^9, 3.718651072173648*^9}, {3.718651123738633*^9,
3.718651128400908*^9}, {3.718651167042204*^9, 3.7186511679318953`*^9}, {
3.718651305271986*^9, 3.7186513060303917`*^9}, {3.718651745158929*^9,
3.718651776732717*^9}, {3.7186521687555513`*^9, 3.718652169458498*^9}, {
3.718662417815364*^9, 3.718662489368642*^9}, {3.7186658589667683`*^9,
3.718665859645213*^9}, {3.718698782230303*^9, 3.7186988344542007`*^9}, {
3.7186988964767437`*^9, 3.718698939785986*^9}, 3.7186989716732607`*^9, {
3.718699736212633*^9, 3.718699763612444*^9}, {3.718699850588698*^9,
3.7186998547630444`*^9}, {3.7186999025700502`*^9,
3.7186999029142447`*^9}, {3.71870021895429*^9, 3.718700221023376*^9}, {
3.718703953199615*^9, 3.718704014089456*^9}, {3.718704056594726*^9,
3.718704071387272*^9}, {3.7187041058659277`*^9, 3.7187041191927137`*^9},
3.718704174531744*^9, {3.7187043614138527`*^9, 3.718704425601963*^9}, {
3.7187045069846697`*^9, 3.718704588241948*^9}, {3.718704635163951*^9,
3.7187046415289297`*^9}, {3.718704724469459*^9, 3.7187048318547363`*^9}, {
3.7187049674817333`*^9, 3.7187050352554693`*^9}, {3.718705170490305*^9,
3.718705181929039*^9}, {3.718705280506051*^9, 3.7187053353807364`*^9}, {
3.718705413718852*^9, 3.71870542444074*^9}, {3.718705544676382*^9,
3.7187055461258802`*^9}, {3.7187055895551987`*^9, 3.718705603731769*^9},
3.718705836986105*^9, {3.718731032626376*^9, 3.7187310334410458`*^9}, {
3.71873143189966*^9, 3.718731447385195*^9}, {3.718732317749276*^9,
3.7187323184428453`*^9}, {3.7187323525499163`*^9,
3.7187323594441957`*^9}, {3.7187324392248297`*^9,
3.7187324724251347`*^9}, {3.718732531187222*^9, 3.718732567626225*^9}, {
3.718732632221732*^9, 3.71873266594901*^9}, {3.718965846382251*^9,
3.718965882512206*^9}, {3.718965916566793*^9, 3.7189659363532763`*^9}, {
3.7189831512345133`*^9, 3.7189831855943727`*^9}, {3.71898321875675*^9,
3.7189832421560373`*^9}, {3.71898332375371*^9, 3.718983364271782*^9}, {
3.718983405258491*^9, 3.718983406601087*^9}, {3.718983666449616*^9,
3.718983666561002*^9}, {3.718984389841057*^9,
3.718984391160057*^9}},ExpressionUUID->"2649b2e4-0b37-4f7a-b3f1-\
ccd31795ac3c"],
Cell[BoxData[{
RowBox[{
RowBox[{"listSize", "=", "30"}], ";"}], "\[IndentingNewLine]",
RowBox[{
RowBox[{"listMax", "=", "100"}], ";"}], "\[IndentingNewLine]",
RowBox[{
RowBox[{
RowBox[{"dist", "[", "rho_", "]"}], ":=",
RowBox[{"CopulaDistribution", "[",
RowBox[{
RowBox[{"{",
RowBox[{"\"\<Binormal\>\"", ",", "rho"}], "}"}], ",",
RowBox[{"{",
RowBox[{
RowBox[{"UniformDistribution", "[",
RowBox[{"{",
RowBox[{"0", ",", "1"}], "}"}], "]"}], ",",
RowBox[{"UniformDistribution", "[",
RowBox[{"{",
RowBox[{"0", ",", "1"}], "}"}], "]"}]}], "}"}]}], "]"}]}],
";"}], "\[IndentingNewLine]",
RowBox[{
RowBox[{
RowBox[{"list", "=",
RowBox[{"listMax", "*",
RowBox[{"RandomVariate", "[",
RowBox[{
RowBox[{"dist", "[",
RowBox[{"-", ".95"}], "]"}], ",", "listSize"}], "]"}]}]}], ";"}],
"\[IndentingNewLine]"}], "\[IndentingNewLine]",
RowBox[{
RowBox[{
RowBox[{"error", "=",
RowBox[{
RowBox[{"1", "/", "listSize"}], "*",
RowBox[{"Sum", "[",
RowBox[{
RowBox[{
RowBox[{"(",
RowBox[{
RowBox[{"Last", "@",
RowBox[{"list", "[",
RowBox[{"[", "i", "]"}], "]"}]}], "-",
RowBox[{"(",
RowBox[{
RowBox[{"m", "*",
RowBox[{"First", "@",
RowBox[{"list", "[",
RowBox[{"[", "i", "]"}], "]"}]}]}], "+", "b"}], ")"}]}], ")"}],
"^", "2"}], ",",
RowBox[{"{",
RowBox[{"i", ",", "1", ",", "listSize"}], "}"}]}], "]"}]}]}], ";"}],
"\[IndentingNewLine]"}], "\[IndentingNewLine]",
RowBox[{
RowBox[{"steps", "=",
RowBox[{"stepGradient", "[",
RowBox[{"error", ",",
RowBox[{"{",
RowBox[{
RowBox[{"-", "1"}], ",", "listMax"}], "}"}], ",", "0.05", ",", "10",
",",
RowBox[{"{",
RowBox[{"m", ",", "b"}], "}"}]}], "]"}]}], ";"}], "\[IndentingNewLine]",
RowBox[{
RowBox[{"{",
RowBox[{"mf", ",", "bf"}], "}"}], "=",
RowBox[{"Last", "@", "steps"}]}], "\[IndentingNewLine]",
RowBox[{
RowBox[{"bestFit", "=",
RowBox[{"Fit", "[",
RowBox[{"list", ",",
RowBox[{"{",
RowBox[{"1", ",", "x"}], "}"}], ",", "x"}], "]"}]}],
";"}], "\[IndentingNewLine]",
RowBox[{"Show", "[",
RowBox[{
RowBox[{"ListPlot", "[",
RowBox[{"list", ",",
RowBox[{"AxesLabel", "\[Rule]",
RowBox[{"{",
RowBox[{"\"\<Number of People\>\"", ",", "\"\<Income\>\""}], "}"}]}]}],
"]"}], ",",
RowBox[{"Plot", "[",
RowBox[{
RowBox[{
RowBox[{"mf", "*", "x"}], "+", "bf"}], ",",
RowBox[{"{",
RowBox[{"x", ",", "0", ",", "listMax"}], "}"}]}], "]"}], ",",
RowBox[{"Plot", "[",
RowBox[{"bestFit", ",",
RowBox[{"{",
RowBox[{"x", ",", "0", ",", "listMax"}], "}"}]}], "]"}]}],
"]"}]}], "Input",
CellChangeTimes->{{3.718966500943892*^9, 3.718966503706725*^9}, {
3.7189666814865*^9, 3.718966807134351*^9}, {3.718966850924716*^9,
3.7189668912218447`*^9}, {3.718966959232778*^9, 3.7189669599847918`*^9}, {
3.718967090336969*^9, 3.7189670925609827`*^9}, {3.718967141967099*^9,
3.718967153087359*^9}, {3.718967497442299*^9, 3.718967570883992*^9}, {
3.71896801470833*^9, 3.7189680790123653`*^9}, {3.718968127715424*^9,
3.718968182587707*^9}, {3.718968216727151*^9, 3.718968244337843*^9}, {
3.718968464053787*^9, 3.718968482587818*^9}, {3.718968516105283*^9,
3.718968519535963*^9}, {3.71896857345539*^9, 3.7189687246157923`*^9}, {
3.718969993587191*^9, 3.718969999002515*^9}, {3.718970040794264*^9,
3.7189700409168367`*^9}, {3.718970161869194*^9, 3.718970261686983*^9}, {
3.718977495419743*^9, 3.71897749729814*^9}, {3.7189775330316467`*^9,
3.718977533145605*^9}, {3.718977563374277*^9, 3.7189776293436537`*^9}, {
3.718977674747095*^9, 3.7189777157708893`*^9}, {3.7189777465718737`*^9,
3.7189778109323397`*^9}, {3.718977848688148*^9, 3.71897785064631*^9}, {
3.7189779281472673`*^9, 3.718978013474715*^9}, {3.718978053374206*^9,
3.718978125407254*^9}, {3.7189781919446783`*^9, 3.718978357445068*^9}, {
3.718978388278936*^9, 3.7189783883535833`*^9}, {3.718981469781571*^9,
3.7189815202752047`*^9}, {3.71898171187558*^9, 3.7189817332432213`*^9}, {
3.7189817899033403`*^9, 3.718981809413087*^9}, {3.718981907505307*^9,
3.718982000495598*^9}, {3.718982052406201*^9, 3.718982126260702*^9}, {
3.718982187474244*^9, 3.718982210083015*^9}, {3.718982296597941*^9,
3.7189823067568197`*^9}, {3.718982502573452*^9, 3.718982510107728*^9},
3.718982556311553*^9, {3.718982588046311*^9, 3.718982603317588*^9}, {
3.718982638786951*^9, 3.7189827378663263`*^9}, {3.718982806221642*^9,
3.718982818764434*^9}, {3.7189829882833*^9, 3.7189830386281557`*^9}, {
3.718983124264194*^9, 3.7189831279672937`*^9}, {3.718983642946259*^9,
3.718983663816594*^9}, {3.718984311148815*^9, 3.718984318884822*^9}, {
3.718984626360708*^9, 3.718984638813925*^9}, 3.718984792076757*^9, {
3.718984867509242*^9, 3.71898486865304*^9}, {3.71898581894001*^9,
3.718985843606532*^9}, {3.718987192279818*^9, 3.7189871971728907`*^9}, {
3.7189878318578787`*^9,
3.7189878664810057`*^9}},ExpressionUUID->"38fb936c-def2-4f0a-ac61-\
f06de0c8e07b"]
},
WindowSize->{1280, 687},
WindowMargins->{{0, Automatic}, {Automatic, 0}},
FrontEndVersion->"11.1 for Mac OS X x86 (32-bit, 64-bit Kernel) (April 27, \
2017)",
StyleDefinitions->"Default.nb"
]
(* End of Notebook Content *)
(* Internal cache information *)
(*CellTagsOutline
CellTagsIndex->{}
*)
(*CellTagsIndex
CellTagsIndex->{}
*)
(*NotebookFileOutline
Notebook[{
Cell[558, 20, 5341, 102, 222, "Input", "ExpressionUUID" -> \
"dd3a8ac6-ae65-4c57-9565-40a69eca9642"],
Cell[5902, 124, 12836, 264, 579, "Input", "ExpressionUUID" -> \
"2649b2e4-0b37-4f7a-b3f1-ccd31795ac3c"],
Cell[18741, 390, 5328, 126, 243, "Input", "ExpressionUUID" -> \
"38fb936c-def2-4f0a-ac61-f06de0c8e07b"]
}
]
*)
(* End of internal cache information *)