-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDyn_corr
More file actions
104 lines (88 loc) · 3.04 KB
/
Dyn_corr
File metadata and controls
104 lines (88 loc) · 3.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu May 8 20:21:13 2025
@author: fvera
"""
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from scipy.stats import pearsonr
import matplotlib.pyplot as plt
import pandas as pd
# Simulate dynamic ground-truth adjacency matrices (OU process)
def generate_dynamic_adjacency(N, T, tau=20, sigma=0.05):
A = np.zeros((T, N, N))
A[0] = np.random.rand(N, N) * 0.1
for t in range(1, T):
dA = -(A[t-1] - A[0]) / tau + sigma * np.random.randn(N, N)
A[t] = A[t-1] + dA
A[t] = np.clip(A[t], 0, 1)
return A
# Simulate DyGAT-predicted adjacency matrices with noise
def simulate_prediction(A_true, noise_level=0.1):
return np.clip(A_true + np.random.randn(*A_true.shape) * noise_level, 0, 1)
# Compute edge-wise Pearson correlations
def edge_correlations(A_true, A_pred):
T, N, _ = A_true.shape
corrs = []
for i in range(N):
for j in range(N):
if i != j:
r, _ = pearsonr(A_true[:, i, j], A_pred[:, i, j])
corrs.append(r)
return np.mean(corrs), corrs
# Parameters
N, T = 100, 300
A_true = generate_dynamic_adjacency(N, T)
A_pred = simulate_prediction(A_true)
# Correlation computation
mean_r, all_r = edge_correlations(A_true, A_pred)
# Plot
plt.figure(figsize=(8, 5))
sns.histplot(all_r, kde=True, bins=30, color='royalblue')
plt.axvline(mean_r, color='red', linestyle='--', label=f'Mean ρ = {mean_r:.2f}')
plt.xlabel('Pearson Correlation (ρ)')
plt.ylabel('Edge Count')
plt.title('Edge-wise Correlation Between True and Predicted Dynamics')
plt.legend()
plt.tight_layout()
plt.savefig("dyn_corr.png")
plt.show()
def generate_synthetic_dygat_data(N=50, T=100, noise_std=0.05, seed=42):
np.random.seed(seed)
A_true = np.zeros((N, N, T))
for i in range(N):
for j in range(N):
if i != j:
x = np.zeros(T)
for t in range(1, T):
x[t] = 0.9 * x[t-1] + 0.05 * np.random.randn()
A_true[i, j, :] = np.tanh(x)
alpha_pred = A_true + noise_std * np.random.randn(N, N, T)
return A_true, alpha_pred
# Generate synthetic data
A_true, alpha_pred = generate_synthetic_dygat_data()
# Compute edge-wise correlation
N, _, T = A_true.shape
correlations = []
for i in range(N):
for j in range(N):
if i != j:
corr, _ = pearsonr(A_true[i, j, :], alpha_pred[i, j, :])
correlations.append(corr)
# Plot the correlation histogram
plt.figure(figsize=(8, 5))
plt.hist(correlations, bins=30, color='steelblue', edgecolor='black', alpha=0.7)
plt.axvline(np.mean(correlations), color='red', linestyle='--', label=f"Mean ρ = {np.mean(correlations):.3f}")
plt.xlabel("Pearson Correlation per Edge")
plt.ylabel("Count")
plt.title("Distribution of Edge-wise Temporal Correlation (DyGAT Simulation)")
plt.legend()
plt.grid(True)
plt.tight_layout()
plt.show()
mean_correlation = np.mean(correlations)
mean_correlation
stats = pd.Series(correlations).describe()
print(stats[['mean', 'std', 'min', 'max']])