Quant Finance with R Part 3: Portfolio Optimization
HTML-код
- Опубликовано: 14 июн 2024
- In this tutorial, we will go into a simple mean-variance optimization in R with the PortfolioAnalytics package...
▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬
Resources:
▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬
-PortfolioAnalytics Documentation: cran.r-project.org/web/packag...
▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬
Links!
▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬
🐱💻 Source code to this series + other resources github.com/fdupuis659/Quant-F...
👾 *My GitHub Page*: github.com/fdupuis659
🐱🐉*Add me on LinkedIn*: / francis-dupuis-b4a45a13a
If you are using the code from the previous tutorial, be careful as you need to consider the portfolio prices and not the returns. I was using portfolio returns which gave me the error of "ROI". Using portfolio prices fixed it.
Thanks for this tutorial. Good stuff and keep it coming
Thanks! You deserve way more subscribers! :)
You're great! thanks a lot!
library(forecast)
library(PortfolioAnalytics)
then,
porf
ty, this is a great series, u provide here. i d like to make a recomnendation: is it possible u make video how to simulate the hurdle rate for a stock by using the monte carlo simulation in combination with the capm?
Try below code to avoid errors:
library(tidyverse)
library(lubridate)
library(quantmod)
library(PerformanceAnalytics)
library(PortfolioAnalytics)
tickers
Great! Is there any way i can get the weights of the tangency porfolio ploted in the efficient frontier?
After I increases the portofolio holding to 25, the optimization did not return correct results. What are the reasons?
great tutorials, thank you. Part 2 worked well. but I have the same issue with "portfolio.spec" as others with Part 3. and i thought "R" was the answer for CAPM solutions. There are "R" online references to "portfolio.spec" (undated) but seems it may have been superseded and/or deleted with no reference to any replacement. that's a shame.
How can we estimate Bayes-Stein shrinkage portfolio ? or Bayesian portfolio based on belief in an asset-pricing model ?
YAAAAH!!
Excellent. ROI package needs to be downloaded with ROI.plugin.quadprog & ROI.plugin.glkp for the optimize.portfolio to work
thanks
what should I do? It doesn't work either.
Error in optimize.portfolio(portfolioReturns, portf, optimize_method = "ROI", :
ROI only solves mean, var/StdDev, HHI, or sample ETL/ES/CVaR type business objectives, choose a different optimize_method.
Having this result, even if I had those ROI packages.
@@user-jk1nh8bw1i same problem
@@user-jk1nh8bw1i same problem. Has anyone found the solution?
I tried to run the optimize.portfolio function and didn't get the weights like you did. I got "PortfolioAnalytics Portfolio Specification" and a bunch of descriptives etc
Hey when I run optimazion.portfolio(data_xts, portfolio=portf_minvar, optimize_method="ROI") it only returns NAs. I suggest that the source of this problem lays within add.constraint(portfolio=portf_minvar, type="box", min=0, max=1). Unfortunately I don't know how to fix this problem. Can anyone help me with this?
Getting this Error ---------Error in gmv_opt(R = R, constraints = constraints, moments = moments, :
"package:ROI" %in% search() || requireNamespace("ROI", quietly = TRUE) is not TRUE-----------------I have installed ROI package as well...But it didn't work... can someone help.
I keep getting an error saying "object = 'daily' not found", this is related to the line with periodicity. Does anyone know how I can fix this?
to access fportfolio i need to install Rcurl and this just will not install for some reason
Why some stocks are above the security market line ?
Hi trying to follow along, the syntax for even downloading port info isn't working. is anyone else having this problem? is it because it is out of date or am I doing something wrong?
How do you do this when rf is not 0? I can change it on the efficient fronter plot, but I would like to do it on the optimize.portfolio function too
when rf is not 0, the the capital market line(tangential line) is not tangential. portfolio returns are lower than the risk free return
I can't do extract efficient portfolio I tried doing exactly the same way as in the video.
Hi , I have a question. I am receiving this error : > optPort
Hey!
I am getting a similar error. Did you find a solution?
@@sherryparikh8310 me to ,Error in optimize.portfolio(portfolioreturns, portf, optimize_method = "ROI", :
ROI only solves mean, var/StdDev, HHI, or sample ETL/ES/CVaR type business objectives, choose a different optimize_method. anyone found a solution ?
> optport = optimize.portfolio(portfolioReturns, portf, optimize_method = 'ROI')
Error in gmv_opt(R = R, constraints = constraints, moments = moments, :
"package:ROI" %in% search() || requireNamespace("ROI", quietly = TRUE) is not TRUE
>
i keep getting this error, I have install packages ROI, ROI.plugin.quadprog and glkp
It seems like portfolio.spec and add.constraint are no longer in the Performance Analytics package. I see them on the old R documentation, but not the new R documentation. I also don't see anything coming when using "?..." for the functions in the Rstudio terminal. Are these operations still possible with the same package?
try PortfolioAnalytics instead of PerformanceAnalytics
Great Tutorial! but i had a problem with "ROI", the packages do not pick this optimization method, so i used "SANN" , yet still iit can not figure out the chart weight, so that is where i got locked up, any help please?
You need to install the packages: "ROI" and "ROI.plugin.quadprog"
The current version of the package "Portfolio Analytics" v 2.0.4 does not include functions like portfolio.spec or optimize.portfolio. Anybody know where it went? I can't find it although I read the change logs.
try PortfolioAnalytics instead of PerformanceAnalytics
Error in optimize.portfolio(portfolioreturns, portf, optimize_method = "ROI", :
ROI only solves mean, var/StdDev, HHI, or sample ETL/ES/CVaR type business objectives, choose a different optimize_method.
portf
same as me?
have you fine the solution?
also me
@@filippoquattrini1855 it works ok now try to install
library(quantmod)
library(PerformanceAnalytics)
library(forecast)
library(PortfolioAnalytics)
@@raghadajouz6827 thanks a lot
this chunk work for me: portf
I can't use the optimize method ROI because when I try it tell me " Error: "package:ROI" %in% search() || requireNamespace("ROI", quietly = TRUE) is not TRUE". What I can do? Help please
check if you have ROI, ROI.plugin.quadprog, and ROI.plugin.glpk libraries installed
@@dakuta I DID what you said and still doesnt work
I dont know what I am doing wrong, but when I try to do the optimize.portfolio, the result in Weights is NA, can anybody tell me what I am doing wrong?
check the portfolio returns. make sure there's only 7 columns corresponding to the closing data for each day
I keep have this error, anyone can help?
Error in optimize.portfolio(portfolioReturns, portf, optimize_method = "ROI") :
ROI only solves mean, var/StdDev, HHI, or sample ETL/ES/CVaR type business objectives, choose a different optimize_method.
Hey im stuck here as well.. Did you find a solution?
Error in gmv_opt(R = R, constraints = constraints, moments = moments, :
paste0("package:", plugin) %in% search() || requireNamespace(plugin, .... is not TRUE
I have experienced, can someone help?
did you solve this one ?
Check if you have ROI, ROI.plugin.quadprog, and ROI.plugin.glpk libraries installed
@@maumrrobot How do you do that?
Stijn van Leeuwen just install those libraries and then run them. The error will disappear.
> install.packages("ROI", "ROI.plugin.quadprog", "ROI.plugin.glpk")
Warning in install.packages :
'lib = "ROI.plugin.quadprog"' is not writable
below is an error I keep getting. I'm not sure exactly how to fix this. is this perhaps because I ended up importing also the Portfolio Analytics library? Thank you in advance.
> optPort
ROI is a type solver that is available as an external package. With some downloads, it's already bundled in. Install the following packages using the install.packages("") command:
ROI
ROI.plugin.quadprog
ROI.plugin.glpk
Then load them like this:
library(ROI)
library(ROI.plugin.quadprog)
library(ROI.plugin.glpk)
You should be good after that...by the way if you want to do your own quadratic programming to get to your desired optimization then you should install.packages("quadprog"). There are other solvers available as well that you can use in the optimization.
Here's a list from the PortfolioAnalytics doc: c("DEoptim", "random",
"ROI", "ROI_old", "pso", "GenSA")
You'll probably have to install the external libraries for some of these also. Hope this helps!
@@codebliss181 Still it gives this error
Error in ROI::L_constraint(L = Amat, dir = dir.vec, rhs = rhs.vec) : all(c(dim_L[1], n_dir) == n_L_constraints) is not TRUE
For some reason for me it keeps telling me that the function “portfolio.spec” cannot be found... any suggestions on how to fix this?
Jsuk, I had to download the library portfolio analytics, now it’s working
@@t567robert2 Did you mean the PerformanceAnalytics library? I tied downloading that and I still get an error with portfolio.spec
I already have the PerformanceAnalytics package installed and I do not find a portfolio.spec function, could anyone help me?
You need to install the PortfolioAnalytics package: install.packages("PortfolioAnalytics")
hey I have a question.How can I extact the portofolios that lie on the efficient frontier only.Because I have 1000 possible portofolios and I want only the optimal (those where lie on the efficient frontier) how can I extract them.thank you
Hi! nice video but i have a problem, when i try to run this
"optPort
Hi, I tried the solution in the link below and it worked (install: ROI, ROI.plugin.quadprog, and ROI.plugin.glpk).
stackoverflow.com/questions/45201485/error-when-trying-to-optimize-with-portfolio-analytics
@@eduardomartincacereslopez6532 thanks that helps🙏
can someone please help