第577行: |
第577行: |
| ==计算EI的源代码== | | ==计算EI的源代码== |
| | | |
− | 这是计算一个马尔科夫概率转移矩阵的Python源代码。输入tpm为一个满足行归一化条件的马尔科夫概率转移矩阵,返回的ei_all为其EI值。
| + | 这是计算一个马尔科夫概率转移矩阵的Python源代码。输入tpm为一个满足行归一化条件的马尔科夫概率转移矩阵,返回的ei_all为其EI值,其余两项分别为确定性和简并性。 |
| python:<syntaxhighlight lang="python3"> | | python:<syntaxhighlight lang="python3"> |
| def tpm_ei(tpm, log_base = 2): | | def tpm_ei(tpm, log_base = 2): |
− | # marginal distribution of y given x ~ Unifrom Dist
| + | # marginal distribution of y given x ~ Unifrom Dist |
− | puy = tpm.sum(axis=0)
| + | puy = tpm.sum(axis=0) |
− | n = tpm.shape[0]
| + | n = tpm.shape[0] |
− | # replace 0 to a small positive number to avoid log error
| + | |
− | eps = 1E-10
| + | # replace 0 to a small positive number to avoid log error |
− | tpm_e = np.where(tpm==0, eps, tpm)
| + | eps = 1E-10 |
− | puy_e = np.where(tpm==0, eps, puy)
| + | tpm_e = np.where(tpm==0, eps, tpm) |
− |
| + | puy_e = np.where(tpm==0, eps, puy) |
− | # calculate EI of specific x
| + | |
− | ei_x = (np.log2(n * tpm_e / puy_e) / np.log2(log_base) * tpm).sum(axis=1)
| + | # calculate EI of specific x |
− |
| + | ei_x = (np.log2(n * tpm_e / puy_e) / np.log2(log_base) * tpm).sum(axis=1) |
− | # calculate total EI
| + | |
− | ei_all = ei_x.mean()
| + | # calculate det and deg |
− | return ei_all
| + | det = np.log2(n) + (tpm * np.log2(tpm_e)).sum(axis=1).mean(axis=0) |
| + | deg = np.log2(n) + (tpm.mean(axis=0) * np.log2(tpm_e.mean(axis=0))).sum() |
| + | |
| + | # calculate total EI |
| + | ei_all = ei_x.mean() |
| + | return ei_all,det/np.log2(log_base),deg/np.log2(log_base) |
| </syntaxhighlight> | | </syntaxhighlight> |
| | | |