- 周辺度数は「特定の軸」についての足し算である
- Rでは、arrayという形式のオブジェクトにapply()関数を使って、特定の軸の組に足し算(sum())を作用させることで得られる
k<-3
r<-sample(2:3,k,replace=TRUE)
print(r)
R<-prod(r)
print(R)
A<-array(1:R,r)
print(A)
apply(A,c(1),sum)
apply(A,c(2),sum)
apply(A,c(3),sum)
apply(A,c(1,2),sum)
apply(A,c(2,3),sum)
apply(A,c(3,1),sum)
apply(A,c(1,2,3),sum)
> k<-3
> r<-sample(2:3,k,replace=TRUE)
> print(r)
[1] 2 3 2
> R<-prod(r)
> print(R)
[1] 12
> A<-array(1:R,r)
> print(A)
, , 1
[,1] [,2] [,3]
[1,] 1 3 5
[2,] 2 4 6
, , 2
[,1] [,2] [,3]
[1,] 7 9 11
[2,] 8 10 12
>
> apply(A,c(1),sum)
[1] 36 42
> apply(A,c(2),sum)
[1] 18 26 34
> apply(A,c(3),sum)
[1] 21 57
> apply(A,c(1,2),sum)
[,1] [,2] [,3]
[1,] 8 12 16
[2,] 10 14 18
> apply(A,c(2,3),sum)
[,1] [,2]
[1,] 3 15
[2,] 7 19
[3,] 11 23
> apply(A,c(3,1),sum)
[,1] [,2]
[1,] 9 12
[2,] 27 30
> apply(A,c(1,2,3),sum)
, , 1
[,1] [,2] [,3]
[1,] 1 3 5
[2,] 2 4 6
, , 2
[,1] [,2] [,3]
[1,] 7 9 11
[2,] 8 10 12
- 軸数kについて、周辺度数の取り方はある。すべてのセルの和も周辺度数の一つとみなせば、取り方はある
- これは、集合のべき集合の要素数であって、べき集合の要素が、周辺度数の取り方を規定しているとも言い換えられる
- そのことを使って、任意の表のすべての周辺度数を計算する処理を以下のように作る
library(sets)
s<-as.set(1:k)
print(s)
powset<-set_power(s)
print(powset)
marginals<-list()
cnt<-1
for(i in powset){
elems<-as.integer(i)
print(elems)
if(length(elems)==0){
marginals[[cnt]]<-list(elems=c(),marginals=sum(A))
}else{
marginals[[cnt]]<-list(elems=elems,marginals=apply(A,elems,sum))
}
cnt<-cnt+1
}
marginals
> library(sets)
>
> s<-as.set(1:k)
>
> print(s)
{1L, 2L, 3L}
>
> powset<-set_power(s)
>
> print(powset)
{{}, {1L}, {2L}, {3L}, {1L, 2L}, {1L, 3L}, {2L, 3L}, {1L, 2L, 3L}}
>
>
> marginals<-list()
>
>
> cnt<-1
> for(i in powset){
+
+ elems<-as.integer(i)
+
+ print(elems)
+
+ if(length(elems)==0){
+
+ marginals[[cnt]]<-list(elems=c(),marginals=sum(A))
+ }else{
+
+ marginals[[cnt]]<-list(elems=elems,marginals=apply(A,elems,sum))
+ }
+
+ cnt<-cnt+1
+ }
integer(0)
[1] 1
[1] 2
[1] 3
[1] 1 2
[1] 1 3
[1] 2 3
[1] 1 2 3
>
> marginals
[[1]]
[[1]]$elems
NULL
[[1]]$marginals
[1] 78
[[2]]
[[2]]$elems
[1] 1
[[2]]$marginals
[1] 36 42
[[3]]
[[3]]$elems
[1] 2
[[3]]$marginals
[1] 18 26 34
[[4]]
[[4]]$elems
[1] 3
[[4]]$marginals
[1] 21 57
[[5]]
[[5]]$elems
[1] 1 2
[[5]]$marginals
[,1] [,2] [,3]
[1,] 8 12 16
[2,] 10 14 18
[[6]]
[[6]]$elems
[1] 1 3
[[6]]$marginals
[,1] [,2]
[1,] 9 27
[2,] 12 30
[[7]]
[[7]]$elems
[1] 2 3
[[7]]$marginals
[,1] [,2]
[1,] 3 15
[2,] 7 19
[3,] 11 23
[[8]]
[[8]]$elems
[1] 1 2 3
[[8]]$marginals
, , 1
[,1] [,2] [,3]
[1,] 1 3 5
[2,] 2 4 6
, , 2
[,1] [,2] [,3]
[1,] 7 9 11
[2,] 8 10 12
CalcAllMarginals<-function(Table,k){
s<-as.set(1:k)
powset<-set_power(s)
marginals<-list()
cnt<-1
for(i in powset){
elems<-as.integer(i)
if(length(elems)==0){
marginals[[cnt]]<-list(elems=c(),marginals=sum(Table))
}else{
marginals[[cnt]]<-list(elems=elems,marginals=apply(Table,elems,sum))
}
cnt<-cnt+1
}
marginals
}
CalcAllMarginals(A,k)