分数= MongoDB中Bernoulli参数的Wilson分数置信区间的下限

时间:2019-04-25 14:23:37

标签: mongodb confidence-interval

我的文档中有许多积极总计条评论。 如何在” How not to sort by average rating”中说明如何使用“ 伯努利参数的Wilson得分置信区间的下限”来计算得分?

下面是当前文档的样子:

{
  "name": "Hotel A",
  "positive_reviews": 2,
  "negative_reviews": 1,
  "total_reviews": 3
},{
  "name": "Hotel B",
  "positive_reviews": 3,
  "negative_reviews": 3,
  "total_reviews": 6
}

我希望计算后显示以下输出:

{
  "name": "Hotel A",
  "positive_reviews": 2,
  "negative_reviews": 1,
  "total_reviews": 3
   "scores": 0.34
},{
  "name": "Hotel B",
  "positive_reviews": 3,
  "negative_reviews": 3,
  "total_reviews": 6
  "scores": 0,19
}

1 个答案:

答案 0 :(得分:1)

我应用了以下公式:

((positive + 1.9208) / (positive + negative) - 
                   1.96 * SQRT((positive * negative) / (positive + negative) + 0.9604) / 
                          (positive + negative)) / (1 + 3.8416 / (positive + negative))

这是MongoDB查询:

db.collection.aggregate([
    {
        $project: {
        name:1,
        positive_reviews:1,
        negative_reviews:1,
        total_reviews:1,
            scores: {
                $divide: [
                        {
                            $subtract: [
                                    {
                                        $divide: [
                                            { $add: ["$positive_reviews",1.9208] },
                                            { $add: ["$positive_reviews","$negative_reviews"] }
                                        ],
                                    },
                                    {
                                        $multiply: [
                                            1.96,
                                            {
                                                $divide: [ 
                                                    { 
                                                        $sqrt: { 
                                                            $add: [
                                                                { 
                                                                    $divide: [ 
                                                                        {
                                                                            $multiply: ["$positive_reviews","$negative_reviews"] 
                                                                        }, 
                                                                        {   
                                                                            $add: ["$positive_reviews","$negative_reviews"]
                                                                        } 
                                                                    ] 
                                                                },
                                                                0.9604
                                                            ]
                                                        }
                                                    },
                                                    {   
                                                        $add: ["$positive_reviews","$negative_reviews"]
                                                    }
                                                ]
                                            }
                                        ]
                                    },
                            ]
                        },
                        {
                            $add: [
                                {
                                    $divide: [ 3.8416, {$add: ["$positive_reviews","$negative_reviews"]}  ]
                                },
                                1
                            ]
                        }
                ]
            },
        }
    }
  ])

我的结果如下:

/* 1 createdAt:26/04/2019, 00:16:16*/
{
    "_id" : ObjectId("5cc200785d13d8232aff6e98"),
    "name" : "Hotel A",
    "positive_reviews" : 2,
    "negative_reviews" : 1,
    "total_reviews" : 3,
    "scores" : 0.20765495512648788
},

/* 2 createdAt:26/04/2019, 00:16:16*/
{
    "_id" : ObjectId("5cc200785d13d8232aff6e99"),
    "name" : "Hotel B",
    "positive_reviews" : 3,
    "negative_reviews" : 3,
    "total_reviews" : 6,
    "scores" : 0.18761280689940865
}