Testing expectations

In our final submit, we analyzed the efficiency of our portfolio, constructed utilizing the historic common methodology to set return expectations. We calculated return and danger contributions and examined modifications in allocation weights as a result of asset efficiency. We briefly thought of whether or not such modifications warranted rebalancing and what influence rebalancing may need on long run portfolio returns given the drag from taxes. On the finish, we requested what efficiency expectations we should always have needed to analyze leads to the primary place. It’s all effectively and good to say {that a} portfolio carried out by “X”, however shouldn’t we’ve had a speculation as to how we anticipated the portfolio to carry out?

Certainly we did. As we famous within the submit, the historic averages methodology assumes the longer term will resemble of the previous. However that may be a slightly nebulous assertion. It begs the query, by how a lot? On this submit, we goal to develop a framework to quantify the probability our portfolio will meet our anticipated efficiency targets. This method may be very a lot influenced by a speak given by Prof. Marcos López de Prado. Among the many many issues mentioned, the one which struck us most is the function simulation performs vis a vis backtesing. True, simulation has it’s personal points, however not less than you’re not counting on a single trial to formulate a speculation. Basing an funding technique on the outcomes of what occurred traditionally is, in some ways, basing it on a single trial regardless of how a lot one believes historical past repeats, rhymes, or rinses.

Sufficient tongue-wagging, let’s get to the info! Right here’s our street map.

Evaluation knowledge

Recall, our portfolio was comprised of the most important asset lessons: shares, bonds, commodities (gold), and actual property. Knowledge was pulled from the FRED database and started in 1987, the earliest date for a whole knowledge set.

We took the typical return and danger of these belongings for the interval of 1987 to 1991 and constructed 1,000 portfolios based mostly on random weights. We then discovered all of the portfolios that met our standards of not lower than a 7% return and less than 10% danger on an annual foundation. The common weights of these chosen portfolios grew to become our portfolio allocation for the check interval. We calculated the return and danger for our portfolio (with out rebalancing) through the first check interval and in contrast that inside one other simulation of 1,000 potential portfolios. We graph that comparability under.

First simulation

For those who’re pondering, why are we speaking about extra simulations after we’ve already achieved two rounds of them, it’s a official query. The reply: we didn’t simulate potential returns and danger. Keep in mind we took the historic five-year common return and danger of the belongings and plugged these numbers into our weighting algorithm to simulate potential portfolios. We didn’t simulate the vary of potential returns. It must be apparent that the returns we hoped to realize based mostly on our portfolio allocation weren’t more likely to be the returns we might obtain. However what ought to we anticipate? Solely 27% of the portfolios in our simulation would have achieved our risk-return constraints based mostly on utilizing the historic common as a proxy for our return expectation.

Is that an correct likelihood? Laborious to inform after we’re basing these return expectations on just one interval. A extra scientific means of taking a look at it may very well be to run a bunch of simulations to quantify how doubtless the chance and return constraints we selected would happen.

We’ll simulate 1,000 completely different return profiles of the 4 asset lessons utilizing their historic common return and danger. However as an alternative of solely utilizing knowledge from 1987, the earliest date wherein we’ve a full knowledge set, we’ll calculate the typical based mostly on as a lot of the info as is out there. That does create one other bias, however on this case solely pertains to actual property, which has the shortest interval from our knowledge supply.

Earlier than we proceed, we need to flag an issue with simulations: your assumptions round danger and return will probably be reproduced carefully within the simulation with out making an adjustment. Say you assume asset A has a 5% return and 12% danger. Guess what? Your simulation can have near a 5% return and 12% danger. Thus we embody a noise issue equal to a imply of zero and the asset’s customary deviation to account for uncertainty round our estimates.

Let’s pattern 4 of the return simulations, run the portfolio allocation simulation on them, and graph the output.

Three of the simulations lead to upwardly sloping risk-return portfolios, one doesn’t. Finance principle posits that one ought to typically be compensated for taking extra danger with larger returns. However there are circumstances the place that doesn’t happen. And this isn’t simply an artifact of randomness; it occurs in actuality too. Consider shopping for shares earlier than the tech or housing bubbles burst.

Wanting in additional element on the graphs, one can see that even when the vary of danger is near the identical, returns aren’t. Therefore, the portfolio weights to realize our risk-return constraints may very well be fairly completely different, maybe not even attainable. For instance, in three out of 4 of our samples, nearly any allocation would get you returns larger than 7% more often than not. However lower than a 3rd of the portfolios would meet the chance constraint. Recall our earlier simulation prompt our likelihood of assembly our risk-return constraints was 27%. Certainly, based mostly on these 4 samples the probability of assembly our risk-return constraints are 8%, 17%, 25%, and 26%. Not an important end result.

Second simulation

Critically, these outcomes had been solely 4 randomly chosen samples from 1,000 simulations. We need to run the portfolio allocation simulation on every of the return simulations. That might yield about 1,000,000 completely different portfolios.

Let’s run these simulations after which graph the histograms. We gained’t graph the risk-return scatter plot as we did above for your entire simulation as a result of the outcomes appear like a snowstorm in Alaska. As an alternative, we’ll pattern 10,000 portfolios and graph them within the standard means.

A large scattering of returns and danger, as one may anticipate.Listed below are the return and danger histograms of the million portfolios.

Portfolio returns are near usually distributed (although that’s largely because of the simulation), whereas danger is skewed because of the means portfolio volatility is calculated. Suffice it to say, these distributions aren’t uncommon. However we must be significantly conscious of the return distribution, which we’ll talk about later.

The common return and danger are 10% and 12% on an annualized foundation. That danger stage suggests a decrease probability of hitting our risk- return constraints. Certainly. after we run the calculation, solely 19% of the portfolios obtain our objective, decrease than our first simulation.


This check gives some stable insights into our authentic portfolio allocation. First, whereas our chosen constraints had been solely achieved in 27% of the portfolios. That didn’t inform us how doubtless these constraints had been to happen over many potential outcomes. Even after the primary five-year check, there wasn’t proof that our constraints had been unrealistic due to the time interval wherein the check fell: particularly, the early-to-mid a part of the tech increase.

After we ran the second five-year check our portfolio missed our targets. However may we are saying with confidence that such an end result was a fluke? It wasn’t till we ran the return and portfolio allocation simulations collectively that we had been capable of quantify the probability of our targets. As famous, solely 19% had been more likely to meet our constraints. Armed with that data, we should always revise our constraints both by reducing our return expectation or rising our danger tolerance.

If we had run that simulation earlier than our first check, it might have been comparatively apparent that the efficiency was uncommon. Certainly, the larger than 9% return with low danger that the portfolio achieved occurred solely 14% of the time in our portfolio simulations. The upshot: not not possible, however not all that doubtless both.

From these outcomes it appears clear that one must be cautious concerning the interval used for a backtest. Clearly, we need to see how the portfolio would have carried out traditionally. However that is just one end result from the lab, so to talk.


Whereas the check outcomes are helpful, there are a few issues with the evaluation we need to level out. First, recall that our portfolio weightings assume we make investments some quantity in all accessible belongings. If we had been to exclude some belongings from the portfolio, the vary of outcomes would improve, as we confirmed in our final submit. That might improve the likelihood of assembly our constraints. However we might additionally need to watch out about how a lot we should always belief that elevated probabilty: it’s doubtless we may get not less than a 5 proportion level enchancment merely as a result of larger randomness.

Second, whereas we simulated asset returns, we didn’t simulate correlations, as mentioned right here. Low correlation amongst belongings reduces portfolio volatility, although variance often has a much bigger influence. However asset correlations aren’t secure over time. Therefore, for completeness our simulations ought to embody the influence of random modifications in correlation. We’d hope that the random attracts implicitly incorporate randomness in correlations, however we’d must run some calculations to substantiate. Alternatively, we may explicitly try and randomize the correlations, however that isn’t as straightforward because it sounds and may result in counter-intuitive, and even subsequent to not possible outcomes.

Third, our simulations drew from a standard distribution and most folk know that returns distributions for monetary belongings aren’t regular. One approach to clear up that is to pattern from historic returns. If the pattern size and timing had been the identical throughout the belongings, then we might have partly addressed the correlation problem too. However, as famous earlier than, our knowledge collection aren’t equal in size. Therefore, we’d be introducing some bias if we solely sampled from these durations wherein we had knowledge for each asset.

Fourth, historic returns aren’t unbiased of each other. In different phrases, at present’s return on inventory ABC, relates, to various levels, on ABC’s returns from yesterday and even additional out, generally known as serial correlation. Equally, ABC’s returns at present exhibit some relation to yesterday’s returns for inventory DEF, bond, GHI, and so forth, generally known as cross-correlation. Block sampling can tackle this to a sure diploma. However selecting the block dimension is non-trivial.

Even when we’re capable of finding a sufficiently lengthy and clear knowledge set, we have to acknowledge that the returns in that pattern, are nonetheless that, a pattern, and thus not consultant of of the complete vary of potential outcomes (no matter that applies in your cosmology). One want solely examine historic returns previous to and after the onset of the covid-19 pandemic to attest to the truth that sudden return patterns nonetheless happen extra usually than anticipated in monetary knowledge. There a bunch of the way to cope with this together with extra subtle sampling strategies, imputing distributions utilizing Bayesian approaches, or bagging exponential smoothing strategies utilizing STL decomposition and Field-Cox transformation. We’ll cowl the final one intimately in our subsequent submit! Kidding apart, we’re an enormous fan of Prof. Hyndman’s work. However the level is dealing with sudden returns requires an increasing number of sophistication. Addressing the deserves of including such sophistication is finest left for an additional submit.

Subsequent steps

Let’s overview. Over the previous seven posts we checked out completely different strategies to set return expectations for belongings we’d need to embody in our portfolio. We then used the only methodology—historic averages—as the muse for our asset allocation. From there, we simulated a variety of potential portfolios, and selected an asset weighting to create a portfolio that will meet our risk-return standards. We then allotted the belongings accordingly and checked out the way it carried out over two five-year check durations. We famous that rebalancing or exclusion of belongings could be warranted, however shelved these briefly. We then examined how cheap our risk-return expectations had been and located that, generally, they appeared a bit unrealistic. What we haven’t checked out is whether or not selecting portfolio weights finally ends up producing a greater end result than the naive equal-weighted portfolio both traditionally or in simulation. And we additionally haven’t checked out discovering an optimum portfolio for a given stage of danger or return. Keep tuned.

Right here’s the R adopted by the Python code used to provide this submit. Order doesn’t replicate choice.

R code:

# Constructed utilizing R 3.6.2 ## Load packages
suppressPackageStartupMessages({ library(tidyquant) library(tidyverse) library(grid)
}) ## Load knowledge
sym_names <- c("inventory", "bond", "gold", "realt", "rfr")
getSymbols(symbols, src="FRED", from = "1970-01-01", to = "2019-12-31") for(i in 1:5){ x <- getSymbols(symbols[i], src = "FRED", from = "1970-01-01", to = "2019-12-31", auto.assign = FALSE) x <- to.month-to-month(x, indexAt ="lastof", OHLC = FALSE) assign(sym_names[i],x)
} dat <- merge(inventory, bond, gold, realt, rfr)
colnames(dat) <- sym_names
dat <- dat["1970/2019"] ## create knowledge body
df <- knowledge.body(date = index(dat), coredata(dat)) %>% mutate_at(vars(-c(date, rfr)), operate(x) x/lag(x)-1) %>% mutate(rfr = rfr/100) df <- df %>% filter(date>="1987-01-01") ## Load simuation operate
port_sim <- operate(df, sims, cols){ if(ncol(df) != cols){ print("Columns do not match") break } # Create weight matrix wts <- matrix(nrow = sims, ncol = cols) for(i in 1:sims){ a <- runif(cols,0,1) b <- a/sum(a) wts[i,] <- b } # Discover returns mean_ret <- colMeans(df) # Calculate covariance matrix cov_mat <- cov(df) # Calculate random portfolios port <- matrix(nrow = sims, ncol = 2) for(i in 1:sims){ port[i,1] <- as.numeric(sum(wts[i,] * mean_ret)) port[i,2] <- as.numeric(sqrt(t(wts[i,]) %*% cov_mat %*% wts[i,])) } colnames(port) <- c("returns", "danger") port <- as.knowledge.body(port) port$Sharpe <- port$returns/port$danger*sqrt(12) max_sharpe <- port[which.max(port$Sharpe),] graph <- port %>% ggplot(aes(danger*sqrt(12)*100, returns*1200, shade = Sharpe)) + geom_point(dimension = 1.2, alpha = 0.4) + scale_color_gradient(low = "darkgrey", excessive = "darkblue") + labs(x = "Threat (%)", y = "Return (%)", title = "Simulated portfolios") out <- checklist(port = port, graph = graph, max_sharpe = max_sharpe, wts = wts) } ## Run simuation
port_sim_1 <- port_sim(df_old[2:61,2:5],1000,4) ## Load portfolio choice operate
port_select_func <- operate(port, return_min, risk_max, port_names){ port_select <- cbind(port$port, port$wts) port_wts <- port_select %>% mutate(returns = returns*12, danger = danger*sqrt(12)) %>% filter(returns >= return_min, danger <= risk_max) %>% summarise_at(vars(4:7), imply) %>% `colnames<-`(port_names) graph <- port_wts %>% rename("Shares" = 1, "Bonds" = 2, "Gold" = 3, "Actual property" = 4) %>% collect(key,worth) %>% ggplot(aes(reorder(key,worth), worth*100 )) + geom_bar(stat='identification', place = "dodge", fill = "blue") + geom_text(aes(label=spherical(worth,2)*100), vjust = -0.5) + scale_y_continuous(limits = c(0,40)) + labs(x="", y = "Weights (%)", title = "Common weights for risk-return constraints") out <- checklist(port_wts = port_wts, graph = graph) out } ## Run choice operate results_1 <- port_select_func(port_sim_1,0.07, 0.1, sym_names[1:4]) ## Calculate likelihood of success
port_success <- spherical(imply(port_sim_1$port$returns > 0.07/12 & port_sim_1$port$danger <= 0.1/sqrt(12)),3)*100 ## Operate for portfolio returns with out rebalancing
rebal_func <- operate(act_ret, weights){ ret_vec <- c() wt_mat <- matrix(nrow = nrow(act_ret), ncol = ncol(act_ret)) for(i in 1:60){ wt_ret <- act_ret[i,]*weights # wt'd return ret <- sum(wt_ret) # whole return ret_vec[i] <- ret weights <- (weights + wt_ret)/(sum(weights)+ret) # new weight based mostly on change in asset worth wt_mat[i,] <- as.numeric(weights) } out <- checklist(ret_vec = ret_vec, wt_mat = wt_mat) out
} ## Run operate and create precise portfolio and knowledge body for graph
port_1_act <- rebal_func(df[62:121,2:5],results_1$port_wts) port_act <- knowledge.body(returns = imply(port_1_act$ret_vec), danger = sd(port_1_act$ret_vec), sharpe = imply(port_1_act$ret_vec)/sd(port_1_act$ret_vec)*sqrt(12)) ## Simulate portfolios on first five-year interval
port_sim_2 <- port_sim(df[62:121,2:5], 1000, 4) ## Graph simulation with chosen portfolio
port_sim_2$graph + geom_point(knowledge = port_act, aes(danger*sqrt(12)*100, returns*1200), dimension = 4, shade="purple") + theme(legend.place = c(0.05,0.8), legend.key.dimension = unit(.5, "cm"), legend.background = element_rect(fill = NA)) ## Load long run knowledge
sym_names <- c("inventory", "bond", "gold", "realt", "rfr") for(i in 1:5){ x <- getSymbols(symbols[i], src = "FRED", from = "1970-01-01", to = "2019-12-31", auto.assign = FALSE) x <- to.month-to-month(x, indexAt ="lastof", OHLC = FALSE) assign(sym_names[i],x)
} dat <- merge(inventory, bond, gold, realt, rfr)
colnames(dat) <- sym_names
dat <- dat["1970/2019"] ## create knowledge body
df <- knowledge.body(date = index(dat), coredata(dat)) %>% mutate_at(vars(-c(date, rfr)), operate(x) x/lag(x)-1) %>% mutate(rfr = rfr/100) df <- df %>% filter(date>="1971-01-01") ## Load lengthy knowledge
df <- readRDS('port_const_long.rds') ## Put together pattern
hist_avg <- df %>% filter(date <= "1991-12-31") %>% summarise_at(vars(-date), checklist(imply = operate(x) imply(x, na.rm=TRUE), sd = operate(x) sd(x, na.rm = TRUE))) %>% collect(key, worth) %>% mutate(key = str_remove(key, "_.*"), key = issue(key, ranges =sym_names)) %>% mutate(calc = c(rep("imply",5), rep("sd",5))) %>% unfold(calc, worth) # Run simulation
sim1 <- checklist()
for(i in 1:1000){ a <- rnorm(60, hist_avg[1,2], hist_avg[1,3]) + rnorm(60, 0, hist_avg[1,3]) b <- rnorm(60, hist_avg[2,2], hist_avg[2,3]) + rnorm(60, 0, hist_avg[2,3]) c <- rnorm(60, hist_avg[3,2], hist_avg[3,3]) + rnorm(60, 0, hist_avg[3,3]) d <- rnorm(60, hist_avg[4,2], hist_avg[4,3]) + rnorm(60, 0, hist_avg[4,3]) df1 <- knowledge.body(a, b, c, d) cov_df1 <- cov(df1) sim1[[i]] <- checklist(df1, cov_df1) names(sim1[[i]]) <- c("df", "cov_df") } # Pattern 4 return paths
# Word that is the code we used to create the graphs that labored repeatedly after we ran the supply code, however wouldn't reproduce precisely in blogdown. Do not know if anybody else has skilled this problem. However after a number of makes an attempt together with repasting the code and even seeing the identical outcomes when run inside the Rmarkdown file, we gave up and skim the saved instance into blogdown. Tell us if you cannot reproduce it. set.seed(123)
grafs <- checklist()
for(i in 1:4){ samp <- pattern(1000,1) grafs[[i]] <- port_sim(sim1[[samp]]$df,1000,4)
} gridExtra::grid.organize(grafs[[1]]$graph + theme(legend.place = "none") + labs(title = NULL), grafs[[2]]$graph + theme(legend.place = "none") + labs(title = NULL), grafs[[3]]$graph + theme(legend.place = "none") + labs(title = NULL), grafs[[4]]$graph + theme(legend.place = "none") + labs(title = NULL), ncol=2, nrow=2, prime = textGrob("4 portfolio and return simulations",gp=gpar(fontsize=12))) # Calculate likelihood of hitting risk-return constraint
probs <- c()
for(i in 1:4){ probs[i] <- spherical(imply(grafs[[i]]$port$returns >= 0.07/12 & grafs[[i]]$port$danger <=0.1/sqrt(12)),2)*100
} probs ## Run simulation # Portfolio weight operate
wt_func <- operate(){ a <- runif(4,0,1) a/sum(a)
} # Portfolio volatility calculation
vol_calc <- operate(cov_dat, weights){ sqrt(t(weights) %*% cov_dat %*% weights)
} # Simulate
# Word this could run fairly shortly, not less than lower than a minute.
portfolios <- checklist()
for(i in 1:1000){ wt_mat <- as.matrix(t(replicate(1000, wt_func(), simplify = "matrix"))) avg_ret <- colMeans(sim1[[i]]$df) returns <- as.vector(avg_ret %*% t(wt_mat)) cov_dat <- cov(sim1[[i]]$df) danger <- apply(wt_mat, 1, operate(x) vol_calc(cov_dat,x)) portfolios[[i]] <- knowledge.body(returns, danger)
} port_1m <- do.name("rbind", portfolios) port_1m_prob <- spherical(imply(port_1m$returns*12 >= 0.07 & port_1m$danger*sqrt(12) <= 0.1),2)*100 ## Graph pattern of port_1m
port_samp = port_1m[sample(1e6, 1e4),] # select 10,000 samples from 1,000,000 portfolios. port_samp %>% mutate(Sharpe = returns/danger) %>% ggplot(aes(danger*sqrt(12)*100, returns*1200, shade = Sharpe)) + geom_point(dimension = 1.2, alpha = 0.4) + scale_color_gradient(low = "darkgrey", excessive = "darkblue") + labs(x = "Threat (%)", y = "Return (%)", title = "Ten thousand samples from simulation of 1 million portfolios") + theme(legend.place = c(0.05,0.8), legend.key.dimension = unit(.5, "cm"), legend.background = element_rect(fill = NA)) # Graph histograms
port_1m %>% mutate(returns = returns*100, danger = danger*100) %>% collect(key, worth) %>% ggplot(aes(worth)) + geom_histogram(bins = 100, fill = "darkblue") + facet_wrap(~key, scales = "free", labeller = as_labeller(c(returns = "Returns", danger = "Threat"))) + scale_y_continuous(labels = scales::comma) + labs(x = "", y = "Rely", title = "Portfolio simulation return and danger histograms")

Python code:

# Constructed utilizing Python 3.7.4 # Load libraries
import pandas as pd
import pandas_datareader.knowledge as internet
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline import seaborn as sns
sns.set() # Load knowledge
start_date = '1970-01-01'
end_date = '2019-12-31'
sym_names = ["stock", "bond", "gold", "realt", 'rfr']
filename = 'data_port_const.pkl' attempt: df = pd.read_pickle(filename) print('Knowledge loaded')
besides FileNotFoundError: print("File not discovered") print("Loading knowledge", 30*"-") knowledge = internet.DataReader(symbols, 'fred', start_date, end_date) knowledge.columns = sym_names data_mon = knowledge.resample('M').final()
df = data_mon.pct_change()['1987':'2019']
df.to_pickle(filename) # If you have not saved the file df = data_mon.pct_change()['1971':'2019']
pd.to_pickle(df,filename) # if you have not saved the file ## Simulation operate
class Port_sim: def calc_sim(df, sims, cols): wts = np.zeros((sims, cols)) for i in vary(sims): a = np.random.uniform(0,1,cols) b = a/np.sum(a) wts[i,] = b mean_ret = df.imply() port_cov = df.cov() port = np.zeros((sims, 2)) for i in vary(sims): port[i,0] = np.sum(wts[i,]*mean_ret) port[i,1] = np.sqrt(np.dot(np.dot(wts[i,].T,port_cov), wts[i,])) sharpe = port[:,0]/port[:,1]*np.sqrt(12) best_port = port[np.where(sharpe == max(sharpe))] max_sharpe = max(sharpe) return port, wts, best_port, sharpe, max_sharpe def calc_sim_lv(df, sims, cols): wts = np.zeros(((cols-1)*sims, cols)) depend=Zero for i in vary(1,cols): for j in vary(sims): a = np.random.uniform(0,1,(cols-i+1)) b = a/np.sum(a) c = np.random.alternative(np.concatenate((b, np.zeros(i))),cols, change=False) wts[count,] = c depend+=1 mean_ret = df.imply() port_cov = df.cov() port = np.zeros(((cols-1)*sims, 2)) for i in vary(sims): port[i,0] = np.sum(wts[i,]*mean_ret) port[i,1] = np.sqrt(np.dot(np.dot(wts[i,].T,port_cov), wts[i,])) sharpe = port[:,0]/port[:,1]*np.sqrt(12) best_port = port[np.where(sharpe == max(sharpe))] max_sharpe = max(sharpe) return port, wts, best_port, sharpe, max_sharpe def graph_sim(port, sharpe): plt.determine(figsize=(14,6)) plt.scatter(port[:,1]*np.sqrt(12)*100, port[:,0]*1200, marker='.', c=sharpe, cmap='Blues') plt.colorbar(label='Sharpe ratio', orientation = 'vertical', shrink = 0.25) plt.title('Simulated portfolios', fontsize=20) plt.xlabel('Threat (%)') plt.ylabel('Return (%)') plt.present() # Plot
port_sim_1, wts_1, _, sharpe_1, _ = Port_sim.calc_sim(df.iloc[1:60,0:4],1000,4) Port_sim.graph_sim(port_sim_1, sharpe_1) ## Choice operate # Constraint operate
def port_select_func(port, wts, return_min, risk_max): port_select = pd.DataFrame(np.concatenate((port, wts), axis=1)) port_select.columns = ['returns', 'risk', 1, 2, 3, 4] port_wts = port_select[(port_select['returns']*12 >= return_min) & (port_select['risk']*np.sqrt(12) <= risk_max)] port_wts = port_wts.iloc[:,2:6] port_wts = port_wts.imply(axis=0) def graph(): plt.determine(figsize=(12,6)) key_names = {1:"Shares", 2:"Bonds", 3:"Gold", 4:"Actual property"} lab_names = [] graf_wts = port_wts.sort_values()*100 for i in vary(len(graf_wts)): title = key_names[graf_wts.index[i]] lab_names.append(title) plt.bar(lab_names, graf_wts) plt.ylabel("Weight (%)") plt.title("Common weights for risk-return constraint", fontsize=15) for i in vary(len(graf_wts)): plt.annotate(str(spherical(graf_wts.values[i])), xy=(lab_names[i], graf_wts.values[i]+0.5)) plt.present() return port_wts, graph() # Graph
results_1_wts,_ = port_select_func(port_sim_1, wts_1, 0.07, 0.1) # Return operate with no rebalancing
def rebal_func(act_ret, weights): ret_vec = np.zeros(len(act_ret)) wt_mat = np.zeros((len(act_ret), len(act_ret.columns))) for i in vary(len(act_ret)): wt_ret = act_ret.iloc[i,:].values*weights ret = np.sum(wt_ret) ret_vec[i] = ret weights = (weights + wt_ret)/(np.sum(weights) + ret) wt_mat[i,] = weights return ret_vec, wt_mat ## Run rebalance operate utilizing desired weights
port_1_act, wt_mat = rebal_func(df.iloc[61:121,0:4], results_1_wts)
port_act = {'returns': np.imply(port_1_act), 'danger': np.std(port_1_act), 'sharpe': np.imply(port_1_act)/np.std(port_1_act)*np.sqrt(12)} # Run simulation on first five-year interval
port_sim_2, wts_2, _, sharpe_2, _ = Port_sim.calc_sim(df.iloc[61:121,0:4],1000,4) # Graph simulation with precise portfolio return
plt.scatter(port_sim_2[:,1]*np.sqrt(12)*100, port_sim_2[:,0]*1200, marker='.', c=sharpe_2, cmap='Blues')
plt.colorbar(label='Sharpe ratio', orientation = 'vertical', shrink = 0.25)
plt.scatter(port_act['risk']*np.sqrt(12)*100, port_act['returns']*1200, c='purple', s=50)
plt.title('Simulated portfolios', fontsize=20)
plt.xlabel('Threat (%)')
plt.ylabel('Return (%)')
plt.present() # Calculate returns and danger for longer interval
# Name prior file for longer interval
df = data_mon.pct_change()['1971':'2019'] hist_mu = df['1971':'1991'].imply(axis=0)
hist_sigma = df['1971':'1991'].std(axis=0) # Run simulation based mostly on historic figures
sim1 = [] for i in vary(1000): #np.random.regular(mu, sigma, obs) a = np.random.regular(hist_mu[0], hist_sigma[0], 60) + np.random.regular(0, hist_sigma[0], 60) b = np.random.regular(hist_mu[1], hist_sigma[1], 60) + np.random.regular(0, hist_sigma[1], 60) c = np.random.regular(hist_mu[2], hist_sigma[2], 60) + np.random.regular(0, hist_sigma[2], 60) d = np.random.regular(hist_mu[3], hist_sigma[3], 60) + np.random.regular(0, hist_sigma[3], 60) df1 = pd.DataFrame(np.array([a, b, c, d]).T) cov_df1 = df1.cov() sim1.append([df1, cov_df1]) # create graph objects
graphs = []
for i in vary(4): samp = np.random.randint(1,1000) port, _, _, sharpe, _ = Port_sim.calc_sim(sim1[samp][0], 1000, 4) graf = [port,sharpe] graphs.append(graf) # Graph pattern portfolios
fig, axes = plt.subplots(2, 2, figsize=(12,6)) for i, ax in enumerate(fig.axes): ax.scatter(graphs[i][0][:,1]*np.sqrt(12)*100, graphs[i][0][:,0]*1200, marker='.', c=graphs[i][1], cmap='Blues') plt.present() # Calculate likelihood of hitting risk-return constraints based mostly on pattern portfolos
probs = []
for i in vary(4): out = spherical(np.imply((graphs[i][0][:,0] >= 0.07/12) & (graphs[i][0][:,1] <= 0.1/np.sqrt(12))),2)*100 probs.append(out) # Simulate portfolios from reteurn simulations
def wt_func(): a = np.random.uniform(0,1,4) return a/np.sum(a) # Word this could run comparatively shortly: lower than a minute. np.random.seed(123)
portfolios = np.zeros((1000, 1000, 2))
for i in vary(1000): wt_mat = np.array([wt_func() for _ in range(1000)]) port_ret = sim1[i][0].imply(axis=0) cov_dat = sim1[i][0].cov() returns = np.dot(wt_mat, port_ret) danger = [np.sqrt(np.dot(np.dot(wt.T,cov_dat), wt)) for wt in wt_mat] portfolios[i][:,0] = returns portfolios[i][:,1] = danger port_1m = portfolios.reshape((1000000,2)) # Discover likelihood of hitting risk-return constraints on simulated portfolios
port_1m_prob = spherical(np.imply((port_1m[:][:,0] > 0.07/12) & (port_1m[:][:,1] <= 0.1/np.sqrt(12))),2)*100
print(f"The likelihood of assembly our portfolio constraints is:{port_1m_prob: 0.0f}%") # Plot pattern portfolios
port_samp = port_1m[np.random.choice(1000000, 10000),:]
sharpe = port_samp[:,0]/port_samp[:,1] plt.determine(figsize=(14,6))
plt.scatter(port_samp[:,1]*np.sqrt(12)*100, port_samp[:,0]*1200, marker='.', c=sharpe, cmap='Blues')
plt.colorbar(label='Sharpe ratio', orientation = 'vertical', shrink = 0.25)
plt.title('Ten thousand samples from a million simulated portfolios', fontsize=20)
plt.xlabel('Threat (%)')
plt.ylabel('Return (%)')
plt.present() # Graph histograms
fig, axes = plt.subplots(1,2, figsize = (12,6)) for idx,ax in enumerate(fig.axes): if idx == 1: ax.hist(port_1m[:][:,1], bins = 100) else: ax.hist(port_1m[:][:,0], bins = 100) plt.present()

Leave a Reply

Your email address will not be published. Required fields are marked *