更改

第326行: 第326行:     
== 代码实现 ==
 
== 代码实现 ==
−
这里分别针对状态空间和变量空间的因果涌现实例,提供了计算有效信息的python代码,以供参考。
+
这里分别针对上述状态空间和变量空间的因果涌现实例,提供了计算微观、宏观层次有效信息的python代码,以供参考。
 +
 
 +
基于状态空间的计算需要给定微观状态的概率转移矩阵,以及状态的
    
python:<syntaxhighlight lang="python3">
 
python:<syntaxhighlight lang="python3">
 
import torch
 
import torch
 +
import itertools
    +
# 计算信息熵
 
def Entropia(a):
 
def Entropia(a):
 
     b = torch.nonzero(a > 0).squeeze(1)
 
     b = torch.nonzero(a > 0).squeeze(1)
 
     return -torch.sum(a[b]*torch.log2(a[b]))
 
     return -torch.sum(a[b]*torch.log2(a[b]))
   −
 
+
# 基于状态概率矩阵计算有效信息
 
def effective_information(G1):
 
def effective_information(G1):
 
     """
 
     """
第364行: 第368行:  
     return Win_entropy - Wout_average
 
     return Win_entropy - Wout_average
    +
# ==========================
 +
# 给定粗粒化方案
 +
# ==========================
   −
# 状态空间的有效信息计算
+
# 状态空间的宏观TPM生成,以及有效信息计算
 
def calc_tpm(micro_tpm, group):
 
def calc_tpm(micro_tpm, group):
 
     """
 
     """
第400行: 第407行:       −
# 变量空间的有效信息计算
+
# 变量空间的微观、宏观TPM生成,以及有效信息计算
 
def calc_bn_ei(micro_mech, micro, edges, elem_group, mech_group):
 
def calc_bn_ei(micro_mech, micro, edges, elem_group, mech_group):
 
     """
 
     """
第420行: 第427行:  
     Returns
 
     Returns
 
     -------
 
     -------
−
     the EI value of micro and macro level.
+
     micro_ei, macro_ei: the EI value of micro, macro level
 +
    S_m, S_M: transition probability matrix of micro, macro level
 +
 
 
     """
 
     """
 
     # generate the statue list of boolean nodes, based on the number of micro elements
 
     # generate the statue list of boolean nodes, based on the number of micro elements
第456行: 第465行:  
     macro_ei = effective_information(S_M)
 
     macro_ei = effective_information(S_M)
   −
     return micro_ei, macro_ei
+
     return micro_ei, macro_ei, S_m, S_M
    
# Example:
 
# Example:
第462行: 第471行:  
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)
 +
CE = macro_ei - micro_ei
    
# Example:提高确定性实例
 
# Example:提高确定性实例
第470行: 第480行:  
mech_group = {'00': '0', '01': '0', '10': '0', '11': '1'}
 
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)
 
micro_ei, macro_ei = calc_bn_ei(micro_mech, micro, edges, elem_group, mech_group)
 +
CE = macro_ei - micro_ei
 +
 +
# ==========================
 +
# 贪婪算法寻找最优粗粒化方案(来源:https://github.com/jkbren/einet)
 +
# ==========================
 +
# 需要在Github下载ce_net.py
 +
from ce_net import *
 +
 +
# directly define the micro TPM, or use the generating methods above in function calc_bn_ei.
 +
micro_tpm = np.array([[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]])
 +
 +
# check_network: transfer the TPM numpy ndarray format to digraph
 +
G = check_network(micro_tpm)
 +
 +
# causal_emergence: the function defined in ce_net.py, optimizing the coarse-graining methods using greedy algorithm.
 +
CE = causal_emergence(G)
    +
EI_gain = CE['EI_macro'] - CE['EI_micro']
 
</syntaxhighlight>
 
</syntaxhighlight>
  
5,443

个编辑