R

时间:2017-05-31 03:12:19

标签: r analysis variance

人。我正在研究sobol灵敏度分析。我尝试以估计和理论方式计算一阶效应和总效应指数。首先,我按照维基百科“基于方差的敏感性分析”中的步骤计算估计值。 These are the estimated equations 这是代码:

x_1<-ceiling(1000*runif(1000))  ## generate x1 from range [1,1000]
x_2<-ceiling(100*runif(1000))   ## generate x2 from range [1,100]
x_3<-ceiling(10*runif(1000))    ## generate x3 from range [1,10]
x<-cbind(x_1,x_2,x_3)           ## combine as one matrix
A<-matrix(x[1:500,],ncol=3)     ## divide this one matrix into two 
sample, this is sample A
B<-matrix(x[501:1000,],ncol=3)  ## this is sample B
AB1<-cbind(B[,1],A[,-1])        ## replace the first column of sample A 
by the first column of sample B
AB2<-cbind(A[,1],B[,2],A[,3])   ## replace the second column of sample 
A by the second column of sample B 
AB3<-cbind(A[,-3],B[,3])        ## replace the third column of sample A 
by the third column of sample B 
trial<-function(x){             ## define the trial function: 
x1+x2*x3^2 
x[,1]+(x[,2])*(x[,3])^2
}

Y_A<-trial(A)                 ## the output of A
Y_B<-trial(B)                 ## the output of B
Y_AB1<-trial(AB1)             ## the output of AB1
Y_AB2<-trial(AB2)             ## the output of AB2
Y_AB3<-trial(AB3)             ## the output of AB3
Y<-matrix(cbind(Y_A,Y_B),ncol=1)  ## the matrix of total outputs

S1<-mean(Y_B*(Y_AB1-Y_A))/var(Y)    ## first order effect of x1 
St1<-(sum((Y_A-Y_AB1)^2)/(2*500))/var(Y) ## total order effect of x1

S2<-mean(Y_B*(Y_AB2-Y_A))/var(Y)     ## first order effect of x2
St2<-(sum((Y_A-Y_AB2)^2)/(2*500))/var(Y)  ## total order effect of x2

S3<-mean(Y_B*(Y_AB3-Y_A))/var(Y)     ## first order effect of x3
St3<-(sum((Y_A-Y_AB3)^2)/(2*500))/var(Y) ## total order effect of x3

S<-matrix(c(S1,St1,S2,St2,S3,St3),nrow=2,ncol=3)  ## define the results 
matrix

rownames(S)<-list("first order","total")
colnames(S)<-list("X1","X2","X3")
S                                               ## print result

结果是:

  X1        X2        X3
first order 0.01557169 0.2383217 0.4279961
total       0.01641255 0.4261444 0.6712027

然后,我想计算理论结果以验证上述估计结果。我按照下图中的公式进行操作。 enter image description here 为了计算部分方差,我以两种方式处理它:monte carlo集成和手工集成(我只是不相信计算机......)

以下是代码:

pimc=function(n){                      ## monte carlo integration
a=runif(n)
  g=(a+mean(x_2)*(mean(x_3)^2))^2
  pimc=mean(g)
  pimc

}

pimc(10000)/var(Y)
                                                                  ## 
doing integration by hand
(1/3+mean(x_2)*(mean(x_3))^2+((mean(x_2))^2)*((mean(x_3))^4))/var(Y)  
## first order effect x1
(mean(x_1)^2+mean(x_1)*mean(x_3)^2+(1/3)*mean(x_3)^4)/var(Y)          
## first order effect x2
(mean(x_1)^2+(2/3)*(mean(x_1)*mean(x_2))+(1/5)*(mean(x_2)^2))/var(Y)  
## first order effect x3

结果如下:

       [,1]
[1,] 0.4483701          ## first order indice of x1 computed by monte 
carlo integration
        [,1]
[1,] 0.4483673          ## first order indice of x1 computed by hand
       [,1]
[1,] 0.05805077         ## first order indice of x2 computed by hand
       [,1]   
[1,] 0.05842811         ## first order indice of x3 computed by hand

通过使用蒙特卡罗积分和手动求解结果是恒定的。 但它们与估算结果完全不同。我不知道为什么。有什么想法吗?请帮帮忙,谢谢! (当我们做部分积分时,我们只查看一个参数,我们将其他参数视为常数。所以我选择将每个参数的均值作为常数。是不是错了?)

0 个答案:

没有答案