Last active
July 3, 2017 02:03
-
-
Save Shikugawa/98dd31ee323046126c0eab84e3b27fb2 to your computer and use it in GitHub Desktop.
正規分布シミュレータのロジック部分
This file contains 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
# | |
# This is the server logic of a Shiny web application. You can run the | |
# application by clicking 'Run App' above. | |
# | |
# Find out more about building applications with Shiny here: | |
# | |
# http://shiny.rstudio.com/ | |
# | |
library(shiny) | |
library(ggplot2) | |
# Define server logic required to draw a histogram | |
shinyServer(function(input, output) { | |
# Plot histgram of random numbers follow normal distribution generated by box-muller method | |
output$distPlot <- renderPlot({ | |
sample <- input$normalsamplenum | |
Z <- sqrt(input$dispersion)*sqrt(-2*log(runif(sample)))*cos(2*pi*runif(sample))+input$mean | |
hist(Z, breaks=25, col = "orange", border="white", | |
xlim = c(-100,100), ylim = c(0.0, 0.03), freq = FALSE, xlab = "sample value", ylab = "density", main = "Histgram of random number generated by box-muller method") | |
par(new=T) | |
plot(density(Z), xlim = c(-100,100), ylim = c(0.0, 0.03), | |
main="" , col="red", xlab = "sample value", ylab = "density") | |
par(new=T) | |
plot(-100:100, dnorm(-100:100, mean = input$mean, sd = sqrt(input$dispersion)), | |
xlim = c(-100,100), xlab = "sample value", ylab = "density", ylim = c(0.0, 0.03), type="l", col="green") | |
}) | |
output$timePlot <- renderPlot({ | |
sample <- input$normalsamplenum | |
Z <- sqrt(input$dispersion)*sqrt(-2*log(runif(sample)))*cos(2*pi*runif(sample))+input$mean | |
cdf <- ecdf(Z) | |
plot(cdf, xlim=c(-100, 100), ylim=c(0,1), col="blue", xlab="index", ylab="sum of density") | |
par(new=T) | |
plot(-100:100, pnorm(-100:100, mean = input$mean, sd=sqrt(input$dispersion)) | |
,xlim=c(-100, 100), ylim=c(0,1),col="red", type = "l", xlab="index", ylab="sum of density") | |
}) | |
# Plot histgram of random numbers follow exponential distribution generated by inverse function method | |
output$expdistPlot <- renderPlot({ | |
z <- runif(input$expsamplenum) | |
result <- -log(1-z)/input$rate | |
hist(result, breaks=100, col="blue", border = "white", | |
xlim = c(0, 3), ylim = c(0, 2000), | |
main="Histgram of random number generated by exponential distribution") | |
}) | |
# plot histgram of random numbers follow poisson distribution, using exponential distribution | |
# poisson distribution is discrete distribution. so we can't use inverse function method. | |
output$poissondistPlot <- renderPlot({ | |
result <- rep(0, times=input$poissonsamplenum) | |
for(i in 1:input$poissonsamplenum){ | |
z <- rexp(100, rate = 2.5) | |
s <- 0 | |
count <- 1 | |
while(s < 1){ | |
s <- s + z[count] | |
count <- count + 1 | |
} | |
result[i] <- count - 1 | |
} | |
hist(result, col="green", border = "white", xlim = c(0, 15), | |
main="Histgram of random number generated by poisson distribution") | |
}) | |
# plot histgram of random numbers follow pareto distribution generated by inverse function method | |
output$paretodistPlot <- renderPlot({ | |
z <- runif(input$paretosamplenum) | |
x <- input$x_0 | |
a <- input$a | |
result <- x/(1-z)^(1/a) | |
hist(result[result < 10], breaks=seq(1, 10, 0.1), col="red", border = "white", ylim = c(0, 10000), | |
main="Histgram of random number generated by pareto distribution") | |
}) | |
# check law of large numbers | |
output$largePlot <- renderPlot({ | |
n <- 1:input$largeSampleNumbers | |
count <- 1 | |
exp.value <- rep(0, times=length(n)) | |
for(i in n){ | |
z <- runif(i) | |
exp.value[count] <- mean(result <- 1/(1-z)^(1/2)) | |
count <- count + 1 | |
} | |
plot(exp.value, type = "l", ylim = c(0, 4), | |
col="orange", xlab = "sample number", ylab = "expected value") | |
}) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment