#sigma <- sqrt(n*p*(1-p))
#mu <- n*p
sigma.squared <- 4
sigma <- sqrt(sigma.squared)
mu <- 7
from <- 5
to <- 13
const <- 1/sqrt(2*pi*sigma^2)
integrand <- function(x) {const * exp(-(((x-mu)^2)/(2*sigma^2)))}
solution <- integrate(integrand, from, to)
solution
## 0.8399948 with absolute error < 2.7e-12
Plot
library(plotly)
#Draw plot
#Transform plot data into data frame
range <- 2*sigma
df <- data.frame(seq(from-2*range, to+range, .1),integrand(seq(from-2*range, to+range, .1)))
colnames(df)<- c('x','y')
#Area Under the Curve
#First subst the data and add the coordinates to make it shade to y = 0
auc <- subset(df, x >from & x < to)
p1 <- ggplot(data=df, aes(x=x, y=y)) +
geom_line() +
geom_area(data=auc, fill="#099dd9", alpha=.5) +
scale_x_continuous(breaks=seq(from-3,to+2,2))+
xlab("Temperatur in Grad Celsius") +
ylab("Wahrscheinlichkeitsdichte") +
ggtitle("Wahrscheinlichkeitsdichtefunktion:\n Normalverteilung")
#Uncomment to see the ggplot2 plot
#p1
# Creating a fancy plotly plot
plot_ly(p1)
mu <- 7
sigma.squared <- 4
sigma <- sqrt(sigma.squared)
a <- 5
b <- 13
to.lower <- (b-mu)/sigma
to.upper <- (a-mu)/sigma
from <- -Inf
c <- 1/sqrt(2*pi)
integrand <- function(t) {c * exp(-.5*(t^2))}
integrate(integrand, from, to.lower)$value - integrate(integrand,from, to.upper)$value
## [1] 0.8399948
#Draw plot
#Transform plot data into data frame
range <- sigma
df <- data.frame(seq(to.upper-2*range, to.lower+range, .1),integrand(seq(to.upper-2*range, to.lower+range, .1)))
colnames(df)<- c('x','y')
#Area Under the Curve
#First subst the data and add the coordinates to make it shade to y = 0
auc <- subset(df, x >to.upper & x < to.lower)
p2 <- ggplot(data=df, aes(x=x, y=y)) +
geom_line() +
geom_area(data=auc, fill="#099dd9", alpha=.5) +
scale_x_continuous(breaks=seq(to.upper-2,to.lower,1))+
xlab("z-Wert") +
ylab("Phi(x)") +
ggtitle("Wahrscheinlichkeitsdichtefunktion:\n Standardnormalverteilung")
#Uncomment to see the ggplot2 plot
#p2
# Creating a fancy plotly plot
plot_ly(p2)
n <-100
p <- 1/6
k<- 12:22
binom <- 0
for(i in k)
binom = binom + choose(n,i) * p^i * (1-p)^(n-i)
binom
## [1] 0.8592305
stirling <- function (n){
sqrt(2*pi*n) * (n/exp(1))^n
}
n <- 100
p <- 1/6
lambda <- n*p
k<- 12:22
poisson <- 0
for(i in k)
poisson <- poisson + exp(-lambda) * (lambda^i)/stirling(i)
poisson
## [1] 0.8254448
n = 100
p = 1/6
sigma <- sqrt(n*p*(1-p))
mu <- n*p
from <- 12
to <- 22
range <- sigma
const <- 1/sqrt(2*pi*sigma^2)
integrand <- function(x) {const * exp(-(((x-mu)^2)/(2*sigma^2)))}
solution <- integrate(integrand, from, to)
solution
## 0.818548 with absolute error < 9.1e-15
#Transform plot data into data frame
df <- data.frame(seq(from-2*range, to+1.5*range, .1),integrand(seq(from-2*range, to+1.5*range, .1)))
colnames(df)<- c('x','y')
#Area Under the Curve
#First subst the data and add the coordinates to make it shade to y = 0
auc <- subset(df, x >from & x < to)
p3 <- ggplot(data=df, aes(x=x, y=y)) +
geom_line() +
geom_area(data=auc, fill="#099dd9", alpha=.5) +
scale_x_continuous(breaks=seq(from-3, to+1,2))+
xlab("Anzahl Treffer (Augenzahl 6)") +
ylab("Wahrscheinlichkeitsdichte") +
ggtitle("Wahrscheinlichkeitsdichtefunktion - Normalverteilung")
#Uncomment to see the ggplot2 plot
#p3
# Creating a fancy plotly plot
plot_ly(p3)
plotly_POST(p3, filename = "r-docs/normal_distribution")
## No encoding supplied: defaulting to UTF-8.
## Success! Modified your plotly here -> https://plot.ly/~slackoverflow/19
plotly_POST(p2, filename = "r-docs/standard_normal_distribution")
## No encoding supplied: defaulting to UTF-8.
## Success! Modified your plotly here -> https://plot.ly/~slackoverflow/17