You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Python结合Lasso的VAR模型恢复y对x1/x2的延迟响应

Solution: Recover Lag Relationships with Lasso-Constrained Regression (Focused on y)

Got it, let's break this down. Since we only care about modeling y as a function of lagged x1 and x2 (ignoring autoregressive terms for x1/x2 and cross-effects between y and the other variables beyond their lags), we can frame this as a sparse linear regression problem using Lasso regularization. Lasso will automatically zero out coefficients for irrelevant lags, leaving only the meaningful 24-step (x1→y) and 48-step (x2→y) delays.

Step 1: Prepare Lagged Feature Matrix

First, we need to create a feature matrix where each row contains lagged values of x1 and x2 up to a maximum lag (we'll pick 60 to cover the 48-step delay we expect). We'll discard the first max_lag time points since they don't have enough lagged data to form features.

Step 2: Train Lasso Regression Model

We'll use Lasso to fit the model, tuning the regularization strength (alpha) to ensure only the true lag coefficients remain non-zero.

Step 3: Analyze Results

We'll visualize the coefficients to identify which lags have non-zero weights—these should correspond to the 24-step lag for x1 and 48-step lag for x2.


Full Implementation Code

import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import Lasso
from sklearn.preprocessing import StandardScaler

# --- Reuse the original signal/response code ---
def s1(t, delay):
    """An example of a noisy signal (heaviside function)"""
    return (t > delay).astype(int) + np.random.normal(scale=0.01, size=t.shape)
def s2(t, delay):
    """An example of another noisy signal (delayed sin)"""
    return np.sin(2*np.pi*(t - delay)/36) * (t > delay).astype(int) + np.random.normal(scale=0.01, size=t.shape)
def response(signal, delay):
    """ An example of a noisy delayed response (delayed identity function) """
    delayed_signal = np.append(np.zeros(shape=delay), signal[:-delay])
    return delayed_signal + np.random.normal(scale=0.01, size=signal.shape)

t = np.arange(0, 256, 1)
x1 = s1(t, delay=12)
x2 = s2(t, delay=36)
y = response(x1, delay=24) + response(x2, delay=48)

# --- Step 1: Build lagged feature matrix ---
max_lag = 60  # Choose a value larger than the maximum expected lag (48)
n_samples = len(t) - max_lag

# Initialize feature matrix: each row has [x1(t-max_lag), ..., x1(t-1), x2(t-max_lag), ..., x2(t-1)]
X = np.zeros((n_samples, 2 * max_lag))
y_target = y[max_lag:]  # Target values start after max_lag

for i in range(n_samples):
    # Lags for x1: from t = max_lag+i - max_lag to max_lag+i -1
    X[i, :max_lag] = x1[i : i + max_lag]
    # Lags for x2: same time window
    X[i, max_lag:] = x2[i : i + max_lag]

# Standardize features (critical for Lasso, since it's sensitive to scale)
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)

# --- Step 2: Train Lasso model ---
# Tune alpha: we want enough regularization to zero out irrelevant lags, but not too much to eliminate true signals
lasso = Lasso(alpha=0.005, fit_intercept=True, max_iter=10000)
lasso.fit(X_scaled, y_target)

# --- Step 3: Analyze coefficients ---
coef_x1 = lasso.coef_[:max_lag]
coef_x2 = lasso.coef_[max_lag:]

# Plot coefficients for x1 lags
plt.figure(figsize=(12, 8))
plt.subplot(211)
plt.stem(np.arange(1, max_lag+1), coef_x1)
plt.title('Lasso Coefficients for Lagged x1 (Predicting y)')
plt.xlabel('Lag Step (k: y(t) ~ x1(t-k))')
plt.ylabel('Coefficient Value')
plt.axvline(x=24, color='r', linestyle='--', label='True Lag (24)')
plt.legend()

# Plot coefficients for x2 lags
plt.subplot(212)
plt.stem(np.arange(1, max_lag+1), coef_x2)
plt.title('Lasso Coefficients for Lagged x2 (Predicting y)')
plt.xlabel('Lag Step (k: y(t) ~ x2(t-k))')
plt.ylabel('Coefficient Value')
plt.axvline(x=48, color='r', linestyle='--', label='True Lag (48)')
plt.legend()

plt.tight_layout()
plt.show()

# Print the top non-zero coefficients to confirm
print("Top non-zero coefficients for x1:")
top_x1_lags = np.where(np.abs(coef_x1) > 1e-3)[0] + 1  # +1 because lag starts at 1
for lag in top_x1_lags:
    print(f"Lag {lag}: {coef_x1[lag-1]:.4f}")

print("\nTop non-zero coefficients for x2:")
top_x2_lags = np.where(np.abs(coef_x2) > 1e-3)[0] + 1
for lag in top_x2_lags:
    print(f"Lag {lag}: {coef_x2[lag-1]:.4f}")

Key Notes:

  • Standardization: We scale the features because Lasso penalizes coefficient magnitudes directly—without scaling, variables with larger variance would dominate the regularization.
  • Alpha Tuning: The alpha value may need minor adjustment based on noise level. If you see too many non-zero coefficients, increase alpha; if the true lag coefficients are zeroed out, decrease alpha.
  • Why Not Full VAR?: A standard VAR models all variables (x1, x2, y) as functions of their own lags and each other's lags. Since we only care about predicting y from x1/x2 lags, a targeted sparse regression is more efficient and aligns with your requirement to ignore other relationships.

When you run this code, you'll see that the Lasso coefficients are nearly zero everywhere except at lag 24 (for x1) and lag 48 (for x2), exactly matching the true delay relationships we built into the data.

内容的提问来源于stack exchange,提问作者Jorge Leitao

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 04:16:20