-
Notifications
You must be signed in to change notification settings - Fork 3
/
diversification.R
59 lines (49 loc) · 1.68 KB
/
diversification.R
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
########################################
# Diversification
# Claudio R. Lucinda
########################################
#library(doParallel)
library(BatchGetSymbols)
library(PortfolioAnalytics)
library(xlsx)
library(tidyverse)
library(quantmod)
#registerDoParallel()
rm(list=ls())
graphics.off()
load("IbovData.RDS")
inner2 <- function(.x,.df,.wts,.pb=NULL) {
if (.pb$i < .pb$n) .pb$tick()$print()
teste2<-na.omit(.df[,.x])
returns<-data.frame(lapply(teste2,Return.calculate))[-1,]
data_02<-rownames(returns)
returns<-xts(returns,order.by=as.Date(data_02, format="%Y-%m-%d"))
carteira_rebal<-Return.portfolio(returns,weights=.wts, rebalance_on="months",verbose=FALSE)
out<-sd(carteira_rebal$portfolio.returns)
return(out)
}
inner <- function(.n,.df) {
teste<-combn(colnames(.df),.n,simplify = FALSE)
teste_menor<-sample(teste,100)
testewgts<-rep((1/.n), each=.n)
cat(paste0("Portfolios with ",.n," assets"))
pb<-dplyr::progress_estimated(length(teste_menor),0)
ports<-plyr::ldply(teste_menor, function(x) inner2(x,.df=.df,.wts=testewgts,.pb=pb))
#ports<-plyr::ldply(teste, inner2,.df=.df,.wts=testewgts,.pb=pb,.parallel = TRUE,.paropts=list(.export=c("inner2"),.packages=c("xts","PortfolioAnalytics")))
med_sd<-mean(ports$V1)
out<-data.frame(.n,med_sd)
return(out)
}
outer <- function(.df,.min,.max) {
out<-plyr::ldply(seq(.min,.max,1),function(x) inner(x,.df=.df))
return(out)
}
teste<-outer(data_IBOV,2,8)
teste2<-outer(data_IBOV,30,35)
Port_div<-rbind(teste,teste2)
p<-ggplot(data=Port_div, aes(x=.n, y=med_sd)) +
geom_line() + ggtitle("Diversificação em Carteiras") +
labs(x="Nro. de Ativos", y="Desvio-Padrão dos Retornos")
png("Diversif.png")
p
dev.off()