Static Portfolio Optimization

classic Classic list List threaded Threaded
12 messages Options
Reply | Threaded
Open this post in threaded view
|

Static Portfolio Optimization

Jesse Velez
Is there any function or example in R or Rmetrics of static portfolio
optimization, where I have a vector of expected returns for N assets and a
expected covariance matrix of said N assets all at a fixed time (say
generated from a MFM risk and return model).

fPortfolio, Portfolio, portfolio.optim appear to all require time series of
returns to generate the expected return and historical covariance matrix for
use in creating weights.

Ideally, I hope to find an example that allows easily allows Long/Short
weights to make the portfolio  market neutral (i.e. Summation of Weights
=0).

Regards,


J. Velez

        [[alternative HTML version deleted]]

_______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-sig-finance
-- Subscriber-posting only.
-- If you want to post, subscribe first.
Reply | Threaded
Open this post in threaded view
|

Re: Static Portfolio Optimization

braverock
Jesse Velez wrote:

> Is there any function or example in R or Rmetrics of static portfolio
> optimization, where I have a vector of expected returns for N assets and a
> expected covariance matrix of said N assets all at a fixed time (say
> generated from a MFM risk and return model).
>
> fPortfolio, Portfolio, portfolio.optim appear to all require time series of
> returns to generate the expected return and historical covariance matrix for
> use in creating weights.
>
> Ideally, I hope to find an example that allows easily allows Long/Short
> weights to make the portfolio  market neutral (i.e. Summation of Weights
> =0).
>  
All the implementations of Markowitz style mean/variance optmization use
quadprog in R.

Plenty of information on the list archives from before all these
packages existed about using quadprog for optimization.

Regards,

  - Brian

--
Brian G. Peterson
http://braverock.com/brian/
Ph: 773-459-4973
IM: bgpbraverock

_______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-sig-finance
-- Subscriber-posting only.
-- If you want to post, subscribe first.
Reply | Threaded
Open this post in threaded view
|

Re: Static Portfolio Optimization

Thomas Etheber
Brian G. Peterson wrote:

> Jesse Velez wrote:
>> Is there any function or example in R or Rmetrics of static portfolio
>> optimization, where I have a vector of expected returns for N assets
>> and a
>> expected covariance matrix of said N assets all at a fixed time (say
>> generated from a MFM risk and return model).
>>
>> fPortfolio, Portfolio, portfolio.optim appear to all require time
>> series of
>> returns to generate the expected return and historical covariance
>> matrix for
>> use in creating weights.
>>
>> Ideally, I hope to find an example that allows easily allows Long/Short
>> weights to make the portfolio  market neutral (i.e. Summation of Weights
>> =0).
>>  
> All the implementations of Markowitz style mean/variance optmization
> use quadprog in R.
>
> Plenty of information on the list archives from before all these
> packages existed about using quadprog for optimization.
>
> Regards,
>
>  - Brian
>

Hi there,

I also had the problem with fixed parameter inputs some time ago.
Implementing methods to perform this tasks would certainly be a nice
improvement of the library (as would be some help/error messages if the
covariance matrix is not positive semidefinite).  
Although Brian's comment is helpful as usual, using basic quadprog
sounds like reinventing the wheel, but might nevertheless be needed to
solve your second task of a market-neutral portfolio.

In order to use prespecified estimates as inputs I helped myself with
overwriting some of the methods. It's not a nice solution, but it worked
for me. You will find the methods attached below.
I didn't check the code again, but I think it should work. Please note,
some other methods of Rmetrics and fPortfolio might rely on the
timeseries objects and might not work properly.

Hth
Thomas

>>>
require(MBESS)
require(fPortfolio)
rm(list=ls())
spec <- portfolioSpec()
constraints <- NULL

portfolioData <- function (data, spec = portfolioSpec())
{  
    ans = NULL
    if(class(data) == "timeSeries") {
       data = sort(data)
       nAssets = dim(data)[2]
       statistics = portfolioStatistics(data, spec)
       tailRisk = spec@model$tailRisk
       ans <- new("fPFOLIODATA", data = list(series = data, nAssets =
nAssets),
           statistics = statistics, tailRisk = tailRisk)
    }
    if(class(data) == "list") {
      statistics = list(mu = data$mu, Sigma = data$Sigma )
      attr(statistics, "estimator") = spec@model$estimator
      ans <- new("fPFOLIODATA", data = list( nAssets = length(data$mu)
), statistics = statistics, tailRisk = list() )
    }
    ans
}


####################################################################################

.efficientConstrainedMVPortfolio <- function (data, spec, constraints)
{
    if (!inherits(data, "fPFOLIODATA"))
        data = portfolioData(data, spec)
    mu = getMu(data)
    Sigma = getSigma(data)
    nAssets = getNumberOfAssets(data)
    targetAlpha = getTargetAlpha(spec)
    solver = getSolver(spec)
    stopifnot(solver == "quadprog" | solver == "Rdonlp2")
    if (solver == "quadprog") {
        portfolio = solveRQuadprog(data, spec, constraints)
    }
    else if (solver == "Rdonlp2") {
        portfolio = solveRDonlp2(data, spec, constraints)
    }
    weights = portfolio$weights
    attr(weights, "status") <- portfolio$status
    names(weights) = names(mu)
    targetReturn = matrix(as.numeric(mu %*% weights), nrow = 1)
    colnames(targetReturn) <- getEstimator(spec)[1]
    covTargetRisk = sqrt(as.numeric(weights %*% Sigma %*% weights))
#   x = getSeries(data)@Data %*% weights
#   VaR = quantile(x, targetAlpha, type = 1)
#   CVaR = VaR - 0.5 * mean(((VaR - x) + abs(VaR - x)))/targetAlpha
#   targetRisk = matrix(c(covTargetRisk, CVaR, VaR), nrow = 1)
#   colnames(targetRisk) <- c("cov", paste(c("CVaR.", "VaR."),
#   targetAlpha * 100, "%", sep = ""))
    targetRisk = matrix(c(covTargetRisk), nrow = 1)
    ## is needed to use the plotting functions....
    targetRisk = matrix(c(covTargetRisk, covTargetRisk ), nrow = 1)
    colnames(targetRisk) <- c( "cov", "dummy" )
    new("fPORTFOLIO", call = match.call(), data = list(data = data),
        spec = list(spec = spec), constraints = as.character(constraints),
        portfolio = list(weights = weights, targetReturn = targetReturn,
            targetRisk = targetRisk, targetAlpha = targetAlpha,
            status = portfolio$status), title = paste("Constrained MV
Portfolio - Solver:",
            solver), description = .description())
}

####################################################################################

.minvarianceConstrainedMVPortfolio <- function (data, spec, constraints)
{
    if (!inherits(data, "fPFOLIODATA"))
        data = portfolioData(data, spec)
    mu = getMu(data)
    Sigma = getSigma(data)
    nAssets = getNumberOfAssets(data)
    targetAlpha = getTargetAlpha(spec)
    .minVariancePortfolioFun = function(x, data, spec, constraints) {
        spec@portfolio$targetReturn = x
        ans = .efficientConstrainedMVPortfolio(data = data, spec = spec,
            constraints = constraints)
        f = getTargetRisk(ans)[1]
        attr(f, "targetReturn") <- getTargetReturn(ans)
        attr(f, "targetRisk") <- getTargetRisk(ans)[1]
        attr(f, "weights") <- getWeights(ans)
        f
    }
    minVar = optimize(.minVariancePortfolioFun, interval = range(mu),
        data = data, spec = spec, constraints = constraints,
        tol = .Machine$double.eps^0.5)
    weights = attr(minVar$objective, "weights")
    names(weights) = names(mu)
    targetReturn = spec@portfolio$targetReturn =
as.numeric(attr(minVar$objective,
        "targetReturn"))
    targetReturn = matrix(targetReturn, nrow = 1)
    colnames(targetReturn) <- spec@model$estimator[1]
    covTargetRisk = as.numeric(attr(minVar$objective, "targetRisk"))
    # x = getSeries(data)@Data %*% weights
    # VaR = quantile(x, targetAlpha, type = 1)
    # CVaR = VaR - 0.5 * mean(((VaR - x) + abs(VaR - x)))/targetAlpha
    #targetRisk = matrix(c(covTargetRisk, CVaR, VaR), nrow = 1)
    #colnames(targetRisk) <- c("cov", paste(c("CVaR.", "VaR."),
    targetRisk = matrix(c(covTargetRisk), nrow = 1)
    ## is needed to use the plotting functions....
    targetRisk = matrix(c(covTargetRisk, covTargetRisk ), nrow = 1)
    colnames(targetRisk) <- c( "cov", "dummy" )
    new("fPORTFOLIO", call = match.call(), data = list(data = data),
        spec = list(spec = spec), constraints = as.character(constraints),
        portfolio = list(weights = weights, targetReturn = targetReturn,
            targetRisk = targetRisk, targetAlpha = targetAlpha,
            status = 0), title = "Minimum Variance Portfolio",
        description = .description())
}

show.fPORTFOLIO <- function (object)
{
    cat("\nTitle:\n ")
    cat(getTitle(object), "\n")
    cat("\nCall:\n ")
    print.default(getCall(object))
    cat("\nPortfolio Weight(s):\n")
    weights = round(getWeights(object), digits = 4)
    if (length(weights) == 1) {
        cat(" ", weights, "\n")
    }
    else {
        print.table(weights)
    }
    cat("\nRiskBudget(s):\n")
    riskBudgets = round(getCovRiskBudgets(object), digits = 4)
    if (length(riskBudgets) == 1) {
        cat(" ", riskBudgets, "\n")
    }
    else {
        print.table(riskBudgets)
    }
    if (FALSE) {
        if (!is.na(getTailRiskBudgets(object))) {
            cat("\nRiskBudget(s):\n")
            riskBudgets = round(getTailRiskBudgets(object), digits = 4)
            if (length(riskBudgets) == 1) {
                cat(" ", riskBudgets, "\n")
            }
            else {
                print.table(riskBudgets)
            }
        }
    }
    targetReturn = object@portfolio$targetReturn
    targetRisk = object@portfolio$targetRisk
    spec = getSpec(object)
    cat("\nTarget Risk(s) and Return(s):\n")
    if (is.null(dim(targetReturn))) {
        targetReturn = matrix(targetReturn, nrow = 1)
        colnames(targetReturn) = getEstimator(spec)[1]
    }
    if (is.null(dim(targetRisk))) {
        targetRisk = matrix(targetRisk, nrow = length(targetRisk) )
        colnames(targetRisk) = getEstimator(spec)[2]
    }
    target = cbind(targetReturn, targetRisk)
    colnames(target) = c(colnames(targetReturn), colnames(targetRisk) )
    if (nrow(target) == 1) {
        print(target[1, ])
    }
    else {
        print(target)
    }
    cat("\nDescription:\n ")
    cat(getDescription(object), "\n")
    invisible(object)
}

setMethod("show", "fPORTFOLIO", show.fPORTFOLIO)

####################################################################################

.portfolioConstrainedMVFrontier <- function (data, spec, constraints)
{
    if (!inherits(data, "fPFOLIODATA"))
        data = portfolioData(data, spec)
    mu = getMu(data)
    Sigma = getSigma(data)
    nAssets = getNumberOfAssets(data)
    targetAlpha = getTargetAlpha(spec)
    nFrontierPoints = getNFrontierPoints(spec)
    targetReturn = targetRisk = targetWeights = error = NULL
    Spec = spec
    solver = spec@solver$solver
    Spec@portfolio$weights = rep(1/nAssets, nAssets)
    k = 0
    solverType = spec@solver$solver
    status = NULL
    for (nTargetReturn in seq(min(mu), max(mu), length = nFrontierPoints)) {
        k = k + 1
        setTargetReturn(Spec) <- nTargetReturn
        nextPortfolio = .efficientConstrainedMVPortfolio(data = data,
            spec = Spec, constraints = constraints)
        Spec@portfolio$weights = nextPortfolio@portfolio$weights
        targetReturn = rbind(targetReturn,
nextPortfolio@portfolio$targetReturn)
        targetRisk = rbind(targetRisk, nextPortfolio@portfolio$targetRisk)
        nextWeights = nextPortfolio@portfolio$weights
        names(nextWeights) = names(mu)
        status = c(status, nextPortfolio@portfolio$status)
        targetWeights = rbind(targetWeights, t(nextWeights))
    }
    Index = (1:length(status))[status == 0]
    weights = targetWeights
    colnames(weights) = names(mu)
    weights = weights[Index, ]
    DIM = dim(targetReturn)[2]
    targetReturn = targetReturn[Index, ]
    targetReturn = matrix(targetReturn, ncol = DIM)
    colnames(targetReturn) = getEstimator(spec)[1]
    targetRisk = targetRisk[Index, ]
    new("fPORTFOLIO", call = match.call(), data = list(data = data),
        spec = list(spec = spec), constraints = as.character(constraints),
        portfolio = list(weights = weights, targetReturn = targetReturn,
            targetRisk = targetRisk, targetAlpha = targetAlpha,
            status = status), title = "Constrained MV Frontier",
        description = .description())
}

####################################################################################

# You should be able to specify the data in this form:
mu <- c( 0.1, 0.08, 0.065)
sigma <- c( 0.18, 0.12, 0.09 )

correlationMatrix <- rbind( c( 1, 0.8, 0.9 ),
                                              c( 0.8, 1, 0.75),
                                              c( 0.9, 0.75, 1) )

covarianceMatrix <- cor2cov(correlationMatrix, sigma )


data = list( mu = mu, Sigma = covarianceMatrix )

# And then do the optimisation
frontier <- portfolioFrontier(data, spec = spec, constraints )

_______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-sig-finance
-- Subscriber-posting only.
-- If you want to post, subscribe first.
Reply | Threaded
Open this post in threaded view
|

Re: Static Portfolio Optimization

DonDiego
Hi,

I tested Thomas' code, but I get an error message in windows R 9.2
1(2009-06-26).

Error: class(data) == "timeSeries" is not TRUE
>

I have already spoken with Thomas. He indicated that his code seems to
work ok in LINUX. However, Thomas gets the same error message I get in
windows.


I was wondering if anyone else has tested the code? Has anyone found a
work around the problem?  It just happens that I too have the need for
this functionality.

Any leads or recommendations will be highly appreciated.



Jorge Nieves


-----Original Message-----
From: [hidden email]
[mailto:[hidden email]] On Behalf Of Thomas
Etheber
Sent: Monday, September 28, 2009 02:29 PM
To: Brian G. Peterson
Cc: [hidden email]; Jesse Velez
Subject: Re: [R-SIG-Finance] Static Portfolio Optimization

Brian G. Peterson wrote:
> Jesse Velez wrote:
>> Is there any function or example in R or Rmetrics of static portfolio

>> optimization, where I have a vector of expected returns for N assets
>> and a expected covariance matrix of said N assets all at a fixed time

>> (say generated from a MFM risk and return model).
>>
>> fPortfolio, Portfolio, portfolio.optim appear to all require time
>> series of returns to generate the expected return and historical
>> covariance matrix for use in creating weights.
>>
>> Ideally, I hope to find an example that allows easily allows
>> Long/Short weights to make the portfolio  market neutral (i.e.
>> Summation of Weights =0).
>>  
> All the implementations of Markowitz style mean/variance optmization
> use quadprog in R.
>
> Plenty of information on the list archives from before all these
> packages existed about using quadprog for optimization.
>
> Regards,
>
>  - Brian
>

Hi there,

I also had the problem with fixed parameter inputs some time ago.
Implementing methods to perform this tasks would certainly be a nice
improvement of the library (as would be some help/error messages if the
covariance matrix is not positive semidefinite).  
Although Brian's comment is helpful as usual, using basic quadprog
sounds like reinventing the wheel, but might nevertheless be needed to
solve your second task of a market-neutral portfolio.

In order to use prespecified estimates as inputs I helped myself with
overwriting some of the methods. It's not a nice solution, but it worked
for me. You will find the methods attached below.
I didn't check the code again, but I think it should work. Please note,
some other methods of Rmetrics and fPortfolio might rely on the
timeseries objects and might not work properly.

Hth
Thomas

>>>
require(MBESS)
require(fPortfolio)
rm(list=ls())
spec <- portfolioSpec()
constraints <- NULL

portfolioData <- function (data, spec = portfolioSpec()) {  
    ans = NULL
    if(class(data) == "timeSeries") {
       data = sort(data)
       nAssets = dim(data)[2]
       statistics = portfolioStatistics(data, spec)
       tailRisk = spec@model$tailRisk
       ans <- new("fPFOLIODATA", data = list(series = data, nAssets =
nAssets),
           statistics = statistics, tailRisk = tailRisk)
    }
    if(class(data) == "list") {
      statistics = list(mu = data$mu, Sigma = data$Sigma )
      attr(statistics, "estimator") = spec@model$estimator
      ans <- new("fPFOLIODATA", data = list( nAssets = length(data$mu)
), statistics = statistics, tailRisk = list() )
    }
    ans
}


########################################################################
############

.efficientConstrainedMVPortfolio <- function (data, spec, constraints) {
    if (!inherits(data, "fPFOLIODATA"))
        data = portfolioData(data, spec)
    mu = getMu(data)
    Sigma = getSigma(data)
    nAssets = getNumberOfAssets(data)
    targetAlpha = getTargetAlpha(spec)
    solver = getSolver(spec)
    stopifnot(solver == "quadprog" | solver == "Rdonlp2")
    if (solver == "quadprog") {
        portfolio = solveRQuadprog(data, spec, constraints)
    }
    else if (solver == "Rdonlp2") {
        portfolio = solveRDonlp2(data, spec, constraints)
    }
    weights = portfolio$weights
    attr(weights, "status") <- portfolio$status
    names(weights) = names(mu)
    targetReturn = matrix(as.numeric(mu %*% weights), nrow = 1)
    colnames(targetReturn) <- getEstimator(spec)[1]
    covTargetRisk = sqrt(as.numeric(weights %*% Sigma %*% weights))
#   x = getSeries(data)@Data %*% weights
#   VaR = quantile(x, targetAlpha, type = 1)
#   CVaR = VaR - 0.5 * mean(((VaR - x) + abs(VaR - x)))/targetAlpha
#   targetRisk = matrix(c(covTargetRisk, CVaR, VaR), nrow = 1)
#   colnames(targetRisk) <- c("cov", paste(c("CVaR.", "VaR."),
#   targetAlpha * 100, "%", sep = ""))
    targetRisk = matrix(c(covTargetRisk), nrow = 1)
    ## is needed to use the plotting functions....
    targetRisk = matrix(c(covTargetRisk, covTargetRisk ), nrow = 1)
    colnames(targetRisk) <- c( "cov", "dummy" )
    new("fPORTFOLIO", call = match.call(), data = list(data = data),
        spec = list(spec = spec), constraints =
as.character(constraints),
        portfolio = list(weights = weights, targetReturn = targetReturn,
            targetRisk = targetRisk, targetAlpha = targetAlpha,
            status = portfolio$status), title = paste("Constrained MV
Portfolio - Solver:",
            solver), description = .description()) }

########################################################################
############

.minvarianceConstrainedMVPortfolio <- function (data, spec, constraints)
{
    if (!inherits(data, "fPFOLIODATA"))
        data = portfolioData(data, spec)
    mu = getMu(data)
    Sigma = getSigma(data)
    nAssets = getNumberOfAssets(data)
    targetAlpha = getTargetAlpha(spec)
    .minVariancePortfolioFun = function(x, data, spec, constraints) {
        spec@portfolio$targetReturn = x
        ans = .efficientConstrainedMVPortfolio(data = data, spec = spec,
            constraints = constraints)
        f = getTargetRisk(ans)[1]
        attr(f, "targetReturn") <- getTargetReturn(ans)
        attr(f, "targetRisk") <- getTargetRisk(ans)[1]
        attr(f, "weights") <- getWeights(ans)
        f
    }
    minVar = optimize(.minVariancePortfolioFun, interval = range(mu),
        data = data, spec = spec, constraints = constraints,
        tol = .Machine$double.eps^0.5)
    weights = attr(minVar$objective, "weights")
    names(weights) = names(mu)
    targetReturn = spec@portfolio$targetReturn =
as.numeric(attr(minVar$objective,
        "targetReturn"))
    targetReturn = matrix(targetReturn, nrow = 1)
    colnames(targetReturn) <- spec@model$estimator[1]
    covTargetRisk = as.numeric(attr(minVar$objective, "targetRisk"))
    # x = getSeries(data)@Data %*% weights
    # VaR = quantile(x, targetAlpha, type = 1)
    # CVaR = VaR - 0.5 * mean(((VaR - x) + abs(VaR - x)))/targetAlpha
    #targetRisk = matrix(c(covTargetRisk, CVaR, VaR), nrow = 1)
    #colnames(targetRisk) <- c("cov", paste(c("CVaR.", "VaR."),
    targetRisk = matrix(c(covTargetRisk), nrow = 1)
    ## is needed to use the plotting functions....
    targetRisk = matrix(c(covTargetRisk, covTargetRisk ), nrow = 1)
    colnames(targetRisk) <- c( "cov", "dummy" )
    new("fPORTFOLIO", call = match.call(), data = list(data = data),
        spec = list(spec = spec), constraints =
as.character(constraints),
        portfolio = list(weights = weights, targetReturn = targetReturn,
            targetRisk = targetRisk, targetAlpha = targetAlpha,
            status = 0), title = "Minimum Variance Portfolio",
        description = .description())
}

show.fPORTFOLIO <- function (object)
{
    cat("\nTitle:\n ")
    cat(getTitle(object), "\n")
    cat("\nCall:\n ")
    print.default(getCall(object))
    cat("\nPortfolio Weight(s):\n")
    weights = round(getWeights(object), digits = 4)
    if (length(weights) == 1) {
        cat(" ", weights, "\n")
    }
    else {
        print.table(weights)
    }
    cat("\nRiskBudget(s):\n")
    riskBudgets = round(getCovRiskBudgets(object), digits = 4)
    if (length(riskBudgets) == 1) {
        cat(" ", riskBudgets, "\n")
    }
    else {
        print.table(riskBudgets)
    }
    if (FALSE) {
        if (!is.na(getTailRiskBudgets(object))) {
            cat("\nRiskBudget(s):\n")
            riskBudgets = round(getTailRiskBudgets(object), digits = 4)
            if (length(riskBudgets) == 1) {
                cat(" ", riskBudgets, "\n")
            }
            else {
                print.table(riskBudgets)
            }
        }
    }
    targetReturn = object@portfolio$targetReturn
    targetRisk = object@portfolio$targetRisk
    spec = getSpec(object)
    cat("\nTarget Risk(s) and Return(s):\n")
    if (is.null(dim(targetReturn))) {
        targetReturn = matrix(targetReturn, nrow = 1)
        colnames(targetReturn) = getEstimator(spec)[1]
    }
    if (is.null(dim(targetRisk))) {
        targetRisk = matrix(targetRisk, nrow = length(targetRisk) )
        colnames(targetRisk) = getEstimator(spec)[2]
    }
    target = cbind(targetReturn, targetRisk)
    colnames(target) = c(colnames(targetReturn), colnames(targetRisk) )
    if (nrow(target) == 1) {
        print(target[1, ])
    }
    else {
        print(target)
    }
    cat("\nDescription:\n ")
    cat(getDescription(object), "\n")
    invisible(object)
}

setMethod("show", "fPORTFOLIO", show.fPORTFOLIO)

########################################################################
############

.portfolioConstrainedMVFrontier <- function (data, spec, constraints) {
    if (!inherits(data, "fPFOLIODATA"))
        data = portfolioData(data, spec)
    mu = getMu(data)
    Sigma = getSigma(data)
    nAssets = getNumberOfAssets(data)
    targetAlpha = getTargetAlpha(spec)
    nFrontierPoints = getNFrontierPoints(spec)
    targetReturn = targetRisk = targetWeights = error = NULL
    Spec = spec
    solver = spec@solver$solver
    Spec@portfolio$weights = rep(1/nAssets, nAssets)
    k = 0
    solverType = spec@solver$solver
    status = NULL
    for (nTargetReturn in seq(min(mu), max(mu), length =
nFrontierPoints)) {
        k = k + 1
        setTargetReturn(Spec) <- nTargetReturn
        nextPortfolio = .efficientConstrainedMVPortfolio(data = data,
            spec = Spec, constraints = constraints)
        Spec@portfolio$weights = nextPortfolio@portfolio$weights
        targetReturn = rbind(targetReturn,
nextPortfolio@portfolio$targetReturn)
        targetRisk = rbind(targetRisk,
nextPortfolio@portfolio$targetRisk)
        nextWeights = nextPortfolio@portfolio$weights
        names(nextWeights) = names(mu)
        status = c(status, nextPortfolio@portfolio$status)
        targetWeights = rbind(targetWeights, t(nextWeights))
    }
    Index = (1:length(status))[status == 0]
    weights = targetWeights
    colnames(weights) = names(mu)
    weights = weights[Index, ]
    DIM = dim(targetReturn)[2]
    targetReturn = targetReturn[Index, ]
    targetReturn = matrix(targetReturn, ncol = DIM)
    colnames(targetReturn) = getEstimator(spec)[1]
    targetRisk = targetRisk[Index, ]
    new("fPORTFOLIO", call = match.call(), data = list(data = data),
        spec = list(spec = spec), constraints =
as.character(constraints),
        portfolio = list(weights = weights, targetReturn = targetReturn,
            targetRisk = targetRisk, targetAlpha = targetAlpha,
            status = status), title = "Constrained MV Frontier",
        description = .description())
}

########################################################################
############

# You should be able to specify the data in this form:
mu <- c( 0.1, 0.08, 0.065)
sigma <- c( 0.18, 0.12, 0.09 )

correlationMatrix <- rbind( c( 1, 0.8, 0.9 ),
                                              c( 0.8, 1, 0.75),
                                              c( 0.9, 0.75, 1) )

covarianceMatrix <- cor2cov(correlationMatrix, sigma )


data = list( mu = mu, Sigma = covarianceMatrix )

# And then do the optimisation
frontier <- portfolioFrontier(data, spec = spec, constraints )

_______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-sig-finance
-- Subscriber-posting only.
-- If you want to post, subscribe first.

_______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-sig-finance
-- Subscriber-posting only.
-- If you want to post, subscribe first.
Reply | Threaded
Open this post in threaded view
|

Re: Static Portfolio Optimization

markleeds
In reply to this post by Jesse Velez
I think I remember Adrian Trapletti implemented static portfolio optimization using quadprog in tseries. It's been a long time so I don't remember the details ( it may be restricted to long only but you can add variables to make
it handle long short ) but you may want to check tseries out.




On Oct 1, 2009, Jorge Nieves <[hidden email]> wrote:

Hi,

I tested Thomas' code, but I get an error message in windows R 9.2
1(2009-06-26).

Error: class(data) == "timeSeries" is not TRUE
>

I have already spoken with Thomas. He indicated that his code seems to
work ok in LINUX. However, Thomas gets the same error message I get in
windows.


I was wondering if anyone else has tested the code? Has anyone found a
work around the problem? It just happens that I too have the need for
this functionality.

Any leads or recommendations will be highly appreciated.



Jorge Nieves


-----Original Message-----
From: [hidden email]
[mailto:[hidden email]] On Behalf Of Thomas
Etheber
Sent: Monday, September 28, 2009 02:29 PM
To: Brian G. Peterson
Cc: [hidden email]; Jesse Velez
Subject: Re: [R-SIG-Finance] Static Portfolio Optimization

Brian G. Peterson wrote:
> Jesse Velez wrote:
>> Is there any function or example in R or Rmetrics of static portfolio

>> optimization, where I have a vector of expected returns for N assets
>> and a expected covariance matrix of said N assets all at a fixed time

>> (say generated from a MFM risk and return model).
>>
>> fPortfolio, Portfolio, portfolio.optim appear to all require time
>> series of returns to generate the expected return and historical
>> covariance matrix for use in creating weights.
>>
>> Ideally, I hope to find an example that allows easily allows
>> Long/Short weights to make the portfolio market neutral (i.e.
>> Summation of Weights =0).
>>
> All the implementations of Markowitz style mean/variance optmization
> use quadprog in R.
>
> Plenty of information on the list archives from before all these
> packages existed about using quadprog for optimization.
>
> Regards,
>
> - Brian
>

Hi there,

I also had the problem with fixed parameter inputs some time ago.
Implementing methods to perform this tasks would certainly be a nice
improvement of the library (as would be some help/error messages if the
covariance matrix is not positive semidefinite).
Although Brian's comment is helpful as usual, using basic quadprog
sounds like reinventing the wheel, but might nevertheless be needed to
solve your second task of a market-neutral portfolio.

In order to use prespecified estimates as inputs I helped myself with
overwriting some of the methods. It's not a nice solution, but it worked
for me. You will find the methods attached below.
I didn't check the code again, but I think it should work. Please note,
some other methods of Rmetrics and fPortfolio might rely on the
timeseries objects and might not work properly.

Hth
Thomas

>>>
require(MBESS)
require(fPortfolio)
rm(list=ls())
spec <- portfolioSpec()
constraints <- NULL

portfolioData <- function (data, spec = portfolioSpec()) {
ans = NULL
if(class(data) == "timeSeries") {
data = sort(data)
nAssets = dim(data)[2]
statistics = portfolioStatistics(data, spec)
tailRisk = spec@model$tailRisk
ans <- new("fPFOLIODATA", data = list(series = data, nAssets =
nAssets),
statistics = statistics, tailRisk = tailRisk)
}
if(class(data) == "list") {
statistics = list(mu = data$mu, Sigma = data$Sigma )
attr(statistics, "estimator") = spec@model$estimator
ans <- new("fPFOLIODATA", data = list( nAssets = length(data$mu)
), statistics = statistics, tailRisk = list() )
}
ans
}


########################################################################
############

.efficientConstrainedMVPortfolio <- function (data, spec, constraints) {
if (!inherits(data, "fPFOLIODATA"))
data = portfolioData(data, spec)
mu = getMu(data)
Sigma = getSigma(data)
nAssets = getNumberOfAssets(data)
targetAlpha = getTargetAlpha(spec)
solver = getSolver(spec)
stopifnot(solver == "quadprog" | solver == "Rdonlp2")
if (solver == "quadprog") {
portfolio = solveRQuadprog(data, spec, constraints)
}
else if (solver == "Rdonlp2") {
portfolio = solveRDonlp2(data, spec, constraints)
}
weights = portfolio$weights
attr(weights, "status") <- portfolio$status
names(weights) = names(mu)
targetReturn = matrix(as.numeric(mu %*% weights), nrow = 1)
colnames(targetReturn) <- getEstimator(spec)[1]
covTargetRisk = sqrt(as.numeric(weights %*% Sigma %*% weights))
# x = getSeries(data)@Data %*% weights
# VaR = quantile(x, targetAlpha, type = 1)
# CVaR = VaR - 0.5 * mean(((VaR - x) + abs(VaR - x)))/targetAlpha
# targetRisk = matrix(c(covTargetRisk, CVaR, VaR), nrow = 1)
# colnames(targetRisk) <- c("cov", paste(c("CVaR.", "VaR."),
# targetAlpha * 100, "%", sep = ""))
targetRisk = matrix(c(covTargetRisk), nrow = 1)
## is needed to use the plotting functions....
targetRisk = matrix(c(covTargetRisk, covTargetRisk ), nrow = 1)
colnames(targetRisk) <- c( "cov", "dummy" )
new("fPORTFOLIO", call = match.call(), data = list(data = data),
spec = list(spec = spec), constraints =
as.character(constraints),
portfolio = list(weights = weights, targetReturn = targetReturn,
targetRisk = targetRisk, targetAlpha = targetAlpha,
status = portfolio$status), title = paste("Constrained MV
Portfolio - Solver:",
solver), description = .description()) }

########################################################################
############

.minvarianceConstrainedMVPortfolio <- function (data, spec, constraints)
{
if (!inherits(data, "fPFOLIODATA"))
data = portfolioData(data, spec)
mu = getMu(data)
Sigma = getSigma(data)
nAssets = getNumberOfAssets(data)
targetAlpha = getTargetAlpha(spec)
.minVariancePortfolioFun = function(x, data, spec, constraints) {
spec@portfolio$targetReturn = x
ans = .efficientConstrainedMVPortfolio(data = data, spec = spec,
constraints = constraints)
f = getTargetRisk(ans)[1]
attr(f, "targetReturn") <- getTargetReturn(ans)
attr(f, "targetRisk") <- getTargetRisk(ans)[1]
attr(f, "weights") <- getWeights(ans)
f
}
minVar = optimize(.minVariancePortfolioFun, interval = range(mu),
data = data, spec = spec, constraints = constraints,
tol = .Machine$double.eps^0.5)
weights = attr(minVar$objective, "weights")
names(weights) = names(mu)
targetReturn = spec@portfolio$targetReturn =
as.numeric(attr(minVar$objective,
"targetReturn"))
targetReturn = matrix(targetReturn, nrow = 1)
colnames(targetReturn) <- spec@model$estimator[1]
covTargetRisk = as.numeric(attr(minVar$objective, "targetRisk"))
# x = getSeries(data)@Data %*% weights
# VaR = quantile(x, targetAlpha, type = 1)
# CVaR = VaR - 0.5 * mean(((VaR - x) + abs(VaR - x)))/targetAlpha
#targetRisk = matrix(c(covTargetRisk, CVaR, VaR), nrow = 1)
#colnames(targetRisk) <- c("cov", paste(c("CVaR.", "VaR."),
targetRisk = matrix(c(covTargetRisk), nrow = 1)
## is needed to use the plotting functions....
targetRisk = matrix(c(covTargetRisk, covTargetRisk ), nrow = 1)
colnames(targetRisk) <- c( "cov", "dummy" )
new("fPORTFOLIO", call = match.call(), data = list(data = data),
spec = list(spec = spec), constraints =
as.character(constraints),
portfolio = list(weights = weights, targetReturn = targetReturn,
targetRisk = targetRisk, targetAlpha = targetAlpha,
status = 0), title = "Minimum Variance Portfolio",
description = .description())
}

show.fPORTFOLIO <- function (object)
{
cat("\nTitle:\n ")
cat(getTitle(object), "\n")
cat("\nCall:\n ")
print.default(getCall(object))
cat("\nPortfolio Weight(s):\n")
weights = round(getWeights(object), digits = 4)
if (length(weights) == 1) {
cat(" ", weights, "\n")
}
else {
print.table(weights)
}
cat("\nRiskBudget(s):\n")
riskBudgets = round(getCovRiskBudgets(object), digits = 4)
if (length(riskBudgets) == 1) {
cat(" ", riskBudgets, "\n")
}
else {
print.table(riskBudgets)
}
if (FALSE) {
if (!is.na(getTailRiskBudgets(object))) {
cat("\nRiskBudget(s):\n")
riskBudgets = round(getTailRiskBudgets(object), digits = 4)
if (length(riskBudgets) == 1) {
cat(" ", riskBudgets, "\n")
}
else {
print.table(riskBudgets)
}
}
}
targetReturn = object@portfolio$targetReturn
targetRisk = object@portfolio$targetRisk
spec = getSpec(object)
cat("\nTarget Risk(s) and Return(s):\n")
if (is.null(dim(targetReturn))) {
targetReturn = matrix(targetReturn, nrow = 1)
colnames(targetReturn) = getEstimator(spec)[1]
}
if (is.null(dim(targetRisk))) {
targetRisk = matrix(targetRisk, nrow = length(targetRisk) )
colnames(targetRisk) = getEstimator(spec)[2]
}
target = cbind(targetReturn, targetRisk)
colnames(target) = c(colnames(targetReturn), colnames(targetRisk) )
if (nrow(target) == 1) {
print(target[1, ])
}
else {
print(target)
}
cat("\nDescription:\n ")
cat(getDescription(object), "\n")
invisible(object)
}

setMethod("show", "fPORTFOLIO", show.fPORTFOLIO)

########################################################################
############

.portfolioConstrainedMVFrontier <- function (data, spec, constraints) {
if (!inherits(data, "fPFOLIODATA"))
data = portfolioData(data, spec)
mu = getMu(data)
Sigma = getSigma(data)
nAssets = getNumberOfAssets(data)
targetAlpha = getTargetAlpha(spec)
nFrontierPoints = getNFrontierPoints(spec)
targetReturn = targetRisk = targetWeights = error = NULL
Spec = spec
solver = spec@solver$solver
Spec@portfolio$weights = rep(1/nAssets, nAssets)
k = 0
solverType = spec@solver$solver
status = NULL
for (nTargetReturn in seq(min(mu), max(mu), length =
nFrontierPoints)) {
k = k + 1
setTargetReturn(Spec) <- nTargetReturn
nextPortfolio = .efficientConstrainedMVPortfolio(data = data,
spec = Spec, constraints = constraints)
Spec@portfolio$weights = nextPortfolio@portfolio$weights
targetReturn = rbind(targetReturn,
nextPortfolio@portfolio$targetReturn)
targetRisk = rbind(targetRisk,
nextPortfolio@portfolio$targetRisk)
nextWeights = nextPortfolio@portfolio$weights
names(nextWeights) = names(mu)
status = c(status, nextPortfolio@portfolio$status)
targetWeights = rbind(targetWeights, t(nextWeights))
}
Index = (1:length(status))[status == 0]
weights = targetWeights
colnames(weights) = names(mu)
weights = weights[Index, ]
DIM = dim(targetReturn)[2]
targetReturn = targetReturn[Index, ]
targetReturn = matrix(targetReturn, ncol = DIM)
colnames(targetReturn) = getEstimator(spec)[1]
targetRisk = targetRisk[Index, ]
new("fPORTFOLIO", call = match.call(), data = list(data = data),
spec = list(spec = spec), constraints =
as.character(constraints),
portfolio = list(weights = weights, targetReturn = targetReturn,
targetRisk = targetRisk, targetAlpha = targetAlpha,
status = status), title = "Constrained MV Frontier",
description = .description())
}

########################################################################
############

# You should be able to specify the data in this form:
mu <- c( 0.1, 0.08, 0.065)
sigma <- c( 0.18, 0.12, 0.09 )

correlationMatrix <- rbind( c( 1, 0.8, 0.9 ),
c( 0.8, 1, 0.75),
c( 0.9, 0.75, 1) )

covarianceMatrix <- cor2cov(correlationMatrix, sigma )


data = list( mu = mu, Sigma = covarianceMatrix )

# And then do the optimisation
frontier <- portfolioFrontier(data, spec = spec, constraints )

_______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-sig-finance
-- Subscriber-posting only.
-- If you want to post, subscribe first.

_______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-sig-finance
-- Subscriber-posting only.
-- If you want to post, subscribe first.

_______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-sig-finance
-- Subscriber-posting only.
-- If you want to post, subscribe first.
Reply | Threaded
Open this post in threaded view
|

Re: Static Portfolio Optimization

Dirk Eddelbuettel

On 1 October 2009 at 15:01, [hidden email] wrote:
| I think I remember Adrian Trapletti implemented static portfolio
| optimization using quadprog in tseries. It's been a long time so I don't
| remember the details ( it may be restricted to long only but you can add
| variables to make<br />it handle long short ) but you may want to check
| tseries out.

Correct -- the tseries package has done since time immportal.

And some seven or eight years ago I sent Adrian a patch, styled after the
discussion in Huang and Litzenberger, that adds the ability to have long and
short positions.  

Which Adrian promptly added and which has been there ever since:

R> head(as.zoo(EuStockMarkets))
           DAX  SMI  CAC FTSE
1991(130) 1629 1678 1773 2444
1991(131) 1614 1688 1750 2460
1991(132) 1607 1679 1718 2448
1991(133) 1621 1684 1708 2470
1991(134) 1618 1687 1723 2485
1991(135) 1611 1672 1714 2467
R> X <- diff(log(as.zoo(EuStockMarkets)))
R> res <- portfolio.optim(X)                 ## Long only
R> res$pw
[1] 0.0000 0.3958 0.0000 0.6042
R> res <- portfolio.optim(X, shorts=TRUE)    ## Long/Short
R> res$pw
[1]  0.02550  0.38213 -0.06377  0.65614
R>

This is arguably a better example than the one in help(portfolio.optim) so
maybe I should send Kurt a new patch :)

Dirk

--
Three out of two people have difficulties with fractions.

_______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-sig-finance
-- Subscriber-posting only.
-- If you want to post, subscribe first.
Reply | Threaded
Open this post in threaded view
|

Re: Static Portfolio Optimization

DonDiego
Thanks for your responses. I found the function "portfolio.optim" and
the tseries series package and a document with explanations. The
function indeed allows for shorts. However, I still do not see how to
pass into the function the vector of expected values and the covariance
matrix. The function's input is the time series themselves. It seems
that the function internally computes both the expected values and the
covariance matrix, and then it performs the optimization.  Please
correct me if I am not reading right the function description and its
specifications.

Thanks,


Jorge Nieves


-----Original Message-----
From: Dirk Eddelbuettel [mailto:[hidden email]]
Sent: Thursday, October 01, 2009 04:25 PM
To: [hidden email]
Cc: Jorge Nieves; [hidden email];
[hidden email]
Subject: Re: [R-SIG-Finance] Static Portfolio Optimization


On 1 October 2009 at 15:01, [hidden email] wrote:
| I think I remember Adrian Trapletti implemented static portfolio
| optimization using quadprog in tseries. It's been a long time so I
| don't remember the details ( it may be restricted to long only but you

| can add variables to make<br />it handle long short ) but you may want

| to check tseries out.

Correct -- the tseries package has done since time immportal.

And some seven or eight years ago I sent Adrian a patch, styled after
the discussion in Huang and Litzenberger, that adds the ability to have
long and short positions.  

Which Adrian promptly added and which has been there ever since:

R> head(as.zoo(EuStockMarkets))
           DAX  SMI  CAC FTSE
1991(130) 1629 1678 1773 2444
1991(131) 1614 1688 1750 2460
1991(132) 1607 1679 1718 2448
1991(133) 1621 1684 1708 2470
1991(134) 1618 1687 1723 2485
1991(135) 1611 1672 1714 2467
R> X <- diff(log(as.zoo(EuStockMarkets)))
R> res <- portfolio.optim(X)                 ## Long only
R> res$pw
[1] 0.0000 0.3958 0.0000 0.6042
R> res <- portfolio.optim(X, shorts=TRUE)    ## Long/Short
R> res$pw
[1]  0.02550  0.38213 -0.06377  0.65614
R>

This is arguably a better example than the one in help(portfolio.optim)
so maybe I should send Kurt a new patch :)

Dirk

--
Three out of two people have difficulties with fractions.

_______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-sig-finance
-- Subscriber-posting only.
-- If you want to post, subscribe first.
Reply | Threaded
Open this post in threaded view
|

Re: Static Portfolio Optimization

Dirk Eddelbuettel
In reply to this post by Dirk Eddelbuettel

On 1 October 2009 at 15:25, Dirk Eddelbuettel wrote:
| Correct -- the tseries package has done since time immportal.

Oops: "... has done this since time immortal" is what I meant.  Someone
please write semantic and syntactic checker for Emacs....

Sorry, Dirk
 
| And some seven or eight years ago I sent Adrian a patch, styled after the
| discussion in Huang and Litzenberger, that adds the ability to have long and
| short positions.  
|
| Which Adrian promptly added and which has been there ever since:
|
| R> head(as.zoo(EuStockMarkets))
|            DAX  SMI  CAC FTSE
| 1991(130) 1629 1678 1773 2444
| 1991(131) 1614 1688 1750 2460
| 1991(132) 1607 1679 1718 2448
| 1991(133) 1621 1684 1708 2470
| 1991(134) 1618 1687 1723 2485
| 1991(135) 1611 1672 1714 2467
| R> X <- diff(log(as.zoo(EuStockMarkets)))
| R> res <- portfolio.optim(X)                 ## Long only
| R> res$pw
| [1] 0.0000 0.3958 0.0000 0.6042
| R> res <- portfolio.optim(X, shorts=TRUE)    ## Long/Short
| R> res$pw
| [1]  0.02550  0.38213 -0.06377  0.65614
| R>
|
| This is arguably a better example than the one in help(portfolio.optim) so
| maybe I should send Kurt a new patch :)
|
| Dirk
|
| --
| Three out of two people have difficulties with fractions.
|
| _______________________________________________
| [hidden email] mailing list
| https://stat.ethz.ch/mailman/listinfo/r-sig-finance
| -- Subscriber-posting only.
| -- If you want to post, subscribe first.

--
Three out of two people have difficulties with fractions.

_______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-sig-finance
-- Subscriber-posting only.
-- If you want to post, subscribe first.
Reply | Threaded
Open this post in threaded view
|

Re: Static Portfolio Optimization

Thomas Etheber
In reply to this post by DonDiego
Jorge Nieves schrieb:

> Hi,
>
> I tested Thomas' code, but I get an error message in windows R 9.2
> 1(2009-06-26).
>
> Error: class(data) == "timeSeries" is not TRUE
>  
>
> I have already spoken with Thomas. He indicated that his code seems to
> work ok in LINUX. However, Thomas gets the same error message I get in
> windows.
>
>  
I don't think the OS is relevant for the code. As I told Jorge the code
worked on an older R Version (R version 2.6.1).

Btw Dirk mentioned a nice alternative - the tseries package. I was
wondering, if one can also prespecify further restrictions to
portfolio.optim or is there just support for long-only portfolios?
As the results of the optimizer are often quite sensitive, one might
want to specify some minimum or maximum portfolio weights to get more
suitable results.

Hth,
Thomas

> I was wondering if anyone else has tested the code? Has anyone found a
> work around the problem?  It just happens that I too have the need for
> this functionality.
>
> Any leads or recommendations will be highly appreciated.
>
>
>
> Jorge Nieves
>
>
> -----Original Message-----
> From: [hidden email]
> [mailto:[hidden email]] On Behalf Of Thomas
> Etheber
> Sent: Monday, September 28, 2009 02:29 PM
> To: Brian G. Peterson
> Cc: [hidden email]; Jesse Velez
> Subject: Re: [R-SIG-Finance] Static Portfolio Optimization
>
> Brian G. Peterson wrote:
>  
>> Jesse Velez wrote:
>>    
>>> Is there any function or example in R or Rmetrics of static portfolio
>>>      
>
>  
>>> optimization, where I have a vector of expected returns for N assets
>>> and a expected covariance matrix of said N assets all at a fixed time
>>>      
>
>  
>>> (say generated from a MFM risk and return model).
>>>
>>> fPortfolio, Portfolio, portfolio.optim appear to all require time
>>> series of returns to generate the expected return and historical
>>> covariance matrix for use in creating weights.
>>>
>>> Ideally, I hope to find an example that allows easily allows
>>> Long/Short weights to make the portfolio  market neutral (i.e.
>>> Summation of Weights =0).
>>>  
>>>      
>> All the implementations of Markowitz style mean/variance optmization
>> use quadprog in R.
>>
>> Plenty of information on the list archives from before all these
>> packages existed about using quadprog for optimization.
>>
>> Regards,
>>
>>  - Brian
>>
>>    
>
> Hi there,
>
> I also had the problem with fixed parameter inputs some time ago.
> Implementing methods to perform this tasks would certainly be a nice
> improvement of the library (as would be some help/error messages if the
> covariance matrix is not positive semidefinite).  
> Although Brian's comment is helpful as usual, using basic quadprog
> sounds like reinventing the wheel, but might nevertheless be needed to
> solve your second task of a market-neutral portfolio.
>
> In order to use prespecified estimates as inputs I helped myself with
> overwriting some of the methods. It's not a nice solution, but it worked
> for me. You will find the methods attached below.
> I didn't check the code again, but I think it should work. Please note,
> some other methods of Rmetrics and fPortfolio might rely on the
> timeseries objects and might not work properly.
>
> Hth
> Thomas
>
>  
> require(MBESS)
> require(fPortfolio)
> rm(list=ls())
> spec <- portfolioSpec()
> constraints <- NULL
>
> portfolioData <- function (data, spec = portfolioSpec()) {  
>     ans = NULL
>     if(class(data) == "timeSeries") {
>        data = sort(data)
>        nAssets = dim(data)[2]
>        statistics = portfolioStatistics(data, spec)
>        tailRisk = spec@model$tailRisk
>        ans <- new("fPFOLIODATA", data = list(series = data, nAssets =
> nAssets),
>            statistics = statistics, tailRisk = tailRisk)
>     }
>     if(class(data) == "list") {
>       statistics = list(mu = data$mu, Sigma = data$Sigma )
>       attr(statistics, "estimator") = spec@model$estimator
>       ans <- new("fPFOLIODATA", data = list( nAssets = length(data$mu)
> ), statistics = statistics, tailRisk = list() )
>     }
>     ans
> }
>
>
> ########################################################################
> ############
>
> .efficientConstrainedMVPortfolio <- function (data, spec, constraints) {
>     if (!inherits(data, "fPFOLIODATA"))
>         data = portfolioData(data, spec)
>     mu = getMu(data)
>     Sigma = getSigma(data)
>     nAssets = getNumberOfAssets(data)
>     targetAlpha = getTargetAlpha(spec)
>     solver = getSolver(spec)
>     stopifnot(solver == "quadprog" | solver == "Rdonlp2")
>     if (solver == "quadprog") {
>         portfolio = solveRQuadprog(data, spec, constraints)
>     }
>     else if (solver == "Rdonlp2") {
>         portfolio = solveRDonlp2(data, spec, constraints)
>     }
>     weights = portfolio$weights
>     attr(weights, "status") <- portfolio$status
>     names(weights) = names(mu)
>     targetReturn = matrix(as.numeric(mu %*% weights), nrow = 1)
>     colnames(targetReturn) <- getEstimator(spec)[1]
>     covTargetRisk = sqrt(as.numeric(weights %*% Sigma %*% weights))
> #   x = getSeries(data)@Data %*% weights
> #   VaR = quantile(x, targetAlpha, type = 1)
> #   CVaR = VaR - 0.5 * mean(((VaR - x) + abs(VaR - x)))/targetAlpha
> #   targetRisk = matrix(c(covTargetRisk, CVaR, VaR), nrow = 1)
> #   colnames(targetRisk) <- c("cov", paste(c("CVaR.", "VaR."),
> #   targetAlpha * 100, "%", sep = ""))
>     targetRisk = matrix(c(covTargetRisk), nrow = 1)
>     ## is needed to use the plotting functions....
>     targetRisk = matrix(c(covTargetRisk, covTargetRisk ), nrow = 1)
>     colnames(targetRisk) <- c( "cov", "dummy" )
>     new("fPORTFOLIO", call = match.call(), data = list(data = data),
>         spec = list(spec = spec), constraints =
> as.character(constraints),
>         portfolio = list(weights = weights, targetReturn = targetReturn,
>             targetRisk = targetRisk, targetAlpha = targetAlpha,
>             status = portfolio$status), title = paste("Constrained MV
> Portfolio - Solver:",
>             solver), description = .description()) }
>
> ########################################################################
> ############
>
> .minvarianceConstrainedMVPortfolio <- function (data, spec, constraints)
> {
>     if (!inherits(data, "fPFOLIODATA"))
>         data = portfolioData(data, spec)
>     mu = getMu(data)
>     Sigma = getSigma(data)
>     nAssets = getNumberOfAssets(data)
>     targetAlpha = getTargetAlpha(spec)
>     .minVariancePortfolioFun = function(x, data, spec, constraints) {
>         spec@portfolio$targetReturn = x
>         ans = .efficientConstrainedMVPortfolio(data = data, spec = spec,
>             constraints = constraints)
>         f = getTargetRisk(ans)[1]
>         attr(f, "targetReturn") <- getTargetReturn(ans)
>         attr(f, "targetRisk") <- getTargetRisk(ans)[1]
>         attr(f, "weights") <- getWeights(ans)
>         f
>     }
>     minVar = optimize(.minVariancePortfolioFun, interval = range(mu),
>         data = data, spec = spec, constraints = constraints,
>         tol = .Machine$double.eps^0.5)
>     weights = attr(minVar$objective, "weights")
>     names(weights) = names(mu)
>     targetReturn = spec@portfolio$targetReturn =
> as.numeric(attr(minVar$objective,
>         "targetReturn"))
>     targetReturn = matrix(targetReturn, nrow = 1)
>     colnames(targetReturn) <- spec@model$estimator[1]
>     covTargetRisk = as.numeric(attr(minVar$objective, "targetRisk"))
>     # x = getSeries(data)@Data %*% weights
>     # VaR = quantile(x, targetAlpha, type = 1)
>     # CVaR = VaR - 0.5 * mean(((VaR - x) + abs(VaR - x)))/targetAlpha
>     #targetRisk = matrix(c(covTargetRisk, CVaR, VaR), nrow = 1)
>     #colnames(targetRisk) <- c("cov", paste(c("CVaR.", "VaR."),
>     targetRisk = matrix(c(covTargetRisk), nrow = 1)
>     ## is needed to use the plotting functions....
>     targetRisk = matrix(c(covTargetRisk, covTargetRisk ), nrow = 1)
>     colnames(targetRisk) <- c( "cov", "dummy" )
>     new("fPORTFOLIO", call = match.call(), data = list(data = data),
>         spec = list(spec = spec), constraints =
> as.character(constraints),
>         portfolio = list(weights = weights, targetReturn = targetReturn,
>             targetRisk = targetRisk, targetAlpha = targetAlpha,
>             status = 0), title = "Minimum Variance Portfolio",
>         description = .description())
> }
>
> show.fPORTFOLIO <- function (object)
> {
>     cat("\nTitle:\n ")
>     cat(getTitle(object), "\n")
>     cat("\nCall:\n ")
>     print.default(getCall(object))
>     cat("\nPortfolio Weight(s):\n")
>     weights = round(getWeights(object), digits = 4)
>     if (length(weights) == 1) {
>         cat(" ", weights, "\n")
>     }
>     else {
>         print.table(weights)
>     }
>     cat("\nRiskBudget(s):\n")
>     riskBudgets = round(getCovRiskBudgets(object), digits = 4)
>     if (length(riskBudgets) == 1) {
>         cat(" ", riskBudgets, "\n")
>     }
>     else {
>         print.table(riskBudgets)
>     }
>     if (FALSE) {
>         if (!is.na(getTailRiskBudgets(object))) {
>             cat("\nRiskBudget(s):\n")
>             riskBudgets = round(getTailRiskBudgets(object), digits = 4)
>             if (length(riskBudgets) == 1) {
>                 cat(" ", riskBudgets, "\n")
>             }
>             else {
>                 print.table(riskBudgets)
>             }
>         }
>     }
>     targetReturn = object@portfolio$targetReturn
>     targetRisk = object@portfolio$targetRisk
>     spec = getSpec(object)
>     cat("\nTarget Risk(s) and Return(s):\n")
>     if (is.null(dim(targetReturn))) {
>         targetReturn = matrix(targetReturn, nrow = 1)
>         colnames(targetReturn) = getEstimator(spec)[1]
>     }
>     if (is.null(dim(targetRisk))) {
>         targetRisk = matrix(targetRisk, nrow = length(targetRisk) )
>         colnames(targetRisk) = getEstimator(spec)[2]
>     }
>     target = cbind(targetReturn, targetRisk)
>     colnames(target) = c(colnames(targetReturn), colnames(targetRisk) )
>     if (nrow(target) == 1) {
>         print(target[1, ])
>     }
>     else {
>         print(target)
>     }
>     cat("\nDescription:\n ")
>     cat(getDescription(object), "\n")
>     invisible(object)
> }
>
> setMethod("show", "fPORTFOLIO", show.fPORTFOLIO)
>
> ########################################################################
> ############
>
> .portfolioConstrainedMVFrontier <- function (data, spec, constraints) {
>     if (!inherits(data, "fPFOLIODATA"))
>         data = portfolioData(data, spec)
>     mu = getMu(data)
>     Sigma = getSigma(data)
>     nAssets = getNumberOfAssets(data)
>     targetAlpha = getTargetAlpha(spec)
>     nFrontierPoints = getNFrontierPoints(spec)
>     targetReturn = targetRisk = targetWeights = error = NULL
>     Spec = spec
>     solver = spec@solver$solver
>     Spec@portfolio$weights = rep(1/nAssets, nAssets)
>     k = 0
>     solverType = spec@solver$solver
>     status = NULL
>     for (nTargetReturn in seq(min(mu), max(mu), length =
> nFrontierPoints)) {
>         k = k + 1
>         setTargetReturn(Spec) <- nTargetReturn
>         nextPortfolio = .efficientConstrainedMVPortfolio(data = data,
>             spec = Spec, constraints = constraints)
>         Spec@portfolio$weights = nextPortfolio@portfolio$weights
>         targetReturn = rbind(targetReturn,
> nextPortfolio@portfolio$targetReturn)
>         targetRisk = rbind(targetRisk,
> nextPortfolio@portfolio$targetRisk)
>         nextWeights = nextPortfolio@portfolio$weights
>         names(nextWeights) = names(mu)
>         status = c(status, nextPortfolio@portfolio$status)
>         targetWeights = rbind(targetWeights, t(nextWeights))
>     }
>     Index = (1:length(status))[status == 0]
>     weights = targetWeights
>     colnames(weights) = names(mu)
>     weights = weights[Index, ]
>     DIM = dim(targetReturn)[2]
>     targetReturn = targetReturn[Index, ]
>     targetReturn = matrix(targetReturn, ncol = DIM)
>     colnames(targetReturn) = getEstimator(spec)[1]
>     targetRisk = targetRisk[Index, ]
>     new("fPORTFOLIO", call = match.call(), data = list(data = data),
>         spec = list(spec = spec), constraints =
> as.character(constraints),
>         portfolio = list(weights = weights, targetReturn = targetReturn,
>             targetRisk = targetRisk, targetAlpha = targetAlpha,
>             status = status), title = "Constrained MV Frontier",
>         description = .description())
> }
>
> ########################################################################
> ############
>
> # You should be able to specify the data in this form:
> mu <- c( 0.1, 0.08, 0.065)
> sigma <- c( 0.18, 0.12, 0.09 )
>
> correlationMatrix <- rbind( c( 1, 0.8, 0.9 ),
>                                               c( 0.8, 1, 0.75),
>                                               c( 0.9, 0.75, 1) )
>
> covarianceMatrix <- cor2cov(correlationMatrix, sigma )
>
>
> data = list( mu = mu, Sigma = covarianceMatrix )
>
> # And then do the optimisation
> frontier <- portfolioFrontier(data, spec = spec, constraints )
>
> _______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-sig-finance
> -- Subscriber-posting only.
> -- If you want to post, subscribe first.
>
>

_______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-sig-finance
-- Subscriber-posting only.
-- If you want to post, subscribe first.
Reply | Threaded
Open this post in threaded view
|

Re: Static Portfolio Optimization

Eric Zivot
I would like to point out a very nice presentation by Guy Yollen at the R in
Finance conference that describes portfolio optimization using the
portfolio.optim function and modifications thereof

http://rinfinance.quantmod.com/presentations/yollin_slides.pdf


Eric Zivot
Professor and Gary Waterman Distinguished Scholar
Department of Economics
Adjunct Professor of Finance, Foster School of Business
Adjunct Professor of Statistics
Box 353330
University of Washington
Seattle, WA 98195-3330
206-543-6715
http://faculty.washington.edu/ezivot



-----Original Message-----
From: [hidden email]
[mailto:[hidden email]] On Behalf Of Thomas Etheber
Sent: Thursday, October 01, 2009 9:41 PM
To: Jorge Nieves
Cc: [hidden email]; Jesse Velez
Subject: Re: [R-SIG-Finance] Static Portfolio Optimization

Jorge Nieves schrieb:

> Hi,
>
> I tested Thomas' code, but I get an error message in windows R 9.2
> 1(2009-06-26).
>
> Error: class(data) == "timeSeries" is not TRUE
>  
>
> I have already spoken with Thomas. He indicated that his code seems to
> work ok in LINUX. However, Thomas gets the same error message I get in
> windows.
>
>  
I don't think the OS is relevant for the code. As I told Jorge the code
worked on an older R Version (R version 2.6.1).

Btw Dirk mentioned a nice alternative - the tseries package. I was
wondering, if one can also prespecify further restrictions to
portfolio.optim or is there just support for long-only portfolios?
As the results of the optimizer are often quite sensitive, one might
want to specify some minimum or maximum portfolio weights to get more
suitable results.

Hth,
Thomas

> I was wondering if anyone else has tested the code? Has anyone found a
> work around the problem?  It just happens that I too have the need for
> this functionality.
>
> Any leads or recommendations will be highly appreciated.
>
>
>
> Jorge Nieves
>
>
> -----Original Message-----
> From: [hidden email]
> [mailto:[hidden email]] On Behalf Of Thomas
> Etheber
> Sent: Monday, September 28, 2009 02:29 PM
> To: Brian G. Peterson
> Cc: [hidden email]; Jesse Velez
> Subject: Re: [R-SIG-Finance] Static Portfolio Optimization
>
> Brian G. Peterson wrote:
>  
>> Jesse Velez wrote:
>>    
>>> Is there any function or example in R or Rmetrics of static portfolio
>>>      
>
>  
>>> optimization, where I have a vector of expected returns for N assets
>>> and a expected covariance matrix of said N assets all at a fixed time
>>>      
>
>  
>>> (say generated from a MFM risk and return model).
>>>
>>> fPortfolio, Portfolio, portfolio.optim appear to all require time
>>> series of returns to generate the expected return and historical
>>> covariance matrix for use in creating weights.
>>>
>>> Ideally, I hope to find an example that allows easily allows
>>> Long/Short weights to make the portfolio  market neutral (i.e.
>>> Summation of Weights =0).
>>>  
>>>      
>> All the implementations of Markowitz style mean/variance optmization
>> use quadprog in R.
>>
>> Plenty of information on the list archives from before all these
>> packages existed about using quadprog for optimization.
>>
>> Regards,
>>
>>  - Brian
>>
>>    
>
> Hi there,
>
> I also had the problem with fixed parameter inputs some time ago.
> Implementing methods to perform this tasks would certainly be a nice
> improvement of the library (as would be some help/error messages if the
> covariance matrix is not positive semidefinite).  
> Although Brian's comment is helpful as usual, using basic quadprog
> sounds like reinventing the wheel, but might nevertheless be needed to
> solve your second task of a market-neutral portfolio.
>
> In order to use prespecified estimates as inputs I helped myself with
> overwriting some of the methods. It's not a nice solution, but it worked
> for me. You will find the methods attached below.
> I didn't check the code again, but I think it should work. Please note,
> some other methods of Rmetrics and fPortfolio might rely on the
> timeseries objects and might not work properly.
>
> Hth
> Thomas
>
>  
> require(MBESS)
> require(fPortfolio)
> rm(list=ls())
> spec <- portfolioSpec()
> constraints <- NULL
>
> portfolioData <- function (data, spec = portfolioSpec()) {  
>     ans = NULL
>     if(class(data) == "timeSeries") {
>        data = sort(data)
>        nAssets = dim(data)[2]
>        statistics = portfolioStatistics(data, spec)
>        tailRisk = spec@model$tailRisk
>        ans <- new("fPFOLIODATA", data = list(series = data, nAssets =
> nAssets),
>            statistics = statistics, tailRisk = tailRisk)
>     }
>     if(class(data) == "list") {
>       statistics = list(mu = data$mu, Sigma = data$Sigma )
>       attr(statistics, "estimator") = spec@model$estimator
>       ans <- new("fPFOLIODATA", data = list( nAssets = length(data$mu)
> ), statistics = statistics, tailRisk = list() )
>     }
>     ans
> }
>
>
> ########################################################################
> ############
>
> .efficientConstrainedMVPortfolio <- function (data, spec, constraints) {
>     if (!inherits(data, "fPFOLIODATA"))
>         data = portfolioData(data, spec)
>     mu = getMu(data)
>     Sigma = getSigma(data)
>     nAssets = getNumberOfAssets(data)
>     targetAlpha = getTargetAlpha(spec)
>     solver = getSolver(spec)
>     stopifnot(solver == "quadprog" | solver == "Rdonlp2")
>     if (solver == "quadprog") {
>         portfolio = solveRQuadprog(data, spec, constraints)
>     }
>     else if (solver == "Rdonlp2") {
>         portfolio = solveRDonlp2(data, spec, constraints)
>     }
>     weights = portfolio$weights
>     attr(weights, "status") <- portfolio$status
>     names(weights) = names(mu)
>     targetReturn = matrix(as.numeric(mu %*% weights), nrow = 1)
>     colnames(targetReturn) <- getEstimator(spec)[1]
>     covTargetRisk = sqrt(as.numeric(weights %*% Sigma %*% weights))
> #   x = getSeries(data)@Data %*% weights
> #   VaR = quantile(x, targetAlpha, type = 1)
> #   CVaR = VaR - 0.5 * mean(((VaR - x) + abs(VaR - x)))/targetAlpha
> #   targetRisk = matrix(c(covTargetRisk, CVaR, VaR), nrow = 1)
> #   colnames(targetRisk) <- c("cov", paste(c("CVaR.", "VaR."),
> #   targetAlpha * 100, "%", sep = ""))
>     targetRisk = matrix(c(covTargetRisk), nrow = 1)
>     ## is needed to use the plotting functions....
>     targetRisk = matrix(c(covTargetRisk, covTargetRisk ), nrow = 1)
>     colnames(targetRisk) <- c( "cov", "dummy" )
>     new("fPORTFOLIO", call = match.call(), data = list(data = data),
>         spec = list(spec = spec), constraints =
> as.character(constraints),
>         portfolio = list(weights = weights, targetReturn = targetReturn,
>             targetRisk = targetRisk, targetAlpha = targetAlpha,
>             status = portfolio$status), title = paste("Constrained MV
> Portfolio - Solver:",
>             solver), description = .description()) }
>
> ########################################################################
> ############
>
> .minvarianceConstrainedMVPortfolio <- function (data, spec, constraints)
> {
>     if (!inherits(data, "fPFOLIODATA"))
>         data = portfolioData(data, spec)
>     mu = getMu(data)
>     Sigma = getSigma(data)
>     nAssets = getNumberOfAssets(data)
>     targetAlpha = getTargetAlpha(spec)
>     .minVariancePortfolioFun = function(x, data, spec, constraints) {
>         spec@portfolio$targetReturn = x
>         ans = .efficientConstrainedMVPortfolio(data = data, spec = spec,
>             constraints = constraints)
>         f = getTargetRisk(ans)[1]
>         attr(f, "targetReturn") <- getTargetReturn(ans)
>         attr(f, "targetRisk") <- getTargetRisk(ans)[1]
>         attr(f, "weights") <- getWeights(ans)
>         f
>     }
>     minVar = optimize(.minVariancePortfolioFun, interval = range(mu),
>         data = data, spec = spec, constraints = constraints,
>         tol = .Machine$double.eps^0.5)
>     weights = attr(minVar$objective, "weights")
>     names(weights) = names(mu)
>     targetReturn = spec@portfolio$targetReturn =
> as.numeric(attr(minVar$objective,
>         "targetReturn"))
>     targetReturn = matrix(targetReturn, nrow = 1)
>     colnames(targetReturn) <- spec@model$estimator[1]
>     covTargetRisk = as.numeric(attr(minVar$objective, "targetRisk"))
>     # x = getSeries(data)@Data %*% weights
>     # VaR = quantile(x, targetAlpha, type = 1)
>     # CVaR = VaR - 0.5 * mean(((VaR - x) + abs(VaR - x)))/targetAlpha
>     #targetRisk = matrix(c(covTargetRisk, CVaR, VaR), nrow = 1)
>     #colnames(targetRisk) <- c("cov", paste(c("CVaR.", "VaR."),
>     targetRisk = matrix(c(covTargetRisk), nrow = 1)
>     ## is needed to use the plotting functions....
>     targetRisk = matrix(c(covTargetRisk, covTargetRisk ), nrow = 1)
>     colnames(targetRisk) <- c( "cov", "dummy" )
>     new("fPORTFOLIO", call = match.call(), data = list(data = data),
>         spec = list(spec = spec), constraints =
> as.character(constraints),
>         portfolio = list(weights = weights, targetReturn = targetReturn,
>             targetRisk = targetRisk, targetAlpha = targetAlpha,
>             status = 0), title = "Minimum Variance Portfolio",
>         description = .description())
> }
>
> show.fPORTFOLIO <- function (object)
> {
>     cat("\nTitle:\n ")
>     cat(getTitle(object), "\n")
>     cat("\nCall:\n ")
>     print.default(getCall(object))
>     cat("\nPortfolio Weight(s):\n")
>     weights = round(getWeights(object), digits = 4)
>     if (length(weights) == 1) {
>         cat(" ", weights, "\n")
>     }
>     else {
>         print.table(weights)
>     }
>     cat("\nRiskBudget(s):\n")
>     riskBudgets = round(getCovRiskBudgets(object), digits = 4)
>     if (length(riskBudgets) == 1) {
>         cat(" ", riskBudgets, "\n")
>     }
>     else {
>         print.table(riskBudgets)
>     }
>     if (FALSE) {
>         if (!is.na(getTailRiskBudgets(object))) {
>             cat("\nRiskBudget(s):\n")
>             riskBudgets = round(getTailRiskBudgets(object), digits = 4)
>             if (length(riskBudgets) == 1) {
>                 cat(" ", riskBudgets, "\n")
>             }
>             else {
>                 print.table(riskBudgets)
>             }
>         }
>     }
>     targetReturn = object@portfolio$targetReturn
>     targetRisk = object@portfolio$targetRisk
>     spec = getSpec(object)
>     cat("\nTarget Risk(s) and Return(s):\n")
>     if (is.null(dim(targetReturn))) {
>         targetReturn = matrix(targetReturn, nrow = 1)
>         colnames(targetReturn) = getEstimator(spec)[1]
>     }
>     if (is.null(dim(targetRisk))) {
>         targetRisk = matrix(targetRisk, nrow = length(targetRisk) )
>         colnames(targetRisk) = getEstimator(spec)[2]
>     }
>     target = cbind(targetReturn, targetRisk)
>     colnames(target) = c(colnames(targetReturn), colnames(targetRisk) )
>     if (nrow(target) == 1) {
>         print(target[1, ])
>     }
>     else {
>         print(target)
>     }
>     cat("\nDescription:\n ")
>     cat(getDescription(object), "\n")
>     invisible(object)
> }
>
> setMethod("show", "fPORTFOLIO", show.fPORTFOLIO)
>
> ########################################################################
> ############
>
> .portfolioConstrainedMVFrontier <- function (data, spec, constraints) {
>     if (!inherits(data, "fPFOLIODATA"))
>         data = portfolioData(data, spec)
>     mu = getMu(data)
>     Sigma = getSigma(data)
>     nAssets = getNumberOfAssets(data)
>     targetAlpha = getTargetAlpha(spec)
>     nFrontierPoints = getNFrontierPoints(spec)
>     targetReturn = targetRisk = targetWeights = error = NULL
>     Spec = spec
>     solver = spec@solver$solver
>     Spec@portfolio$weights = rep(1/nAssets, nAssets)
>     k = 0
>     solverType = spec@solver$solver
>     status = NULL
>     for (nTargetReturn in seq(min(mu), max(mu), length =
> nFrontierPoints)) {
>         k = k + 1
>         setTargetReturn(Spec) <- nTargetReturn
>         nextPortfolio = .efficientConstrainedMVPortfolio(data = data,
>             spec = Spec, constraints = constraints)
>         Spec@portfolio$weights = nextPortfolio@portfolio$weights
>         targetReturn = rbind(targetReturn,
> nextPortfolio@portfolio$targetReturn)
>         targetRisk = rbind(targetRisk,
> nextPortfolio@portfolio$targetRisk)
>         nextWeights = nextPortfolio@portfolio$weights
>         names(nextWeights) = names(mu)
>         status = c(status, nextPortfolio@portfolio$status)
>         targetWeights = rbind(targetWeights, t(nextWeights))
>     }
>     Index = (1:length(status))[status == 0]
>     weights = targetWeights
>     colnames(weights) = names(mu)
>     weights = weights[Index, ]
>     DIM = dim(targetReturn)[2]
>     targetReturn = targetReturn[Index, ]
>     targetReturn = matrix(targetReturn, ncol = DIM)
>     colnames(targetReturn) = getEstimator(spec)[1]
>     targetRisk = targetRisk[Index, ]
>     new("fPORTFOLIO", call = match.call(), data = list(data = data),
>         spec = list(spec = spec), constraints =
> as.character(constraints),
>         portfolio = list(weights = weights, targetReturn = targetReturn,
>             targetRisk = targetRisk, targetAlpha = targetAlpha,
>             status = status), title = "Constrained MV Frontier",
>         description = .description())
> }
>
> ########################################################################
> ############
>
> # You should be able to specify the data in this form:
> mu <- c( 0.1, 0.08, 0.065)
> sigma <- c( 0.18, 0.12, 0.09 )
>
> correlationMatrix <- rbind( c( 1, 0.8, 0.9 ),
>                                               c( 0.8, 1, 0.75),
>                                               c( 0.9, 0.75, 1) )
>
> covarianceMatrix <- cor2cov(correlationMatrix, sigma )
>
>
> data = list( mu = mu, Sigma = covarianceMatrix )
>
> # And then do the optimisation
> frontier <- portfolioFrontier(data, spec = spec, constraints )
>
> _______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-sig-finance
> -- Subscriber-posting only.
> -- If you want to post, subscribe first.
>
>

_______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-sig-finance
-- Subscriber-posting only.
-- If you want to post, subscribe first.

_______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-sig-finance
-- Subscriber-posting only.
-- If you want to post, subscribe first.
Reply | Threaded
Open this post in threaded view
|

Re: Static Portfolio Optimization

Patrick Burns-2
In reply to this post by DonDiego
Jorge Nieves wrote:
> Thanks for your responses. I found the function "portfolio.optim" and
> the tseries series package [...] However, I still do not see how to
> pass into the function the vector of expected values and the covariance
> matrix.
[...]

This bothers me on two counts:

1) computational
2) subject matter

That Jorge is not finding the functionality
that he wants means that modular programming
is not being used.  The modular approach
would have a function that does the optimization
with the expected returns and variance as
arguments.  That function would then be
used by a function that does the larger task.

Okay, an advantage of R is that it is generally
easy to modify functions for your own purposes.
But it is better to organize ocmputations so
that people don't feel compelled to do that.
Let's abandon the SAS monolith culture.

I haven't investigated the functions that are
under discussion, so perhaps I am misunderstanding
what they are doing.  But if they are using the
history of returns to predict future returns, that
is almost always going to be pretty much complete
nonsense -- with or without the Efficient Market
Hypothesis.

I would hope the R community embrace quality in
subject matter decisions as well as computational
quality.


Patrick Burns
[hidden email]
+44 (0)20 8525 0696
http://www.burns-stat.com
(home of "The R Inferno" and "A Guide for the Unwilling S User")


>
> Thanks,
>
>
> Jorge Nieves
>
>
> -----Original Message-----
> From: Dirk Eddelbuettel [mailto:[hidden email]]
> Sent: Thursday, October 01, 2009 04:25 PM
> To: [hidden email]
> Cc: Jorge Nieves; [hidden email];
> [hidden email]
> Subject: Re: [R-SIG-Finance] Static Portfolio Optimization
>
>
> On 1 October 2009 at 15:01, [hidden email] wrote:
> | I think I remember Adrian Trapletti implemented static portfolio
> | optimization using quadprog in tseries. It's been a long time so I
> | don't remember the details ( it may be restricted to long only but you
>
> | can add variables to make<br />it handle long short ) but you may want
>
> | to check tseries out.
>
> Correct -- the tseries package has done since time immportal.
>
> And some seven or eight years ago I sent Adrian a patch, styled after
> the discussion in Huang and Litzenberger, that adds the ability to have
> long and short positions.  
>
> Which Adrian promptly added and which has been there ever since:
>
> R> head(as.zoo(EuStockMarkets))
>            DAX  SMI  CAC FTSE
> 1991(130) 1629 1678 1773 2444
> 1991(131) 1614 1688 1750 2460
> 1991(132) 1607 1679 1718 2448
> 1991(133) 1621 1684 1708 2470
> 1991(134) 1618 1687 1723 2485
> 1991(135) 1611 1672 1714 2467
> R> X <- diff(log(as.zoo(EuStockMarkets)))
> R> res <- portfolio.optim(X)                 ## Long only
> R> res$pw
> [1] 0.0000 0.3958 0.0000 0.6042
> R> res <- portfolio.optim(X, shorts=TRUE)    ## Long/Short
> R> res$pw
> [1]  0.02550  0.38213 -0.06377  0.65614
> R>
>
> This is arguably a better example than the one in help(portfolio.optim)
> so maybe I should send Kurt a new patch :)
>
> Dirk
>
> --
> Three out of two people have difficulties with fractions.
>
> _______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-sig-finance
> -- Subscriber-posting only.
> -- If you want to post, subscribe first.
>
>

_______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-sig-finance
-- Subscriber-posting only.
-- If you want to post, subscribe first.
Reply | Threaded
Open this post in threaded view
|

Re: Static Portfolio Optimization

Adrian Trapletti
In reply to this post by Jesse Velez
I agree with Patrick. I wrote tseries about 10(?) years ago and for
sure, it may be possible to improve it. Concerning the second point, it
is in fact highly non-trivial if not impossible to construct portfolios
that are "better" out-of-sample than naive portfolios. Raman Uppal from
the London Business School and other authors wrote several nice papers
about the subject. Googling I found the following one:
http://papers.ssrn.com/sol3/papers.cfm?abstract_id=911512

Best regards
Adrian

> Message: 4
> Date: Fri, 02 Oct 2009 19:57:58 +0100
> From: Patrick Burns <[hidden email]>
> Subject: Re: [R-SIG-Finance] Static Portfolio Optimization
> To: Jorge Nieves <[hidden email]>
> Cc: [hidden email], [hidden email]
> Message-ID: <[hidden email]>
> Content-Type: text/plain; charset=ISO-8859-1; format=flowed
>
> Jorge Nieves wrote:
>  
>> > Thanks for your responses. I found the function "portfolio.optim" and
>> > the tseries series package [...] However, I still do not see how to
>> > pass into the function the vector of expected values and the covariance
>> > matrix.
>>    
> [...]
>
> This bothers me on two counts:
>
> 1) computational
> 2) subject matter
>
> That Jorge is not finding the functionality
> that he wants means that modular programming
> is not being used.  The modular approach
> would have a function that does the optimization
> with the expected returns and variance as
> arguments.  That function would then be
> used by a function that does the larger task.
>
> Okay, an advantage of R is that it is generally
> easy to modify functions for your own purposes.
> But it is better to organize ocmputations so
> that people don't feel compelled to do that.
> Let's abandon the SAS monolith culture.
>
> I haven't investigated the functions that are
> under discussion, so perhaps I am misunderstanding
> what they are doing.  But if they are using the
> history of returns to predict future returns, that
> is almost always going to be pretty much complete
> nonsense -- with or without the Efficient Market
> Hypothesis.
>
> I would hope the R community embrace quality in
> subject matter decisions as well as computational
> quality.
>
>
> Patrick Burns
> [hidden email]
> +44 (0)20 8525 0696
> http://www.burns-stat.com
> (home of "The R Inferno" and "A Guide for the Unwilling S User")
>
>
>  

--
Adrian Trapletti
Steinstrasse 9b
8610 Uster
Switzerland

Phone : +41 (0) 44 9945630
Mobile : +41 (0) 76 3705631

Email : [hidden email]

_______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-sig-finance
-- Subscriber-posting only.
-- If you want to post, subscribe first.