portfolio_moments: Specify custom functions to be used with Portfolio Analytics

Description Usage Arguments Details Value Author(s) See Also Examples

View source: R/portfolio_moments.R

Description

portfolio_moments uses purrr syntax for formulas. This enables a flexible and succinct way to define anonymous (lambda) functions.

Usage

1
portfolio_moments(R, mu = NULL, sigma = NULL, m3 = NULL, m4 = NULL)

Arguments

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 map family of functions.

Details

mu, sigma, m3 and m4 can be a passed as a function, a formula or a vector:

R, the first argument of portfolio_moments, does not need to be specified in optimize_portfolio. Use only mu, sigma, m3 and m4.

Value

A list with the suplied components.

Author(s)

Bernardo Reckziegel

See Also

[purrr::map()], [purrr::as_mapper()], [[rlang::as_function()] and [PortfolioAnalytics::optimize.portfolio()]

Examples

 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.

Reckziegel/PortfolioMoments documentation built on May 29, 2019, 1:21 p.m.