cvxgrp / cov_pred_finance

Apache License 2.0
54 stars 7 forks source link

Calling `cov_pred_finance` from `cvx_portfolio`? #83

Open enzbus opened 1 year ago

enzbus commented 1 year ago

Hello @tschm and @kasperjo. I've been looking at this repository and your paper and I think it would be easy to write a class that wraps the functionality you provide to integrate in cvxportfolio. In the simplest case it would be something like

class CvxCovariance(BaseForecast):
    def __init__(self, **all_your_hyperparameters):
        # save the hyperparameters
        ...
    def _pre_evaluation(self, universe, *args):
        # initialize your objects knowing the size of the universe
        ...
    def _values_in_time(self, past_returns, **kwargs):
        # do the computation for covariance at time t knowing past returns (up to time t-1)
        ...
        return sqrt_Sigma  # so that Sigma = sqrt_Sigma.T @ sqrt_Sigma

It would be invoked simply by the user and operate completely independently, so the user doesn't have to worry about passing data in the right format, .... (See the examples to see what I mean.) All the cvxpy operations are done from cvxportfolio, and it also takes care of adding and removing assets from the universe as IPOs and bankruptcies happen.

I'm not sure if your method is safe against missing values (past_returns could have nan at the top of some of the columns) but if not you can just throw an error if the user provides such data.

This could even just be in your examples (or cvxportfolio's) and not in any package proper, to not change dependencies.

If you want I can help you to interface with some more of the more advanced facilities, like multi-period forecasting (provide forecast at time t for covariance at time t+tau), on- and off-line caching (which saves on disk expensive computation, safely against parallel execution), ....

tschm commented 1 year ago

@enzbus Thank you for that.

Dealing with NaNs is an interesting question. I don't want to throw an error if an asset joins later. At the same time I don't want to interpolate its missing history with zero returns (in particular before an asset awakes)

Also, I want to avoid dictating how we treat missing values. It's the responsibility of the user to submit good estimates and make the combination exercise not falling over if a covariance matrix contains a NaN row and a NaN column (enough to check the diagonal). This is simple with pandas.

I doubt it would be a good idea to create a dependency on cvxportfolio within cov_pred_finance (BaseForecast?). We could add a simple iterator (over time) combining covariance matrices and yielding an estimate for each time t. It should be easy to use this within cvxportfolio.

stephenpboyd commented 1 year ago

For sure cov_pred_finance should have no dependency on cvxportfolio. It of course would be used with it, but it cannot depend on it.

On Sat, Jul 15, 2023 at 8:33 PM Thomas Schmelzer @.***> wrote:

@enzbus https://github.com/enzbus Thank you for that.

Dealing with NaNs is an interesting question. I don't want to throw an error if an asset joins later. At the same time I don't want to interpolate its missing history with zero returns (in particular before an asset awakes)

Also, I want to avoid dictating how we treat missing values. It's the responsibility of the user to submit good estimates and make the combination exercise not falling over if a covariance matrix contains a NaN row and a NaN column (enough to check the diagonal). This is simple with pandas.

I doubt it would be a good idea to create a dependency on cvxportfolio within cov_pred_finance (BaseForecast?). We could add a simple iterator (over time) combining covariance matrices and yielding an estimate for each time t. It should be easy to use this within cvxportfolio.

— Reply to this email directly, view it on GitHub https://github.com/cvxgrp/cov_pred_finance/issues/83#issuecomment-1636971003, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAT4UNVWVURMWMGTS64MJTDXQNOJHANCNFSM6AAAAAA2LIQB5Y . You are receiving this because you are subscribed to this thread.Message ID: @.***>

enzbus commented 11 months ago

Bumping this; in the meantime cvxportfolio's interface has got cleaner, but the code snippet from above is still about right. Do you think it's possible to provide the functionality of cvxcovariance following that API? (See https://github.com/cvxgrp/cvxportfolio/blob/master/examples/user_provided_forecasters.py for a full example.) Thanks @tschm @kasperjo .