Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions Practicals/Florian/Excerise 1_Florian.Rmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
---
title: "Excersie 1"
author: "Florian van Leeuwen"
date: "9/16/2022"
output: pdf_document
---

```{r, packges}
require(ggplot2)
```


## 8. Monte Carlo simulation exercisec
```{r}
# function to create CI for a normal distribution
ci <- function(n, seed){
set.seed(seed)
CI <- matrix(nrow = n, ncol = 4)
for (i in 1:n){
samples <- c()
samples <- rnorm(n)
mean <- mean(samples)
SE <- sqrt(sd(samples)/n)
lower <- mean - 1.96*SE
upper <- mean + 1.96*SE
CI[i,1] <- lower
CI[i,2] <- upper
CI[i,3] <- mean
CI[i,4] <- i
}
return(CI)
}
```
```{r}
# simulate data
data_ci <- ci(100,123)
```

```{r}
# add names and change to df for ggplot
colnames(data_ci) <- c("L", "U", "M", "N")
data_ci <- as.data.frame(data_ci)

# Create indictor for if 0 in the CI
data_ci2 <- data_ci %>%
mutate(outside = as.factor(ifelse(U < 0 | L > 0, 1, 0)))
```

```{r}
# make a plot
ggplot(data_ci2, aes(x = N, y = M, color = outside)) +
geom_point(size = 4) +
geom_errorbar(aes(ymax = U, ymin = L))
```

110 changes: 110 additions & 0 deletions Practicals/Florian/Excersie2.Rmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
---
title: "Exercise"
author: "Florian van Leeuwen"
date: "9/30/2022"
output: pdf_document
---

```{r}
library(tidyverse)
```

# How long can a player lose in roullete by only betting on even or uneven?
```{r}

Roulette <- function(N_spins, cutoff_plot, seed){
set.seed(seed)
result <- c()

for (i in 1:N_spins){
number <- sample(seq(0,36,1), 1)
if ((number %% 2) == 0){
if (number == 0) {
result[i] = "Zero"
}
else{
result[i] = "Even"
}
}
else{
result[i] = "Un Even"
}
}

X <- rle(result)
X2 <- data_frame(
N <- X$lengths,
V <- X$values
)
colnames(X2) <- c("N", "V")

P1 <- ggplot(X2, aes(x = N, fill = V)) +
geom_histogram(binwidth = 1, alpha = 0.7) +
scale_fill_manual(values=c("red", "black", "green")) +
scale_x_continuous(breaks= seq(1, max(X2$N), 1)) +
labs(title = paste("Number of times the ball falls in the same category in ",N_spins, "tries"), x = "Number of sequence", y = "Count", fill = "Where the ball lands") +
theme_classic()

P2 <- ggplot(X2, aes(x = N, fill = V)) +
geom_histogram(binwidth = 1, alpha = 0.7) +
scale_fill_manual(values=c("red", "black", "green")) +
scale_x_continuous(breaks= seq(1, max(X2$N), 1),
limits = c(cutoff_plot, max(X2$N))) +
labs(title = paste("Number of times the ball falls in the same category in ",N_spins, "tries"), x = "Number of sequence", y = "Count", fill = "Where the ball lands") +
theme_classic()

return(list(P1, P2))
}

Roulette(100000000, 15, 100)
```





# OLD PROJECT

# How many of the sample number of dice can we throw in 3 tries?
```{r}
dice <- seq(1,6,1)
N_dice <- 5
N <- 100


Yazhee <- function(dice, N_dice,N, seed){
set.seed(seed)
result <- c()
Trow_1 <- sample(dice, N_dice, replace = TRUE)
Trow_1_count <- as.data.frame(table(Trow_1))
Trow_2 <- c(sample(dice, (N_dice-Trow_1_count$Freq[1]), replace = TRUE),rep(as.numeric(as.character(Trow_1_count$Trow_1[1])),Trow_1_count$Freq[1]))
Trow_2_count <- as.data.frame(table(Trow_2))
Trow_3 <-c(sample(dice, (N_dice-Trow_2_count$Freq[1]), replace = TRUE),rep(as.numeric(as.character(Trow_2_count$Trow_1))[1],Trow_2_count$Freq[1]))
#Trow_3_count <- as.data.frame(table(Trow_3))
#result[i] <- Trow_3_count$Freq[1]
return(list(T1 = Trow_1, T1c = Trow_1_count, T2 = Trow_2, T2c = Trow_2_count, T3 = Trow_3))
}


Yazhee(dice, N_dice,N, 124)
```

```{r}
Yazhee2 <- function(dice, N_dice,N, seed){
set.seed(seed) # set seed

# empty var
trow <- matrix(nrow = N, ncol = N_dice)

for (i in 1:N){
trow[i,] <- sample(dice, N_dice, replace = TRUE)
}

return(table(trow[i,]))
}

X <- Yazhee2(dice, N_dice,N, 126)

sort(as.data.frame(X), decreasing = T)
```

Loading