更改

跳到导航 跳到搜索
第326行: 第326行:     
== 代码实现 ==
 
== 代码实现 ==
−
这里分别针对状态空间和变量空间的因果涌现实例,提供了python代码实现的参考。
+
这里分别针对状态空间和变量空间的因果涌现实例,提供了计算有效信息的python代码,以供参考。
    
python:<syntaxhighlight lang="python3">
 
python:<syntaxhighlight lang="python3">
第365行: 第365行:        +
# 状态空间的有效信息计算
 
def calc_tpm(micro_tpm, group):
 
def calc_tpm(micro_tpm, group):
 
     """
 
     """
第399行: 第400行:        +
# 变量空间的有效信息计算
 +
def calc_bn_ei(micro_mech, micro, edges, elem_group, mech_group):
 +
    """
 +
    Calculate the effective information of boolean network in micro and macro level, given the micro mechanism and
 +
    coarse-graining methods.
 +
 +
    Parameters
 +
    ----------
 +
    micro_mech: boolean function, e.g., {'00': [0.7, 0.3], '01': [0.7, 0.3], '10': [0.7, 0.3], '11': [0, 1]} the
 +
            value means the probability of transferring to '0' and '1', correspondingly.
 +
    micro: the number of micro elements
 +
    edges: a list of tuple. the list means the input micro elements. e.g., edges[1] = (2, 3) means
 +
            the input of elements[1] is [2] and [3]
 +
    elem_group: the coarse-graining methods of micro elements, e.g., [(0, 1), (2, 3)] means coarse-graining
 +
            elements[0],[1] to be a macro element, and the [2] and [3] to be another.
 +
    mech_group: the coarse-graining method of micro_mech, e.g.,  mech_group = {'00': '0', '01': '0', '10': '0',
 +
            '11': '1'}
 +
 +
    Returns
 +
    -------
 +
    the EI value of micro and macro level.
 +
    """
 +
    # generate the statue list of boolean nodes, based on the number of micro elements
 +
    Nmi = 2 ** micro
 +
    node_list = list(itertools.product(['0', '1'], repeat=micro))
 +
    S_m = torch.zeros((Nmi, Nmi), dtype=torch.float)
 +
 +
    # calculate the transition probability matrix of micro level
 +
    for i, pre in enumerate(node_list):
 +
        for j, tmp in enumerate(node_list):
 +
            tr_pr = 1
 +
            for z in range(micro):
 +
                srce1 = pre[edges[z][0]]
 +
                srce2 = pre[edges[z][1]]
 +
                tr_pr *= micro_mech[srce1 + srce2][int(tmp[z])]
 +
            S_m[i][j] = tr_pr
 +
 +
    # calculate the grouping matrix from micro to macro level, based on the elem_group and mech_group.
 +
    unique = set(mech_group.values())
 +
    Nma = len(unique) ** len(elem_group)
 +
    group = torch.zeros((Nmi, Nma), dtype=torch.float)
 +
    for i, item in enumerate(node_list):
 +
        Sma = ''
 +
        for elem in elem_group:
 +
            Smi = ''.join(item[idx] for idx in elem)
 +
            Sma += mech_group[Smi]
 +
        group[i, int(Sma, base=len(unique))] = 1
 +
 +
    # calculate the transition probability matrix of macro level
 +
    S_M = group.t() @ S_m @ group
 +
    S_M = S_M / torch.sum(S_M, axis=1).unsqueeze(1)
 +
 +
    # calculate the EI of micro and macro level
 +
    micro_ei = effective_information(S_m)
 +
    macro_ei = effective_information(S_M)
 +
 +
    return micro_ei, macro_ei
 +
 +
# Example:
 
micro_tpm = torch.tensor([[1/3, 1/3, 1/3, 0], [1/3, 1/3, 1/3, 0], [1/3, 1/3, 1/3, 0], [0, 0, 0, 1]])
 
micro_tpm = torch.tensor([[1/3, 1/3, 1/3, 0], [1/3, 1/3, 1/3, 0], [1/3, 1/3, 1/3, 0], [0, 0, 0, 1]])
 
group = [(0, 1, 2), (3,)]
 
group = [(0, 1, 2), (3,)]
 
micro_ei, macro_ei, macro_tpm = calc_tpm(micro_tpm, group)
 
micro_ei, macro_ei, macro_tpm = calc_tpm(micro_tpm, group)
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
 +
 +
# Example:提高确定性实例
 +
micro_mech = {'00': [0.7, 0.3], '01': [0.7, 0.3], '10': [0.7, 0.3], '11': [0, 1]}
 +
micro = 4
 +
edges = [(2, 3), (2, 3), (0, 1), (0, 1)]
 +
elem_group = [(0, 1), (2, 3)]
 +
mech_group = {'00': '0', '01': '0', '10': '0', '11': '1'}
 +
micro_ei, macro_ei = calc_bn_ei(micro_mech, micro, edges, elem_group, mech_group)
    
== 应用 ==
 
== 应用 ==
5,443

个编辑

导航菜单