-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path1-distributions.Rmd
107 lines (72 loc) · 1.9 KB
/
1-distributions.Rmd
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
---
title: "Excercises"
output: html_notebook
---
# Settings
R setup
```{r}
knitr::opts_chunk$set(engine.path = list(
python = "/usr/local/anaconda3/bin/python",
julia = "/Applications/Julia-1.3.app/Contents/Resources/julia/bin/"
))
```
Python setup
```{python}
#import numpy as np ## numerical
#import scipy as sp ## scientific
import scipy.stats as stats
import pandas as pd ## data processing
```
Julia setup
```{julia}
using Distributions ## for distributions
using DataFrames
```
# Exercise 1
Assume that football player with success rate 0.4 shot 10 times on goal. Let $X$ be a random variable denoting number of successful scores. Please find:
1. Distribution of $X$
2. Probability that football player score exactly 4 times ($P(X=4)$)
3. Probability that football player score at least 7 times ($P(X>=7) = 1- P(X <= 6)$)
## Solutions in R
```{r}
## Solution to 1
data.frame(trials = 0:10, p = dbinom(x = 0:10, size = 10, prob = 0.4))
## Solution to 2
dbinom(x = 4, size = 10, prob = 0.4)
## Solution to 3
1 - pbinom(q = 6, size = 10, prob = 0.4)
```
## Solutions in Python
```{python}
## Solution to 1
x = range(0,11)
p = [stats.binom.pmf(i, 10, 0.4) for i in x]
pd.DataFrame(data = {"x": x, "p": p})
## Solution to 2
stats.binom.pmf(4,10,0.4)
## Solution to 3
1 - stats.binom.cdf(6,10,0.4)
```
## Solutions in Julia
```{julia}
## Solution to 1
hcat(0:10, [pdf(Binomial(10,0.4), i) for i in 0:10]) ## or hcat(0:10, pdf.(Binomial(10,0.5), 0:10))
## Solution to 2
pdf(Binomial(10,0.4), 4)
## Solution to 3
1 - cdf(Binomial(10,0.4), 6)
```
# Exercise 2
Number of car accidents in one day in some city follows Poisson distribution with expected value $\lambda=2$. Find the probability that at most 4 car accidents happen.
## Solution in R
```{r}
ppois(q = 4, lambda = 2)
```
## Solution in Python
```{python}
stats.poisson.cdf(4, 2)
```
## Solution in Julia
```{julia}
cdf(Poisson(2), 4)
```