-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlecture2.tex
610 lines (504 loc) · 16 KB
/
lecture2.tex
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
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
%Lecture 2
%Copyright (C) 2019 Patrick Diehl
%
%This program is free software: you can redistribute it and/or modify
%it under the terms of the GNU General Public License as published by
%the Free Software Foundation, either version 3 of the License, or
%(at your option) any later version.
%This program is distributed in the hope that it will be useful,
%but WITHOUT ANY WARRANTY; without even the implied warranty of
%MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
%GNU General Public License for more details.
%You should have received a copy of the GNU General Public License
%along with this program. If not, see <http://www.gnu.org/licenses/>.
\providecommand\classoption{12pt}
\documentclass[\classoption]{beamer}
\beamertemplatenavigationsymbolsempty
% usepackage
\input{template/packages}
\input{template/variables.tex}
% frame slide
\title{\coursename}
\subtitle{Lecture 2: Monte Carlo, Vectors, and Algorithms}
\author{\tiny Patrick Diehl \orcid{0000-0003-3922-8419}}
%\institute {
% \href{}{\tt \scriptsize \today}
%}
\date {
\tiny \url{\courseurl}
\vspace{2cm}
\doclicenseThis
}
\usepackage{ifxetex}
\ifxetex
\usepackage{fontspec}
\setmainfont{Raleway}
\fi
\ifluatex
\usepackage{fontspec}
\setmainfont{Raleway}
\fi
\begin{document} {
\setbeamertemplate{footline}{}
\frame {
\titlepage
}
}
\frame{
\tableofcontents
}
\AtBeginSection[]{
\begin{frame}
\vfill
\centering
\begin{beamercolorbox}[sep=8pt,center,shadow=true,rounded=true]{title}
\usebeamerfont{title}\insertsectionhead\par%
\end{beamercolorbox}
\vfill
\end{frame}
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Reminder}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}{Lecture 1}
\begin{block}{What you should know from last lecture}
\begin{itemize}
\item Structure of a C++ program
\item \lstinline|std::string|
\begin{itemize}
\item Reading strings \lstinline|std::cin|
\item Writing strings \lstinline|std:cout|
\end{itemize}
\item Looping and counting
\begin{itemize}
\item \lstinline|while|
\item \lstinline|for|
\end{itemize}
\item Conditionals \lstinline|if|
\item Operators
\item Built-in types
\end{itemize}
\end{block}
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Monte Carlo methods}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}{Monte Carlo method}
\textbf{Monte Carlo methods} are computational algorithms which rely on repeated random sampling to obtain numerical results. More details~\cite{shonkwiler2009explorations}.
\begin{block}{Three problem classes~\cite{kroese2014monte}:}
\begin{enumerate}
\item Optimization
\item Numerical integration
\item Probability distributions
\end{enumerate}
\end{block}
\begin{block}{General pattern:}
\begin{enumerate}
\item Define the input parameters
\item Randomly chose input parameters
\item Do deterministic computations on the inputs
\item Aggregate the results
\end{enumerate}
\end{block}
\end{frame}
\begin{frame}{Example: Estimate the value of $\pi$}
\begin{columns}
\begin{column}{0.3\textwidth}
\centering
\only<1>{
\begin{tikzpicture}
\draw (0,0) -- (2,0) -- (2,2) -- (0,2) -- cycle;
\end{tikzpicture}
}
\only<2>{
\begin{tikzpicture}
\draw (0,0) -- (2,0) -- (2,2) -- (0,2) -- cycle;
\draw (1,1) circle (1cm);
\end{tikzpicture}
}
\end{column}
\begin{column}{0.7\textwidth}
\begin{block}{Ingredients}
\begin{itemize}
\item Unit square $1\times 1$
\item circle with the radius of $r=\sfrac{1}{2}$
\item Area of the circle $A_c=\pi r^2 = \sfrac{\pi}{4} $
\item Area of the square $A_s = 1 \cdot 1 = 1$
\item Recall: $A_c=\sfrac{\pi}{4}\rightarrow \pi = 4A_c$
\item Hint: $\sfrac{A_c}{A_s} = A_c$ because $A_s=1$
\end{itemize}
\end{block}
\end{column}
\end{columns}
%\vspace{1cm}
Now compute PI by using the two areas:
\begin{align*}
\pi \approx 4 \sfrac{A_c}{A_s}
\end{align*}
The areas can be approximated by using $N$ random samples $(x,y)$ and count the points inside the circle $N_c$
\begin{align*}
\pi \approx 4 \sfrac{N_c}{N}
\end{align*}
\end{frame}
\begin{frame}{Algorithm}
\begin{enumerate}
\item Generate random coordinates $(x,y)\in \mathbb{R}^2$
\item Check if $x^2+y^2 \leq 1$
\begin{itemize}
\item Update $N_c$ if $\leq 1$
\end{itemize}
\item Increment $n$ the interval
\item If $n$ < $n_{\text{total}}$ go to 1
\item Calculate $\pi \approx 4 \sfrac{N_c}{n_\text{total}}$
\item Print result
\end{enumerate}
\vspace{0.25cm}
\textcolor{azure}{Interactive question:} \\
\vspace{0.125cm}
Which features of C++ do we need? \\
\vspace{0.25cm}
\only<2>{
\lstinline|for|, \lstinline|if|, \lstinline|std::cout|, and random numbers
}
\end{frame}
\begin{frame}{Random numbers\footnote{\tiny\url{https://en.cppreference.com/w/cpp/numeric/random/srand}}}
\lstinputlisting{cpp/lecture2-random.cpp}
More details: Section~3.2~\cite{knuth1997art2} and \cite{matsumoto1998mersenne}
\end{frame}
\begin{frame}{Uniform distributed random numbers\footnote{\tiny\url{https://en.cppreference.com/w/cpp/numeric/random/uniform_int_distribution}} \footnote{\tiny\url{https://en.cppreference.com/w/cpp/numeric/random/uniform_real_distribution}}}
\lstinputlisting{cpp/lecture2-distrandom.cpp}
\end{frame}
\begin{frame}{Sketches for for various random numbers}
\begin{figure}[ht]
\begin{subfigure}{.3\textwidth}
\centering
% include first image
\def\x{0}
\def\y{0}
\def\k{0}
\def\radius{3}
\begin{tikzpicture}
\draw[fill=cadetgrey, opacity=0.1] (\radius,0) arc(0:90:\radius) -- (0,0) -- cycle;
\draw[gray, opacity=0.25] (0,0) rectangle (\radius,\radius);
\draw[->] (0,0) -- (1.1*\radius,0);
\draw[->] (0,0) -- (0,1.1*\radius);
\foreach \i in {1,2,...,10}{%
%\typeout{Point \i}%
\pgfmathsetmacro\x{\radius*rnd}%
%\typeout{X \x}%
\pgfmathsetmacro\y{\radius*rnd}%
%\typeout{Y \y}%
\pgfmathsetmacro\k{(pow(\x,2)+pow(\y,2)) <pow(\radius,2)}%
%\typeout{im Kreis?: \k}%
\pgfmathparse{ifthenelse(\k==1,"awesome","azure")}%
\fill[\pgfmathresult] (\x,\y)circle(0.75pt);%
}
\end{tikzpicture}
\caption{$N=10$}
\label{fig:sub-first}
\end{subfigure}
\hfill
\begin{subfigure}{.3\textwidth}
\centering
% include second image
\def\x{0}
\def\y{0}
\def\k{0}
\def\radius{3}
\begin{tikzpicture}
\draw[fill=cadetgrey, opacity=0.1] (\radius,0) arc(0:90:\radius) -- (0,0) -- cycle;
\draw[gray, opacity=0.25] (0,0) rectangle (\radius,\radius);
\draw[->] (0,0) -- (1.1*\radius,0);
\draw[->] (0,0) -- (0,1.1*\radius);
\foreach \i in {1,2,...,100}{%
%\typeout{Point \i}%
\pgfmathsetmacro\x{\radius*rnd}%
%\typeout{X \x}%
\pgfmathsetmacro\y{\radius*rnd}%
%\typeout{Y \y}%
\pgfmathsetmacro\k{(pow(\x,2)+pow(\y,2)) <pow(\radius,2)}%
%\typeout{im Kreis?: \k}%
\pgfmathparse{ifthenelse(\k==1,"awesome","azure")}%
\fill[\pgfmathresult] (\x,\y)circle(0.75pt);%
}
\end{tikzpicture}
\caption{$N=100$}
\end{subfigure}
\hfill
\begin{subfigure}{.3\textwidth}
\centering
% include second image
\def\x{0}
\def\y{0}
\def\k{0}
\def\radius{3}
\begin{tikzpicture}
\draw[fill=cadetgrey, opacity=0.1] (\radius,0) arc(0:90:\radius) -- (0,0) -- cycle;
\draw[gray, opacity=0.25] (0,0) rectangle (\radius,\radius);
\draw[->] (0,0) -- (1.1*\radius,0);
\draw[->] (0,0) -- (0,1.1*\radius);
\foreach \i in {1,2,...,1000}{%
%\typeout{Point \i}%
\pgfmathsetmacro\x{\radius*rnd}%
%\typeout{X \x}%
\pgfmathsetmacro\y{\radius*rnd}%
%\typeout{Y \y}%
\pgfmathsetmacro\k{(pow(\x,2)+pow(\y,2)) <pow(\radius,2)}%
%\typeout{im Kreis?: \k}%
\pgfmathparse{ifthenelse(\k==1,"awesome","azure")}%
\fill[\pgfmathresult] (\x,\y)circle(0.75pt);%
}
\end{tikzpicture}
\caption{$N=1000$}
\label{fig:sub-second}
\end{subfigure}
\caption{Distribution of the point inside the circle (\textcolor{awesome}{red}) and outside of the circle (\textcolor{azure}{blue}) for $N=10$, $N=100$, and $N=1000$ random numbers.}
\label{fig:monte:carlo:samples}
% This example was adapted from https://tex.stackexchange.com/questions/244488/monte-carlo-method-drawing
\end{figure}
\end{frame}
\newcommand\montecarlo[2]{
\def\x{0}
\def\y{0}
\def\k{0}
\def\n{0}
\def\r{0}
\def\pi{3.1415}
\def\e{0}
\def\radius{0.5}
\foreach \i in {1,2,...,#1}{%
%\typeout{Point \i}%
\pgfmathsetmacro\x{abs(\radius*rnd)}%
%\typeout{X \x}%
\pgfmathsetmacro\y{abs(\radius*rnd)}%
%\typeout{Y \y}%
\pgfmathsetmacro\k{(pow(\x,2)+pow(\y,2)) <pow(\radius,2)}%
%\typeout{im Kreis?: \k}%
\pgfmathparse{ifthenelse(\k==1,1,0)}%
\pgfmathparse{\n+\pgfmathresult}
\xdef\n{\pgfmathresult}
%\typeout{N \n}
}
\pgfmathparse{4*\n/#1}
\xdef\r{\pgfmathresult}
%\typeout{R \r}
\pgfmathparse{(\r-\pi)/\pi*100}
\xdef\e{abs(\pgfmathresult)}
%\typeout{E \e}
\addplot [only marks,mark=*] coordinates { (#2,\e) };
}
\begin{frame}{How many random numbers are needed?}
\begin{figure}[tb]
\centering
\begin{tikzpicture}
\begin{axis}
[
xmin=0, xmax=4,
grid=major,
xlabel=Numer of random numbers $10^x$,
ylabel=Absolute Error (\%)
]
\montecarlo{10}{1}
\montecarlo{100}{2}
\montecarlo{1000}{3}
\end{axis}
\end{tikzpicture}
\end{figure}
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Containers \& Algorithms}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}{Computation of the average\footnote{\tiny\url{https://en.cppreference.com/w/cpp/io/manip/setprecision}}}
\lstinputlisting{cpp/lecture2-average.cpp}
\end{frame}
\begin{frame}{Reading multiple values}
\begin{block}{Recall: Reading one value}
\lstinline| std::cin >> x|
\end{block}
\begin{block}{Reading multiple values}
\lstinputlisting[firstline=7, lastline=13]{cpp/lecture2-average.cpp}
\end{block}
Note that the \lstinline|while| loops stops by inserting \lstinline|\\n|
\end{frame}
\begin{frame}{What about computing the median?}
\begin{block}{We have to:}
\begin{itemize}
\item We have to store all values without knowing the amount in advance
\item We have to sort all the values
\item We have to select the middle value(s) efficiently
\end{itemize}
\end{block}
\pause
\begin{block}{What we will use:}
\begin{itemize}
\item Containers: \lstinline| std::vector | to store the elements
\item Algorithm, like sorting or accumulate
\item Iterators for accessing elements
\end{itemize}
\end{block}
\end{frame}
\begin{frame}{Advanced computing of the average}
\lstinputlisting{cpp/lecture2-averageContainers.cpp}
\end{frame}
\begin{frame}{Initialization of Vectors\footnote{\tiny{\url{https://en.cppreference.com/w/cpp/container/vector}}}}
\begin{block}{Initialization of an empty vector}
\begin{itemize}
\item \lstinline| std::vector<double> values;|
\item \lstinline| values.empty() | will return \lstinline|true|
\item \lstinline| values.size() | will return zero
\end{itemize}
\end{block}
\begin{block}{Initialization}
\begin{itemize}
\item \lstinline| std::vector<double> values = \{1, 2.5\}; |
\item \lstinline| values.empty() | will return \lstinline|false|
\item \lstinline| values.size() | will return two
\end{itemize}
\end{block}
\textcolor{red}{Note that C++ starts counting by 0 and the length is always number of elements minus one!}
\end{frame}
\begin{frame}{Manipulating vectors}
\begin{block}{Inserting}
\begin{itemize}
\item \lstinline| values.push_back(3.5); | append 3.5 at the end
\item \lstinline| values[3] = 1.5; | replaces the third element by 1.5
\end{itemize}
\end{block}
\begin{block}{Accessing}
\begin{itemize}
\item \lstinline| values[i] | returns element $i$
\item \lstinline| values.first() | returns the first element
\item \lstinline| values.last() | return the last element
\end{itemize}
\end{block}
\begin{block}{Deleting}
\begin{itemize}
\item \lstinline|values.pop_back(); | deletes the last element
\item \lstinline|values.erase(values.start()+i)| deletes the $i$-th element
\end{itemize}
\end{block}
\end{frame}
\begin{frame}{Algorithms}
\begin{block}{Accumulate\footnote{\tiny\url{https://en.cppreference.com/w/cpp/algorithm/accumulate}}}
\lstinline| double sum = std::accumulate(v.begin(), v.end(), 0.0f); |
\begin{itemize}
\item The first two arguments define the range of the vector
\item The third is the initial value of the sum
\item The header \lstinline|\#include <numerics>| provides this functionality
\end{itemize}
\end{block}
\begin{block}{Sorting\footnote{\tiny\url{https://en.cppreference.com/w/cpp/algorithm/sort}}}
\lstinline|std::sort(s.begin(), s.end());|
\begin{itemize}
\item The header \lstinline|\#include <algorithm>| provides this functionality
\end{itemize}
\end{block}
\end{frame}
\begin{frame}[fragile]{Looping over a vector}
\begin{block}{Long form}
\begin{lstlisting}
for( size_t i = 0; i < value.size() ; i++)
{
values[i] *= 2;
}
\end{lstlisting}
\end{block}
\begin{block}{Short form}
\begin{lstlisting}
for (auto& v : values)
{
v *= 2;
}
\end{lstlisting}
\end{block}
\begin{itemize}
\item \lstinline|auto| is equivalent to \lstinline|double v : values|\footnote{\tiny\url{https://en.cppreference.com/w/cpp/language/auto}}
\end{itemize}
\end{frame}
\begin{frame}{Recall: Advanced computing of the average}
\lstinputlisting{cpp/lecture2-averageContainers.cpp}
\end{frame}
\begin{frame}[fragile]{Computing the median}
\begin{block}{Code}
\begin{lstlisting}
typedef std::vector<double>::size_type vec_sz;
vec_sz size = values.size();
double median = 0;
vec_sz mid = size/2;
if( size % 2 == 0 )
median = 0.5*(values[mid] + values[mid-1]);
else
median = values[mid];
\end{lstlisting}
\end{block}
\begin{block}{Features}
\begin{itemize}
\item \lstinline|typedef|\footnote{\tiny\url{https://en.cppreference.com/w/cpp/language/typedef}} is useful to not write large expression again
\item Conditional parameter\footnote{\tiny\url{https://en.cppreference.com/w/cpp/language/operator_other}} \lstinline|median = size % 2 == 0 ? true : false; |
\end{itemize}
\end{block}
\end{frame}
\begin{frame}{Advanced computing of the median}
\lstinputlisting{cpp/lecture2-median.cpp}
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Functions}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{Function definition\footnote{\tiny\url{https://en.cppreference.com/w/c/language/function_definition}}}
\begin{block}{Example: maximum}
\begin{lstlisting}
int max(int a, int b)
{
return a>b?a:b;
}
\end{lstlisting}
\end{block}
\begin{block}{Each function has}
\begin{itemize}
\item a name \lstinline|max|
\item a return type \lstinline|int max()|
\item a return value \lstinline|return a>b?a:b;|
\item some function parameters \lstinline| max(int a, int b)| or none \lstinline| max(void) |
\end{itemize}
\end{block}
Function are defined between \lstinline|#include| and \lstinline|int main (void)|
\end{frame}
\begin{frame}[fragile]{Function definition for the median}
\begin{block}{Function definition}
\begin{lstlisting}
double median(std::vector<double> values)
{
std::sort(values.begin(),values.end());
vec_sz mid = values.size() / 2;
double res = values.size() % 2 == 0 ?
0.5*(values[mid]+values[mid-1]) : values[mid];
return res;
}
\end{lstlisting}
\end{block}
\begin{block}{Example}
\lstinline|double result = median(values);|
\end{block}
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Summary}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}{Summary}
\begin{block}{After this lecture, you should know}
\begin{itemize}
\item Monte Carlo Methods
\item Random numbers
\item Containers like \lstinline|std::vector|
\item Functions
\end{itemize}
\end{block}
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{References}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[t, allowframebreaks]
\frametitle{References}
\bibliographystyle{plain}
\bibliography{bib}
\end{frame}
\end{document}