[MATRIX-SIG] Calculating correlation Matrix

Hoon Yoon - IPT Quant hyoon@nyptsrv1.etsd.ml.com
Tue, 15 Jul 1997 21:51:13 -0400


Hello,

  The following should be fairly simple, but I am having problems
  with it for some reason. I am attempting to calulate correlation
  matrix of 2 D returns, but I cannot get a identitity diagnonal
  using following code. I been messing around it for quite a while,
  but can't figure out why meanc fails to begin with. It shoudl have
  something to do with 2 D matricies I am used to vs more advanced
  n-D in NumPy. I thought I made approapriate adj, but still doesn't
  work.
  Also, if anyone know of good stat func for NumPy, I would really
  appreciate the source.
Thanks much in advance,

Hoon,
(the Baffled)

* none of these works for complex number, because I don't use them.
def vcm(x):
	# var/cov matrix from a data matrix
	return moment(x-transpose(meanc(x))) / (len(x)-1)

def moment(y):
	return dot(transpose(y),y)

def meanc(x):
	return up_1d(add.reduce(x) / len(x))

def varianc(x):
	return up_1d((add.reduce((x-meanc(x))**2)/(len(x)-1)) ** .5)

def stdc(x):
	var = varianc(x)
	return up_1d(var ** .5)

def corrx(x):
	vc  = vcm(x)
	std = up_1d(diagonal(vc))
	return dot(vc,(1/dot(std, transpose(std))))

def up_1d(x):
	if len(x.shape) == 1:
		return reshape(x, (len(x),1))
	else:
		return x


ln_gold = log(gold_a[1:,:]/gold_a[:-1,:])
corr    = corrx(ln_gold)

_______________
MATRIX-SIG  - SIG on Matrix Math for Python

send messages to: matrix-sig@python.org
administrivia to: matrix-sig-request@python.org
_______________