Skip to content

Instantly share code, notes, and snippets.

@flare9x
Created September 12, 2019 01:02
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save flare9x/73f8f78e153334ebc6dae971b9e9bc06 to your computer and use it in GitHub Desktop.
Save flare9x/73f8f78e153334ebc6dae971b9e9bc06 to your computer and use it in GitHub Desktop.
Stocks Vs Real Estate
# Stocks vs Real Estate
library(fredr)
library(lubridate)
library(scales)
library(ggplot2)
library(TTR)
library(PerformanceAnalytics)
# Set key
fredr_set_key("your_key")
#
# request data
homeownership_data = fredr(
series_id = "GS10",
observation_start = as.Date("1984-01-01")
)
head(SP500)
# Plot close prices, calculate annual returns and maximum draw down
SP500 = read.csv("C:/Users/Andrew.Bannerman/Desktop/Real Estate/Data/Macro/GSPC_daily_data.csv")
# Convert date
SP500$Date <- ymd(SP500$Date)
# Calcualte annual returns
SP500$returns = ROC(SP500$Adj.Close, n = 1, type = "discrete")
#average_annual_return = mean(SP500$annual_returns, na.rm=T)
SP500[is.na(SP500)] = 0
SP500$cumulative_rets = cumprod(SP500$returns + 1) -1
# Maximum dd
roll_max = runMax(SP500$Adj.Close, n = 252, cumulative = FALSE)
roll_min = runMin(SP500$Adj.Close, n = 252, cumulative = FALSE)
daily_dd = roll_min/roll_max - 1.0
SP500$max_daily_dd = runMin(daily_dd, n = 252, cumulative = FALSE)
# maximum rolling 1 year draw down
min(daily_dd, na.rm = T)
mean(SP500$max_daily_dd, na.rm = T)
# plot data
ggplot(data=SP500, aes(x=Date, y=Adj.Close, colour="red")) +
geom_line()+
ggtitle("SP500 Closing Prices - Daily")+
theme(legend.position = "none")
ggplot(data=SP500, aes(x=Date, y=max_daily_dd, colour="red")) +
geom_line()+
ggtitle("SP500 Maximum Drawdown - Rolling 252 (1 trading year)")+
theme(legend.position = "none")
# Create time series object
xts1 = xts(SP500$returns, order.by=as.Date(SP500$Date, format="%y-%m-%d"))
colnames(xts1) <- c("sp500")
plot(xts1)
charts.PerformanceSummary(xts1,main="SP500 Index - Returns", colorset=rainbow12equal)
cum.ret <- Return.cumulative(xts1, geometric = TRUE)
annualized <- Return.annualized(xts1, scale = NA, geometric = TRUE)*100
dd <- maxDrawdown(xts1)
sharpe <- SharpeRatio.annualized(xts1, Rf = 0, scale = NA, geometric = TRUE)
# Calculate growth of 10,000 since 1984
SP500 = subset(SP500, Date >= as.Date("1986-01-01"))
xts1 = xts(SP500$returns, order.by=as.Date(SP500$Date, format="%y-%m-%d"))
colnames(xts1) <- c("sp500")
plot(xts1)
charts.PerformanceSummary(xts1,main="SP500 Index - Returns", colorset=rainbow12equal)
cum.ret <- Return.cumulative(xts1, geometric = TRUE)
annualized <- Return.annualized(xts1, scale = NA, geometric = TRUE)*100
dd <- maxDrawdown(xts1)
sharpe <- SharpeRatio.annualized(xts1, Rf = 0, scale = NA, geometric = TRUE)
###======================
# Stocks vs Real Estate
library(fredr)
library(lubridate)
library(scales)
library(ggplot2)
library(TTR)
library(PerformanceAnalytics)
# Set key
fredr_set_key("833af24cef7a3b0108fb6687ac12c721")
#
# request data
homeownership_data = fredr(
series_id = "GS10",
observation_start = as.Date("1984-01-01")
)
head(SP500)
# Plot close prices, calculate annual returns and maximum draw down
SP500 = read.csv("C:/Users/Andrew.Bannerman/Desktop/Real Estate/Data/Macro/GSPC_daily_data.csv")
# Convert date
SP500$Date <- ymd(SP500$Date)
# Calcualte annual returns
SP500$returns = ROC(SP500$Adj.Close, n = 1, type = "discrete")
#average_annual_return = mean(SP500$annual_returns, na.rm=T)
SP500[is.na(SP500)] = 0
SP500$cumulative_rets = cumprod(SP500$returns + 1) -1
# Maximum dd
roll_max = runMax(SP500$Adj.Close, n = 252, cumulative = FALSE)
roll_min = runMin(SP500$Adj.Close, n = 252, cumulative = FALSE)
daily_dd = roll_min/roll_max - 1.0
SP500$max_daily_dd = runMin(daily_dd, n = 252, cumulative = FALSE)
# maximum rolling 1 year draw down
min(daily_dd, na.rm = T)
mean(SP500$max_daily_dd, na.rm = T)
# plot data
ggplot(data=SP500, aes(x=Date, y=Adj.Close, colour="red")) +
geom_line()+
ggtitle("SP500 Closing Prices - Daily")+
theme(legend.position = "none")
ggplot(data=SP500, aes(x=Date, y=max_daily_dd, colour="red")) +
geom_line()+
ggtitle("SP500 Maximum Drawdown - Rolling 252 (1 trading year)")+
theme(legend.position = "none")
# Create time series object
xts1 = xts(SP500$returns, order.by=as.Date(SP500$Date, format="%y-%m-%d"))
colnames(xts1) <- c("sp500")
plot(xts1)
charts.PerformanceSummary(xts1,main="SP500 Index - Returns", colorset=rainbow12equal)
cum.ret <- Return.cumulative(xts1, geometric = TRUE)
annualized <- Return.annualized(xts1, scale = NA, geometric = TRUE)*100
dd <- maxDrawdown(xts1)
sharpe <- SharpeRatio.annualized(xts1, Rf = 0, scale = NA, geometric = TRUE)
# Calculate growth of 10,000 since 1984
SP500 = subset(SP500, Date >= as.Date("1986-01-01"))
xts1 = xts(SP500$returns, order.by=as.Date(SP500$Date, format="%y-%m-%d"))
colnames(xts1) <- c("sp500")
plot(xts1)
charts.PerformanceSummary(xts1,main="SP500 Index - Returns", colorset=rainbow12equal)
cum.ret <- Return.cumulative(xts1, geometric = TRUE)
annualized <- Return.annualized(xts1, scale = NA, geometric = TRUE)*100
dd <- maxDrawdown(xts1)
sharpe <- SharpeRatio.annualized(xts1, Rf = 0, scale = NA, geometric = TRUE)
###====
# Fred Data
# FRED API key
library(fredr)
library(lubridate)
library(scales)
library(ggplot2)
# USHOWN = Anual Homeownership Rate for the United States # RHORUSQ156N = Percent, Quarterly, Not Seasonally Adjusted
# GS10 = 10 year treasury
# DGS2 = 2 year treasury
# Rental Vacancy Data = RRVRUSQ156N
# Civilian Unemployment Rate (UNRATE)
# Rental Vacancy Rate for the United States (RRVRUSQ156N)
# Set key
fredr_set_key("your_key")
# request data
homeownership_data = fredr(
series_id = "GS10",
observation_start = as.Date("1984-01-01")
)
# Home price index
home_price_index = fredr(
series_id = "CSUSHPINSA",
observation_start = as.Date("1987-01-01"),
frequency = "a"
)
home_price_index$perc_change = NA
for (i in 2:nrow(home_price_index)) {
home_price_index$perc_change[i] = (home_price_index$value[i] - home_price_index$value[i-1]) / home_price_index$value[i]
}
mean(home_price_index$perc_change, na.rm=T)*100
# rolling DD
# Maximum dd
home_price_index = fredr(
series_id = "CSUSHPINSA",
observation_start = as.Date("1987-01-01")
)
roll_max = runMax(home_price_index$value, n = 2, cumulative = FALSE)
daily_dd = home_price_index$value/roll_max - 1.0
home_price_index$max_daily_dd = runMin(daily_dd, n = 1, cumulative = FALSE)
# maximum rolling 1 year draw down
min(home_price_index$max_daily_dd, na.rm = T)
mean(home_price_index$max_daily_dd, na.rm = T)
# consumer price index
cpi_all = fredr(
series_id = "CPIAUCSL",
observation_start = as.Date("1984-01-01"),
frequency = "a"
)
cpi_all$perc_change = NA
for (i in 2:nrow(cpi_all)) {
cpi_all$perc_change[i] = (cpi_all$value[i] - cpi_all$value[i-1]) / cpi_all$value[i]
}
mean(cpi_all$perc_change, na.rm=T)*100
# Load data ALL
homeownership_data = read.csv("C:/Users/Andrew.Bannerman/Desktop/Real Estate/Data/Macro/home_ownership_rate.csv",header=TRUE, sep=",",skip=0,stringsAsFactors=FALSE)
treasury_2 = read.csv("C:/Users/Andrew.Bannerman/Desktop/Real Estate/Data/Macro/DGS2.csv",header=TRUE, sep=",",skip=0,stringsAsFactors=FALSE)
treasury_10 = read.csv("C:/Users/Andrew.Bannerman/Desktop/Real Estate/Data/Macro/DGS10.csv",header=TRUE, sep=",",skip=0,stringsAsFactors=FALSE)
recession_dates = read.csv("C:/Users/Andrew.Bannerman/Desktop/Real Estate/Data/Macro/NBER_recession_dates.csv",header=TRUE, sep=",",skip=0,stringsAsFactors=FALSE)
unemployment = read.csv("C:/Users/Andrew.Bannerman/Desktop/Real Estate/Data/Macro/UNRATE.csv",header=TRUE, sep=",",skip=0,stringsAsFactors=FALSE)
rental_vacancy_rate = read.csv("C:/Users/Andrew.Bannerman/Desktop/Real Estate/Data/Macro/RRVRUSQ156N.csv",header=TRUE, sep=",",skip=0,stringsAsFactors=FALSE)
# Convert all recession dates to a readable format
dates_peak = paste0(recession_dates$month_peak,"-",recession_dates$day_peak,"-",recession_dates$year_peak)
#dates_peak[1] = ""
dates_peak = as.Date(dates_peak, format="%B-%d-%Y")
dates_trough = paste0(recession_dates$month_trough,"-",recession_dates$day_trough,"-",recession_dates$year_trough)
dates_trough = as.Date(dates_trough, format="%B-%d-%Y")
# Recession Dates
# Recession data frame
recession_df = data.frame(dates_peak,dates_trough)
colnames = c("Peak","Trough")
colnames(recession_df) = colnames
recessions_trim = subset(recession_df, Peak >= min(treasury_2$DATE))
#home ownership recession
home_recession = subset(recession_df, Peak >= min(homeownership_data$DATE))
# Unemployment
unemployment_recession = subset(recession_df, Peak >= min(unemployment$DATE))
# Rental vacancy rate recssion trim
rental_vacancy_recession = subset(recession_df, Peak >= min(rental_vacancy_rate$DATE))
# Case Shiller recession trim
case_shiller_recession = subset(recession_df, Peak >= min(home_price_index$date))
# Change Date Format
homeownership_data$DATE = mdy(homeownership_data$DATE)
treasury_2$DATE = ymd(treasury_2$DATE)
treasury_10$DATE = ymd(treasury_10$DATE)
unemployment$DATE = ymd(unemployment$DATE)
rental_vacancy_rate$DATE = ymd(rental_vacancy_rate$DATE)
home_price_index$date <- ymd(home_price_index$date)
# Change character to numeric
treasury_2$DGS2 = as.numeric(as.character(treasury_2$DGS2))
treasury_10$DGS10 = as.numeric(as.character(treasury_10$DGS10))
# Homeonwership Plot
# Plot Data US Wide
ggplot() +
theme_bw()+
geom_point(data=homeownership_data,aes(x = DATE, y = RHORUSQ156N,colour="#FF9999")) +
geom_line(data=homeownership_data, aes(x = DATE, y = RHORUSQ156N,colour="#FF9999")) +
theme(axis.text.x = element_text(size=10,angle = 90),legend.position="right")+
ggtitle("Homeownership Rate for the United States")+
ylab("(%)")+
xlab("Year")+
labs(caption = "Source: U.S. Bureau of the Census Release: Housing Vacancies and Homeownership\nRecession Data:The National Bureau of Economic Research")+
#labs(colour="Net Absorption")+
scale_y_continuous(breaks = seq(0, max(homeownership_data$RHORUSQ156N), by = .5))+
#scale_x_continuous(breaks = seq(min(homeownership_data$DATE), max(homeownership_data), by = 2))+
theme(legend.position="none")+
scale_x_date(breaks = date_breaks("12 months"),
labels=date_format(format = "%Y-%m-%d", tz = "UTC"))+
geom_rect(data=home_recession, aes(xmin=Peak, xmax=Trough, ymin=-Inf, ymax=+Inf), fill='grey', alpha=0.2)+
annotate("text", x = as.Date("2009-3-01"), y = 63.5, label = "Recession",colour = "darkgrey")
# FED Rates
# Plot Data US Wide
ggplot() +
theme_bw()+
geom_line(data=treasury_10,aes(x = DATE, y = DGS10,colour="black")) +
geom_line(data=treasury_2, aes(x = DATE, y = DGS2,colour="orange")) +
theme(axis.text.x = element_text(size=10,angle = 90),legend.position="right")+
ggtitle("10-Year Treasury vs. 2-Year Treasury")+
ylab("Rate")+
xlab("Date")+
labs(caption = "Source: Board of Governors of the Federal Reserve System (US) Release: H.15 Selected Interest Rates\nRecession Data:The National Bureau of Economic Research")+
#labs(colour="Net Absorption")+
#scale_y_continuous(breaks = seq(0, max(homeownership_data$RHORUSQ156N), by = .5))+
#scale_x_continuous(breaks = seq(min(homeownership_data$DATE), max(homeownership_data), by = 2))+
theme(legend.position="none")+
geom_rect(data=recessions_trim, aes(xmin=Peak, xmax=Trough, ymin=-Inf, ymax=+Inf), fill='pink', alpha=0.2)+
scale_x_date(breaks = date_breaks("12 months"),
labels=date_format(format = "%Y-%m-%d", tz = "UTC"))+
annotate("text", x = as.Date("2009-3-01"), y = 10, label = "Recession",colour = "pink")
# Prepare the 10 year and 2 year treasury yield spread
spread_df = subset(treasury_10, DATE >= min(treasury_2$DATE))
spread_df$yield_spread = spread_df$DGS10 - treasury_2$DGS2
# Plot yield spread
ggplot() +
theme_bw()+
geom_line(data=spread_df,aes(x = DATE, y = yield_spread,colour="black")) +
theme(axis.text.x = element_text(size=10,angle = 90),legend.position="right")+
ggtitle("10-Year Treasury vs. 2-Year Treasury Yield Spread Tightens")+
ylab("Rate")+
xlab("Date")+
labs(caption = "Source: Board of Governors of the Federal Reserve System (US) Release: H.15 Selected Interest Rates\nRecession Data:The National Bureau of Economic Research")+
#labs(colour="Net Absorption")+
#scale_y_continuous(breaks = seq(0, max(homeownership_data$RHORUSQ156N), by = .5))+
#scale_x_continuous(breaks = seq(min(homeownership_data$DATE), max(homeownership_data), by = 2))+
theme(legend.position="none")+
geom_rect(data=recessions_trim, aes(xmin=Peak, xmax=Trough, ymin=-Inf, ymax=+Inf), fill='pink', alpha=0.2)+
scale_x_date(breaks = date_breaks("12 months"),
labels=date_format(format = "%Y-%m-%d", tz = "UTC"))+
annotate("text", x = as.Date("2009-3-01"), y = -1, label = "Recession",colour = "pink")+
geom_hline(yintercept = 0.00,color='grey', size=.5)
# plot unemployment
ggplot() +
theme_bw()+
geom_line(data=unemployment,aes(x = DATE, y = UNRATE,colour="black")) +
theme(axis.text.x = element_text(size=10,angle = 90),legend.position="right")+
ggtitle("Civilian Unemployment Rate - 16yrs and over")+
ylab("Rate")+
xlab("Date")+
labs(caption = "Source: U.S. Bureau of Labor Statistics Release: Employment Situation\nRecession Data:The National Bureau of Economic Research")+
#labs(colour="Net Absorption")+
#scale_y_continuous(breaks = seq(0, max(homeownership_data$RHORUSQ156N), by = .5))+
#scale_x_continuous(breaks = seq(min(homeownership_data$DATE), max(homeownership_data), by = 2))+
theme(legend.position="none")+
geom_rect(data=unemployment_recession, aes(xmin=Peak, xmax=Trough, ymin=-Inf, ymax=+Inf), fill='pink', alpha=0.2)+
scale_x_date(breaks = date_breaks("12 months"),
labels=date_format(format = "%Y-%m-%d", tz = "UTC"))+
annotate("text", x = as.Date("2009-3-01"), y = 3, label = "Recession",colour = "pink")
# Plot Rental Vacancy Rate
ggplot() +
theme_bw()+
geom_line(data=rental_vacancy_rate,aes(x = DATE, y = RRVRUSQ156N,colour="black")) +
theme(axis.text.x = element_text(size=10,angle = 90),legend.position="right")+
ggtitle("Rental Vacancy Rate for the United States")+
ylab("Rate")+
xlab("Date")+
labs(caption = "Source: U.S. Bureau of the Census Release: Housing Vacancies and Homeownership\nRecession Data:The National Bureau of Economic Research")+
#labs(colour="Net Absorption")+
#scale_y_continuous(breaks = seq(0, max(homeownership_data$RHORUSQ156N), by = .5))+
#scale_x_continuous(breaks = seq(min(homeownership_data$DATE), max(homeownership_data), by = 2))+
theme(legend.position="none")+
geom_rect(data=rental_vacancy_recession, aes(xmin=Peak, xmax=Trough, ymin=-Inf, ymax=+Inf), fill='grey', alpha=0.2)+
scale_x_date(breaks = date_breaks("12 months"),
labels=date_format(format = "%Y-%m-%d", tz = "UTC"))+
annotate("text", x = as.Date("2009-3-01"), y = 6.0, label = "Recession",colour = "lightgrey")
# PLot case shiller home price index
ggplot(data=home_price_index, aes(x=date, y=value, colour="red")) +
geom_line()+
ggtitle("Case-Shiller U.S. National Home Price Index - Annual")+
theme(legend.position = "none")
ggplot() +
theme_bw()+
geom_line(data=home_price_index,aes(x = date, y = value,colour="black")) +
theme(axis.text.x = element_text(size=10,angle = 90),legend.position="right")+
ggtitle("S&P/Case-Shiller U.S. National Home Price Index - Annual Data")+
ylab("Rate")+
xlab("Date")+
labs(caption = "Source: S&P Dow Jones Indices LLC, S&P/Case-Shiller U.S. National Home Price Index\nRecession Data:The National Bureau of Economic Research")+
#labs(colour="Net Absorption")+
#scale_y_continuous(breaks = seq(0, max(homeownership_data$RHORUSQ156N), by = .5))+
#scale_x_continuous(breaks = seq(min(homeownership_data$DATE), max(homeownership_data), by = 2))+
theme(legend.position="none")+
geom_rect(data=case_shiller_recession, aes(xmin=Peak, xmax=Trough, ymin=-Inf, ymax=+Inf), fill='grey', alpha=0.2)+
scale_x_date(breaks = date_breaks("12 months"),
labels=date_format(format = "%Y-%m-%d", tz = "UTC"))+
annotate("text", x = as.Date("2009-3-01"), y = 6.0, label = "Recession",colour = "lightgrey")
##=====
# Fair market rents - HUD
# Source: https://www.huduser.gov/portal/datasets/fmr.html#history
library(fredr)
library(lubridate)
library(scales)
library(ggplot2)
data = read.csv("C:/Users/Andrew.Bannerman/Desktop/Real Estate/Rental Property Research and Deals/FMR_All_1983_2019_rev.csv",stringsAsFactors = FALSE, header=T)
head(data)
# custom column index positions
beds_0 = seq(4,length(data)-16,7)
beds_1 = seq(5,length(data)-16,7)
beds_2 = seq(6,length(data)-16,7)
beds_3 = seq(7,length(data)-16,7)
beds_4 = seq(8,length(data)-16,7)
start = 1983
end = 2019
years = end - start
bedrooms = 0:4
beds_0_data_extract = list()
beds_1_data_extract = list()
beds_2_data_extract = list()
beds_3_data_extract = list()
beds_4_data_extract = list()
i = 1
for (i in 1:years) {
cat("this is iteration",i)
beds_0_data_extract[[i]] = data.frame(data[,beds_0[i]])
beds_1_data_extract[[i]] = data.frame(data[,beds_1[i]])
beds_2_data_extract[[i]] = data.frame(data[,beds_2[i]])
beds_3_data_extract[[i]] = data.frame(data[,beds_3[i]])
beds_4_data_extract[[i]] = data.frame(data[,beds_4[i]])
}
# make data frames
beds_0_data_df = rev(do.call(cbind,beds_0_data_extract))
beds_1_data_df = rev(do.call(cbind,beds_1_data_extract))
beds_2_data_df = rev(do.call(cbind,beds_2_data_extract))
beds_3_data_df = rev(do.call(cbind,beds_3_data_extract))
beds_4_data_df = rev(do.call(cbind,beds_4_data_extract))
# Date index
date_index = seq(1984,end,1)
date_index = paste0("1","-","1","-",date_index)
date_index = mdy(date_index)
# rename columns
colnames(beds_0_data_df) = date_index
colnames(beds_1_data_df) = date_index
colnames(beds_2_data_df) = date_index
colnames(beds_3_data_df) = date_index
colnames(beds_4_data_df) = date_index
# Extract national averages
beds_0_mean = apply( beds_0_data_df , 2,mean, na.rm=T )
beds_1_mean = apply( beds_1_data_df , 2,mean, na.rm=T )
beds_2_mean = apply( beds_2_data_df , 2,mean, na.rm=T )
beds_3_mean = apply( beds_3_data_df , 2,mean, na.rm=T )
beds_4_mean = apply( beds_4_data_df , 2,mean, na.rm=T )
all_df = data.frame(beds_0_mean,beds_1_mean,beds_2_mean,beds_3_mean,beds_4_mean)
# Yearly Changes
beds_0_delta = seq(0,nrow(all_df)-1,1)
beds_1_delta = seq(0,nrow(all_df)-1,1)
beds_2_delta = seq(0,nrow(all_df)-1,1)
beds_3_delta = seq(0,nrow(all_df)-1,1)
beds_4_delta = seq(0,nrow(all_df)-1,1)
beds_0_perc = seq(0,nrow(all_df)-1,1)
beds_1_perc = seq(0,nrow(all_df)-1,1)
beds_2_perc = seq(0,nrow(all_df)-1,1)
beds_3_perc = seq(0,nrow(all_df)-1,1)
beds_4_perc = seq(0,nrow(all_df)-1,1)
i=1
for (i in 2:nrow(all_df)) {
beds_0_delta[i] = all_df$beds_0_mean[i] -all_df$beds_0_mean[i-1]
beds_1_delta[i] = all_df$beds_1_mean[i] -all_df$beds_1_mean[i-1]
beds_2_delta[i] = all_df$beds_2_mean[i] -all_df$beds_2_mean[i-1]
beds_3_delta[i] = all_df$beds_3_mean[i] -all_df$beds_3_mean[i-1]
beds_4_delta[i] = all_df$beds_4_mean[i] -all_df$beds_4_mean[i-1]
beds_0_perc[i] = (all_df$beds_0_mean[i] - all_df$beds_0_mean[i-1])/ all_df$beds_0_mean[i]
beds_1_perc[i] = (all_df$beds_1_mean[i] - all_df$beds_1_mean[i-1])/ all_df$beds_1_mean[i]
beds_2_perc[i] = (all_df$beds_2_mean[i] - all_df$beds_2_mean[i-1])/ all_df$beds_2_mean[i]
beds_3_perc[i] = (all_df$beds_3_mean[i] - all_df$beds_3_mean[i-1])/ all_df$beds_3_mean[i]
beds_4_perc[i] = (all_df$beds_4_mean[i] - all_df$beds_4_mean[i-1])/ all_df$beds_4_mean[i]
}
# Average change
mean_yearly_beds_0 = mean(beds_0_delta)
mean_yearly_beds_1 = mean(beds_1_delta)
mean_yearly_beds_2 = mean(beds_2_delta)
mean_yearly_beds_3 = mean(beds_3_delta)
mean_yearly_beds_4 = mean(beds_4_delta)
mean_perc_yearly_beds_0 = mean(beds_0_perc) *100
mean_perc_yearly_beds_1 = mean(beds_1_perc) *100
mean_perc_yearly_beds_2 = mean(beds_2_perc) *100
mean_perc_yearly_beds_3 = mean(beds_3_perc) *100
mean_perc_yearly_beds_4 = mean(beds_4_perc) *100
all_change = c(mean_yearly_beds_0,mean_yearly_beds_1,mean_yearly_beds_2,mean_yearly_beds_3,mean_yearly_beds_4)
all_change = as.data.frame(all_change)
all_perc_change = c(mean_perc_yearly_beds_0,mean_perc_yearly_beds_1,mean_perc_yearly_beds_2,mean_perc_yearly_beds_3,mean_perc_yearly_beds_4)
all_perc_change = as.data.frame(all_perc_change)
# Set colour columns
all_change$cols = c("bed_0","bed_1","bed_2","bed_3","bed_4")
all_perc_change$cols = c("bed_0","bed_1","bed_2","bed_3","bed_4")
# rent increase over time
all_rent_increase = rep(.00025,nrow(all_df))
rets =rep(0.08,nrow(all_df))
out = rets
for (i in 1:length(rets)) {
if (i == 1) {
temp = (rets[i] + all_rent_increase[i])
out[i] = temp
} else {
temp = all_rent_increase[i]
out[i] = temp + out[i-1]
}
}
# x axis
index = c(0,1,2,3,4)
library(ggplot2)
ggplot(all_change,aes(x=index,y=all_change,fill=cols)) +
geom_bar(stat = "identity") +
geom_text(label = round(all_change$all_change,digits =2),size = 3,
position = position_stack(vjust = 0.5))+
theme_bw()+
ggtitle("Mean Annual $ Change - HUD Fair Market Rents")+
ylab("Annual Mean Change $")+
xlab("Bedroom")+
scale_y_continuous(breaks = seq(0, max(all_change$all_change)+5, by = 5))+
labs(colour="Bedroom Size")
ggplot(all_perc_change,aes(x=index,y=all_perc_change,fill=cols)) +
geom_bar(stat = "identity") +
geom_text(label = round(all_perc_change$all_perc_change,digits =2),size = 3,
position = position_stack(vjust = 0.5))+
theme_bw()+
ggtitle("Mean Annual % Change - HUD Fair Market Rents")+
ylab("Annual % Mean Change")+
xlab("Bedroom")+
scale_y_continuous(breaks = seq(0, max(all_perc_change$all_perc_change)+.1, by = .2))+
labs(colour="Bedroom Size")
# Fred Data
# FRED API key 833af24cef7a3b0108fb6687ac12c721
# Set key
fredr_set_key("833af24cef7a3b0108fb6687ac12c721")
require(fredr)
# request data
all_cpi_data = fredr(
series_id = "CPALTT01USA661S",
observation_start = as.Date("1984-01-01")
)
# percent change
cpi_perc_changes = seq(1,nrow(all_cpi_data),1)
for (i in 2:length(cpi_perc_changes)) {
cpi_perc_changes[i] = (all_cpi_data$value[i] -all_cpi_data$value[i-1]) / all_cpi_data$value[i]
}
mean(cpi_perc_changes)*100
all_cpi_data = data.frame(all_cpi_data,cumsum(cpi_perc_changes))
ggplot() +
theme_bw()+
geom_point(data=all_cpi_data,aes(x = date, y = cumsum.cpi_perc_changes.,colour="#FF9999")) +
geom_line(data=all_cpi_data, aes(x = date, y = cumsum.cpi_perc_changes.,colour="#FF9999")) +
theme(axis.text.x = element_text(size=10,angle = 90),legend.position="right")+
ggtitle("Consumer Price Index: Total All Items for the United States - Cumulative % Change")+
ylab("(%)")+
xlab("Year")+
labs(subtitle="Date Range - 1984 to 2019")+
#labs(colour="Net Absorption")+
scale_y_continuous(breaks = seq(0, max(all_cpi_data$cumsum.cpi_perc_changes.), by = .2))+
#scale_x_continuous(breaks = seq(min(homeownership_data$DATE), max(homeownership_data), by = 2))+
theme(legend.position="none")+
scale_x_date(breaks = date_breaks("24 months"),
labels=date_format(format = "%Y-%m-%d", tz = "UTC"))
# plots
date_index = seq(1984,end,1)
date_index = paste0("1","-","1","-",date_index)
date_index = mdy(date_index)
# plots and prepare NBER recession data
recession_dates = read.csv("C:/Users/Andrew.Bannerman/Desktop/Real Estate/Data/Macro/NBER_recession_dates.csv",header=TRUE, sep=",",skip=0,stringsAsFactors=FALSE)
# recession data
# Convert all recession dates to a readable format
dates_peak = paste0(recession_dates$month_peak,"-",recession_dates$day_peak,"-",recession_dates$year_peak)
#dates_peak[1] = ""
dates_peak = as.Date(dates_peak, format="%B-%d-%Y")
dates_trough = paste0(recession_dates$month_trough,"-",recession_dates$day_trough,"-",recession_dates$year_trough)
dates_trough = as.Date(dates_trough, format="%B-%d-%Y")
# Recession Dates
# Recession data frame
recession_df = data.frame(dates_peak,dates_trough)
colnames = c("Peak","Trough")
colnames(recession_df) = colnames
recessions_trim = subset(recession_df, Peak >= min(date_index))
# Set colour columns
all_df$bed_0 = "bed_0"
all_df$bed_1 = "bed_1"
all_df$bed_2 = "bed_2"
all_df$bed_3 = "bed_3"
all_df$bed_4 = "bed_4"
# Plot Data
ggplot() +
geom_line(data=all_df, aes(x = date_index, y = beds_0_mean,group=bed_0,colour=bed_0)) +
geom_line(data=all_df, aes(x = date_index, y = beds_1_mean,group=bed_1,colour=bed_1)) +
geom_line(data=all_df, aes(x = date_index, y = beds_2_mean,group=bed_2,colour=bed_2)) +
geom_line(data=all_df, aes(x = date_index, y = beds_3_mean,group=bed_3,colour=bed_3)) +
geom_line(data=all_df, aes(x = date_index, y = beds_4_mean,group=bed_4,colour=bed_4)) +
theme(axis.text.x = element_text(size=6,angle = 90),legend.position="right")+
ggtitle("HUD Fair Market Rent Values - National Annual Average")+
ylab("HUD FMR")+
xlab("Date")+
labs(colour="Bedroom Size")+
scale_y_continuous(breaks = seq(0, max(all_df$beds_4_mean)+200, by = 100))+
geom_rect(data=recessions_trim, aes(xmin=Peak, xmax=Trough, ymin=-Inf, ymax=+Inf), fill='grey', alpha=0.2)+
scale_x_date(breaks = date_breaks("12 months"),
labels=date_format(format = "%Y-%m-%d", tz = "UTC"))+
annotate("text", x = as.Date("2009-03-06"), y = 1350, label = "Recession",colour = "grey")+
theme_classic(base_size = 15)+
theme(axis.text.x = element_text(angle = 90, hjust = 1))+
theme(panel.grid.minor = element_blank(),
panel.grid.major = element_line(colour = "grey",size=0.1))+
labs(caption=("Recession Data Source: National Bureau of Economic Research"))+
theme(plot.caption = element_text(color = "black", face = "italic",size = 10))
#===
# Amortization
# https://www.wikihow.com/Calculate-Amortization
# Step 1 - Gather the information you need to calculate the loan’s amortization
property_value = 100000
loan_amount = .75
down_payment = property_value-(loan_amount * property_value)
debt_borrowed = property_value - down_payment
interest_rate = 0.06 # annual interest rate
loan_term = 360 # months
# Calculate the principal / interest over the loan term
# Initialize outputs
remaining_loan_amount = seq(1,loan_term,1)
interest = seq(1,loan_term,1)
principal = seq(1,loan_term,1)
interest_principal = seq(1,loan_term,1)
# for loop to create amortization schedule
i=1
for (i in 1:length(remaining_loan_amount)) {
# set the first row at 1 = 1 - this is the first principal and interest payment
if (i == 1) {
I = debt_borrowed *(interest_rate/12) # note converting annual interest to monthly (interest_rate/12)
P = (I / (1-(1/(1+interest_rate/12)^(loan_term)))) - I # Calculate the interest + principal then subtract interest
IP = I + P
interest[i] = I
principal[i] = P
interest_principal[i] = IP
remaining_loan_amount[i] = debt_borrowed - P
} else {
# Using the data from the first row - continue to pay down princial and debt
I = remaining_loan_amount[i-1] *(interest_rate/12) # note converting annual interest to monthly (interest_rate/12)
P = (I / (1-(1/(1+interest_rate/12)^(loan_term-i)))) - I # Calculate the interest + principal then subtract interest also note loan_term-i = reducing the loan term through the course of the loan
IP = I + P
interest[i] = I
principal[i] = P
interest_principal[i] = IP
remaining_loan_amount[i] = remaining_loan_amount[i-1] - P
cat("This is Month",i,"Amoritization Schedule\n")
}
}
# data prune
principal[loan_term] = 0
interest[loan_term] = 0
remaining_loan_amount[loan_term] = 0
# plot data
month_index = seq(1,360,1)
ggplot() +
geom_line(aes(x = month_index, y = interest), color = "red") +
geom_line(aes(x = month_index, y = principal), color = "blue") +
xlab('Loan Term Remaining (months)') +
ylab('Per month $ payment')+
ggtitle("Mortgage Amortization - Principal / Interest Relationship")+
theme(legend.position = "none")+
scale_x_continuous(breaks = round(seq(1, 360, by = 19)))+
annotate("text", x = 40, y = 400, label = "Monthly Interest Payments",colour = "red")+
annotate("text", x = 40, y = 50, label = "Monthly Principal Payments",colour = "blue")
# calculate % stakes in the deal over the amortization schedule
owner_equity_stake = (cumsum(principal) + down_payment) / property_value
bank_equity_stake = remaining_loan_amount / property_value
month_index = seq(1,360,1)
ggplot() +
geom_line(aes(x = month_index, y = bank_equity_stake), color = "red") +
geom_line(aes(x = month_index, y = owner_equity_stake), color = "blue") +
xlab('Loan Term Remaining (months)') +
ylab('%')+
ggtitle("Mortgage Amortization - Time Dependant Owner and Bank Equity In The Deal")+
theme(legend.position = "none")+
scale_x_continuous(breaks = round(seq(1, 360, by = 19)))+
annotate("text", x = 40, y = .8, label = "Bank Equity Stake In Deal",colour = "red")+
annotate("text", x = 40, y = .2, label = "Owner Equity Stake In Deal",colour = "blue")
# Cumulative return
month_index = seq(1,360,1)
ggplot() +
geom_line(aes(x = month_index, y = cumsum(principal)), color = "blue") +
xlab('Loan Term Remaining (months)') +
ylab('Deal Equity')+
ggtitle("Mortgage Amortization - Cumulative Equity In The Deal")+
theme(legend.position = "none")+
scale_x_continuous(breaks = round(seq(1, 360, by = 19)))+
scale_y_continuous(breaks = round(seq(0, 75000, by = 5000)))+
annotate("text", x = 115, y = 65000, label = "Cumulative Sum - Monthly Principal Payments",colour = "blue")
# Real Estate Depreciation
library(fredr)
library(lubridate)
library(scales)
library(ggplot2)
# Step 1 - Calculate Depreciation
cost_basis = 275000
useful_life = 27.5
annual_depreciation_value = cost_basis / useful_life # full year depreciation value
year_placed_in_service = 2019 # starting year placed in-service
month_placed_in_service = 2 # starting month placed in-service +++= note used to calculate the partial month depreciation value
start_date = paste0(month_placed_in_service,"-",01,"-",year_placed_in_service)
start_date = as.Date(start_date, format="%m-%d-%Y")
library(lubridate)
end_date = start_date + months(useful_life*12) # end date
month_taken_out_of_service = month(end_date) # end month +++= note used to calculate the partial month depreciation value
year_taken_out_of_service = year(end_date) # end year
# calculate first and end year depreciation
first_year_depreciation = (((12 - month_placed_in_service)+.5) / 12) * (cost_basis / useful_life)
end_year_depreciation = ((month_taken_out_of_service-.5) / 12) * (cost_basis / useful_life)
# Step 2 - Build the depreciation schedule
year_index = seq(year_placed_in_service,year_taken_out_of_service,1)
depreciation_expense = rep(annual_depreciation_value,length(year_index))
depreciation_expense[1] = first_year_depreciation
depreciation_expense[length(year_index)] = end_year_depreciation
accumulated_depreciation = cumsum(depreciation_expense)
# Step 3 - Calculate tax savings
marginal_tax_rate = .24
tax_savings = depreciation_expense * marginal_tax_rate
ggplot() +
geom_point(aes(x = year_index, y = cumsum(tax_savings)), color = "blue") +
geom_line(aes(x = year_index, y = cumsum(tax_savings)), color = "blue")+
xlab('Useful Life (months)') +
ylab('Tax Savings')+
ggtitle("Real Estate Depreciation - Cumulative Tax Savings @ marginal tax rate 24 %")+
theme(legend.position = "none")+
scale_x_continuous(breaks = round(seq(year_placed_in_service, year_taken_out_of_service, by = 2)))+
scale_y_continuous(breaks = round(seq(0, max(cumsum(tax_savings)), by = 2000)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment