 # Analyze Financial Data with Python: Optimal Portfolios Function

I copied the random portfolios and optimal portfolio functions from https://www.codecademy.com/paths/finance-python/tracks/visualizing-finance-data/modules/portfolio-optimization/lessons/mv-portfolio-optimization/exercises/efficient-frontier-iii and pasted them into Jupyter Notebook for my capstone project.

My first issue with the optimal portfolio function is the 3rd line:

returns = np.transpose(returns.as_matrix())

I get the error: ‘DataFrame’ object has no attribute ‘as_matrix’

when I submit: weights, returns, risks = optimal_portfolio(monthly_returns[1:])

I was able to avoid this issue by alter the code to returns = np.transpose(returns.values) but I’m not sure if that’s correct.

My second error when I submit: weights, returns, risks = optimal_portfolio(monthly_returns[1:]) is:

ArithmeticError Traceback (most recent call last)
~\miniconda3\lib\site-packages\cvxopt\misc.py in factor(W, H, Df)
1428 if type(F[‘S’]) is matrix:
-> 1429 lapack.potrf(F[‘S’])
1430 else:

ArithmeticError: 2

During handling of the above exception, another exception occurred:

ArithmeticError Traceback (most recent call last)
~\miniconda3\lib\site-packages\cvxopt\coneprog.py in coneqp(P, q, G, h, dims, A, b, initvals, kktsolver, xnewcopy, xdot, xaxpy, xscal, ynewcopy, ydot, yaxpy, yscal, **kwargs)
2064 for rti in W[‘rti’]: rti[::rti.size+1 ] = 1.0
-> 2065 try: f = kktsolver(W)
2066 except ArithmeticError:

~\miniconda3\lib\site-packages\cvxopt\coneprog.py in kktsolver(W)
1980 def kktsolver(W):
-> 1981 return factor(W, P)
1982

~\miniconda3\lib\site-packages\cvxopt\misc.py in factor(W, H, Df)
1443 if type(F[‘S’]) is matrix:
-> 1444 lapack.potrf(F[‘S’])
1445 else:

ArithmeticError: 2

During handling of the above exception, another exception occurred:

ValueError Traceback (most recent call last)
in
----> 1 weights, returns, risks = optimal_portfolio(monthly_returns[1:])

in optimal_portfolio(returns)
45 b = opt.matrix(1.0)
46 #Calculate efficient frontier weights using quadratic programming
—> 47 portfolios = [solvers.qp(mu*S, -pbar, G, h, A, b)[‘x’] for mu in mus]
48 ##CALCULATE RISKS AND RETURNS FOR FRONTIER
49 returns = [blas.dot(pbar, x) for x in portfolios]

in (.0)
45 b = opt.matrix(1.0)
46 #Calculate efficient frontier weights using quadratic programming
—> 47 portfolios = [solvers.qp(mu*S, -pbar, G, h, A, b)[‘x’] for mu in mus]
48 ##CALCULATE RISKS AND RETURNS FOR FRONTIER
49 returns = [blas.dot(pbar, x) for x in portfolios]

~\miniconda3\lib\site-packages\cvxopt\coneprog.py in qp(P, q, G, h, A, b, solver, kktsolver, initvals, **kwargs)
4483 ‘residual as dual infeasibility certificate’: dinfres}
4484
-> 4485 return coneqp(P, q, G, h, None, A, b, initvals, kktsolver = kktsolver, options = options)

~\miniconda3\lib\site-packages\cvxopt\coneprog.py in coneqp(P, q, G, h, dims, A, b, initvals, kktsolver, xnewcopy, xdot, xaxpy, xscal, ynewcopy, ydot, yaxpy, yscal, **kwargs)
2065 try: f = kktsolver(W)
2066 except ArithmeticError:
-> 2067 raise ValueError(“Rank(A) < p or Rank([P; A; G]) < n”)
2068
2069

ValueError: Rank(A) < p or Rank([P; A; G]) < n

What am I doing wrong?

Hey, Basically the as_matrix() method depreciated, i saw this on another post about this:

• Basically just replace the as_matrix() with to_numpy()

“”"
Please refer to the documentation of pandas.DataFrame.as_matrix.
“Deprecated since version 0.23.0”
https://pandas.pydata.org/pandas-docs/version/0.25.1/reference/api/pandas.DataFrame.as_matrix.html
As suggested in pandas documentation, you may consider using pandas.DataFrame.to_numpy instead.
https://pandas.pydata.org/pandas-docs/version/0.25.1/reference/api/pandas.DataFrame.to_numpy.html#pandas.DataFrame.to_numpy
“”"