- Medidas Estadísticas de Dispersión Relativa - junio 11, 2024
- Modelos SARIMA(Arima Estacionales).¿Qué son y cómo usarlos para Predecir? - enero 4, 2024
- ¿Qué es el RUIDO BLANCO ? Econometría en R - junio 21, 2023
Codigo R
# Portfolios de Markowitz
# ** #media- varianzas/Var/Cvar
## porfolio analytics
#install.packages(«portfolio»)
library(portfolio)
library(quadprog)
args(solve.QP)
## mínimizar matriz de varianzas – covarianzas
#Dmat matrix appearing in the quadratic function to be minimized
#dvec vector appearing in the quadratic function to be minimized
#Amat matrix defining the constraints
#bvec vector holding the values of $b_0$ (defaults to zero).
#meq the first meq constraints are treated as equality constraints,
#all further as inequality constraints (defaults to
#install.packages(«quantmod»)
library(quantmod)
#getFX(«EUR/USD»)
library(«tseries»)
library(«fImport»)
library(«urca»)
library(«vars»)
getSymbols(«CADCHF=X»,src=»yahoo»,from = «2017-02-18»)
getSymbols(«AUDNZD=X»,src=»yahoo»,from = «2017-02-18»)
getSymbols(«AUDUSD=X»,src=»yahoo»,from = «2017-02-18»)
getSymbols(«EURCHF=X»,src=»yahoo»,from = «2017-02-18»)
getSymbols(«EURGBP=X»,src=»yahoo»,from = «2017-02-18»)
getSymbols(«EURCAD=X»,src=»yahoo»,from = «2017-02-18»)
getSymbols(«CADCHF=X»,src=»yahoo»,from = «2019-01-01»)
getSymbols(«AUDNZD=X»,src=»yahoo»,from = «2019-01-01»)
getSymbols(«AUDUSD=X»,src=»yahoo»,from = «2019-01-01»)
getSymbols(«EURCHF=X»,src=»yahoo»,from = «2019-01-01»)
getSymbols(«EURGBP=X»,src=»yahoo»,from = «2019-01-01»)
getSymbols(«EURCAD=X»,src=»yahoo»,from = «2019-01-01»)
CADCHF= `CADCHF=X`[,4] # precios diarios de cierre
AUDNZD= `AUDNZD=X`[,4]
AUDUSD= `AUDUSD=X`[,4]
EURCHF= `EURCHF=X`[,4]
EURGBP= `EURGBP=X`[,4]
EURCAD= `EURCAD=X`[,4]
#### tabla
forex_mark <-na.omit(cbind(CADCHF,AUDNZD,AUDUSD,EURCHF,EURGBP,EURCAD))
plot(forex_mark)
#Calculamos los retornos y x 100 para calcularlos en %
forex_mark_ret=na.omit(returns(forex_mark))
forex_mark_ret = 100 * forex_mark_ret
Forex_ret<-as.timeSeries(forex_mark_ret)
names(Forex_ret)<- cbind(«CADCHF»,»AUDNZD»,»AUDUSD»,»EURCHF»,»EURGBP»,»EURCAD»)
plot(Forex_ret)
apply(Forex_ret,2,mean) #♠ media diaria
#mu<-matrix(apply(Forex_ret,2,mean))
#mean(mu)
#head(FX)
portfolio <- function(assetReturns, targetReturn)
{
# Arguments:
# assetReturns – multivariate data set of asset returns
# target Return – the portfolios target return
# 1 Create Portfolio Settings:
nAssets = ncol(assetReturns)
Dmat = cov(assetReturns)
dvec = rep(0, times=nAssets)
Amat = t(rbind(
Return=colMeans(assetReturns),
Budget=rep(1, nAssets),
LongOnly=diag(nAssets)))
bvec = c(
Return=targetReturn,
budget=1,
LongOnly=rep(0, times=nAssets))
meq = 2
# 2 Optimize Weights:
portfolio = solve.QP(Dmat, dvec, Amat, bvec, meq)
weights = round(portfolio$solution, digits = 4)
names(weights) = colnames(assetReturns)
# Return Value:
list(
weights = 100*weights,
risk = portfolio$value,
return = targetReturn)
}
#library(fBasics)
assetReturns <- Forex_ret[, 1:6]
names(assetReturns)
targetReturn <- mean(colMeans(assetReturns))
targetReturn # media diaria todo el portfolio
#install.packages(«portfolio»)
#library(portfolio)
portfolio <- portfolio(assetReturns, targetReturn)
portfolio
weights = portfolio$weights
names(weights) = colnames(Forex_ret)
weights
sum(weights)
# c(weightedReturn = round((weights %*% colMeans(assetReturns))[[1]],
#3), targetReturn = round(100 * targetReturn, 3))
args(pie)
Weights = weights[weights > 0]
pie(Weights, labels = names(Weights))
title(main = «Cartera Forex 2020»)
Hola amigo,
Me sale este error, a qué crees que se deba:
Error in solve.QP(Dmat, dvec, Amat, bvec, meq) :
matrix D in quadratic function is not positive definite!
tengo que revisar el código, pero imagino que será porque la matriz D con concuerda con las restriciones. Ahi es que solo sale el error