Não foi possível enviar o arquivo. Será algum problema com as permissões?
Diferenças
Aqui você vê as diferenças entre duas revisões dessa página.
Ambos lados da revisão anterior Revisão anterior Próxima revisão | Revisão anterior Próxima revisão Ambos lados da revisão seguinte | ||
pessoais:eder [2011/06/05 20:03] eder |
pessoais:eder [2011/06/06 20:39] eder [section 5] |
||
---|---|---|---|
Linha 24: | Linha 24: | ||
<code R> | <code R> | ||
###-----------------------------------------------------------------### | ###-----------------------------------------------------------------### | ||
- | ###buf | + | buffon <- function(n,l=1,a=1){ |
- | buf <- function(n){ | + | if(a<l){cat('Erro: a < l, deve ser a > l\n')} |
- | ttt <- NULL | + | if(a>=l){ |
- | ttt[1] <- 0 | + | theta <- runif(n,0,pi) |
- | x <- runif(n) | + | dist <- runif(n,0,a/2) |
- | th <- runif(n,0,pi) | + | inter <- sum(dist <= l/2*sin(theta)) |
- | st <- sin(th) | + | phi_est <- round((n/inter)*(2*l/a),12) |
- | for ( i in 1:n){ | + | cat('Número Simulação',n,'phi_estimado',phi_est,'Erro',round(pi-phi_est,12),'\n') |
- | if(st[i]>x[i]){ | + | return(c(n,phi_est)) |
- | ttt[i+1] <- ttt[i]+1 | + | }} |
- | } | + | |
- | else { | + | n <- seq(10000,1000000,by=20000) |
- | ttt[i+1] <- ttt[i] | + | res <- matrix(NA,ncol=2,nrow=length(n)) |
- | }} | + | con <- 1 |
- | if (ttt[n+1]>0){ | + | for (i in n){ |
- | plot((0:n)[ttt>0],2*(0:n)[ttt>0]/ttt[ttt>0],type='l',xlab='numero simulação',ylab='pi') | + | res[con,] <- buffon(i) |
- | } | + | con <- con+1 |
- | else{print('no sucesso')} | + | } |
- | abline(pi,0) | + | |
- | } | + | plot(res,type='l',ylab=expression(pi),xlab='Simulações') |
- | + | abline(h=pi,col='red') | |
- | buf(100000) | + | |
###-----------------------------------------------------------------### | ###-----------------------------------------------------------------### | ||
### MOnte carlo | ### MOnte carlo | ||
Linha 78: | Linha 77: | ||
MCcirculo(1,seq(5,5000,by=1000),plotS=FALSE) | MCcirculo(1,seq(5,5000,by=1000),plotS=FALSE) | ||
###-----------------------------------------------------------------### | ###-----------------------------------------------------------------### | ||
- | ### inversão de p | ||
### Inversão de Probabilidade | ### Inversão de Probabilidade | ||
+ | ### OBJ: gerar x~exp transformando de uma uniforme | ||
NS <- 10000 | NS <- 10000 | ||
+ | lam <- 0.5 | ||
+ | #f(x)=exp(lam) F(x)=1-exp(-lam*x), logo: F^-1(x)= -lam^-1*log(1-x) | ||
+ | Gexp <- function(x,lam){-(log(1-U))/lam} | ||
+ | |||
U <- runif(NS) | U <- runif(NS) | ||
- | X <- - log(U) | + | X <- Gexp(U,lam) |
- | Y <- rexp(NS) | + | Y <- rexp(NS,lam) |
par(mfrow=c(1,3)) | par(mfrow=c(1,3)) | ||
hist(U,freq=FALSE,main='Uniforme',col='lightblue') | hist(U,freq=FALSE,main='Uniforme',col='lightblue') | ||
lines(density(U),col='red',lwd=2) | lines(density(U),col='red',lwd=2) | ||
+ | |||
hist(X,freq=FALSE,main='Expoencial via uniforme',col='lightblue') | hist(X,freq=FALSE,main='Expoencial via uniforme',col='lightblue') | ||
lines(density(X),col='red',lwd=2) | lines(density(X),col='red',lwd=2) | ||
- | lines(curve(dexp(x,1),min(X),max(X),add=TRUE),col='blue',lwd=2) | + | lines(curve(dexp(x,lam),min(X),max(X),add=TRUE),col='blue',lwd=2) |
hist(Y,freq=FALSE,main='Expoencial do R',col='lightblue') | hist(Y,freq=FALSE,main='Expoencial do R',col='lightblue') | ||
lines(density(Y),col='red',lwd=2) | lines(density(Y),col='red',lwd=2) | ||
- | lines(curve(dexp(x,1),min(Y),max(Y),add=TRUE),col='blue',lwd=2) | + | lines(curve(dexp(x,lam),min(Y),max(Y),add=TRUE),col='blue',lwd=2) |
###-----------------------------------------------------------------### | ###-----------------------------------------------------------------### | ||
### Regressão Beta | ### Regressão Beta |