-
Notifications
You must be signed in to change notification settings - Fork 0
/
ptII_quan_Bayes_MaximumEntropy_helpfuncs.r
72 lines (60 loc) · 1.58 KB
/
ptII_quan_Bayes_MaximumEntropy_helpfuncs.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
### (C) 2005-2023 by Leo Guertler
### R-code supplement
### to the book
###
### "Subjektive Ansichten und objektive Betrachtungen"
###
### written by Gürtler & Huber (2023)
###
### All R-code is published under the GPL v3 license:
###
### https://www.gnu.org/licenses/gpl-3.0.en.html
###
### except for 'borrowed' code - see links and references.
### For this R-code the original license of the respective
### authors is valid.
###
### R-code published on
###
### https://osdn.net/projects/mixedmethod-rcode
### https://github.com/abcnorio/mixedmethod-rcode
# file:
# ptII_quan_Bayes_MaximumEntropy_helpfuncs.r
# location:
# chap. 6 [6.14]
# Maximum Entropy
# HELPER FUNCTIONS
###### function to calculate entropy H(X) of a perfect fair coin
H1 <- function(e=NA, base=2)
{
#p = prob of event e - here binary, ie. e=2
p <- 1/e
H <- -sum(replicate(e,p*log(p, base=base)))
return(H)
}
# call:
# H1(e=2,base=2)
# H1(e=3,base=2)
########################## END OF FUNCTION
###### function to calculate entropy H(X) of a not-perfect fair coin
H2 <- function(p) -( p*log2(p) + (1-p)*log2(1-p) )
# call:
# p <- 0.5
# q <- 1-p
# H2(p=p)
########################## END OF FUNCTION
###### function to simulate a dice and the entropy
dice <- function(k, N, seed=83345)
{
if(!is.null(seed)) set.seed(seed)
x <- round((k-1)*runif(N))+1
v <- rep(NA,k)
nk <- sapply(seq_along(1:k), function(i) sum(x == i))
probs <- nk/N
names(probs) <- names(nk) <- 1:k
H <- -sum(probs*log(probs))
return(list(faces=x, nk=nk, probs=probs, H=H))
}
# call:
# d1 <- dice(k=6, N=100)
########################## END OF FUNCTION