More Related Content
More from Nagi Teramo (20)
続わかりやすいパターン認識11章(11.1 - 11.4)
- 31. テーブルへの分割方法とその生起確率
(n=4, α=2)
(a)テーブル数c (b)テーブルの客数 客の分割方法 (d)生起確率
1 (4) (① ② ③ ④) 1/10
2
(2, 2)
(① ②) (③ ④) 1/30
(① ③) (② ④) 1/30
(① ④) (② ③) 1/30
(3, 1)
(① ② ③) (④) 1/15
(① ② ④) (③) 1/15
(① ③ ④) (②) 1/15
(② ③ ④) (①) 1/15
3 (2, 1, 1)
(① ②) (③) (④) 1/15
(① ③) (②) (④) 1/15
(① ④) (②) (③) 1/15
(② ③) (①) (④) 1/15
(② ④) (①) (③) 1/15
(③ ④) (①) (②) 1/15
4 (1, 1, 1, 1) (① ② ③ ④) 2/15 31
- 40. ピットマン・ヨー過程兼CPRのコード
40
#ピットマン・ヨー過程+CRPを生成する関数
pitman_yor <- function(size, alpha, beta)
{
table <- c(1)
x <- matrix(c(1,1), ncol=2)
for(n in seq_len(size))
{
denominator <- sum(table) + alpha
prob <- c(table - beta, alpha + (length(table)-1)*beta)/denominator
i <- sample(1:(length(table)+1), 1, prob=prob)
table[i] <- ifelse(is.na(table[i]), 0, table[i]) + 1
x <- rbind(x, c(n, length(table)))
}
list(x=x, table=sort(table, decreasing=TRUE))
}
- 41. ピットマン・ヨー過程兼CPRのコード
41
#図11.5
x10 <- pitman_yor(10^3, 10 , 0)
x2 <- pitman_yor(10^3, 2 , 0)
ylim <- range(x10$x[,2])
plot(x10$x, ylim=ylim)
par(new=T)
plot(x2$x, ylim=ylim)
#図11.6
x <- pitman_yor(10^3, 2 , 0)
barplot(x$table, names.arg=seq_len(length(x$table)))
- 42. ピットマン・ヨー過程兼CPRのコード
42
#図11.7
x4 <- pitman_yor(10^3, 2, 0.4)
x3 <- pitman_yor(10^3, 2, 0.3)
x2 <- pitman_yor(10^3, 2, 0.2)
x0 <- pitman_yor(10^3, 2, 0.0)
ylim <- range(x4$x[,2])
plot(x4$x, ylim=ylim)
par(new=T)
plot(x3$x, ylim=ylim)
par(new=T)
plot(x2$x, ylim=ylim)
par(new=T)
plot(x0$x, ylim=ylim)