gpt4 book ai didi

javascript - 在 Bonferroni 不等式的 JS 实现中避免多重循环

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:12:21 27 4
gpt4 key购买 nike

我正在尝试实现“Bonferroni 不等式”,它使用 Javascript UDF 为 GCP BigQuery 上的数据科学用例的许多独立事件的联合概率建模。但是我对 JS 很不熟悉,对好的做法一无所知。

要应用的公式如下:

P(U Ai) = SUM(P(Ai)) - SUM(P(Ai)*P(Aj)) + SUM(P(Ai)*P(Aj)*P(Ak) - ... i != j != k

我对此函数的输入是单个事件概率的数组:

[P(A1), P(A2), P(A3), ...] 

我本能地在行中创建了“for 循环”来获得结果,但是,看到如此丑陋的代码令人心痛,所以想知道你们中是否有人知道如何以更优雅和优化的方式实现它?

这是我为 4 级 Bonferroni 不等式编写的函数:

function unionBoundProbability(probList){

var intersection2 = 0;
var intersection3 = 0;
var intersection4 = 0;
var i = 0;
var j = 0;
var k = 0;
var l = 0;
var sum = 0;
var product = 1;

var sum = probList.reduce((a, b) => a + b, 0);

for(i = 0; i < probList.length; i++){
product *= probList[i];
for(j = i+1; j < probList.length; j++){
intersection2 += probList[i]*probList[j];
for(k = j+1; k < probList.length; k++){
intersection3 += probList[i]*probList[j]*probList[k];
for(l = k+1; l < probList.length; l++){
intersection4 += probList[i]*probList[j]*probList[k]*probList[l];
}
}
}
}

switch (probList.length) {
case 0:
return 0;
break;
case 1:
return probList[0];
break;
case 2:
return sum - product;
break;
case 3:
return sum - intersection2 + product;
break
case 4:
return sum - intersection2 + intersection3 - product;
case 5 :
return sum - intersection2 + intersection3 - intersection4 + product;
default:
return Math.max((sum - intersection2 + intersection3 - intersection4), Math.max.apply(Math, probList));
}
}

我想做的是计算作为输入传递的所有概率的并集概率的近似值。

如果我的概率小于 5,则 switch 语句应用确切的公式。否则,默认情况下应用 Bonferroni 近似,(因为我正在模拟信号被接收的机会,如果估计小于最好天线的概率,那么我保留最好的天线)。

谢谢你的帮助

最佳答案

此示例遵循来自 https://www.probabilitycourse.com/chapter6/6_2_1_union_bound_and_exten.php 的以下等式

P(⋃(i=1 => n)Ai)=∑(i=1 => n) P(Ai) − ∑(i<j) P(Ai ∩ Aj) + ∑(i<j<k) P(Ai ∩ Aj ∩ Ak) − ... +(−1)^n−1 P(⋂(i=1 => n) Ai)

我不知道您在给出的示例中包含阶乘的原因,但我没有包含阶乘,因为它们不在上面的等式中。

// Recursive function to update sums of each level
function updateSums(sums, probList, maxLevel, currentLevel = 1, currentProduct = 1, lastIndex = -1) {
// Example case: maxLevel = 4, curentLevel = 3, path = { 1: 0, 2: 1 }, currentProduct = probList[0] * probList[1]
// Loops through all entries except 0 and 1 and adds the products to sums[2], for each entry also calculates level 4 sums

for (let i = lastIndex + 1; i < probList.length; i++) {
const nextProduct = currentProduct * probList[i];
sums[currentLevel - 1] += nextProduct;

if (currentLevel < maxLevel) {
// get the next level product sums for current product
updateSums(sums, probList, maxLevel, currentLevel + 1, nextProduct, i);
}
}
}

// Main function
function inequality(probList) {
probList = probList.sort((a, b) => b - a).slice(0, 4);

// Calculate maxLevel
const maxLevel = probList.length;
if (!maxLevel) return 0;

// create am array of sums, each entry represents 1 level
const sums = (new Array(maxLevel)).fill(0);
updateSums(sums, probList, maxLevel);

return sums.reduce((a, c, i) => {
return a + ((i % 2) ? -1 : 1) * c;
}, 0);
}

console.log(inequality(probList));

PS: This is written in ES6

关于javascript - 在 Bonferroni 不等式的 JS 实现中避免多重循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58498907/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com