Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python numpy addition error

Tags:

python

numpy

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

like image 629
Dan Avatar asked Jun 06 '12 03:06

Dan


People also ask

How do I sum two arrays in NumPy?

To add the two arrays together, we will use the numpy. add(arr1,arr2) method. In order to use this method, you have to make sure that the two arrays have the same length. If the lengths of the two arrays are​ not the same, then broadcast the size of the shorter array by adding zero's at extra indexes.

How do I add items to a NumPy array?

Use append() to add an element to Numpy Array. Use concatenate() to add an element to Numpy Array. Use insert() to add an element to Numpy Array.

How do I fix NumPy Ndarray object has no attribute append?

The numpy. ndarray does not have an append method, and hence it throws AttributeError. We can resolve this error by using the numpy. append() method provided by the NumPy library.

Can you append to a NumPy array?

Append values to the end of an array. Values are appended to a copy of this array. These values are appended to a copy of arr.


1 Answers

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])
like image 185
David Wolever Avatar answered Nov 07 '22 11:11

David Wolever