Title: | Approximation of the Shapley Values Based on Experimental Designs |
---|---|
Description: | Estimating the Shapley values using the algorithm in the paper Liuqing Yang, Yongdao Zhou, Haoda Fu, Min-Qian Liu and Wei Zheng (2024) <doi:10.1080/01621459.2023.2257364> "Fast Approximation of the Shapley Values Based on Order-of-Addition Experimental Designs". You provide the data and define the value function, it retures the estimated Shapley values based on sampling methods or experimental designs. |
Authors: | Liuqing Yang [aut, cre, cph] |
Maintainer: | Liuqing Yang <[email protected]> |
License: | MIT + file LICENSE |
Version: | 1.0.0 |
Built: | 2025-02-26 02:58:44 UTC |
Source: | https://github.com/cran/ShapDoE |
The main algorithm for estimating the Shapley value
est.sh(method, d, n, val, ..., p = NA, f_d = NA)
est.sh(method, d, n, val, ..., p = NA, f_d = NA)
method |
the method used for estimating,'SRS' meas simple random sampling, 'StrRS' means structured simple random sampling, 'LS' means Latin square and 'COA' means component orthogonal array. |
d |
an integer, the number of players. |
n |
an integer, the sample size. |
val |
the predefined value function. |
... |
other parameters used in val(sets,...). |
p |
a prime, the bottom number of d. |
f_d |
a vector represents the coefficients of primative polynomial on GF(d). For example the primative polynomial on GF(3^2) is x^2+x+2, then let f_d=c(1,1,2). |
a vector including estimated Shapley values of all players.
temp_adjacent<-matrix(0,nrow=8,ncol=8) temp_adjacent[1,6:8]<-1;temp_adjacent[2,7]<-1;temp_adjacent[c(4,6,7),8]<-1; temp_adjacent<-temp_adjacent+t(temp_adjacent) temp_val<-function(sets,adjacent){ if(length(sets)==1) val<-0 else{ subadjacent<-adjacent[sets,sets] nsets<-length(sets) A<-diag(1,nsets); B<-matrix(0,nsets,nsets) for(l in 1:(nsets-1)){ A<-A%*%subadjacent B<-B+A } val<-ifelse(sum(B==0)>nsets,0,1) } return(val) } est.sh('SRS',8,112,temp_val,temp_adjacent) est.sh('StrRS',8,112,temp_val,temp_adjacent) est.sh('LS',8,112,temp_val,temp_adjacent) est.sh('COA',8,112,temp_val,temp_adjacent,p=2,f_d=c(1,0,1,1))
temp_adjacent<-matrix(0,nrow=8,ncol=8) temp_adjacent[1,6:8]<-1;temp_adjacent[2,7]<-1;temp_adjacent[c(4,6,7),8]<-1; temp_adjacent<-temp_adjacent+t(temp_adjacent) temp_val<-function(sets,adjacent){ if(length(sets)==1) val<-0 else{ subadjacent<-adjacent[sets,sets] nsets<-length(sets) A<-diag(1,nsets); B<-matrix(0,nsets,nsets) for(l in 1:(nsets-1)){ A<-A%*%subadjacent B<-B+A } val<-ifelse(sum(B==0)>nsets,0,1) } return(val) } est.sh('SRS',8,112,temp_val,temp_adjacent) est.sh('StrRS',8,112,temp_val,temp_adjacent) est.sh('LS',8,112,temp_val,temp_adjacent) est.sh('COA',8,112,temp_val,temp_adjacent,p=2,f_d=c(1,0,1,1))
Estimating the Shapley value based on component orthogonal array (COA) with a prime power d
est.shcoa(d, n, val, p, f_d, ...)
est.shcoa(d, n, val, p, f_d, ...)
d |
a power of prime p, the number of players. |
n |
an integer, the sample size. |
val |
the predefined value function. |
p |
a prime, the bottom number of d. |
f_d |
a vector represents the coefficients of primative polynomial on GF(d). For example the primative polynomial on GF(3^2) is x^2+x+2, then let f_d=c(1,1,2). |
... |
other parameters used in val(sets,...). |
a vector including estimated Shapley values of all players based on COA.
temp_adjacent<-matrix(0,nrow=8,ncol=8) temp_adjacent[1,6:8]<-1;temp_adjacent[2,7]<-1;temp_adjacent[c(4,6,7),8]<-1; temp_adjacent<-temp_adjacent+t(temp_adjacent) temp_val<-function(sets,adjacent){ if(length(sets)==1) val<-0 else{ subadjacent<-adjacent[sets,sets] nsets<-length(sets) A<-diag(1,nsets); B<-matrix(0,nsets,nsets) for(l in 1:(nsets-1)){ A<-A%*%subadjacent B<-B+A } val<-ifelse(sum(B==0)>nsets,0,1) } return(val) } est.shcoa(8,112,temp_val,2,c(1,0,1,1),temp_adjacent)
temp_adjacent<-matrix(0,nrow=8,ncol=8) temp_adjacent[1,6:8]<-1;temp_adjacent[2,7]<-1;temp_adjacent[c(4,6,7),8]<-1; temp_adjacent<-temp_adjacent+t(temp_adjacent) temp_val<-function(sets,adjacent){ if(length(sets)==1) val<-0 else{ subadjacent<-adjacent[sets,sets] nsets<-length(sets) A<-diag(1,nsets); B<-matrix(0,nsets,nsets) for(l in 1:(nsets-1)){ A<-A%*%subadjacent B<-B+A } val<-ifelse(sum(B==0)>nsets,0,1) } return(val) } est.shcoa(8,112,temp_val,2,c(1,0,1,1),temp_adjacent)
Estimating the Shapley value based on component orthogonal array (COA) with a prime d
est.shcoa.prime(d, n, val, ...)
est.shcoa.prime(d, n, val, ...)
d |
a prime, the number of players. |
n |
an integer, the sample size. |
val |
the predefined value function. |
... |
other parameters used in val(sets,...). |
a vector including estimated Shapley values of all players based on COA.
temp_adjacent<-matrix(0,nrow=5,ncol=5) temp_adjacent[1,c(2,3,5)]<-1;temp_adjacent[2,4]<-1;temp_adjacent[3,5]<-1; temp_adjacent<-temp_adjacent+t(temp_adjacent) temp_val<-function(sets,adjacent){ if(length(sets)==1) val<-0 else{ subadjacent<-adjacent[sets,sets] nsets<-length(sets) A<-diag(1,nsets); B<-matrix(0,nsets,nsets) for(l in 1:(nsets-1)){ A<-A%*%subadjacent B<-B+A } val<-ifelse(sum(B==0)>nsets,0,1) } return(val) } est.shcoa.prime(5,20,temp_val,temp_adjacent)
temp_adjacent<-matrix(0,nrow=5,ncol=5) temp_adjacent[1,c(2,3,5)]<-1;temp_adjacent[2,4]<-1;temp_adjacent[3,5]<-1; temp_adjacent<-temp_adjacent+t(temp_adjacent) temp_val<-function(sets,adjacent){ if(length(sets)==1) val<-0 else{ subadjacent<-adjacent[sets,sets] nsets<-length(sets) A<-diag(1,nsets); B<-matrix(0,nsets,nsets) for(l in 1:(nsets-1)){ A<-A%*%subadjacent B<-B+A } val<-ifelse(sum(B==0)>nsets,0,1) } return(val) } est.shcoa.prime(5,20,temp_val,temp_adjacent)
Estimating the Shapley value based on Latin square (LS)
est.shls(d, n, val, ...)
est.shls(d, n, val, ...)
d |
an integer, the number of players. |
n |
an integer, the sample size. |
val |
the predefined value function. |
... |
other parameters used in val(sets,...). |
a vector including estimated Shapley values of all players based on LS.
temp_adjacent<-matrix(0,nrow=8,ncol=8) temp_adjacent[1,6:8]<-1;temp_adjacent[2,7]<-1;temp_adjacent[c(4,6,7),8]<-1; temp_adjacent<-temp_adjacent+t(temp_adjacent) temp_val<-function(sets,adjacent){ if(length(sets)==1) val<-0 else{ subadjacent<-adjacent[sets,sets] nsets<-length(sets) A<-diag(1,nsets); B<-matrix(0,nsets,nsets) for(l in 1:(nsets-1)){ A<-A%*%subadjacent B<-B+A } val<-ifelse(sum(B==0)>nsets,0,1) } return(val) } est.shls(8,56,temp_val,temp_adjacent)
temp_adjacent<-matrix(0,nrow=8,ncol=8) temp_adjacent[1,6:8]<-1;temp_adjacent[2,7]<-1;temp_adjacent[c(4,6,7),8]<-1; temp_adjacent<-temp_adjacent+t(temp_adjacent) temp_val<-function(sets,adjacent){ if(length(sets)==1) val<-0 else{ subadjacent<-adjacent[sets,sets] nsets<-length(sets) A<-diag(1,nsets); B<-matrix(0,nsets,nsets) for(l in 1:(nsets-1)){ A<-A%*%subadjacent B<-B+A } val<-ifelse(sum(B==0)>nsets,0,1) } return(val) } est.shls(8,56,temp_val,temp_adjacent)
Estimating the Shapley value based on simple random sampling (SRS)
est.shsrs(d, n, val, ...)
est.shsrs(d, n, val, ...)
d |
an integer, the number of players. |
n |
an integer, the sample size. |
val |
the predefined value function. |
... |
other parameters used in val(sets,...). |
a vector including estimated Shapley values of all players based on SRS.
temp_adjacent<-matrix(0,nrow=8,ncol=8) temp_adjacent[1,6:8]<-1;temp_adjacent[2,7]<-1;temp_adjacent[c(4,6,7),8]<-1; temp_adjacent<-temp_adjacent+t(temp_adjacent) temp_val<-function(sets,adjacent){ if(length(sets)==1) val<-0 else{ subadjacent<-adjacent[sets,sets] nsets<-length(sets) A<-diag(1,nsets); B<-matrix(0,nsets,nsets) for(l in 1:(nsets-1)){ A<-A%*%subadjacent B<-B+A } val<-ifelse(sum(B==0)>nsets,0,1) } return(val) } est.shsrs(8,112,temp_val,temp_adjacent)
temp_adjacent<-matrix(0,nrow=8,ncol=8) temp_adjacent[1,6:8]<-1;temp_adjacent[2,7]<-1;temp_adjacent[c(4,6,7),8]<-1; temp_adjacent<-temp_adjacent+t(temp_adjacent) temp_val<-function(sets,adjacent){ if(length(sets)==1) val<-0 else{ subadjacent<-adjacent[sets,sets] nsets<-length(sets) A<-diag(1,nsets); B<-matrix(0,nsets,nsets) for(l in 1:(nsets-1)){ A<-A%*%subadjacent B<-B+A } val<-ifelse(sum(B==0)>nsets,0,1) } return(val) } est.shsrs(8,112,temp_val,temp_adjacent)
Estimating the Shapley value based on structured simple random sampling (StrRS)
est.shstrrs(d, n, val, ...)
est.shstrrs(d, n, val, ...)
d |
an integer, the number of players. |
n |
an integer, the sample size. |
val |
the predefined value function. |
... |
other parameters used in val(sets,...). |
a vector including estimated Shapley values of all players based on StrRS.
temp_adjacent<-matrix(0,nrow=8,ncol=8) temp_adjacent[1,6:8]<-1;temp_adjacent[2,7]<-1;temp_adjacent[c(4,6,7),8]<-1; temp_adjacent<-temp_adjacent+t(temp_adjacent) temp_val<-function(sets,adjacent){ if(length(sets)==1) val<-0 else{ subadjacent<-adjacent[sets,sets] nsets<-length(sets) A<-diag(1,nsets); B<-matrix(0,nsets,nsets) for(l in 1:(nsets-1)){ A<-A%*%subadjacent B<-B+A } val<-ifelse(sum(B==0)>nsets,0,1) } return(val) } est.shstrrs(8,112,temp_val,temp_adjacent)
temp_adjacent<-matrix(0,nrow=8,ncol=8) temp_adjacent[1,6:8]<-1;temp_adjacent[2,7]<-1;temp_adjacent[c(4,6,7),8]<-1; temp_adjacent<-temp_adjacent+t(temp_adjacent) temp_val<-function(sets,adjacent){ if(length(sets)==1) val<-0 else{ subadjacent<-adjacent[sets,sets] nsets<-length(sets) A<-diag(1,nsets); B<-matrix(0,nsets,nsets) for(l in 1:(nsets-1)){ A<-A%*%subadjacent B<-B+A } val<-ifelse(sum(B==0)>nsets,0,1) } return(val) } est.shstrrs(8,112,temp_val,temp_adjacent)
Polynomial additive defined on GF(s) with a prime s
gfpoly.add(f1, f2, s)
gfpoly.add(f1, f2, s)
f1 |
a vector represents the coefficients of the first addend polynomial. For example, if the dividend is x^5+2x^3+1, then f1=c(1,0,2,0,0,1). |
f2 |
a vector represents the coefficients of the second addend polynomial. For example, if the divisor is x^4+2, then f2=c(1,0,0,0,2). |
s |
a prime, the order of the Galois filed (GF). |
a vector represents the coefficients of the resulting polynomial. For example, the result c(1, 1, 2, 0, 0, 0) represents x^5+x^4+2x^3.
gfpoly.add(c(1,0,2,0,0,1),c(1,0,0,0,2),3)
gfpoly.add(c(1,0,2,0,0,1),c(1,0,0,0,2),3)
Polynomial division defined on GF(s) with a prime s
gfpoly.div(f1, f2, s)
gfpoly.div(f1, f2, s)
f1 |
a vector represents the coefficients of the dividend polynomial. For example, if the dividend is x^5+2x^3+1, then f1=c(1,0,2,0,0,1). |
f2 |
a vector represents the coefficients of the dividend polynomial. For example, if the divisor is x^4+2, then f2=c(1,0,0,0,2). |
s |
a prime, the order of the Galois filed (GF). |
a vector represents the coefficients of the resulting polynomial. For example, the result c(2,0,1,1) represents 2x^3+x+1.
gfpoly.div(c(1,0,2,0,0,1),c(1,0,0,0,2),3)
gfpoly.div(c(1,0,2,0,0,1),c(1,0,0,0,2),3)
Polynomial mutiplication defined on GF(s) with a prime s
gfpoly.multi(f1, f2, s)
gfpoly.multi(f1, f2, s)
f1 |
a vector represents the coefficients of the first multiplier polynomial. For example, if the dividend is x^5+2x^3+1, then f1=c(1,0,2,0,0,1). |
f2 |
a vector represents the coefficients of the second multiplier polynomial. For example, if the divisor is x^4+2, then f2=c(1,0,0,0,2). |
s |
a prime, the order of the Galois filed (GF). |
a vector represents the coefficients of the resulting polynomial. For example, the result c(1,0,2,0,2,1,1,0,0,2) represents x^9+2x^7+2x^5+x^4+x^3+2.
gfpoly.multi(c(1,0,2,0,0,1),c(1,0,0,0,2),3)
gfpoly.multi(c(1,0,2,0,0,1),c(1,0,0,0,2),3)
Determine whether an integer is a prime
is.prime(x)
is.prime(x)
x |
the integer to be determined. |
the result: TRUE (x is a prime) or FALSE (x is not a prime).
is.prime(7) is.prime(8)
is.prime(7) is.prime(8)
Generate a component orthogonal array (COA) with a prime power d
onecoa(d, p, f_d)
onecoa(d, p, f_d)
d |
a power of prime p, the column of the resulting COA. |
p |
a prime, the bottom number of d. |
f_d |
a vector represents the coefficients of primative polynomial on GF(d). For example the primative polynomial on GF(3^2) is x^2+x+2, then let f_d=c(1,1,2). |
a COA with d(d-1) rows and d columns.
onecoa(9,3,c(1,1,2))
onecoa(9,3,c(1,1,2))
Generate a component orthogonal array (COA) with a prime d
onecoa.prime(d)
onecoa.prime(d)
d |
a prime, the column of the resulting COA. |
a COA with d(d-1) rows and d columns.
onecoa.prime(5)
onecoa.prime(5)
Generate an Latin square (LS)
onels(d)
onels(d)
d |
an integer, the run size of the resulting LS. |
an LS with d rows and d columns.
onels(5)
onels(5)
Polynomial division
poly.div(f1, f2)
poly.div(f1, f2)
f1 |
a vector represents the coefficients of the dividend polynomial. For example, if the dividend is x^5+2x^3+1, then f1=c(1,0,2,0,0,1). |
f2 |
a vector represents the coefficients of the dividend polynomial. For example, if the divisor is x^4+2, then f2=c(1,0,0,0,2). |
a vector represents the coefficients of the resulting polynomial. For example, the result c(2,0,-2,1) represents 2x^3-2x+1.
poly.div(c(1,0,2,0,0,1),c(1,0,0,0,2))
poly.div(c(1,0,2,0,0,1),c(1,0,0,0,2))
Generate the structured samples of simple random samples
structed.perm(permatrix, jcom, d)
structed.perm(permatrix, jcom, d)
permatrix |
a matrix, each row is a permutation. |
jcom |
an integer, represents the target component. Hope that the component jcom appears the same number of at each position. |
d |
the number of components. |
a matrix represents the structured samples.
temp_samples<-matrix(nrow=10,ncol=5) for(i in 1:10){temp_samples[i,]<-sample(1:5,5)} structed.perm(temp_samples,3,5)
temp_samples<-matrix(nrow=10,ncol=5) for(i in 1:10){temp_samples[i,]<-sample(1:5,5)} structed.perm(temp_samples,3,5)