-
Notifications
You must be signed in to change notification settings - Fork 1
/
normalCDF.R
128 lines (99 loc) · 2.92 KB
/
normalCDF.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
library(stats)
require(graphics)
darkred <- "#990000ff"
mygreen <- "#00aa00ff"
alphafill <- rgb(0, 0, 1,0.2)
par(cex=1.25)
xseq <- seq(-4,4,0.005)
normcdf <- pnorm(xseq, mean=0, sd=1)
# If you want to lay a standard normal PDF on top of a plot:
plotGaussianPDF <- function() {
mean=0
sd <- 1
xvalues <- xseq*sd + mean
hx <- dnorm(xvalues,mean,sd)
i <- xvalues >= -4 & xvalues <= 4
lines(x,hx,col="#990000ff", lwd=2)
return(1)
}
plotGaussianCDF <- function(maintitle) {
plot(xseq,normcdf,
main=maintitle,
xlab="Standard Deviations",
ylab="Cumulative Probability",
xlim=c(-4,4),
ylim=c(0,1),
pch=".",
axes=F
)
polygon(c(-4,xseq,4), c(0,normcdf,0), col="gray80")
axis(side=1,at=seq(-4,4,1), pos=0)
axis(side=2,at=seq(0,1,0.2), pos=-4)
return(1)
}
####################################
# Save the plots to PNG files:
####################################
png(file="NormalCDFplain.png",
width=6.5, height=5, units="in",
res=300
)
plotGaussianCDF(maintitle="Cumulative Distribution Function (Normal)")
abline(v=0, lty=2)
abline(h=0.5,lty=2, lwd=2, col="blue")
text (x=-1.5,y=0.88, "Total Probability", col="navy")
arrows(-1,0.85 ,
x1=0.5, y1= 0.7,
code=2, length=0.11,
angle=15, lwd=2, col="navy"
)
dev.off()
png(file="NormalCDF.png",
width=6.5, height=5, units="in",
res=300
)
plotGaussianCDF(maintitle="Probability Some Event\nHappens at or Below a Given Z")
abline(v=0, lty=2)
abline(h=1, lty=2)
abline(h=0.5,lty=2, lwd=2, col="blue")
y2<-pnorm(1, mean=0, sd=1)
lines(x=c(-4,1), y=c(y2,y2), lwd=1, col=mygreen)
lines(x=c(1,1), y=c(0,y2), lwd=1, col=mygreen)
arrows(-1.8,0.7,
x1=-0.5, y1=y2,
code=2,
length=0.1, angle=15, lwd=2,
col="navy"
)
text(-2.5,0.75, "Cumulative\nprobability\nat Z=1")
dev.off()
# Now for a bit more explanation:
png(file="NormalCDF1SD.png", width=6.5, height=5, units="in", res=300)
plotGaussianCDF(maintitle="Probability Some Event\nHappens Between Two Z-scores")
y1<-pnorm(-1, mean=0, sd=1)
y2<-pnorm(1, mean=0, sd=1)
polygon(c(-1,-1, 1, 1), c(y1,y2,y2,y1), col=alphafill)
abline(h=y1, col=darkred)
abline(h=y2, col=darkred)
abline(v=-1, col=mygreen, lwd=2)
abline(v=1, col=mygreen, lwd=2)
abline(v=0,lty=2, lwd=2, col="gray40")
dev.off()
png(file="NormalCDF1SD68.png", width=6.5, height=5, units="in", res=300)
plotGaussianCDF(maintitle="Probability Some Event\nHappens Happens Between Two Z-scores")
y1<-pnorm(-1,mean=0, sd=1)
y2<-pnorm(1, mean=0, sd=1)
polygon(c(-1,-1, 1, 1), c(y1,y2,y2,y1), col=alphafill)
abline(h=y1, col=darkred)
abline(h=y2, col=darkred)
abline(v=-1, col=mygreen, lwd=2)
abline(v=1, col=mygreen, lwd=2)
abline(v=0,lty=2, lwd=2, col="gray40")
#lines(x=c(-2,-2), y=c(y1,y2), lwd=3)
arrows(x0=-2, y0=y1,
x1=-2, y1=y2,
code=3, length=0.11, angle=15, lwd=2
)
text(-2.65,0.7, "This\ndistance\nis 0.68!")
dev.off()
# EOF