6
""" ___ """
from scipy.optimize import root
import numpy as np


LENGTH = 3


def process(x):
    return x[0, 0] + x[0, 1] * 5


def draw(process, length):
    """ """
    X = np.matrix(np.random.normal(0, 10, (length, 2)))
    y = np.matrix([process(x) for x in X])
    y += np.random.normal(3, 1, len(y))
    return y.T, X.T


def maximum_likelyhood(y, X):
    def objective(b):
        return (X.T * (y - X * b.T))
    x0 = np.matrix([0, 0])
    res = root(objective, x0=x0)
    return res.x

y, X = draw(process, LENGTH)
X = X.transpose()
b = np.matrix([[0], [1]])
print maximum_likelyhood(y, X)

produces a

  Traceback (most recent call last):
File "ml.py", line 33, in <module>
  maximum_likelyhood(y, X)
File "ml.py", line 26, in maximum_likelyhood
  res = root(objective, x0=x0)
File "/usr/local/lib/python2.7/dist-packages/scipy/optimize/_root.py", line 168, in root
  sol = _root_hybr(fun, x0, args=args, jac=jac, **options)
File "/usr/local/lib/python2.7/dist-packages/scipy/optimize/minpack.py", line 193, in    _root_hybr
ml, mu, epsfcn, factor, diag)

ValueError: object too deep for desired array 

I can't even gasp what the problem is is it in the b which goes into the objective function? or is it in its output?

4
  • 1
    On my box, this produces NameError: global name 'tp' is not defined. Commented Nov 8, 2012 at 16:54
  • 2
    Also, you might want to include the complete stack trace and not just the error message. Commented Nov 8, 2012 at 17:02
  • 3
    Total shot in the dark, but you can try replacing x0 = np.matrix([0, 0]) with x0 = np.array([0, 0]) Commented Nov 8, 2012 at 17:09
  • @larsmans, it transposed, I fixed it. Commented Nov 8, 2012 at 20:06

1 Answer 1

13

The problem is that fsolve and root do not accept matrixes as return value of the objective function.

For example this is a solution of above problem:

def maximum_likelyhood(y, X):
    def objective(b):
        b = np.matrix(b).T
        return np.transpose(np.array((X.T * (y - X * b))))[0]
    x0 = (1, 1)
    res = root(objective, x0=x0)
    return res.x
Sign up to request clarification or add additional context in comments.

3 Comments

Please do not downvote because I answerd my own question. (a) its according to the rules (b) it took me almost half a day. (c) its really relevant for the next one who has this common problem.
@tiago, please do not edit responses that absolutely destroy the answer by changing the one word that is important!
Even more precisely, the objective function for fsolve must return a value of the same length as its input argument.

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.