error in optimization
Closed this issue · 3 comments
[100%%*] 10 of 10 completed
w = port.optimization(
^^^^^^^^^^^^^^^^^^
File "/Users/gaetano/miniconda3/envs/python311/lib/python3.11/site-packages/riskfolio/src/Portfolio.py", line 2623, in optimization
objective = cp.Minimize(risk * 1000 + penalty_factor * 1000)
^^^^
UnboundLocalError: cannot access local variable 'risk' where it is not associated with a value
Hi @c-vision,
Can you send a reproducible code in the same format of Riskfolio-Lib examples?
Best,
Dany
import riskfolio as rp
import yfinance as yf
assets = ["PANW", "NVDA", "AAPL", "MSFT", "GOOG", "TSLA", "AB", "DIS", "AXP", "^GSPC"]
data = yf.download(assets, start="2018-01-01", end="2024-05-05")
data = data.loc[:, "Adj Close"]
returns = data.pct_change().dropna()
returns_bench = returns.pop("^GSPC").to_frame()
port = rp.Portfolio(returns)
port.assets_stats(
method_mu="hist",
method_cov="hist",
)
port.benchindex = returns_bench
Max Sharpe Portfolio
w = port.optimization(
model="Classic",
rm="CVar",
obj="Sharpe",
hist=True,
rf=0,
l=0,
)
print(w)
ax = rp.plot_pie(
w=w,
others=0.05,
nrow=25,
height=6,
width=10,
ax=None
)
rp.plot_series(returns=returns, w=w)
rp.plot_drawdown(returns=returns, w=w)
rp.plot_table(returns=returns, w=w)
requirements
alphalens-reloaded==0.4.3
appdirs==1.4.4
appnope==0.1.3
arch==7.1.0
astropy==6.0.1
astropy-iers-data==0.2024.9.23.0.31.43
asttokens==2.4.0
backcall==0.2.0
beautifulsoup4==4.12.2
Bottleneck @ file:///private/var/folders/sy/f16zz6x50xz3113nwtb9bvq00000gp/T/abs_29949159-f86f-474b-bc1f-aaa1e0e222b4ofusifik/croots/recipe/bottleneck_1657175564045/work
certifi==2023.7.22
charset-normalizer==3.2.0
clarabel==0.9.0
contourpy==1.1.0
cvxopt==1.3.2
cvxpy==1.5.3
cycler==0.11.0
dataclasses==0.6
decorator==5.1.1
ecos==2.0.14
empyrical==0.5.5
empyrical-reloaded==0.5.9
exceptiongroup==1.1.3
executing==1.2.0
fonttools==4.42.1
frozendict==2.3.8
html5lib==1.1
ib==0.8.0
idna==3.4
importlib-resources==6.0.1
ipython==8.15.0
jedi==0.19.0
joblib==1.3.2
kiwisolver==1.4.5
lxml==4.9.3
matplotlib==3.9.2
matplotlib-inline==0.1.6
mkl-fft==1.3.6
mkl-random @ file:///Users/ec2-user/mkl/mkl_random_1682994911338/work
mkl-service==2.4.0
multitasking==0.0.11
networkx==3.2.1
numexpr @ file:///private/var/folders/sy/f16zz6x50xz3113nwtb9bvq00000gp/T/abs_1b50c1js9s/croot/numexpr_1683227065029/work
numpy @ file:///private/var/folders/c_/qfmhj66j0tn016nkx_th4hxm0000gp/T/abs_2ajpp3regc/croot/numpy_and_numpy_base_1691164374110/work
osqp==0.6.7.post1
packaging==23.1
pandas==2.2.3
pandas-datareader==0.10.0
parso==0.8.3
patsy==0.5.3
peewee==3.17.6
pexpect==4.8.0
pickleshare==0.7.5
Pillow==9.5.0
platformdirs==4.3.6
plotly==4.14.3
prompt-toolkit==3.0.39
ptyprocess==0.7.0
pure-eval==0.2.2
pybind11==2.13.6
pyerfa==2.0.1.4
pyfolio==0.9.2
Pygments==2.16.1
pykalman==0.9.5
pyparsing==3.0.9
python-dateutil @ file:///tmp/build/80754af9/python-dateutil_1626374649649/work
pytz @ file:///private/var/folders/sy/f16zz6x50xz3113nwtb9bvq00000gp/T/abs_ddzpsmm2_f/croot/pytz_1671697430473/work
PyYAML==6.0.2
qdldl==0.1.7.post4
requests==2.31.0
retrying==1.3.4
Riskfolio-Lib==6.2.3
scikit-learn==1.3.0
scipy==1.11.2
scs==3.2.7
seaborn==0.10.1
six @ file:///tmp/build/80754af9/six_1644875935023/work
soupsieve==2.4.1
stack-data==0.6.2
statsmodels==0.13.5
threadpoolctl==3.2.0
tqdm==4.66.1
traitlets==5.9.0
typing-extensions==3.10.0.2
tzdata @ file:///croot/python-tzdata_1690578112552/work
universal-portfolios==0.4.12
urllib3==1.26.16
wcwidth==0.2.6
webencodings==0.5.1
XlsxWriter==3.2.0
yfinance==0.2.43
zipp==3.16.2
Your code is wrong, check documentation an examples.