rm(list=ls())
##
## Integração de Monte Carlo - Uma ilustração como introdução
##
##
## Exemplo I
##
##
## h_1(x) = 3x + 2
##
## a) \int_0^1 h_1(x) dx = 3.5
## b) \int_0^2 h_1(x) dx = 10
##
##rm(list=ls())
##
h1 <- function(x) 3*x + 2
I.0a <- 3.5
I.0b <- 10

## I.1
## a)
curve(h1, from = 0, to = 1, ylim = c(0, 6))

xseq <- seq(0, 1, length=10)
lines(xseq, h1(xseq), type = "h", lty = 2)
(m.h1 <- mean(h1(xseq)))
abline(h = m.h1 , col = 2)
(1-0) * m.h1
##
## b)
curve(h1, from = 0, to = 2, ylim = c(0, 10))

xseq <- seq(0, 2, length=10)
lines(xseq, h1(xseq), type = "h", lty = 2)
(m.h1 <- mean(h1(xseq)))
abline(h = m.h1 , col = 2)
(2-0) * m.h1
##

## I.2
## a)
integrate(h1, lower = 0, upper = 1)
## b)
integrate(h1, lower = 0, upper = 2)


## I.3
## a)
curve(h1, from = 0, to = 1, ylim = c(0, 6))

u <- runif(10, 0, 1)
lines(u, h1(u), type = "h", lty = 2, col = 4)
(m.h1 <- mean(h1(u)))
abline(h = m.h1 , col = 4)
abline(h = I.0a , col = 2)
(1-0) * m.h1
##
## b)
curve(h1, from = 0, to = 2, ylim = c(0, 10))

u <- runif(10, 0, 2)
lines(u, h1(u), type = "h", lty = 2, col = 4)
(m.h1 <- mean(h1(u)))
abline(h = m.h1 , col = 4)
abline(h = I.0b/2, col = 2)
(2-0) * m.h1

rm(list=ls())

###################################################
##
## Exemplo II
##
##
## h_2(x) = x^2 + 0.5 * x
##
## a) \int_0^1 h_2(x) dx = 0,5833333 (7/12)
## b) \int_0^2 h_2(x) dx = 3,66666666 (11/3)
##
h2 <- function(x) x^2 + 0.5 * x
curve(h2, from = 0, to = 1, ylim = c(0, 1.7))
curve(h2, from = 0, to = 2, ylim = c(0, 6))

## a)
II.0a <- 7/12
## b)
II.0b <- 11/3

## II.1
curve(h2, from = 0, to = 1, ylim = c(0, 1.7))
## a)
xseq <- seq(0, 1, length=10)
lines(xseq, h2(xseq), type = "h", lty = 2)
lines(xseq, h2(xseq), type = "l", lty = 2, col = 2)
(m.h2 <- mean(h2(xseq)))
abline(h = m.h2 , col = 2)
abline(h = II.0a)
(1-0) * m.h2
##
curve(h2, from = 0, to = 2, ylim = c(0, 6))
## b)
xseq <- seq(0, 2, length=10)
lines(xseq, h2(xseq), type = "h", lty = 2)
(m.h2 <- mean(h2(xseq)))
abline(h = m.h2 , col = 2)
abline(h = II.0b/2)
(2-0) * m.h2
##

## II.2
## a)
integrate(h2, lower = 0, upper = 1)
## b)
integrate(h2, lower = 0, upper = 2)


## II.3
curve(h2, from = 0, to = 1, ylim = c(0, 1.7))

## a)
u <- runif(10, 0, 1)
lines(u, h2(u), type = "h", lty = 2, col = 4)
(m.h2 <- mean(h2(u)))
abline(h = m.h2 , col = 4)
abline(h = II.0a , col = 2)
(1-0) * m.h2
##
curve(h2, from = 0, to = 2, ylim = c(0, 6))

## b)
u <- runif(10, 0, 2)
lines(u, h2(u), type = "h", lty = 2, col = 4)
(m.h2 <- mean(h2(u)))
abline(h = m.h2 , col = 4)
abline(h = II.0b/2, col = 2)
(2-0) * m.h2

rm(list=ls())

###################################################
##
## Exemplo III
##
## h_3(x) = exp(-2*x)
##
## \int_0^1 h_3(x) dx = 0,43233
## \int_0^2 h_3(x) dx = 0,49084
##
h3 <- function(x) exp(-2*x)
curve(h3, from = 0, to = 1, ylim = c(0, 1.1))
curve(h3, from = 0, to = 2, ylim = c(0, 1.1))
## a)
III.0a <- (-1/2)*exp(-2) - (-1/2) * exp(0)
## b)
III.0b <- (-1/2)*exp(-2*2) - (-1/2) * exp(0)

## III.1
curve(h3, from = 0, to = 1, ylim = c(0, 1.1))

## a)
xseq <- seq(0, 1, length=10)
lines(xseq, h3(xseq), type = "h", lty = 2)
lines(xseq, h3(xseq), type = "l", lty = 2, col = 2)
(m.h3 <- mean(h3(xseq)))
abline(h = m.h3 , col = 4)
abline(h = III.0a, col = 2)
(1-0) * m.h3
##
curve(h3, from = 0, to = 2, ylim = c(0, 1.1))

## b)
xseq <- seq(0, 2, length=10)
lines(xseq, h3(xseq), type = "h", lty = 2)
lines(xseq, h3(xseq), type = "l", lty = 2, col = 2)
(m.h3 <- mean(h3(xseq)))
abline(h = m.h3 , col = 4)
abline(h = III.0b/2, col = 2)
(2-0) * m.h3
##

## III.2
## a)
integrate(h3, lower = 0, upper = 1)
## b)
integrate(h3, lower = 0, upper = 2)

## III.3
curve(h3, from = 0, to = 1, ylim = c(0, 1.1))

## a)
u <- runif(10, 0, 1)
lines(u, h3(u), type = "h", lty = 2, col = 4)
(m.h3 <- mean(h3(u)))
abline(h = m.h3 , col = 4)
abline(h = III.0a , col = 2)
(1-0) * m.h3
##
curve(h3, from = 0, to = 2, ylim = c(0, 1.1))

## b)
u <- runif(10, 0, 2)
lines(u, h3(u), type = "h", lty = 2, col = 4)
(m.h3 <- mean(h3(u)))
abline(h = m.h3 , col = 4)
abline(h = III.0b/2, col = 2)
(2-0) * m.h3

rm(list=ls())

###################################################

##
## Amostragem por (função de) importância
##

## 1. 
## \int_{0}^{\infty} exp\{ - 2x \}

III.0inf <- 0.5 # -(1/2)*exp(-2*Inf) - (-1/2) * exp(0)

integrate(h3, lower = 0, upper = Inf)

curve(h3, from = 0, to = 2, ylim = c(0, 1.1))
curve(dexp(x, 1), from = 0, to = 2, add = TRUE, col = 6)

N <- 10000

x <- rexp(N, 1)
h.f <- h3(x)/dexp(x, 1)
(m.h.f <- mean(h.f))
abline(h = m.h.f , col = 4)
III.02 <- (-1/2)*exp(-2*2) - (-1/2) * exp(0)
abline(h = III.0inf, col = 2)

## Comparação MC simples e amostragem por importância
## verificando como se aproximam do valor verdadeiro
h.f.mean <- cumsum(h.f)/(1:N)
plot(h.f.mean, type = "l", col = 6)
abline(h = III.02, col = 2)

h3.mean <- 2*cumsum(h3(runif(N, 0, 2)))/(1:N)
lines(h3.mean, type = "l", col = 4)

## fazendo gráfico a partir de N=51
plot(h3.mean[-(1:50)], type = "l", col = 4, ylim = c(0.45, 0.55))
lines(h.f.mean[-(1:50)], type = "l", col = 6)
III.02 <- (-1/2)*exp(-2*2) - (-1/2) * exp(0)
abline(h = III.02, col = 2)


## 2. Integrando em um intervalo
## \int_{0}^{2} exp\{ - 2x \}
integrate(h3, lower = 0, upper = 2)


## Simulando da exponencial truncanda F(x)/F(2) 
curve(h3, from = 0, to = 2, ylim = c(0, 1.1))
curve(dexp(x, 1), from = 0, to = 2, add = TRUE, col = 6)

## Gerando N valores de 0 a 2 (truncados)
N <- 100
x <- rexp(N, 1)
cond <- sum(x > 2)
while(cond > 0){
 x[x > 2] <- rexp(cond, 1)
 cond <- sum(x > 2)
}
sum(x <= 2)

lines(x, h3(x), type = "h")

## função de importância é agora a exponencial truncada
h.f <- h3(x)/((dexp(x, 1)/(1-exp(-1*2))))
(m.h.f <- mean(h.f))

abline(h = m.h.f , col = 4)
abline(h = III.0inf, col = 2)

#rm(list=ls())

