Skip to content

Commit

Permalink
Update BottomUpParceLiNGAM
Browse files Browse the repository at this point in the history
  • Loading branch information
ikeuchi-screen committed May 14, 2021
1 parent e2c1ef2 commit f4cdfde
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
24 changes: 16 additions & 8 deletions lingam/bottom_up_parce_lingam.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,12 @@ def _extract_partial_orders(self, pk):
no_path_pairs = pairs[counts < 2]

check_pairs = np.concatenate([path_pairs, no_path_pairs[:, [1, 0]]])
if len(check_pairs) == 0:
# If no pairs are extracted from the specified prior knowledge,
# discard the prior knowledge.
self._Aknw = None
return None

pairs = np.unique(check_pairs, axis=0)
return pairs[:, [1, 0]] # [to, from] -> [from, to]

Expand Down Expand Up @@ -286,22 +292,24 @@ def _estimate_adjacency_matrix(self, X, prior_knowledge=None):
self : object
Returns the instance itself.
"""
sink_vars = get_sink_variables(prior_knowledge)
exo_vars = get_exo_variables(prior_knowledge)
if prior_knowledge is not None:
pk = prior_knowledge.copy()
np.fill_diagonal(pk, 0)

B = np.zeros([X.shape[1], X.shape[1]], dtype='float64')
for i in range(1, len(self._causal_order)):
target = self._causal_order[i]

# target is not used for prediction if it is included in exogenous variables
if target in exo_vars:
continue

# Flatten the array into one dimension
predictors = self._flatten(self._causal_order[:i])

# sink variables are not used as predictors
predictors = [v for v in predictors if v not in sink_vars]
# Exclude variables specified in no_path with prior knowledge
if prior_knowledge is not None:
predictors = [p for p in predictors if pk[target, p] != 0]

# target is exogenous variables if predictors are empty
if len(predictors) == 0:
continue

B[target, predictors] = predict_adaptive_lasso(
X, predictors, target)
Expand Down
2 changes: 1 addition & 1 deletion tests/test_multi_group_direct_lingam.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ def test_prior_knowledge_invalid():

# prior knowledge: invalid
pk = np.array([
[0, -1, -1],
[0, -1, 1],
[-1, 0, -1],
[-1, -1, 0],
[-1, -1, -1],
Expand Down

0 comments on commit f4cdfde

Please sign in to comment.