I'm getting a very odd error using a basic shortcut method in python. It seems, unless I'm being very stupid, I get different values for A = A + B, and A += B. Here is my code:
def variance(phi,sigma,numberOfIterations):
variance = sigma
for k in range(1,numberOfIterations):
phik = np.linalg.matrix_power(phi,k)
variance = variance + phik*sigma*phik.T
return variance
This basically just calculates the covariance of a vector autoregression. So for:
phi = np.matrix('0.7 0.2 -0.1; 0.001 0.8 0.1; 0.001 0.002 0.9')
sigma = np.matrix('0.07 0.01 0.001; 0.01 0.05 0.004; 0.001 0.004 0.01')
I get:
variance(phi,sigma,10) =
[[ 0.1825225 0.07054728 0.00430524]
[ 0.07054728 0.14837229 0.02659357]
[ 0.00430524 0.02659357 0.04657858]]
This is correct I believe (agrees with Matlab). Now if I change the line above to
variance += phik*sigma*(phik.T)
I get:
variance(phi,sigma,10) =
[[ 0.34537165 0.20258329 0.04365378]
[ 0.20258329 0.33471052 0.1529369 ]
[ 0.04365378 0.1529369 0.19684553]]
Whats going on?
Many thanks
Dan
A = A.__add__(B)!=A = A.__iadd__(B)if A is mutable