You are on page 1of 1

File: /home/henry/Desktop/research/getCovariantMatrix.

py

Page 1 of 1

#!/usr/local/bin/python
#Fitting a data set to a exponential a*exp(b)+c
import math
import random
from iminuit import Minuit
import numpy as np
from numpy.linalg import inv
# definition of CovN(nt, nt_prime) = < (C(nt) - <C(nt)>_N) (C(nt_prime) - <C(nt_prime)>_N)>
# C(nt)=ct and C(nt_prime) = ctp
# arrays passed must be the same size, this is same N, but the may have different size in t.
#this gives the covariance of the vectors, and returns a number
#tested and working
def covN(ct,ctp):
covN = float(0)
N = len(ct)
ct_average = np.average(ct)
ctp_average = np.average(ctp)
temp = (ct-ct_average)*(ctp-ctp_average)
covN = np.average(temp)/(N-1.0)
return float(covN)
#get the covariant matrix C_ij of a data matrix ct[configurations,time],that is the correlation between
#different time elements, where the elements C_ij = CovN(nt, nt_prime)
#index order ct[N][nt] and ctp[N][nt_prime]
#a should be covM numpy array
def covMatrix(ct, covM, isCorrelated):
m = len(ct[0,:])
n = m
for x in range(0,m):
if isCorrelated:
for y in range(0,n):
covM[x,y]= covN(ct[:,x],ct[:,y])
else:
covM[x,x]= covN(ct[:,x],ct[:,x])

You might also like