from pypfopt.efficient_frontier import EfficientFrontier
from pypfopt import risk_models
from pypfopt import expected_returns

# Read in price data
df = pd.read_csv("Asset_Regime.csv", parse_dates=True, index_col="date")

# Calculate expected returns and sample covariance
mu = expected_returns.mean_historical_return(df)
S = risk_models.sample_cov(df)

# Optimise for maximal Sharpe ratio
ef = EfficientFrontier(mu, S)
raw_weights = ef.max_sharpe()
cleaned_weights = ef.clean_weights()

My data looks like

US Equities World Equities  US Treasuries   High Yield  Corp Bonds  Real Estate Commodities
Jan-85  0.081301    0.028511    0.031500    0.037908    0.048963    0.056605    0.021351
Feb-85  0.030075    -0.009204   -0.044692   0.012689    -0.042029   0.016448    -0.015217
Mar-85  -0.007299   0.075134    0.028719    0.004323    0.032666    -0.006716   0.037171
Apr-85  -0.012255   -0.002459   0.023084    0.018215    0.037125    0.000906    -0.035116
May-85  0.064516    0.040245    0.086780    0.042363    0.104199    0.027241    0.004351

and returns

{'US Equities': nan, 'World Equities': nan, 'US Treasuries': nan, 'High Yield': nan, 'Corp Bonds': nan, 'Real Estate': nan, 'Commodities': nan}
Expected annual return: nan%
Annual volatility: nan%
Sharpe Ratio: nan
(nan, nan, nan)

  • 2
    $\begingroup$ If you do data.desribe() what do you get? You should solve this for yourself in excel, then match in back to python. $\endgroup$
    – jason m
    Nov 6, 2019 at 0:03

1 Answer 1


it might be

  1. the data still contains non-numerical elements such as left-over headers, titles or dates
  2. missing, nan, imaginary or incoherent values in the data file
  3. non-corresponding lengths of time series/columns that do have legible elements
  4. incomputable mishaps similar to division by zero that are causing the nans
  5. negative values, such as negative volatility estimates or transformations thereof, that the theory/formulas/code cannot cope with
  6. the user is using a pre-made package without looking at its code and seeing what it's really doing instead of writing their own script

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.