Description Usage Arguments Details Value Author(s) See Also Examples
View source: R/portfolio_moments.R
portfolio_moments
uses purrr
syntax for formulas. This enables a flexible and succinct way to define anonymous (lambda) functions.
1 |
R |
A vector, matrix, data.frame, xts, timeSeries, zoo or a tibble object. |
mu, sigma, m3, m4 |
A function, formula, or vector (not necessarily atomic). Follows the same syntax as the |
mu
, sigma
, m3
and m4
can be a passed as a function, a formula or a vector:
If a __function__: use it as it is.
If a __formula__: e.g. ~ . + 2
is converted to a function. If the function requires a time series to run refer to it with .
.
If a __vector__ e.g. ~ 0.01
is converted to a function and all forecasts get the same value, 0.01.
R
, the first argument of portfolio_moments
, does not need to be specified in optimize_portfolio
. Use only mu
, sigma
, m3
and m4
.
A list with the suplied components.
Bernardo Reckziegel
[purrr::map()], [purrr::as_mapper()], [[rlang::as_function()] and [PortfolioAnalytics::optimize.portfolio()]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | library(dplyr)
library(PortfolioAnalytics)
data(edhec)
# function format:
portfolio_moments(
R = edhec,
mu = forecast::auto.arima,
sigma = cov_shrink_to_market
)
# formula format: use to customize your call
# (ps: works for "sigma" as well)
portfolio_moments(
R = edhec,
mu = ~ forecast::auto.arima(y = ., max.p = 2, max.q = 2, stationary = TRUE),
sigma = cov_shrink_to_market
)
# vector format:
portfolio_moments(
R = edhec,
mu = ~ 0.01, # this estimator is 100% bias and 0% variance.
sigma = cov_shrink_to_market
)
# When optimizing the portfolio use "mu" and "sigma" only.
mean_var_pspec <- portfolio.spec(assets = colnames(edhec)) %>%
add.constraint(portfolio = ., type = "box", min = 0.00, max = 1.00) %>%
add.objective(portfolio = ., type = "risk", name = "var") %>%
add.objective(portfolio = ., type = "return", name = "mean")
optimize_portfolio(
R = edhec,
portfolio = mean_var_pspec,
optimize_method = 'random',
search_size = 500,
mu = forecast::ets, # no need to use portfolio_moments()
sigma = stats::cov, # just "mu" and "sigma"
message = FALSE,
trace = FALSE
)
# Note that the sample-mean and the sample-covariance are also accepted.
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.