收敛交叉映射CCM学习记录(1)

在研究rEDM的CCM函数时,发现帮助文档中的描述与实际示例存在不一致。根据文档,第二列应表示从Y预测X的相关系数,但在示例中似乎相反。针对这一矛盾,作者提出了疑问,希望得到澄清。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

今天发现了一个问题,在rEDM的帮助文档里,它说CCM()函数会产生一个数据框,第一列是库的长度,第二三列是相关系数,“第二列是从Y中预测的X,第三列是从X中预测的Y”。这就意味着在第二列中:第二列是X的预测值与X的观测值之间的相关系数,也就是说假设X是原因,Y是结果,因为只有这样的因果方向,才会从Y中预测X。

However,在它的鱼类和温度的示例里,和上述文字描述截然相反

data(sardine_anchovy_sst)
df <- CCM( dataFrame=sardine_anchovy_sst, E=3, Tp=0, columns="anchovy",
target="np_sst", libSizes="10 70 10", sample=100 )
df
 #LibSize anchovy:np_sst np_sst:anchovy
#1      10      0.1057165    -0.03350884
#2      20      0.1467477    -0.04642011
#3      30      0.1483597    -0.04260756
#4      40      0.1884354    -0.03005727
#5      50      0.1945121    -0.06274684
#6      60      0.2129041    -0.04468005
#7      70      0.2165172    -0.0675
### 收敛交叉映射CCM)算法实现 #### Python 实现 以下是基于Python的收敛交叉映射CCM)算法的一个简单实现: ```python import numpy as np from scipy.spatial import KDTree def ccm(x, y, E=3, Tp=1, lib_size=None): """ Convergent Cross Mapping (CCM) Implementation. Parameters: x : array-like Time series data for variable X. y : array-like Time series data for variable Y. E : int Embedding dimension. Tp : int Prediction interval. lib_size : int or None Library size; if None, uses entire length of the shorter time series. Returns: rho_values : list Correlation coefficients between predicted and actual values. """ # Ensure library sizes match n = min(len(x), len(y)) if not lib_size: lib_size = n # Create shadow manifolds M_x = [] M_y = [] for i in range(E-1, n): M_x.append([x[i-j] for j in reversed(range(E))]) M_y.append([y[i-j] for j in reversed(range(E))]) M_x = np.array(M_x[:lib_size-E+1]) M_y = np.array(M_y[:lib_size-E+1]) tree = KDTree(M_y) predictions = [] actuals = [] for t in range(lib_size, n-Tp): # Find k-nearest neighbors in M_y dist, idx = tree.query(M_x[t], k=E) weights = [d / sum(dist) for d in dist] prediction = 0 for w, neighbor_idx in zip(weights, idx): prediction += w * y[neighbor_idx + Tp] predictions.append(prediction) actuals.append(y[t + Tp]) correlation_coefficient = np.corrcoef(predictions, actuals)[0][1] return correlation_coefficient # Example usage time_series_x = np.random.rand(1000) time_series_y = np.sin(time_series_x) + np.random.normal(scale=0.1, size=time_series_x.shape) rho_xy = ccm(time_series_x, time_series_y) print(f"Correlation coefficient from X to Y: {rho_xy}") rho_yx = ccm(time_series_y, time_series_x) print(f"Correlation coefficient from Y to X: {rho_yx}") ``` 此代码实现了基本的CCM逻辑,即通过嵌入维度构建影子流形,并利用K最近邻法预测目标变量未来的值。最终计算实际值与预测值之间的皮尔逊相关系数作为衡量标准。 #### MATLAB 实现 对于MATLAB用户而言,下面是一个简单的CCM函数定义: ```matlab function rho = ccm(x, y, E, Tp, libSize) % CCM Convergent Cross Mapping function % % Inputs: % x - Time series vector for variable X % y - Time series vector for variable Y % E - Embedding Dimension % Tp - Prediction Interval % libSize - Size of the library used for reconstruction n = min(length(x), length(y)); if isempty(libSize) libSize = n; end % Construct Shadow Manifold M_x = []; for i = E:n-libSize+1 temp = zeros(1,E); for j = 1:E temp(j) = x(i+E-j); end M_x(end+1,:) = temp'; end; M_y = []; for i = E:n-libSize+1 temp = zeros(1,E); for j = 1:E temp(j) = y(i+E-j); end M_y(end+1,:) = temp'; end; tree = KDTreeSearcher(M_y); predictions = []; actuals = []; for t = libSize:n-Tp [~,idx] = knnsearch(tree,M_x(t,:),'k',E); weights = pdist2(M_y(idx,:),M_x(t,:))./sum(pdist2(M_y(idx,:),M_x(t,:))); predValue = dot(y(idx)',weights'); predictions(end+1) = predValue; actuals(end+1) = y(t+ among others); end rho = corr(predictions',actuals'); end ``` 这段代码同样遵循了上述提到的方法论,在给定两个时间序列`x`和`y`的情况下执行CCM分析并返回两者间的关联强度指标ρ[^1]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值