-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path1_5_Statistics_information.R
224 lines (164 loc) · 5.22 KB
/
1_5_Statistics_information.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# Binary data of parameter P(X=1)=0.7
n <- 100
x <- sample(c(0,1), n, replace=T, prob=c(.3,.7))
par(mfrow=c(1,1))
plot(x, type='h',main="Bernoulli variables, prob=(.3,.7)")
barplot(table(x)/n, ylim = c(0,1))
# Binary/ Bernoulli distribution of parameter p=0.7
n <- 1000
x <- sample(c(0,1), n, replace=T, prob=c(.3,.7))
par(mfrow=c(1,1))
plot(x, type='h', main="Bernoulli variables, prob=(.3,.7)")
barplot(table(x)/n, ylim = c(0,1), main = "Bar plot")
# Poisson data
data_pois<-rpois(n = 500,lambda = 2.54)
## TABLE of DISCRETE DATA
t<- table(data_pois)
print(t)
# jpeg("barplot.jpeg")
barplot(t,xlab = "Values", ylab = "Frequency", horiz = F, col = 3, border = 2)
barplot(t/sum(t),xlab = "Values", ylab = "Probability", horiz = F, col=5 ,border = 2)
# dev.off()
## MEAN
data_mean<-mean(data_pois)
cat("Mean of the data=", data_mean,"\n")
## VARIANCE
data_var<-var(data_pois)
cat("Varicance of the data=", data_var,"\n")
## STANDARD DEVIATION
data_sd<-sd(data_pois)
cat("Standred deviation of the data=", data_sd,"\n")
#######################
print(summary(data_pois))
#dev.off()
##############################
#Multinomial(k,p_vector)
##############################
k <- 5 # categories
n <- 10000 # sample size
p <- c(.2,.5,.1,.1,.1) # probbility vector
x1 <- sample(1:k, n, replace=T, prob=p) # data
print(table(x1)/n )
par(mfrow=c(1,1))
plot(table(x1)/n,ylim=c(0,1), col=3, main = " Multinomial(k,p_vector)")
lines(p, type='p', col=2, lwd=2)
##############################
#UNIFORM(a,b)
##############################
a<-0
b<-1
n<- 1000 #Sample size
x1<-runif(n, a, b)
s<-seq(a,b,length=11)
par(mfrow=c(1,1))
hist(x1, col=5,probability = T, breaks =s, main='UNIFORM(0,1)')
lines(dunif(s,a,b)~s, col=2, lwd=2, lty=2)
a<-2.3
b<-5.8
x2<-a+(b-a)*x1
ss<-a+s*(b-a)
par(mfrow=c(3,1))
hist(x1, col=5,probability = T, breaks =s, main='UNIFORM(0,1)')
lines(dunif(s,0,1)~s, col=2, lwd=2, lty=1)
hist(x2, col=7,probability = T, breaks =ss, main='UNIFORM(a,b)')
lines(dunif(ss,a,b)~ss, col=2, lwd=2, lty=2)
plot(dunif(s,0,1)~s, xlim=c(0,b),ylim=c(0,1), col=2, lwd=2, type='l')
lines(dunif(ss,a,b)~ss, col=2, lwd=2, lty=2)
#################################
# NORMAL(mu, sigma)
#################################
mu=0 # location
sigma=1 # scale
n<-1000
x1<-rnorm(n, mu,sigma)
s<-seq(min(x1),max(x1),by=0.05)
par(mfrow=c(1,1))
hist(x1,probability = T,breaks = 100, col=8, main='NORMAL(0,1)')
lines(dnorm(s, mean=0, sd=1)~s, col=2, lwd=3)
par(mfrow=c(1,1))
hist(x1,probability = T,breaks = 100, col=8, main='NORMAL(0,1)')
lines(dnorm(s, mean=0, sd=1)~s, col=2, lwd=3)
lines(density(x1), col=4, lwd=2)
y1<-x1
s1<-s
y2<-2+0.5*x1
s2<-2+0.5*s
y3<- -3+1.5*x1
s3<- -3+1.5*s
par(mfrow=c(3,1))
hist(y1,probability = T,breaks = 100, col=8, xlim=c(-7,4))
lines(dnorm(s1, mean=0, sd=1)~s1, col=2, lwd=3)
hist(y2,probability = T,breaks = 100, col=4, xlim=c(-7,4))
lines(dnorm(s2, mean=2, sd=0.5)~s2, col=2, lwd=3)
hist(y3,probability = T,breaks = 100, col=5, xlim=c(-7,4))
lines(dnorm(s3, mean= -3, sd=1.5)~s3, col=2, lwd=3)
cat("mean(Y1)=", mean(y1), "sd(Y1)=",sd(y1),"\n")
cat("mean(Y2)=", mean(y2), "sd(Y2)=",sd(y2),"\n")
cat("mean(Y3)=", mean(y3), "sd(Y2)=",sd(y3),"\n")
##### ESTIMATION ########
########## MLE ###############
for(i in 1 : 16){
n<-50*2^i
x1<-rnorm(n, mu,sigma)
s<-seq(min(x1),max(x1),by=0.05)
par(mfrow=c(1,1))
hist(x1,probability = T,breaks = 100, col=8, main="PARAMETRIC")
lines(dnorm(s, mean=0, sd=1)~s, col=2, lwd=3)
lines(dnorm(s, mean=mean(x1), sd=sd(x1))~s, col=4, lwd=2)
Sys.sleep(2)
}
######### KDE##############
for(i in 1 : 16){
n<-50*2^i
x1<-rnorm(n, mu,sigma)
s<-seq(min(x1),max(x1),by=0.05)
par(mfrow=c(1,1))
hist(x1,probability = T,breaks = 100, col=8, main='KDE')
lines(dnorm(s, mean=0, sd=1)~s, col=2, lwd=3)
lines(density(x1), col=4, lwd=2)
Sys.sleep(2)
}
########### ENTROPY #########
#install.packages("entropy")
library("entropy")
# sample from continuous uniform distribution
x1 = runif(10000)
hist(x1, xlim=c(0,1), freq=FALSE)
# 'discretize' puts observations from a continuous random variable into bins and returns the corre-sponding vector of counts.
#discretize into 10 categories
y1 = discretize(x1, numBins=10, r=c(0,1))
plot(y1)
print(y1)
# compute entropy from counts
entropy(y1,unit = "log") # empirical estimate near theoretical maximum
log(10) # theoretical value for discrete uniform distribution with 10 bins
# sample from a non-uniform distribution
x2 = rbeta(10000, 3, 4)
hist(x2, xlim=c(0,1), freq=FALSE)
# discretize into 10 categories and estimate entropy
y2 = discretize(x2, numBins=10, r=c(0,1))
plot(y2)
print(y2)
# estimated entropy from data
est_dbeta<-entropy(y2)
s<-seq(0,1, by=0.1)
prob_beta<-pbeta(q=s,shape1 = 3,shape2 = 4)
dprob<-prob_beta[-1]-prob_beta[-11]
# theoritial enropy after discretization
edbeta<-sum(dprob*(-log(dprob)))
cat("Estimated entropy=", est_dbeta ,'\n')
cat("Computed entropy=", edbeta ,'\n')
y2d = discretize2d(x1, x2, numBins1=10, numBins2=10)
# joint entropy
H12 = entropy(y2d )
H12
log(100) # theoretical maximum for 10x10 table
# mutual information
mi.empirical(y2d) # approximately zero
# another way to compute mutual information
# compute marginal entropies
H1 = entropy(rowSums(y2d))
H2 = entropy(colSums(y2d))
H1+H2-H12 # mutual entropy
# KL divergence
KL.empirical(y1,y2)