Summary Files Commits Branches @commit @diff @stats
path: root / Year 3 / Statistics / hw2.tex
blob: 6fbeeb025f1834de88125953d02a0b0fe1fe3f6c (plain) (blame)
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
% Document MUST be built with XeLaTeX!
\documentclass[11pt,a4paper]{article}

\usepackage[margin=1.5cm]{geometry}

% Поддръжка на български език
% Thanks to https://tex.stackexchange.com/a/136926
\usepackage{fontspec}
\defaultfontfeatures{Ligatures=TeX}\setmainfont{CMU Serif}
\setsansfont{CMU Sans Serif}

\usepackage[bulgarian,english]{babel}

\usepackage{amsmath}            % Математически символи
\usepackage{extarrows}
\usepackage{amsfonts}           % Още математически символи
\usepackage{cancel}             % Зачертаване
\usepackage[dvipsnames]{xcolor} % Цветни кутийки + допълнителни цветове
\usepackage{graphicx}           % Изображения

\usepackage{textcomp}
\usepackage{listings}
\newfontfamily{\lstsansserif}[Scale=1]{DejaVuSansMono}
\lstset{ 
  basicstyle=\lstsansserif\footnotesize,        % the size of the fonts that are used for the code
  breakatwhitespace=false,         % sets if automatic breaks should only happen at whitespace
  breaklines=true,                 % sets automatic line breaking
  frame=single,	                   % adds a frame around the code
  keepspaces=true,                 % keeps spaces in text, useful for keeping indentation of code (possibly needs columns=flexible)
  numbers=none,                    % where to put the line-numbers; possible values are (none, left, right)
  showspaces=false,                % show spaces everywhere adding particular underscores; it overrides 'showstringspaces'
  showstringspaces=false,          % underline spaces within strings only
  showtabs=false,                  % show tabs within strings adding particular underscores
  literate = {-}{-}1{"}{\textquotedbl}1{*}{\normalfont{*}}1,
}

\usepackage{enumitem}
\setlist[enumerate]{label=\arabic*.}

\newcommand{\indep}{\perp \!\!\! \perp}

\usepackage[pdfauthor={Камен Димитров Младенов, },
		    pdftitle={Решения на задачи от Домашно 1 по ВИС практикум},
		    pdfsubject={},
		    pdfkeywords={},
		    colorlinks=true,
		    urlcolor=blue]{hyperref}

\title{\vspace{-1.5cm}Решения на задачи от Домашно 1 по ВИС практикум}
\author{Камен Димитров Младенов, FN}
\date{16.04.2024}

\begin{document}

\maketitle

\paragraph{Задача 1.}

Генерирайте стойности $x_1, x_2, ..., x_n$ на случайна величина $X = W_1 + W_2$, където \\ $W_i \sim \text{Exp}(1)$ и
$W_1 \indep W_2$. Проверете нулевата хипотезата, че генерираните данни са от нормално разпределение с
помощта на теста на Шапиро–Уилк (Shapiro–Wilk). Повторете $10000$ пъти за \\ $n = 10, 20, 30, 40, 50, 60$.
Колко често заключението на теста е вярно? Представете чрез подходяща графика честотата на вярно
заключение в зависимост от $n$.

\subparagraph{Решение} \hfill

\lstinputlisting[language=R, firstline=1, lastline=18, caption=Сорс-код]{hw2.r}

Заключението е вярно през средно $\sim 27.8\%$ от времето.

Графика на честотата на вярно заключение в зависимост от $n$: \newline
\includegraphics[scale=0.7]{hw2-ex1.png}

\newpage

\paragraph{Задача 2.}

В кутия има $2$ зелени, $3$ бели и $5$ жълти топки. Симулирайте $n$ тегления на топка с връщане.
Използвайте генерираните данни, за да проверите хипотезата, че вероятността да изтеглим зелена топка
е $1/5$. Повторете $10000$ пъти за $n = 50, 100, 200, 500$. Колко често заключението на теста е вярно?
Представете чрез подходяща графика честотата на вярно заключение в зависимост от $n$.

\subparagraph{Решение} \hfill

\begin{lstlisting}[language=R, caption=Сорс-код]
ex2 <- function(greens = 2, whites = 3, yellows = 5, ns = c(50, 100, 200, 500)) {
  balls <- append(append(rep("green", greens), rep("white", whites)), rep("yellow", yellows))
  
  check_hyp <- function(n, iters=10000) {
    accepts <- sum(sapply(1:iters, function(i) {
      sampled_greens <- sum(sapply(1:n, function(s) {
        return (sample(balls, 1, replace=TRUE) == "green")
      }))
      
      return (prop.test(sampled_greens, n, 1/5)$p.value > 0.05)
    }))
    
    return (accepts / iters)
  }
  
  ys <- sapply(ns, check_hyp)
  plot(ns, ys, type="l", xlab="n", ylab="Съотношение на вярност на заключението")
  text(ns, ys, ys)
}

ex2()
\end{lstlisting}

Заключението е вярно през средно $\sim 96.6\%$ от времето.

Графика на честотата на вярно заключение в зависимост от $n$: \newline
\includegraphics[scale=0.7]{hw2-ex2.png}

\end{document}