6

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

2
  • 1
    A = A.__add__(B) != A = A.__iadd__(B) if A is mutable Commented Jun 6, 2012 at 3:50
  • It's also kind of weird to have a variable in the scope of your function with the same name as the function. Commented Jun 6, 2012 at 6:47

1 Answer 1

7

The culprit is:

variance = sigma

If you change that to:

variance = sigma.copy()

You'll see the correct result.

This is because += actually performs a (more efficient) in-place addition… And since both variance and sigma reference the same array, both will be updated. For example:

>>> sigma = np.array([1])
>>> variance = sigma
>>> variance += 3
>>> sigma
array([4])
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.