# Calculating the covariance between 1-D arrays for incorporation into propagation of uncertainty

I have four 1-D arrays of dependent variables. They contain hundreds of data points but I have cropped them to 20 in this example. Each point represents a grid cell on a map.

# Using Python:
import numpy as np

A=np.asarray([0.195, 0.154, 0.208, 0.22, 0.204, 0.175, 0.184, 0.187, 0.171, 0.2, 0.222, 0.235, 0.206, 0.215, 0.222, 0.252, 0.269, 0.251, 0.285, 0.28])
B=np.asarray([0.119, 0.134, 0.132, 0.121, 0.11, 0.097, 0.13, 0.106, 0.103, 0.139, 0.124, 0.147, 0.152, 0.123, 0.177, 0.172, 0.18, 0.182, 0.197, 0.193])
C=np.asarray([0.11, 0.1, 0.103, 0.111, 0.105, 0.098, 0.099, 0.093, 0.105, 0.099, 0.113, 0.093, 0.104, 0.095, 0.099, 0.105, 0.108, 0.128, 0.125, 0.118])
D=np.asarray([-0.015, -0.015, -0.007, -0.02, 0.002, 0.009, 0.019, 0.0, -0.02, -0.001, -0.006, -0.015, -0.03, -0.036, -0.051, -0.058, -0.065, -0.081, -0.082, -0.055])


For the sake of example let's say these 4 variables are 'amount of sunlight', 'amount of rain', 'soil porosity' and 'altitude'. Each time I measure each variable there is an associated measurement error, which I record in the arrays:

A_err=np.asarray([ 0.016,  0.015,  0.017,  0.016,  0.015,  0.016,  0.016,  0.018, 0.015,  0.014,  0.015,  0.016,  0.017,  0.016,  0.017,  0.017, 0.017,  0.017,  0.017,  0.017])
B_err=np.asarray([ 0.045,  0.049,  0.039,  0.044,  0.036,  0.027,  0.032,  0.033, 0.029,  0.036,  0.032,  0.027,  0.04 ,  0.022,  0.034,  0.026, 0.021,  0.028,  0.035,  0.028])
C_err=np.zeros(20)+0.7
D_err=np.zeros(20)+0.9


Y, let's call this 'plant growth rate', is the sum of A, B, C and D:

Y=A+B+C+D


Clearly Y will have a different value in each of the 20 grid cells. What I am trying to calculate is the error on each Y measurement. Initially I was calculating this simply as:

Y_err=np.sqrt(A_err**2 + B_err**2 + C_err**2 + D_err**2)


But this includes no covariance terms and I believe I need them since A, B, C and D are not independent (one affects the other). Since I do not have expressions for the variables A, B, C and D in terms of one another, the only way I can see to calculate the covariances is to use the correlation-covariance matrix:

X = np.vstack([A,B,C,D])
C = np.cov(X)
print(C)
[[  1.31819737e-03   9.52921053e-04   2.17881579e-04  -8.58197368e-04]
[  9.52921053e-04   9.87252632e-04   1.69478947e-04  -7.97089474e-04]
[  2.17881579e-04   1.69478947e-04   9.47868421e-05  -1.88007895e-04]
[ -8.58197368e-04  -7.97089474e-04  -1.88007895e-04   8.85081579e-04]]


I am unclear as to whether I can then simply add each of the six non-diagonal matrix terms:

σ_AB=9.52921053e-04
σ_AC=2.17881579e-04

Y_err=np.sqrt(A_err**2 + B_err**2 + C_err**2 + D_err**2 + 2*σ_AB + 2*σ_AC + 2*σ_AD + 2*σ_BC + 2*σ_BD + 2*σ_CD)