【问题标题】:How to reduce for-loops in this code?如何减少此代码中的 for 循环?
【发布时间】:2013-11-14 15:49:35
【问题描述】:

我正在做涉及太多 for 循环的计算。我将不胜感激任何可以消除一些循环以使算法更有效的想法。这是我想要得到的数学表达式:随机变量 Y 的离散分布。

Pr(Y=y)=
∑_Pr(Z=z) ∙∑_Pr((X=x) ∑_Pr(W=w) ∙∑_Pr(R=r│W=w) ∙Pr(S=z+y-x-r|W=w) Y,Z,X,W,R,S 是离散随机变量,它们是相关的。我知道每个术语的表达式,但只有概率计算,而不是近似分布。

    array Y[max_Y+1];  % store the distribution of Y
temp1=0, temp2=0, temp3=0, temp4=0; % summation for partial distributions
for y = 0 max_Y
    temp1=0;
    for z = 0 : 5- y
        temp2=0;
        for x=0:5
            temp3=0;
            for w=0:5
                temp4=0
                for r=0:w
                    temp4=temp4+Pr(R=r│W=w)∙Pr(S=z+y-x-r|W=w);
                    end
                temp3=temp3+temp4*Pr(W=w);
                end
            temp2= temp2+temp3*Pr(X=x);
            end
       temp1=temp1+temp2* P(Z=z);
       end
   Y[y]=temp1;
   end

非常感谢! 酯类

【问题讨论】:

  • 我看不懂它的写法。修改或转储。

标签: algorithm for-loop probability performance


【解决方案1】:

从我在每次迭代中注意到的情况来看,只有术语 Pr(S=z+y-x-r|W=w) & Pr(Z=z) 取决于您的函数输入变量 Y,因此可以使用单独的 for 循环预先计算所有其他值,然后只计算 Pr(S=z+y-x-r|W=w)*Pr(Z=z)*precomputed

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-03-18
    • 2023-03-25
    • 2021-07-19
    • 2014-11-27
    • 1970-01-01
    • 2012-12-03
    • 2015-08-04
    相关资源
    最近更新 更多