对for循环进行矢量化
是否可以矢量化以下函数(对for循环进行矢量化,r,parallel-processing,data.table,vectorization,R,Parallel Processing,Data.table,Vectorization,是否可以矢量化以下函数(f) x <- rep(c(rep(c(0.2,-0.2),4),0.2,0.2,-0.2,0.2),20) > f(0.5, x) [1] 16.56635
f
)
x <- rep(c(rep(c(0.2,-0.2),4),0.2,0.2,-0.2,0.2),20)
> f(0.5, x)
[1] 16.56635
> f_version2(0.5, x)
[1] 16.56635
我有一个向量x
,我想通过改变p
使函数f
的输出值最大化
x <- rep(c(rep(c(0.2,-0.2),4),0.2,0.2,-0.2,0.2),20)
> f(0.5, x)
[1] 16.56635
> f_version2(0.5, x)
[1] 16.56635
但是这个函数非常慢,因为它没有矢量化,所以我想知道是否有一个好的方法来实现它。我们的想法是在将来将其并行化,并可能使用data.table
来加速它
x <- rep(c(rep(c(0.2,-0.2),4),0.2,0.2,-0.2,0.2),20)
> f(0.5, x)
[1] 16.56635
> f_version2(0.5, x)
[1] 16.56635
我的真实数据要大得多…所以我提供了一个模拟示例
# My mock data
x <- data.frame(x=rep(c(rep(c(0.2,-0.2),4),0.2,0.2,-0.2,0.2),20))
# The function to optimise for
f <- function(p,x){
# Generate columns before filling
x$multiplier <- NA
x$cumulative <- NA
for(i in 1:nrow(x)){
# Going through each row systematically
if(i==1){
# If first row do a slightly different set of commands
x[i,'multiplier'] <- 1 * p
x[i,'cumulative'] <- (x[i,'multiplier'] * x[i,'x']) + 1
} else {
# For the rest of the rows carry out these commands
x[i,'multiplier'] <- x[i-1,'cumulative'] * p
x[i,'cumulative'] <- (x[i,'multiplier'] * x[i,'x']) + x[i-1,'cumulative']
}
}
# output the final row's output for the cumulative column
as.numeric(x[nrow(x),'cumulative'])
}
# Checking the function works by putting in a test value of p = 0.5
f(0.5,x)
# Now optimise the function between the interval of p between 0 and 1
optim.p <- optimise(f=f, interval=c(0,1),x, maximum=TRUE)
# Viewing the output of optim.p
optim.p
x <- rep(c(rep(c(0.2,-0.2),4),0.2,0.2,-0.2,0.2),20)
> f(0.5, x)
[1] 16.56635
> f_version2(0.5, x)
[1] 16.56635
#我的模拟数据
x(编辑-忘记了我写的文章的第一部分,现在就放进去)
x <- rep(c(rep(c(0.2,-0.2),4),0.2,0.2,-0.2,0.2),20)
> f(0.5, x)
[1] 16.56635
> f_version2(0.5, x)
[1] 16.56635
通过检查函数f
的实际功能,可以简化您的问题。因为我很懒,所以我要写<代码> x[i,'乘子'/c>作为MI,<代码> x [ i,'累积' ] < /> >为彝,< <代码> x[i,'x' ] < /> >席。
x <- rep(c(rep(c(0.2,-0.2),4),0.2,0.2,-0.2,0.2),20)
> f(0.5, x)
[1] 16.56635
> f_version2(0.5, x)
[1] 16.56635
让我们看看f
中的等式。我们首先来看案例i>1
:
x <- rep(c(rep(c(0.2,-0.2),4),0.2,0.2,-0.2,0.2),20)
> f(0.5, x)
[1] 16.56635
> f_version2(0.5, x)
[1] 16.56635
mi=yi-1*p
依=mi*席+Y-1~<
x <- rep(c(rep(c(0.2,-0.2),4),0.2,0.2,-0.2,0.2),20)
> f(0.5, x)
[1] 16.56635
> f_version2(0.5, x)
[1] 16.56635
将上述m_i替换为:
x <- rep(c(rep(c(0.2,-0.2),4),0.2,0.2,-0.2,0.2),20)
> f(0.5, x)
[1] 16.56635
> f_version2(0.5, x)
[1] 16.56635
Y=(Yi-1*P)*席+Y-1//…
Yi=Y-1*(P*席+ 1)< /P>
x <- rep(c(rep(c(0.2,-0.2),4),0.2,0.2,-0.2,0.2),20)
> f(0.5, x)
[1] 16.56635
> f_version2(0.5, x)
[1] 16.56635
这样就无需计算乘法器
列
x <- rep(c(rep(c(0.2,-0.2),4),0.2,0.2,-0.2,0.2),20)
> f(0.5, x)
[1] 16.56635
> f_version2(0.5, x)
[1] 16.56635
现在再仔细看一下你的i==1
案例,我们发现如果我们把y0放在1,那么下面的结果适用于所有i=1,nrow(x)
:
x <- rep(c(rep(c(0.2,-0.2),4),0.2,0.2,-0.2,0.2),20)
> f(0.5, x)
[1] 16.56635
> f_version2(0.5, x)
[1] 16.56635
yi=yi-1(pxi+1)----(1)
x <- rep(c(rep(c(0.2,-0.2),4),0.2,0.2,-0.2,0.2),20)
> f(0.5, x)
[1] 16.56635
> f_version2(0.5, x)
[1] 16.56635
查看函数f
,您要计算的是yn:
x <- rep(c(rep(c(0.2,-0.2),4),0.2,0.2,-0.2,0.2),20)
> f(0.5, x)
[1] 16.56635
> f_version2(0.5, x)
[1] 16.56635
yn=yn-1(pxn+1)
x <- rep(c(rep(c(0.2,-0.2),4),0.2,0.2,-0.2,0.2),20)
> f(0.5, x)
[1] 16.56635
> f_version2(0.5, x)
[1] 16.56635
如果我们使用(1)替换上述公式中的yn-1,会发生什么情况
x <- rep(c(rep(c(0.2,-0.2),4),0.2,0.2,-0.2,0.2),20)
> f(0.5, x)
[1] 16.56635
> f_version2(0.5, x)
[1] 16.56635
yn=yn-2(pxn-1+1)(pxn+1)
x <- rep(c(rep(c(0.2,-0.2),4),0.2,0.2,-0.2,0.2),20)
> f(0.5, x)
[1] 16.56635
> f_version2(0.5, x)
[1] 16.56635
现在我们用yn-2的公式替换上述内容:
x <- rep(c(rep(c(0.2,-0.2),4),0.2,0.2,-0.2,0.2),20)
> f(0.5, x)
[1] 16.56635
> f_version2(0.5, x)
[1] 16.56635
yn=yn-3(pxn-2+1)(pxn-1+1)(pxn+1)
x <- rep(c(rep(c(0.2,-0.2),4),0.2,0.2,-0.2,0.2),20)
> f(0.5, x)
[1] 16.56635
> f_version2(0.5, x)
[1] 16.56635
你明白了,对吧?我们一直替换到y1:
x <- rep(c(rep(c(0.2,-0.2),4),0.2,0.2,-0.2,0.2),20)
> f(0.5, x)
[1] 16.56635
> f_version2(0.5, x)
[1] 16.56635
yn=y0(px1+1)(px2+1)…(pxn-1+1)(pxn+1)
x <- rep(c(rep(c(0.2,-0.2),4),0.2,0.2,-0.2,0.2),20)
> f(0.5, x)
[1] 16.56635
> f_version2(0.5, x)
[1] 16.56635
但请记住,y0只是1。因此,要计算f(x,p)
的值,我们只需:
x <- rep(c(rep(c(0.2,-0.2),4),0.2,0.2,-0.2,0.2),20)
> f(0.5, x)
[1] 16.56635
> f_version2(0.5, x)
[1] 16.56635
f(x,p)=(px1+1)(px2+1)…(pxn-1+1)(pxn+1)
x <- rep(c(rep(c(0.2,-0.2),4),0.2,0.2,-0.2,0.2),20)
> f(0.5, x)
[1] 16.56635
> f_version2(0.5, x)
[1] 16.56635
其中n
是nrow(x)
。也就是说,为每个i
计算p*x[i,'x']+1
,并将它们全部相乘
x <- rep(c(rep(c(0.2,-0.2),4),0.2,0.2,-0.2,0.2),20)
> f(0.5, x)
[1] 16.56635
> f_version2(0.5, x)
[1] 16.56635
要将R中的数字向量相乘,可以使用prod
。因此,如果x
只是一个向量:
f_version2 <- function(p, x) {
return(prod(p * x + 1))
}
x <- rep(c(rep(c(0.2,-0.2),4),0.2,0.2,-0.2,0.2),20)
> f(0.5, x)
[1] 16.56635
> f_version2(0.5, x)
[1] 16.56635
f_version2(编辑-忘记了我写的文章的第一部分,现在就放进去)
x <- rep(c(rep(c(0.2,-0.2),4),0.2,0.2,-0.2,0.2),20)
> f(0.5, x)
[1] 16.56635
> f_version2(0.5, x)
[1] 16.56635
通过检查函数f
的实际功能,可以简化您的问题。因为我很懒,所以我要写<代码> x[i,'乘子'/c>作为MI,<代码> x [ i,'累积' ] < /> >为彝,< <代码> x[i,'x' ] < /> >席。
x <- rep(c(rep(c(0.2,-0.2),4),0.2,0.2,-0.2,0.2),20)
> f(0.5, x)
[1] 16.56635
> f_version2(0.5, x)
[1] 16.56635
让我们看看f
中的等式。我们首先来看案例i>1
:
x <- rep(c(rep(c(0.2,-0.2),4),0.2,0.2,-0.2,0.2),20)
> f(0.5, x)
[1] 16.56635
> f_version2(0.5, x)
[1] 16.56635
mi=yi-1*p
依=mi*席+Y-1~<
x <- rep(c(rep(c(0.2,-0.2),4),0.2,0.2,-0.2,0.2),20)
> f(0.5, x)
[1] 16.56635
> f_version2(0.5, x)
[1] 16.56635
将上述m_i替换为:
x <- rep(c(rep(c(0.2,-0.2),4),0.2,0.2,-0.2,0.2),20)
> f(0.5, x)
[1] 16.56635
> f_version2(0.5, x)
[1] 16.56635
Y=(Yi-1*P)*席+Y-1//…
Yi=Y-1*(P*席+ 1)< /P>
x <- rep(c(rep(c(0.2,-0.2),4),0.2,0.2,-0.2,0.2),20)
> f(0.5, x)
[1] 16.56635
> f_version2(0.5, x)
[1] 16.56635
这样就无需计算乘法器
列
x <- rep(c(rep(c(0.2,-0.2),4),0.2,0.2,-0.2,0.2),20)
> f(0.5, x)
[1] 16.56635
> f_version2(0.5, x)
[1] 16.56635
现在再仔细看一下你的i==1
案例,我们发现如果我们把y0放在1,那么下面的结果适用于所有i=1,nrow(x)
:
x <- rep(c(rep(c(0.2,-0.2),4),0.2,0.2,-0.2,0.2),20)
> f(0.5, x)
[1] 16.56635
> f_version2(0.5, x)
[1] 16.56635
yi=yi-1(pxi+1)----(1)
x <- rep(c(rep(c(0.2,-0.2),4),0.2,0.2,-0.2,0.2),20)
> f(0.5, x)
[1] 16.56635
> f_version2(0.5, x)
[1] 16.56635
查看函数f
,您要计算的是yn:
x <- rep(c(rep(c(0.2,-0.2),4),0.2,0.2,-0.2,0.2),20)
> f(0.5, x)
[1] 16.56635
> f_version2(0.5, x)
[1] 16.56635
yn=yn-1(pxn+1)
x <- rep(c(rep(c(0.2,-0.2),4),0.2,0.2,-0.2,0.2),20)
> f(0.5, x)
[1] 16.56635
> f_version2(0.5, x)
[1] 16.56635
如果我们使用(1)替换上述公式中的yn-1,会发生什么情况
x <- rep(c(rep(c(0.2,-0.2),4),0.2,0.2,-0.2,0.2),20)
> f(0.5, x)
[1] 16.56635
> f_version2(0.5, x)
[1] 16.56635
yn=yn-2(pxn-1+1)(pxn+1)
x <- rep(c(rep(c(0.2,-0.2),4),0.2,0.2,-0.2,0.2),20)
> f(0.5, x)
[1] 16.56635
> f_version2(0.5, x)
[1] 16.56635
现在我们用yn-2的公式替换上述内容:
x <- rep(c(rep(c(0.2,-0.2),4),0.2,0.2,-0.2,0.2),20)
> f(0.5, x)
[1] 16.56635
> f_version2(0.5, x)
[1] 16.56635
yn=yn-3(pxn-2+1)(pxn-1+1)(pxn+1)
x <- rep(c(rep(c(0.2,-0.2),4),0.2,0.2,-0.2,0.2),20)
> f(0.5, x)
[1] 16.56635
> f_version2(0.5, x)
[1] 16.56635
你明白了,对吧?我们一直替换到y1:
x <- rep(c(rep(c(0.2,-0.2),4),0.2,0.2,-0.2,0.2),20)
> f(0.5, x)
[1] 16.56635
> f_version2(0.5, x)
[1] 16.56635
yn=y0(px1+1)(px2+1)…(pxn-1+1)(pxn+1)
x <- rep(c(rep(c(0.2,-0.2),4),0.2,0.2,-0.2,0.2),20)
> f(0.5, x)
[1] 16.56635
> f_version2(0.5, x)
[1] 16.56635
但请记住,y0只是1。因此,要计算f(x,p)
的值,我们只需:
x <- rep(c(rep(c(0.2,-0.2),4),0.2,0.2,-0.2,0.2),20)
> f(0.5, x)
[1] 16.56635
> f_version2(0.5, x)
[1] 16.56635
f(x,p)=(px1+1)(px2+1)…(pxn-1+1)(pxn+1)
x <- rep(c(rep(c(0.2,-0.2),4),0.2,0.2,-0.2,0.2),20)
> f(0.5, x)
[1] 16.56635
> f_version2(0.5, x)
[1] 16.56635
其中n
是nrow(x)
。也就是说,为每个i
计算p*x[i,'x']+1
,并将它们全部相乘
x <- rep(c(rep(c(0.2,-0.2),4),0.2,0.2,-0.2,0.2),20)
> f(0.5, x)
[1] 16.56635
> f_version2(0.5, x)
[1] 16.56635
要将R中的数字向量相乘,可以使用prod
。因此,如果x
只是一个向量:
f_version2 <- function(p, x) {
return(prod(p * x + 1))
}
x <- rep(c(rep(c(0.2,-0.2),4),0.2,0.2,-0.2,0.2),20)
> f(0.5, x)
[1] 16.56635
> f_version2(0.5, x)
[1] 16.56635
f_version2+6.02*10^23仅最后一句话的评分+6.02*10^23仅最后一句话的评分!