MulaiMulai sekarang secara gratis

Custom objective function

A key feature of PortfolioAnalytics is that the name for an objective is a valid R function. The package was designed to be flexible and modular, and custom objective functions are a great example of this. A few guidelines should be followed for defining a custom moment function:

  • The objective function must return a single value for the optimizer to minimize or maximize.
  • It is strongly encouraged to use R for the asset returns and weights for the portfolio weights.

These argument names are detected automatically and handled in an efficient manner. Any other arguments for the objective function can be passed in as a named list to arguments in the add.objective() function.

Latihan ini adalah bagian dari kursus

Intermediate Portfolio Analysis in R

Lihat Kursus

Petunjuk latihan

  • Define a custom objective function to compute portfolio annualized standard deviation.

Latihan interaktif praktis

Cobalah latihan ini dengan menyelesaikan kode contoh berikut.

# Custom annualized portfolio standard deviation
pasd <- function(___, ___, sigma, scale = 12){
  sqrt(as.numeric(t(___) %*% ___ %*% ___)) * sqrt(scale)
}
Edit dan Jalankan Kode