简化计算,所以它可以利用矩阵运算完成它可以、矩阵

2023-09-12 21:25:34 作者:一般小妞一般纯

的基本操作我是在相同长度的两个概率向量的操作。 我们姑且称之为A,B。在R中的公式是:

  T = 1-PROD(1-A * B)
 

这是,其结果是一个标量,所述(1-AB)是逐点的操作,其结果是一个向量,其第i个元素是 1 A_I * b_i 。该运营商给出了向量元素的产品。 这样做的意义(因为你可以猜)是这样的:假设A的概率为每一种疾病(或其它信号)的氮源有某种疾病。 B是概率为每个源来传输该疾病,如果它们有它,到目标的矢量。结果是目标的概率获取疾病从(至少一个)的源。

好了,现在我有很多类型的信号,所以我有很多A的载体。并为每个类型的信号我有很多目标,各有不同的概率传输(或多​​个B向量),我想计算的T的结果为一对。 理想情况下,矩阵乘法可以做的伎俩,如果这次行动是一个内积的载体。但我的操作是不是这样的(我认为)。

我寻找的是某种载体A和B上的改造,所以我可以使用矩阵乘法。任何其他建议,以简化我的计算是值得欢迎的。

下面是一个例子(以R code)

  A = rbind(C(0.9,0.1,0.3),C(0.7,0.2,0.1))
一个
#也就是说,源2的概率有疾病/信号1为0.1(A [1,2]
#既行也不列需要总结为1。
B = cbind(C(0,0.3,0.9),C(0.9,0.6,0.3),C(0.3,0.8,0.3),C(0.4,0.5,1))
乙
#即,目标4的概率获取来自源2的疾病是0.5乙[2,4]
#再次,没有什么需要总结到1这里

#的结果应该是:
C = T(适用(A,1,函数(x)的申请(B,2,功能(Y)1-PROD(1-X * Y))))
#基本上循环,每行的B中的每一列和
#计算所需的式
C
#虽然这是很优雅,它是不是很有效,我期待的转变
#我的A,B矩阵这样我就可以写的,原则上
#C = F(A)%*%G(B),其中f(A)是我的改变A,G(B)是我的转变(B),
#和%*%是矩阵乘法

#请注意,如果在式代替(1-刺(1-XY))以上和(X * Y),其结果
#正是矩阵乘法,这就是为什么我认为,我不是说太远
#和想享受矩阵已经实现优化的好处
#乘法。
 
华为公开 光计算芯片以及光计算装置 专利

解决方案

首先我要指出的R code可能会误导一些Matlab的用户,因为 A * B R中等同于 A. * B 在Matlab(逐元素相乘)。我在计算中使用的符号变量,使所发生的操作更加明确。

  SYMS A11 A12 A21 A22 B11 B12 B21 B22
SYMS A13 A31 A23 A32 A33
SYMS B13 B31 B23 B32 B33
 

首先考虑最简单的情况下,我们只有1个向量A和1向量B:

  A1 = [A11,A21]。
B1 = [B11,B21]。
 

你想要的结果是

  1  - 刺(1-A1 * B1)
=
1  - (A11 * B11  -  1)*(A12 * B12  -  1)
 

现在假设我们有3个向量A和2载体乙堆叠旁边的另一列:

  A3 = [A11 A12 A13,A21 A22 A23; A31 A32 A33]。
B2 = [B11 B12,B21 B22; B31 B32]
 

为了拿到A3列搭配的列向量B2你可以做以下的所有可能的组合向量的所有可能的组合的指标:

  [INDA INDB] = meshgrid(1:3,1:2);
 

现在因为对于两个向量成对产品A,B也认为, A * B = * A ,我们可以只保留了独特的双指数。你可以做到这一点,如下所示:

  INDA = triu(INDA); INDB = triu(INDB);
INDA =重塑(INDA(INDA大于0),[],1); INDB =重塑(INDB(INDB大于0),[],1);
 

现在,你要可以计算的结果是:

 的结果= 1  - 督促(1-A3(:,INDA)* B2(:,INDB))
 

只是为了更好的可读性:

  pretty的(结果。)

=

  +  -   -  +
  | (A11 B11  -  1)(A21 B21  -  1)(A31 B31  -  1)+ 1 |
  | |
  | (A12 B11  -  1)(A22 B21  -  1)(A32 B31  -  1)+ 1 |
  | |
  | (A12 B12  -  1)(A22 B22  -  1)(A32 B32  -  1)+ 1 |
  | |
  | (A13 B11  -  1)(A23 B21  -  1)(A33 B31  -  1)+ 1 |
  | |
  | (A13 B12  -  1)(A23 B22  -  1)(A33 B32  -  1)+ 1 |
  +  -   -  +
 

The basic operation I have is an operation on two probability vectors of the same length. let's call them A,B. in R the formula is:

t = 1-prod(1-A*B)

that is, the result is a scalar, the (1-AB) is a point-wise operation, whose result is a vector whose i'th element is 1-a_i*b_i. The prod operator gives the product of the elements of the vector. The meaning of this (as you could guess) is this: suppose A is the probability for each of N sources of a disease (or other signal) to have a certain disease. B is the vector of probabilities for each of sources to transmit the disease, if they have it, to the target. The outcome is the probability of the target to acquire the disease from (at least one of) the sources.

Ok, so now I have many types of signals, so I have many "A" vectors. and for each type of signal I have many targets, each with different probability of transmission (or many "B" vectors), and I want to compute the "t" outcome for each pair. Ideally, a matrix multiplication can do the trick if the operation was an "inner product" of the vectors. but my operation is not such (I think).

What I look for is some kind of a transformation on the vectors A and B, so I could use matrix multiplication. Any other suggestion to simplify my computation is welcome.

Here is an example (code in R)

A = rbind(c(0.9,0.1,0.3),c(0.7,0.2,0.1))
A 
# that is, the probability of source 2 to have disease/signal 1 is 0.1 (A[1,2]
# neither rows nor columns need to sum to 1.
B = cbind(c(0,0.3,0.9),c(0.9,0.6,0.3),c(0.3,0.8,0.3),c(0.4,0.5,1))
B
# that is, the probability of target 4 to acquire a disease from source 2 is 0.5 B[2,4]
# again, nothing needs to sum to 1 here

# the outcome should be:
C = t(apply(A,1,function(x) apply(B,2,function(y) 1-prod(1-x*y))))
# which basically loops on every row in A and every column in B and 
# computes the required formula
C
# while this is quite elegant, it is not very efficient, and I look for transformations
# on my A,B matrices so I could write, in principle
# C = f(A)%*%g(B), where f(A) is my transformed A, g(B) is my transformed(B),
# and %*% is matrix multiplication

# note that if replace (1-prod(1-xy)) in the formula above with sum(x*y), the result
# is exactly matrix multiplication, which is why I think, I'm not too far from that
# and want to enjoy the benefits of already implemented optimizations of matrix
# multiplications.

解决方案

First I should note that the R code might be misleading to some Matlab users because A*B in R is equivalent to A.*B in Matlab (element-wise multiplication). I used symbolic variables in my calculations so that the operations that take place are clearer.

syms a11 a12 a21 a22 b11 b12 b21 b22
syms a13 a31 a23 a32 a33
syms b13 b31 b23 b32 b33

First consider the easiest case we have only 1 vector A and 1 vector B :

A1 = [a11;a21] ;
B1 = [b11;b21] ;

The result you want is

1 - prod(1-A1.*B1)
=
1 - (a11*b11 - 1)*(a12*b12 - 1)

Now assume we have 3 vectors A and 2 vectors B stacked one next to the other in columns:

A3 = [a11 a12 a13;a21 a22 a23; a31 a32 a33];
B2 = [b11 b12 ;b21 b22 ; b31 b32];

In order to get the indices of all the possible combinations of columns vectors of A3 paired with all the possible combinations of column vectors of B2 you can do the following:

[indA indB] = meshgrid(1:3,1:2);

Now since for pairwise product of two vectors a,b it holds that a.*b = b.*a we can just keep the unique pairs of indices. You can do that as follows:

indA = triu(indA); indB = triu(indB);
indA = reshape(indA(indA>0),[],1); indB = reshape(indB(indB>0),[],1);

Now the result that you want could be calculated:

result = 1 - prod(1-A3(:,indA).*B2(:,indB))

Just for better readability:

pretty(result.')

=

  +-                                               -+ 
  |  (a11 b11 - 1) (a21 b21 - 1) (a31 b31 - 1) + 1  | 
  |                                                 | 
  |  (a12 b11 - 1) (a22 b21 - 1) (a32 b31 - 1) + 1  | 
  |                                                 | 
  |  (a12 b12 - 1) (a22 b22 - 1) (a32 b32 - 1) + 1  | 
  |                                                 | 
  |  (a13 b11 - 1) (a23 b21 - 1) (a33 b31 - 1) + 1  | 
  |                                                 | 
  |  (a13 b12 - 1) (a23 b22 - 1) (a33 b32 - 1) + 1  | 
  +-                                               -+

 
精彩推荐