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

Комментарии • 73

  • @nachiketrko
    @nachiketrko 3 года назад +11

    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.

  • @tronization
    @tronization 4 года назад

    Thanks for this tutorial. Good stuff and keep it coming

  • @biancadegroot7213
    @biancadegroot7213 4 года назад

    Thanks! You deserve way more subscribers! :)

  • @jac6003
    @jac6003 3 года назад

    You're great! thanks a lot!

  • @eljuan9754
    @eljuan9754 3 года назад +3

    library(forecast)
    library(PortfolioAnalytics)
    then,
    porf

  • @Kig_Ama
    @Kig_Ama 4 года назад

    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?

  • @vineetsansi
    @vineetsansi Год назад +3

    Try below code to avoid errors:
    library(tidyverse)
    library(lubridate)
    library(quantmod)
    library(PerformanceAnalytics)
    library(PortfolioAnalytics)
    tickers

  • @DuGaita
    @DuGaita 3 года назад

    Great! Is there any way i can get the weights of the tangency porfolio ploted in the efficient frontier?

  • @yanzhongxu8580
    @yanzhongxu8580 5 лет назад +1

    After I increases the portofolio holding to 25, the optimization did not return correct results. What are the reasons?

  • @alanhill5337
    @alanhill5337 3 года назад +2

    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.

  • @svuraki4ofara344
    @svuraki4ofara344 4 года назад

    How can we estimate Bayes-Stein shrinkage portfolio ? or Bayesian portfolio based on belief in an asset-pricing model ?

  • @rjmorpheus
    @rjmorpheus 5 лет назад +1

    YAAAAH!!

  • @kirankumarkv858
    @kirankumarkv858 3 года назад +12

    Excellent. ROI package needs to be downloaded with ROI.plugin.quadprog & ROI.plugin.glkp for the optimize.portfolio to work

    • @elnokeadorml
      @elnokeadorml 3 года назад

      thanks

    • @user-jk1nh8bw1i
      @user-jk1nh8bw1i 3 года назад

      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.

    • @fu955
      @fu955 2 года назад

      @@user-jk1nh8bw1i same problem

    • @nikoskouretsis1772
      @nikoskouretsis1772 Год назад

      @@user-jk1nh8bw1i same problem. Has anyone found the solution?

  • @jamesbevan1
    @jamesbevan1 3 года назад

    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

  • @griselwurscht
    @griselwurscht 4 года назад

    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?

  • @easydrafting
    @easydrafting 3 года назад +2

    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.

  • @hadifattah622
    @hadifattah622 Год назад

    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?

  • @fu955
    @fu955 2 года назад

    to access fportfolio i need to install Rcurl and this just will not install for some reason

  • @marioemanuele9512
    @marioemanuele9512 3 месяца назад

    Why some stocks are above the security market line ?

  • @coreyhillman7506
    @coreyhillman7506 2 года назад

    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?

  • @OutperformMP
    @OutperformMP 2 года назад

    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

    • @insteadtechsolutions4276
      @insteadtechsolutions4276 Год назад

      when rf is not 0, the the capital market line(tangential line) is not tangential. portfolio returns are lower than the risk free return

  • @aydanp7020
    @aydanp7020 2 года назад

    I can't do extract efficient portfolio I tried doing exactly the same way as in the video.

  • @ThePengmun
    @ThePengmun 4 года назад +2

    Hi , I have a question. I am receiving this error : > optPort

    • @sherryparikh8310
      @sherryparikh8310 3 года назад

      Hey!
      I am getting a similar error. Did you find a solution?

    • @fu955
      @fu955 2 года назад

      @@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 ?

  • @lorenzofeijoo4284
    @lorenzofeijoo4284 4 года назад +1

    > 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

  • @thatPSNguy99
    @thatPSNguy99 4 года назад

    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?

    • @maartenlabrie6628
      @maartenlabrie6628 3 года назад +1

      try PortfolioAnalytics instead of PerformanceAnalytics

  • @mukailarafiu2718
    @mukailarafiu2718 4 года назад

    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?

    • @jamesbevan1
      @jamesbevan1 3 года назад +1

      You need to install the packages: "ROI" and "ROI.plugin.quadprog"

  • @benhoward7116
    @benhoward7116 3 года назад

    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.

    • @maartenlabrie6628
      @maartenlabrie6628 3 года назад +1

      try PortfolioAnalytics instead of PerformanceAnalytics

  • @fu955
    @fu955 2 года назад

    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.

  • @numberpercents4495
    @numberpercents4495 4 года назад +4

    portf

    • @raghadajouz6827
      @raghadajouz6827 4 года назад

      same as me?
      have you fine the solution?

    • @filippoquattrini1855
      @filippoquattrini1855 4 года назад

      also me

    • @raghadajouz6827
      @raghadajouz6827 4 года назад +14

      ​@@filippoquattrini1855 it works ok now try to install
      library(quantmod)
      library(PerformanceAnalytics)
      library(forecast)
      library(PortfolioAnalytics)

    • @filippoquattrini1855
      @filippoquattrini1855 4 года назад

      @@raghadajouz6827 thanks a lot

    • @jac6003
      @jac6003 3 года назад

      this chunk work for me: portf

  • @takao95
    @takao95 4 года назад +3

    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

    • @dakuta
      @dakuta 4 года назад +2

      check if you have ROI, ROI.plugin.quadprog, and ROI.plugin.glpk libraries installed

    • @lorenzofeijoo4284
      @lorenzofeijoo4284 4 года назад

      @@dakuta I DID what you said and still doesnt work

  • @tomaspicca6559
    @tomaspicca6559 4 года назад

    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?

    • @Patrick-rj8gh
      @Patrick-rj8gh 4 года назад

      check the portfolio returns. make sure there's only 7 columns corresponding to the closing data for each day

  • @mousaajouz6935
    @mousaajouz6935 4 года назад

    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.

    • @magnuslyng9580
      @magnuslyng9580 4 года назад

      Hey im stuck here as well.. Did you find a solution?

  • @haozhehuang6932
    @haozhehuang6932 4 года назад +2

    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?

    • @Federico-pn9hp
      @Federico-pn9hp 4 года назад

      did you solve this one ?

    • @maumrrobot
      @maumrrobot 4 года назад

      Check if you have ROI, ROI.plugin.quadprog, and ROI.plugin.glpk libraries installed

    • @stijnvanleeuwen3647
      @stijnvanleeuwen3647 4 года назад

      @@maumrrobot How do you do that?

    • @maumrrobot
      @maumrrobot 4 года назад

      Stijn van Leeuwen just install those libraries and then run them. The error will disappear.

    • @rajiyengar5525
      @rajiyengar5525 3 года назад

      > install.packages("ROI", "ROI.plugin.quadprog", "ROI.plugin.glpk")
      Warning in install.packages :
      'lib = "ROI.plugin.quadprog"' is not writable

  • @t567robert2
    @t567robert2 5 лет назад

    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

    • @codebliss181
      @codebliss181  5 лет назад +2

      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!

    • @877050831
      @877050831 3 года назад

      @@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

  • @t567robert2
    @t567robert2 5 лет назад

    For some reason for me it keeps telling me that the function “portfolio.spec” cannot be found... any suggestions on how to fix this?

    • @t567robert2
      @t567robert2 5 лет назад +2

      Jsuk, I had to download the library portfolio analytics, now it’s working

    • @cooltraderf
      @cooltraderf 5 лет назад

      @@t567robert2 Did you mean the PerformanceAnalytics library? I tied downloading that and I still get an error with portfolio.spec

    • @henriquefaria6940
      @henriquefaria6940 5 лет назад

      I already have the PerformanceAnalytics package installed and I do not find a portfolio.spec function, could anyone help me?

    • @dimuthu999
      @dimuthu999 5 лет назад +5

      You need to install the PortfolioAnalytics package: install.packages("PortfolioAnalytics")

  • @Vaggos16
    @Vaggos16 4 года назад

    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

  • @Marco.21
    @Marco.21 5 лет назад

    Hi! nice video but i have a problem, when i try to run this
    "optPort

    • @eduardomartincacereslopez6532
      @eduardomartincacereslopez6532 5 лет назад +4

      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

    • @lvincent.4983
      @lvincent.4983 Год назад

      @@eduardomartincacereslopez6532 thanks that helps🙏

  • @nkunam
    @nkunam 5 лет назад

    can someone please help