Giter VIP home page Giter VIP logo

Comments (12)

dcajasn avatar dcajasn commented on May 25, 2024

Hi @rserranon your data is the problem, your dataframe Y has strings values instead of floats.

from riskfolio-lib.

rserranon avatar rserranon commented on May 25, 2024

Thanks @dcajasn , but my Y seems to be fine.

display(Y)
print(Y.dtypes)
print(Y.index.dtype)

image

DBC     float64
PHYS    float64
UVXY    float64
HYG     float64
VYM     float64
BTC     float64
dtype: object

datetime64[ns]

Actually, when I run an optimization without constraints everything works fine.

method_mu='hist' # Method to estimate expected returns based on historical data.
method_cov='hist' # Method to estimate covariance matrix based on historical data.

port.assets_stats(
    method_mu=method_mu, 
    method_cov=method_cov, 
    d=0.94
)

model='Classic'
rm = 'MV'
obj = 'Sharpe'
hist = True
rf = 0
l = 0

w = port.optimization(
    model=model, 
    rm=rm, 
    obj=obj, 
    rf=rf, 
    l=l, 
    hist=hist
)

display(w.T)

image

from riskfolio-lib.

dcajasn avatar dcajasn commented on May 25, 2024

Try Y.cov() and see if there is nan or inf values

from riskfolio-lib.

rserranon avatar rserranon commented on May 25, 2024
print(Y.cov())
           DBC      PHYS      UVXY       HYG       VYM       BTC
DBC   0.000137  0.000025 -0.000294  0.000025  0.000051  0.000026
PHYS  0.000025  0.000084  0.000010  0.000008  0.000005  0.000023
UVXY -0.000294  0.000010  0.005590 -0.000248 -0.000569 -0.000312
HYG   0.000025  0.000008 -0.000248  0.000034  0.000049  0.000022
VYM   0.000051  0.000005 -0.000569  0.000049  0.000124  0.000046
BTC   0.000026  0.000023 -0.000312  0.000022  0.000046  0.000922

from riskfolio-lib.

dcajasn avatar dcajasn commented on May 25, 2024

The error comes from scipy sqrtm function, try:

from scipy.linalg import sqrtm
G = sqrtm(Y.cov().to_numpy())

from riskfolio-lib.

rserranon avatar rserranon commented on May 25, 2024

Thanks @dcajasn, if I do.

from scipy.linalg import sqrtm
G = sqrtm(Y.cov().to_numpy())
print(G)

I get this:

[[ 0.01102919  0.00125662 -0.00330654  0.00075003  0.00139286  0.00035808]
 [ 0.00125662  0.00906535  0.00022052  0.0005299   0.00018038  0.00057842]
 [-0.00330654  0.00022052  0.07427423 -0.00292332 -0.006729   -0.00292199]
 [ 0.00075003  0.0005299  -0.00292332  0.00450864  0.00214881  0.00033992]
 [ 0.00139286  0.00018038 -0.006729    0.00214881  0.00844002  0.00065165]
 [ 0.00035808  0.00057842 -0.00292199  0.00033992  0.00065165  0.03021191]]

from riskfolio-lib.

dcajasn avatar dcajasn commented on May 25, 2024

Hi @rserranon, you are using an old version of riskfolio-lib, update to latest version.

from riskfolio-lib.

rserranon avatar rserranon commented on May 25, 2024

Thanks, yes it seems to be a problem with openbb_terminal.sdk that installs/require Riskfolio-Lib 3.3.0, unfortunately, if I upgrade to Riskfolio-Lib 4.1.1 (latest) opebb SDK doesn't work. I will create a separate environment and make my test with Riskfolio-Lib 4.4.1 and update this issue.

from riskfolio-lib.

dcajasn avatar dcajasn commented on May 25, 2024

Oh that was the problem. I worked on openbb just two months in 2022 to add the Riskfolio-Lib functions into the terminal. However, since version 4.0.0 Riskfolio-lib require additional files to build a c++ extension, that could be generating conflicts with openbb sdk.

from riskfolio-lib.

dcajasn avatar dcajasn commented on May 25, 2024

I'm going to close the issue because it is an openbb issue not a Riskfolio-Lib issue

from riskfolio-lib.

rserranon avatar rserranon commented on May 25, 2024

I'm getting the same error with Riskfolio-Lib 4.1.1

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[7], line 11
      8 obj = 'Sharpe'
      9 rf = 0
---> 11 w = port.optimization(
     12     model=model,
     13     rm=rm,
     14     obj=obj,
     15     rf=rf
     16 )
     18 display(w.T)

File ~/Riskfolio-test/venv/lib/python3.11/site-packages/riskfolio/src/Portfolio.py:1316, in Portfolio.optimization(self, model, rm, obj, kelly, rf, l, hist)
   1313 # MV Model Variables
   1315 g = cp.Variable(nonneg=True)
-> 1316 G = sqrtm(sigma)
   1317 risk1 = g**2
   1318 devconstraints = [cp.SOC(g, G.T @ w)]

File ~/Riskfolio-test/venv/lib/python3.11/site-packages/scipy/linalg/_matfuncs_sqrtm.py:167, in sqrtm(A, disp, blocksize)
    118 """
    119 Matrix square root.
    120 
   (...)
    164 
    165 """
    166 byte_size = np.asarray(A).dtype.itemsize
--> 167 A = _asarray_validated(A, check_finite=True, as_inexact=True)
    168 if len(A.shape) != 2:
    169     raise ValueError("Non-matrix input to matrix function.")

File ~/Riskfolio-test/venv/lib/python3.11/site-packages/scipy/_lib/_util.py:255, in _asarray_validated(a, check_finite, sparse_ok, objects_ok, mask_ok, as_inexact)
    253 if not objects_ok:
    254     if a.dtype is np.dtype('O'):
--> 255         raise ValueError('object arrays are not supported')
    256 if as_inexact:
    257     if not np.issubdtype(a.dtype, np.inexact):

ValueError: object arrays are not supported

from riskfolio-lib.

dcajasn avatar dcajasn commented on May 25, 2024

Hi @rserranon as I told you this is not a Riskfolio-Lib issue, it's a problem with your environment, the data your are using or incompabilities with non oficial libraries like openbb sdk. I only recommend to use Riskfolio-Lib with scientific Python distributions like anaconda or winpython. If you run tutorial 1 on google colab, there isn't a problem with constraints.

from riskfolio-lib.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.