-
Notifications
You must be signed in to change notification settings - Fork 5
/
slides.tex
307 lines (276 loc) · 11.2 KB
/
slides.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
% Packages
% sudo tlmgr install silence appendixnumberbeamer fira fontaxes mwe noto csquotes babel helvet
%--- Preamble ---------------------------------------------------------%
% Load LaTeX packages
\documentclass[aspectratio=169]{beamer} % supports floating text in any location
\usetheme[darkmode]{pureminimalistic}
%\usetheme[lightmode]{pureminimalistic}
\AtBeginSection[]{%
\frame<beamer>{
\frametitle{Sumário para \secname}
\setcounter{tocdepth}{10}
\tableofcontents[currentsubsection, sectionstyle=hide/hide, subsectionstyle=show/show/hide, subsubsectionstyle=show/show/show/hide] % Show current section and current section's subsections, but hide other section's section and subsections!
}
}
\usepackage[utf8]{inputenc}
\usepackage{csquotes,xpatch}% recommended
%\usepackage[english]{babel}
%\usepackage[american]{babel}
\usepackage[english,main=portuguese]{babel}
\usepackage{hyphenat}
\hyphenation{mate-mática recu-perar}
\usepackage{graphicx}
\usepackage{tabularx}
\usepackage{booktabs}
\usepackage{bookmark}
% Hyperlinks
\usepackage{hyperref}
\hypersetup{%
%colorlinks=false,% hyperlinks will not be colored
%linkbordercolor=white,
%allcolors=white,
%pdfborderstyle={/S/U/W 1}% border style will be underline of width 1pt
}
% Math
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{wasysym}
% Algorithms
\usepackage[ruled,vlined,portuguese]{algorithm2e}
\SetKwFor{Para}{para}{}{}
% Code
\usepackage{listings}
\usepackage{lstbayes} % Stan
\lstset{
language=R,
moredelim=**[is][\color{blue}]{@}{@}}
% strikethought text
\usepackage[normalem]{ulem}
% Plots
\usepackage{pgfplots}
\graphicspath{{logos}{../images}{images}}
\pgfplotsset{height=6cm, % only if needed
width=12cm,
compat=1.17,
legend style = {fill = black, draw = white},
contour/every contour label/.style={
every node/.style={mapped color!50!black,fill=black}}
}
\usepackage{tikz}
\usepackage{tikzsymbols} % Metropolis Little Man
\usetikzlibrary{shapes}
\usetikzlibrary{arrows}
\usetikzlibrary{positioning}
\usetikzlibrary{calc}
\usetikzlibrary{automata} % State in Markov Chains
\usetikzlibrary{intersections}
\usetikzlibrary{backgrounds}
\usetikzlibrary{external}
\tikzexternalize[prefix=tikz/]
% Espaço Amostral Subfiguras
\usepackage{subfigure}
\definecolor{gray80}{gray}{0.8}
\definecolor{gray60}{gray}{0.6}
\definecolor{colorA}{rgb}{102, 255, 255}
\definecolor{colorB}{rgb}{0, 102, 255}
\usepackage{transparent}
% Modelos Multiníveis
\usepackage{adjustbox}
\newcommand*{\offset}{0.025}
\definecolor{light}{RGB}{188, 188, 220}
\definecolor{mid}{RGB}{124, 124, 185}
\definecolor{dark}{RGB}{39, 39, 143}
\definecolor{highlight}{RGB}{180, 31, 180}
% Animations
\usepackage{media9}
\usepackage{multimedia}
\addmediapath{animations}
\addmediapath{../images}
% Bibliography
\usepackage[
backend=biber,
doi=true,
style=apa,
url=true,
eprint=false]{biblatex}
\usepackage{csquotes}
\addbibresource{../bib/bibliografia.bib}
% this makes it possible to add backup slides, without counting them
\usepackage{appendixnumberbeamer}
\renewcommand{\appendixname}{\texorpdfstring{\translate{appendix}}{appendix}}
% footer page
\renewcommand{\pageword}{Página}
% Math Font Default (Fira is strange)
\renewcommand\mathfamilydefault{\rmdefault}
% if loaded after begin{document} a warning will appear: "pdfauthor already used"
\title[Estatística Bayesiana]{Estatística Bayesiana}
\author{Jose Storopoli \newline
\texttt{josees@uni9.pro.br}}
\institute{Universidade Nove de Julho - UNINOVE}
\date{Maio 2022}
%%%% Maths crap
\newtheorem{theo}{Theorema}[]
\newtheorem{defn}{Definição}[]
\newtheorem{question}{Questão}[]
\newtheorem{idea}{Ideia}[]
\newtheorem{exemplo}{Exemplo}[]
\newtheorem{property}{Propriedade}[]
\begin{document}
%--- Title Page -------------------------------------------------------%
\maketitle
%--- Intro Bayes for Everyone ----------------------------------------%
\begingroup
\AtBeginSection[]{}
\include{intro.tex}
\endgroup
%--- Table of Contents-------------------------------------------------%
\setcounter{tocdepth}{1}
\begin{frame}[plain, noframenumbering]{Sumário}
\tableofcontents
\end{frame}
%--- Distributions ----------------------------------------------------%
\tikzset{
declare function={
discreteuniform(\a,\b)=1/(\b-\a+1);%
binomial(\n,\p)=\n!/(x!*(\n-x)!)*\p^x*(1-\p)^(\n-x);%
poisson(\l)=(\l^x)*exp(-\l)/(x!);%
negativebinomial(\r,\p)=((x+\r-1)!/((\r-1)!*x!))*((1-\p)^x*\p^\r);%
continuousuniform(\a,\b)=1/(\b-\a);%
gaussian(\m,\s)=1/(\s*sqrt(2*pi))*exp(-((x-\m)^2)/(2*\s^2));%
lognormal(\m,\s)=1/(x*\s*sqrt(2*pi))*exp(-((ln(x)-\m)^2)/(2*\s^2));%
exponential(\l)=\l*exp(-\l*x);%
gamma(\z)=2.506628274631*sqrt(1/\z)+ 0.20888568*(1/\z)^(1.5)+ 0.00870357*(1/\z)^(2.5)- (174.2106599*(1/\z)^(3.5))/25920- (715.6423511*(1/\z)^(4.5))/1244160)*exp((-ln(1/\z)-1)*\z;%
student(\n)=gamma((\n+1)/2.)/(sqrt(\n*pi)*gamma(\n/2.))*((1+(x*x)/\n)^(-(\n+1)/2.));%
beta(\a,\b)=(x^(\a-1)*(1-x)^(\b-1)/((gamma(\a)*gamma(\b))/gamma(\a+\b));%
binormal(\ma,\sa,\mb,\sb,\ro)=exp(-(((x-\ma)/\sa)^2+((y-\mb)/\sb)^2-(2*\ro)*((x-\ma)/\sa)*((y-\mb)/\sb))/(2*(1-\ro^2)))/(2*pi*\sa*\sb*(1-\ro^2)^0.5);%
conditionalbinormal(\yc,\ma,\sa,\mb,\sb,\ro)=exp(-(((x-\ma)/\sa)^2+((\yc-\mb)/\sb)^2-(2*\ro)*((x-\ma)/\sa)*((\yc-\mb)/\sb))/(2*(1-\ro^2)))/(2*pi*\sa*\sb*(1-\ro^2)^0.5);%
sumtwonormals(\ma,\sa,\wa,\mb,\sb,\wb)=(\wa*gaussian(\ma,\sa))+(\wb*gaussian(\mb,\sb));%
normcdf(\m,\s)=1/(1 + exp(-0.07056*((x-\m)/\s)^3 - 1.5976*(x-\m)/\s));%
}
}
%--- Lectures --------------------------------------------------------%
\include{1-Estatistica_Bayesiana}
\include{2-Distribuicoes_Estatisticas}
\include{3-rstanarm_brms}
\include{4-Priors}
\include{5-Priors_Posteriors_Checks}
\include{6-Regressao_Linear}
\include{7-Regressao_Logistica}
\include{7.1-Regressao_Ordinal}
\include{8-Regressao_Poisson}
\include{9-Regressao_Robusta}
\include{10-Modelos_Multiniveis}
\include{11-MCMC}
\include{12-Comparacao_Modelos}
%--- Citations -------------------------------------------------------%
\begingroup
\AtBeginSection[]{}
\section{Referências}
\begin{frame}[allowframebreaks]{Referências}
\printbibliography
\end{frame}
\endgroup
%--- Appendix Slides -------------------------------------------------%
\appendix % do not count the following slides for the total number
\section*{Backup Slides}
\begin{frame}[plain, noframenumbering]{Licença}
\centering
\vfill
\Large O texto e as figuras desses slides possuem uma
\href{https://creativecommons.org/licenses/by-nc-sa/4.0/deed.pt}{Licença
Creative Commons
Atribuição-NãoComercial-CompartilhaIgual 4.0 Internacional (CC BY-NC-SA 4.0)}
\vfill
\includegraphics[width = 0.2\textwidth]{CC_SA.png}
\end{frame}
\begin{frame}[plain, noframenumbering]{Como citar esse Conteúdo}
\centering
\vfill
\Large \fullcite{storopoli2021estatisticabayesianaR}
\vfill
\end{frame}
\begin{frame}[plain, noframenumbering, label=appendixmontyhall, fragile]{Simulação Monte Carlo do Problema de Monty Hall em R}
\begin{lstlisting}[basicstyle=\footnotesize, language=R]
monty <- function(){
door_car <- sample(1:3L, 1)
door_pick <- sample(1:3L, 1)
door_goats <- setdiff(1:3L, door_car)
if(door_pick == door_car) {
door_show <- sample(door_goats, 1)
} else{
door_show <- setdiff(door_goats, door_pick)
}
door_switch <- setdiff(1:3L, c(door_pick, door_show))[1]
door_pick <- door_switch
}
wins <- 0
ntrials <- 10000
for (i in 1:ntrials){
wins <- wins + monty()
}
wins / ntrials
\end{lstlisting}
\end{frame}
\begin{frame}[plain, noframenumbering, fragile]{Simulação Monte Carlo do Problema de Monty Hall em Julia}
\begin{lstlisting}[basicstyle=\footnotesize, language=Matlab,escapeinside=\{\}]
function monty()
door_car = rand(1:3)
door_pick = rand(1:3)
door_goats = setdiff([1;2;3],door_car)
door_show = door_pick == door_car ? rand(door_goats) : setdiff(door_goats,door_pick)
door_switch = setdiff([1;2;3], [door_pick; door_show])[1]
door_pick = door_switch
return door_pick == door_car
end
ntrials = 10_000
wins = reduce(+, (monty() for i {$\in$} 1:ntrials))
wins / ntrials
\end{lstlisting}
\end{frame}
\begin{frame}[plain, noframenumbering, label=appendixnormal]{Como surgiu a distribuição Normal\footnote{Abraham de Moivre em 1738}\footnote{Uma melhor explanação pode ser encontrada \href{http://www.stat.yale.edu/~pollard/Courses/241.fall2014/notes2014/Bin.Normal.pdf}{clidando aqui}}}
$$
\begin{aligned}
\text{Binomial}(n, k) &= \binom{n}{k} p^k (1-p)^{n-k} \\
n! &\approx \sqrt{2 \pi n} \left(\frac{n}{e}\right)^n &\text{(Stirling)} \\
\lim_{n \to \infty} \binom{n}{k} p^k (1-p)^{n-k} &= \frac{1}{\sqrt{2 \pi npq}} e^{-\frac{(k - np)^2}{2npq}}
\end{aligned}
$$
Sabemos que na binomial: $\mathrm{E} = np$ e $\mathrm{Var} = npq$; logo substituindo $\mathrm{E}$ por $\mu$ $\mathrm{Var}$ por $\sigma^2$:
$$\lim_{n \to \infty} \binom{n}{k} p^k (1-p)^{n-k} = \frac{1}{\sigma \sqrt{2 \pi}} e^{-\frac{(k - \mu)^2}{\sigma^2}}$$
\end{frame}
\begin{frame}[plain, noframenumbering, label=appendixscopus]{Buscas Scopus do \texttt{Stan}}
\begin{vfilleditems}
\item Uso Geral na Scopus: \texttt{ALL((brms AND burkner) OR (gelman AND hoffman AND stan) OR mc-stan.org OR rstanarm OR pystan OR (rstan AND NOT mit))}
\item Uso em Comparação com outras Ferramentas Bayesianas: \texttt{REF((gelman AND hoffman AND stan) OR mc-stan.org) AND REF(brms AND burkner) AND REF(pystan) AND REF(rstanarm) AND REF(rstan AND NOT mit)}
\item Uso das Ferramentas Bayesians: a busca anterior adicionado de \texttt{REF(PyMC3 OR (PyMC* AND fonnesbeck)) AND REF(tensorflow) AND REF(pytorch) AND REF(Keras)}
\end{vfilleditems}
\end{frame}
\begin{frame}[plain, noframenumbering, label=appendixnqr]{Decomposição QR}
\footnotesize
Em Álgebra Linear 101 aprendemos que qualquer matriz
(até mesmo as retangulares) podem ser decompostas em um produto de duas matrizes:
\begin{vfilleditems}
\footnotesize
\item $\mathbf{Q}$: uma matriz ortogonal (suas colunas são vetores unitários
ortogonais, \textit{i.e.} $\mathbf{Q}^T = \mathbf{Q}^{-1}$)
\item $\mathbf{R}$: uma matrix triangular superior
\end{vfilleditems}
\footnotesize
Agora vamos incorporar a decomposição QR no modelo de regressão linear.
Aqui, usarei o QR "fino" em vez do "gordo", que escala $\mathbf{Q}$ e $\mathbf{R}$
matrizes por um fator de $\sqrt{n-1}$ onde $n$ é o número de linhas de $\mathbf{X}$.
Na prática, é melhor implementar a decomposição QR fina, que é preferível à decomposição QR gorda.
É numericamente mais estável. Matematicamente, a decomposição QR fina é:
$$
\begin{aligned}
\mathbf{X} &= \mathbf{Q}^* \mathbf{R}^* \\
\mathbf{Q}^* &= \mathbf{Q} \cdot \sqrt{n - 1} \\
\mathbf{R}^* &= \frac{1}{\sqrt{n - 1}} \cdot \mathbf{R}\\
\boldsymbol{\mu} &= \alpha + \mathbf{X} \cdot \boldsymbol{\beta} + \sigma \\
&= \alpha + \mathbf{Q}^* \cdot \mathbf{R}^* \cdot \boldsymbol{\beta} + \sigma \\
&= \alpha + \mathbf{Q}^* \cdot (\mathbf{R}^* \cdot \boldsymbol{\beta}) + \sigma \\
&= \alpha + \mathbf{Q}^* \cdot \widetilde{\boldsymbol{\beta}} + \sigma
\end{aligned}
$$
\end{frame}
\end{document}