-1

All, I've looked at the following answer and it all looks good. Matrix multiplication, solve Ax = b solve for x I must be doing something wrong though if I work out the problem by hand I'm getting a different solution.

Method 1:

A = [[0,1,0],
     [0,0,1],
     [.5,.5,0]]
b = [1,1,1]
x = numpy.linalg.lstsq(A,b)
x

yields

(array([ 1.,  1.,  1.]),
 array([], dtype=float64),
 3,
 array([ 1.14412281,  1.        ,  0.43701602]))

Method 2 (As suggested by Kevin below I transposed the matrix):

A = [[0,0,.5],[1,0,.5],[0,1,0]]
b = [1,1,1]
x = numpy.linalg.lstsq(A,b)
x

yields

(array([ 0.,  1.,  2.]),
 array([], dtype=float64),
 3,
 array([ 1.14412281,  1.        ,  0.43701602]))

If I work out Ax=b by hand I get x = 1/5*[1,2,2]. Note that I am working example 11.19 from the link below:
https://www.probabilitycourse.com/chapter11/11_3_2_stationary_and_limiting_distributions.php

What am I missing?

If I follow the example on this link: https://docs.scipy.org/doc/numpy/reference/generated/numpy.linalg.solve.html

3x+y=9
x+2y=8
[x,y]=[2,3]

I do get the right solution if I do it with python or by hand. Any pointers? I must be missing something very simple here

Community
  • 1
  • 1
  • if you're using python and numpy, you may want to add those tags to the question so that people will see it. – Heretic Monkey Oct 27 '16 at 18:21
  • 1
    Your by-hand math is wrong. You're going in the wrong directions along A when you multiply. – user2357112 Oct 27 '16 at 18:24
  • I'm actually following example 11.19 from the link below to compute the stationary distribution. Admittedly I'm computing P*pi = pi and I am getting the correct answer by hand. If I try to reverse the computation as follows I get [0,1,2] which again is not right. Where is my issue exactly? x = np.dot(b,np.linalg.inv(A)) https://www.probabilitycourse.com/chapter11/11_3_2_stationary_and_limiting_distributions.php – fullofquestions Oct 27 '16 at 18:46

1 Answers1

1

I think when you're doing it by hand you're mixing up rows and columns.

A = [0, 1, 0]
    [0, 0, 1]
    [.5, .5, 0] 

yields x = [1, 1, 1]. However,

A = [0, 0, .5]
    [1, 0, .5]
    [0, 1, 0] 

yields x = 1/2[1, 2, 2] as you got when you did it by hand.

  • Thank you. I'm following you here and basically fixing my A to your last one and running x = numpy.linalg.lstsq(A,b) Out[14]: (array([ 0., 1., 2.]), array([], dtype=float64), 3, array([ 1.14412281, 1. , 0.43701602])) which I don't believe is [1,2,2] . What did you do to solve this exactly? – fullofquestions Oct 27 '16 at 18:53
  • I just did them each by hand. "x" is really a vector, which means it has values [x1, x2, x3]. For convenience though, I will use x = [a, b, c]. – Devin White Oct 27 '16 at 19:03
  • So for the correct matrix, changing Ax = b to: 0*a + b + 0*c = 1, 0*a + 0*b + c = 1, a/2 + b/2 + 0*c = 1 then it becomes clear that b = 1 and c = 1. Using that information, a/2 + 1/2 = 1 yields a = 1. – Devin White Oct 27 '16 at 19:10
  • You are correct. I need to revisit my linear algebra because I'm not setting up the calculation to be correct. – fullofquestions Oct 27 '16 at 19:18