亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

作為 PC 算法的一部分,在 python 中測試條件獨立性

作為 PC 算法的一部分,在 python 中測試條件獨立性

蠱毒傳說 2021-09-11 13:36:17
我正在python中實現PC算法。這種算法構建了一個 n 變量高斯分布的圖形模型。這個圖形模型基本上是有向無環圖的骨架,這意味著如果一個結構像:(x1)---(x2)---(x3)在圖中,則 x1 與給定 x2 的 x3 無關。更一般地,如果 A 是圖的鄰接矩陣且 A(i,j)=A(j,i) = 0(i 和 j 之間缺少邊),則 i 和 j 條件獨立,所有變量出現在從 i 到 j 的任何路徑中。出于統計和機器學習的目的,可以“學習”底層圖形模型。如果我們對聯合高斯 n 變量隨機變量有足夠的觀察,我們可以使用 PC 算法,其工作原理如下:given n as the number of variables observed, initialize the graph as G=K(n) for each pair i,j of nodes:    if exists an edge e from i to j:        look for the neighbours of i        if j is in neighbours of i then remove j from the set of neighbours        call the set of neighbours k        TEST if i and j are independent given the set k, if TRUE:             remove the edge e from i to j該算法還計算圖的分離集,該分離集由另一種算法使用,該算法構建從骨架開始的 dag 和由 pc 算法返回的分離集。這是我到目前為止所做的:def _core_pc_algorithm(a,sigma_inverse):l = 0N = len(sigma_inverse[0])n = range(N)sep_set = [ [set() for i in n] for j in n]act_g = complete(N)z = lambda m,i,j : -m[i][j]/((m[i][i]*m[j][j])**0.5)while l<N:    for (i,j) in itertools.permutations(n,2):        adjacents_of_i = adj(i,act_g)        if j not in adjacents_of_i:            continue        else:            adjacents_of_i.remove(j)        if len(adjacents_of_i) >=l:            for k in itertools.combinations(adjacents_of_i,l):                if N-len(k)-3 < 0:                    return (act_g,sep_set)                if test(sigma_inverse,z,i,j,l,a,k):                    act_g[i][j] = 0                    act_g[j][i] = 0                    sep_set[i][j] |= set(k)                    sep_set[j][i] |= set(k)    l = l + 1此函數實現了一個統計測試,該測試使用了估計偏相關的 Fisher z 變換。我以兩種方式使用這個算法:從線性回歸模型生成數據并將學習到的 DAG 與預期的進行比較讀取數據集并學習底層 DAG在這兩種情況下,我并不總是得到正確的結果,要么是因為我知道某個數據集背后的 DAG,要么是因為我知道生成模型但它與我的算法學習的模型不一致。我完全知道這是一項重要的任務,即使在我在這里省略的部分代碼中,我也可能誤解了理論概念以及犯了錯誤;但首先我想知道(從比我更有經驗的人那里),如果我寫的測試是正確的,并且是否有執行此類測試的庫函數,我嘗試搜索但我找不到任何合適的功能。
查看完整描述

1 回答

?
交互式愛情

TA貢獻1712條經驗 獲得超3個贊

我切入正題。上面代碼中最關鍵的問題,關于以下錯誤:


sqrt(n-len(k)-3)*abs(z(sigma_inverse[i][j])) <= phi(1-alpha/2)

我誤解了 n 的平均值,它不是精度矩陣的大小,而是多變量觀察的總數(在我的情況下,是 10000 而不是 5)。另一個錯誤的假設是 z(sigma_inverse[i][j]) 必須提供 i 和 j 的部分相關性,給定所有其余部分。這是不正確的,z 是精度矩陣的適當子集上的 Fisher 變換,它估計給定 K 時 i 和 j 的偏相關。正確的測試如下:


if len(K) == 0: #CM is the correlation matrix, we have no variables conditioning (K has 0 length)

    r = CM[i, j] #r is the partial correlation of i and j 

elif len(K) == 1: #we have one variable conditioning, not very different from the previous version except for the fact that i have not to compute the correlations matrix since i start from it, and pandas provide such a feature on a DataFrame

    r = (CM[i, j] - CM[i, K] * CM[j, K]) / math.sqrt((1 - math.pow(CM[j, K], 2)) * (1 - math.pow(CM[i, K], 2))) #r is the partial correlation of i and j given K

else: #more than one conditioning variable

    CM_SUBSET = CM[np.ix_([i]+[j]+K, [i]+[j]+K)] #subset of the correlation matrix i'm looking for

    PM_SUBSET = np.linalg.pinv(CM_SUBSET) #constructing the precision matrix of the given subset

    r = -1 * PM_SUBSET[0, 1] / math.sqrt(abs(PM_SUBSET[0, 0] * PM_SUBSET[1, 1]))

r = min(0.999999, max(-0.999999,r)) 

res = math.sqrt(n - len(K) - 3) * 0.5 * math.log1p((2*r)/(1-r)) #estimating partial correlation with fisher's transofrmation

return 2 * (1 - norm.cdf(abs(res))) #obtaining p-value

我希望有人能發現這有幫助


查看完整回答
反對 回復 2021-09-11
  • 1 回答
  • 0 關注
  • 338 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號