Brightway2蒙特卡罗模拟中的负对数正态结果

2024-09-28 17:20:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我不知道如何设置BW2,以便在对数正态分布参数的MC模拟中获得负值,例如模拟负排放。示例:

from brightway2 import *
import numpy as np


mydb = Database('mydb')

mydb.write({
    ('mydb', 'Some activity'): {
        'name': 'Some activity',
        'unit': 'kWh',
        'exchanges': [{
            'input': ('mydb', 'Carbon dioxide'),
            'amount': 20, # positive!
            'unit': 'kg',
            'type': 'biosphere', 
            'uncertainty type' : 2,
            'loc' : np.log(20), 
            'scale' : 1.01 
            }]
    },
    ('mydb', 'Carbon dioxide'): {'name': 'Carbon dioxide', 'unit': 'kg', 'type': 'biosphere'}
    })

exc = list(mydb.get('Some activity').exchanges())[0]
exc.as_dict()
exc.random_sample(n=10)  

这很管用。我得到:

Out[8]: 
array([ 25.20415107,  17.48476344,  16.98842921,   3.79548038,
    12.54165042,  27.93752377,   7.57070571,  43.22285015,
    48.44984804,  13.83083672]) # everything fine

现在让我们假设我想要得到相同的值,但为负:array([ -25.20415107, -17.48476344, etc. ...,因为我假设我的二氧化碳吸收量为-20千克二氧化碳。如果我写'amount': -20,我会得到一个奇怪的结果:

Out[9]: 
array([   0.73060359,   36.69825867,    5.71416558,   10.78119397,
     16.24447705,    2.96507057,    6.73564118,   19.24411117,
      7.23110067,  126.42690714])

我知道对数正态分布不可能是负的,但我所期望的是,分布是基于‘loc’和‘scale’信息的正值计算出来的,然后根据‘amount’信息进行反转。这对于在负排放清单上执行MC是必要的。有什么线索吗?谢谢


Tags: importastype对数unitsomeactivitymc
1条回答
网友
1楼 · 发布于 2024-09-28 17:20:52

有两个问题阻碍了预期的行为:

  1. amount字段中给出负值是不够的;RNG代码在stats_arrays库中,您必须将negative字段设置为Truesee docs)。你知道吗
  2. brightway2-data中有一个bug,在7e3341c2.4.6版本中修复,它阻止了键negativeexchange.uncertainty中使用。你知道吗

通常,从其他格式导入数据时,negative字段会自动设置,例如SimaPro CSVEcospold 1。此外,当数据库被处理为参数数组时,negative字段也是来自amount fieldalways set。这种情况下的区别是,您直接从stats_arrays调用函数,而不是通过brightway2-calc。你知道吗

在最新安装中添加negative字段:

from brightway2 import *
import numpy as np
projects.set_current("SO 45935773")
bw2setup()

mydb = Database('mydb')
gwp = ('IPCC 2013', 'climate change', 'GWP 100a')
co2 = get_activity(('biosphere3', '349b29d1-3e58-4c66-98b9-9d1a076efd2e'))

mydb.write({
    ('mydb', 'Some activity'): {
        'name': 'Some activity',
        'unit': 'kWh',
        'exchanges': [{
            'input': co2.key,
            'amount': -20, # negative
            'negative': True,
            'unit': 'kg',
            'type': 'biosphere', 
            'uncertainty type' : 2,
            'loc' : np.log(20), 
            'scale' : 1.01 
        }]
    }
})

exc = list(mydb.get('Some activity').exchanges())[0]
exc.random_sample(n=10)  

产生预期行为:

array([ -3.24683872,  -5.01873359, -31.54532003, -40.59523805,
       -54.00447092,  -6.11459063, -41.5250442 ,  -8.05295075,
       -31.46077832, -29.8769442 ])

相关问题 更多 >