Last active
October 1, 2021 00:07
-
-
Save deomorxsy/6b55d6a8cf4f88c54c4a72e8fbb77822 to your computer and use it in GitHub Desktop.
monte carlo methods
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Vectorized code for monte carlo dart simulation | |
throws = 100 | |
landed = 0 #landed in circle | |
#throws=100 | |
x = runif(n=throws, min=-1, max=1) | |
y = runif(n=throws, min=-1, max=1) | |
soma_quadrados <- (x^2) + (y^2) | |
dardos_indexados <- which(soma_quadrados <= 1) | |
no_alvo <- length(dardos_indexados) | |
print(4 * (no_alvo/throws)) | |
plot(x, y) | |
for (i in seq(1, throws)) { | |
plot(x[1:i], y[1:i], xlim=c(-1,1), ylim=c(-1,1)) | |
points(x[i], y[i], col="red") | |
Sys.sleep(0.5) | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Looped code for monte carlo dart simulation | |
throws = 100 #1000000 | |
landed = 0 #landed in circle | |
for (i in seq(1,throws)){ | |
x = runif(1, min=-1, max=1) | |
y = runif(1, min=-1, max=1) | |
if ((x^2)+(y^2) <= 1) { | |
landed = landed + 1 | |
} | |
} | |
print(4 * (landed/throws)) | |
for (i in seq(1, throws)) { | |
plot(x[1:i], y[1:i], xlim=c(-1,1), ylim=c(-1,1)) | |
points(x[i], y[i], col="red") | |
Sys.sleep(0.5) | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/R --vanilla | |
setwd("~/Documentos/coding/projetos/rscripts/bib-packrat") | |
packrat::on("~/Documentos/coding/projetos/rscripts/bib-packrat") | |
#set.seed(165) | |
#runif(10) | |
n <- 10000 | |
f <- function(x) x^2 | |
plot(runif(n), runif(n), col='blue', pch=20) | |
curve(f, 0, 1, n=100, col='white', add=TRUE) | |
ps <- matrix(runif(2*n), ncol=2) | |
g <- function(x,y) y <= x^2 | |
z <- g(ps[,1], ps[,2]) | |
#plot(ps[z, 1], ps[z, 2], col='blue', pch=20) | |
plot(ps[!z, 1], ps[!z, 2], col='blue', pch=20) | |
points(ps[z, 1], ps[z, 2], col="green", pch=20) | |
#points(ps[!z, 1], ps[!z, 2], col="green", pch=20) | |
curve(f, 0,1, n=100, col='red', add=TRUE) | |
## Aproximação de erro | |
# Gera uma sequência entre 1 e 7, pulando casas decimais de 2 em 2 | |
ks <- seq(1, 7, by=.2) | |
g <- function(k) { | |
n <- 10^k #eleva 10 a K, parametro da funcao | |
f <- function(x, y) y <= x^2 | |
z <- f(runif(n), runif(n)) | |
length(z[z]) / n | |
} | |
a <- sapply(ks, g) | |
# Dardos | |
n <- 1000000 | |
for i in seq(n) { | |
x = runif() | |
}i | |
#Baseado em geometria: A probabilidade de jogar dentro do círculo é proporcional à área do círculo. Se dividirmos a área do círculo pela área do quadrado, temos nossa resposta. r=1, l=2, (pi*r^2)/(l^2) | pi/4 | |
#n= numero de pontos, dardos jogados | |
n = 1000000 #quanto mais amostras, mais próximo da realidade | |
circ = 0 | |
#circ= acumulador, chamado de "running total" ou soma parcial. Vai acumular todas as vezes que o dardo estiver de fato dentro do círculo, somando um toda vez que isso for verdade. Dessa forma, no final, temos apenas que fazer a razão entre o número de dardos dentro da circunferência, ou seja, CIRC, sobre o número total de dados jogados, N. C/N = Fração de dados que eu joguei aleatoriamente dentro do alvo, essa é a nossa "área em baixo da curva". | |
for i in (i...n) { | |
x = rand(-1, 1) | |
y = rand(-1, 1) | |
if ((x^2 + y^2) <= 1) { | |
circ += 1 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment