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