2013-11-21 20 views

Trả lời

4

Theo ước tính tiêu chuẩn của hệ số autocovariance cho tín hiệu rời rạc, mà có thể được biểu diễn theo phương trình:

enter image description here

... nơi x(i) là một tín hiệu nhất định (ví dụ cụ thể vector 1D), k tượng trưng cho sự thay đổi của x(i) tín hiệu bằng k mẫu, N là chiều dài của x(i) tín hiệu, và:

enter image description here

... đó là trung bình đơn giản, chúng ta có thể viết:

''' 
Calculate the autocovarriance coefficient. 
''' 

import numpy as np 

Xi = np.array([1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5]) 
N = np.size(Xi) 
k = 5 
Xs = np.average(Xi) 

def autocovariance(Xi, N, k, Xs): 
    autoCov = 0 
    for i in np.arange(0, N-k): 
     autoCov += ((Xi[i+k])-Xs)*(Xi[i]-Xs) 
    return (1/(N-1))*autoCov 

print("Autocovariance:", autocovariance(Xi, N, k, Xs)) 

Nếu bạn muốn chuẩn hóa hệ số autocovariance, mà sẽ trở thành hệ số tự tương quan thể hiện dưới dạng:

enter image description here

... hơn bạn chỉ cần thêm vào đoạn code trên chỉ là hai dòng bổ sung:

def autocorrelation(): 
    return autocovariance(Xi, N, k, Xs)/autocovariance(Xi, N, 0, Xs) 

Đây là kịch bản đầy đủ:

''' 
Calculate the autocovarriance and autocorrelation coefficients. 
''' 

import numpy as np 

Xi = np.array([1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5]) 
N = np.size(Xi) 
k = 5 
Xs = np.average(Xi) 

def autocovariance(Xi, N, k, Xs): 
    autoCov = 0 
    for i in np.arange(0, N-k): 
     autoCov += ((Xi[i+k])-Xs)*(Xi[i]-Xs) 
    return (1/(N-1))*autoCov 

def autocorrelation(): 
    return autocovariance(Xi, N, k, Xs)/autocovariance(Xi, N, 0, Xs) 

print("Autocovariance:", autocovariance(Xi, N, k, Xs)) 
print("Autocorrelation:", autocorrelation()) 
+0

Numpy đã có mọi thứ cần thiết để tính [tương quan] (https://docs.scipy.org/doc/numpy/reference/generated/numpy.correlate.html). (Mà thậm chí có thể được tăng tốc với [scipy.signal.fftconvolve] (http://docs.scipy.org/doc/scipy-0.15.1/reference/generated/scipy.signal.fftconvolve.html).) Chỉ sau đó cần được nhân với [phương sai] (http://docs.scipy.org/doc/numpy/reference/generated/numpy.var.html) để có được tính tự động. – Celelibi

1

Nhận mẫu tự hiệp phương sai:

# cov_auto_samp(X,delta)/cov_auto_samp(X,0) = auto correlation 
def cov_auto_samp(X,delta): 
    N = len(X) 
    Xs = np.average(X) 
    autoCov = 0.0 
    times = 0.0 
    for i in np.arange(0, N-delta): 
     autoCov += (X[i+delta]-Xs)*(X[i]-Xs) 
     times +=1 
    return autoCov/times 
0

Một tinh chỉnh nhỏ cho các câu trả lời trước, tránh các vòng python for và sử dụng các hoạt động mảng numpy thay thế. Điều này sẽ nhanh hơn nếu bạn có nhiều dữ liệu.

def lagged_auto_cov(Xi,t): 
    """ 
    for series of values x_i, length N, compute empirical auto-cov with lag t 
    defined: 1/(N-1) * \sum_{i=0}^{N-t} (x_i - x_s) * (x_{i+t} - x_s) 
    """ 
    N = len(time_series) 

    # use sample mean estimate from whole series 
    Xs = np.mean(Xi) 

    # construct copies of series shifted relative to each other, 
    # with mean subtracted from values 
    end_padded_series = np.zeros(N+t) 
    end_padded_series[:N] = Xi - Xs 
    start_padded_series = np.zeros(N+t) 
    start_padded_series[t:] = Xi - Xs 

    auto_cov = 1./(N-1) * np.sum(start_padded_series*end_padded_series) 
    return auto_cov 

So sánh điều này chống lại các mã @bluevoxel 's, sử dụng một chuỗi thời gian của 50.000 điểm dữ liệu và tính toán tự động tương quan cho một giá trị cố định duy nhất của lag, mã vòng lặp python for trung bình khoảng 30 milli-giây và sử dụng các mảng có dải màu trung bình nhanh hơn 0,3 milli giây (chạy trên máy tính xách tay của tôi).

Các vấn đề liên quan