knitr::opts_chunk$set(collapse = T, eval = T, warning = F, message = F, comment = "#>") folder <- "literature_files"; dir.create(folder) download.file("https://www.dropbox.com/s/htnd7o9nnkk8ng8/references.bib?dl=1", paste(folder, "references.bib", sep = "/")) path <- here::here("development", "storethat.sqlite")
At the time of writing, the analytics part of the pipeline in finRes focusses on asset pricing.
The FFresearch package provides data on classic asset pricing factors and a number of sort portfolios. The data is pulled directly from Kenneth French's data library and tidied up for seamless consumption in R.
library(FFresearch) data(list = c( "factors", "portfolios_univariate", "portfolios_bivariate", "portfolios_trivariate", "portfolios_industries", "variables", "breakpoints"), package = "FFresearch" )
The portfolios_univariate
dataset provides various feature time series for Fama/French portfolios formed on single variable sorts. Sorting variables include size, book-to-market, operating profitability and investment:
head(portfolios_univariate)
The portfolios_bivariate
dataset provides various feature time series for Fama/French portfolios formed on two variable sorts. Sorting variables include size, book-to-market, operating profitability and investment:
head(portfolios_bivariate)
The portfolios_trivariate
dataset provides various feature time series for Fama/French portfolios formed on three variable sorts. Sorting variables include size, book-to-market, operating profitability and investment:
head(portfolios_trivariate)
The portfolios_industries
dataset provides various feature time series for Fama/French industry portfolios [@fama_industry_1997]:
head(portfolios_industries)
The factors
dataset provides the return (factors) and level (risk free rate) time series for the classic Fama/French asset pricing factors as used in their three [@fama_cross_section_1992; @fama_common_1993; @fama_size_1995] and most recently five-factor [@fama_five_factor_2015; @fama_dissecting_2016; @fama_international_2017] asset pricing models very popular to the asset pricing enthusiasts:
head(factors)
The variables
dataset is a helper dataset that provides details, including construction methods, for the variables used to construct the portfolios and asset pricing factors above:
head(variables)
The breakpoints
dataset is a helper dataset that provides the times series for the variables breakpoints used to construct the variables that in turn allow the construction of the portfolios and asset pricing factors above-mentioned:
head(breakpoints)
Meanwhile, the factors package provides various asset pricing research factor time series for convenient consumption in R with the data directly pulled from the authors' website. The current version includes the factor data from Kenneth's French, also available in the FFresearch package described above, as well as factor data from Robert F. Stambaugh.
library(factors) data(list = c("fama_french", "stambaugh"), package = "factors")
The fama_french
dataset provides the return (factors) and level (risk free rate) time series for the classic Fama/French asset pricing factors as used in their three [@fama_cross_section_1992; @fama_common_1993; @fama_size_1995] and most recently five-factor [@fama_five_factor_2015; @fama_dissecting_2016; @fama_international_2017] asset pricing models very popular to the asset pricing enthusiasts:
head(fama_french)
The stambaugh
dataset provides the return (factors) and level (risk free rate) time series for various research asset pricing factors put together by Robert F. Stambaugh and collaborators including Lubos Pastor and Yu Yuan. The factors include traded & non-traded liquidity [@pastor_liquidity_2003], as well as market, size and two 'mispricing' factors: management & performance [@stambaugh_mispricing_2016]:
head(stambaugh)
factorem on the other hand provides tools for outright factor construction from data retrieved with pullit that facilitate asset pricing research and factor investment backtesting. The returned objects carry corresponding return & positions time series and a number of methods help with performance analysis. The package is organised around a workhorse function and a series of wrappers for asset pricing factors popular in the literature. The latter functions get raw financial data retrieved from Bloomberg using the pullit package and return S4 objects that carry data belonging to the corresponding factor including positions and return time series.
The factorem
function is the workhorse function in factorem. From raw financial data and a set of parameter specifications it constructs a complete backtest of the corresponding asset pricing factor and returns the whole time series for the factor positions and return. For the record the S4 object returned by the function also contains the original raw financial and the set of parameters supplied as well as the original call to the function. The raw financial data should be supplied in a format similar to that of historical datasets returned returned by historical data query functions in pullit:
library(pullit); library(lubridate) tickers_equity <- c("LZB US Equity", "SGA US Equity", "AGCO US Equity", "CLR US Equity", "GHC US Equity", "MAN US Equity", "SITE US Equity", "AJRD US Equity", "COMM US Equity", "GME US Equity", "MEI US Equity", "SMP US Equity") start <- "2016-01-01"; end <- "2017-12-31" equity_data_market <- pull_equity_market(source = "Bloomberg", tickers_equity, start, end, verbose = F) get_data(equity_data_market)
library(pullit); library(lubridate) # tickers_equity <- c("ADM US Equity", "CIVI US Equity", "GBX US Equity", "LIND US Equity", # "SERV US Equity", "AE US Equity", "CLGX US Equity", "GDI US Equity", # "LZB US Equity", "SGA US Equity", "AGCO US Equity", "CLR US Equity", # "GHC US Equity", "MAN US Equity", "SITE US Equity", "AJRD US Equity", # "COMM US Equity", "GME US Equity", "MEI US Equity", "SMP US Equity") tickers_equity <- c("LZB US Equity", "SGA US Equity", "AGCO US Equity", "CLR US Equity", "GHC US Equity", "MAN US Equity", "SITE US Equity", "AJRD US Equity", "COMM US Equity", "GME US Equity", "MEI US Equity", "SMP US Equity") start <- "2016-01-01"; end <- "2017-12-31" equity_data_market <- pull_equity_market( source = "storethat", tickers = tickers_equity, start = start, end = end, verbose = F, file = path ) pullit::get_data(equity_data_market)
From there, construct a bespoke asset pricing factor using the factorem function. I.e., an equity momentum factor:
library(factorem) ranking_period = 1L factor <- factorem( name = "momentum", data = pullit::get_data(equity_data_market), sort_levels = FALSE, weighted = FALSE, ranking_period = ranking_period ) factor
factorem provides wrapper methods for popular asset pricing factors in the literature. At the time of writing the factors covered in the package include, equity market and momentum as well as futures market, momentum, commercial hedging pressure (CHP), open interest and term structure factors. The author welcomes pull requests that would help expanding the current coverage.
The equity market factor encompasses the entire cross-section of a defined investment opportunity set. It gained popularity in the 1960s for the central role it plays in the Capital Asset Pricing Model [@treynor_market_1961; @treynor_toward_1961; @sharpe_capital_1964; @mosin_equilibrium_1966; @lintner_valuation_1975] and remains the most popular factor to date in the literature when it comes to explaining the cross-section of equity returns:
equity_market <- market_factor(data = equity_data_market) equity_market
The equity momentum factor is another popular factor in the asset pricing literature. After being first introduced in @carhart_persistence_1997 it eventually drew the field leaders' attention in @fama_size_2012 The equity momentum sorts on prior returns over a defined ranking period:
equity_momentum <- momentum_factor(data = equity_data_market, ranking_period = ranking_period) equity_momentum
tickers_futures <- c( "C A Comdty", "CLA Comdty", "COA Comdty", "GCA Comdty", "HOA Comdty", "KWA Comdty", "LHA Comdty", "QSA Comdty", "S A Comdty", "SBA Comdty", "W A Comdty" ) futures_data_TS <- pull_futures_market(source = "Bloomberg", type = "term structure", tickers_futures, start, end, verbose = F) get_data(futures_data_TS)
tickers_futures <- c( "C A Comdty", "CLA Comdty", "COA Comdty", "GCA Comdty", "HOA Comdty", "KWA Comdty", "LHA Comdty", "QSA Comdty", "S A Comdty", "SBA Comdty", "W A Comdty" ) futures_data_TS <- pull_futures_market( source = "storethat", type = "term structure", tickers_futures, start, end, verbose = F, file = path ) pullit::get_data(futures_data_TS)
factorem provides futures equivalent for the factors above including a futures market factor that takes equally weighted positions on the nearby front contract of the commodity futures series provided:
futures_market <- market_factor(data = futures_data_TS) futures_market
The momentum factor is also popular in the futures asset pricing literature in particular in the context of commodity markets [@miffre_momentum_2007]. As does its equity equivalent, it sorts on prior returns:
ranking_period = 1L futures_momentum <- momentum_factor(data = futures_data_TS, ranking_period = ranking_period) futures_momentum
The futures commercial hedging pressure factor is based on the well-known hedging pressure-based theory [@keynes_treatise_1930; @hicks_value_1939; @houthakker_restatement_1957; @cootner_returns_1960] which postulates that futures prices for a given commodity are inversely related to the extent that commercial hedgers are short or long and the mimicking portfolio here aims at capturing the impact of hedging pressure as a systemic factor [@basu_capturing_2013].
futures_data_CFTC <- pull_futures_CFTC(source = "Bloomberg", tickers_futures, start, end, verbose = F) ranking_period = 1L futures_CHP <- CHP_factor(price_data = futures_data_TS, CHP_data = futures_data_CFTC, ranking_period = ranking_period) futures_CHP
futures_data_CFTC <- pull_futures_CFTC( source = "storethat", tickers_futures, start, end, verbose = F, file = path ) ranking_period = 1L futures_CHP <- CHP_factor( price_data = futures_data_TS, CHP_data = futures_data_CFTC, ranking_period = ranking_period ) futures_CHP
The open interest factor relates to futures market liquidity and is also present in the literature where it is described as having explanatory power on the cross-section of commodity futures returns [@hong_what_2012]. It comes in two flavours in factorem, nearby and aggregate.
The nearby open interest factor is concerned with liquidity at the very front end of the term structure where it sorts on nearby front contract's open interest:
ranking_period = 1L futures_OI_nearby <- OI_nearby_factor(data = futures_data_TS, ranking_period = ranking_period) futures_OI_nearby
In contrast, the aggregate open interest factor is concerned with term structure liquidity as a whole and sorts on open interest summed up over all the contracts of a particular futures series:
futures_data_agg <- pull_futures_market(source = "Bloomberg", type = "aggregate", tickers_futures, start, end, verbose = F) ranking_period = 1L futures_OI_aggregate <- OI_aggregate_factor(price_data = futures_data_market, aggregate_data = futures_data_agg, ranking_period = ranking_period) futures_OI_aggregate
futures_data_agg <- pull_futures_market(source = "storethat", type = "aggregate", tickers_futures, start, end, verbose = F, file = path) ranking_period = 1L futures_OI_aggregate <- OI_aggregate_factor(price_data = futures_data_TS, aggregate_data = futures_data_agg, ranking_period = ranking_period) futures_OI_aggregate
The futures term structure factor [@szymanowska_anatomy_2014; @fuertes_commodity_2015] is concerned with the shape (steepness) of the futures term structure and sorts on roll yield:
ranking_period = 1L futures_TS <- TS_factor(data = futures_data_TS, ranking_period = ranking_period) futures_TS
Accessor methods allow access to the content of the factor objects returned by the functions described above:
get_name(factor)
get_positions(factor)
get_returns(factor)
factorem::get_data(factor)
get_parameters(factor)
get_call(factor)
A summary method returns a performance summary of the corresponding factor:
summary(factor)
The plotit package, also part of the finRes suite, provides plot methods for the factor objects returned by the functions above:
The plot function dispatched on a factor object from factorem with the parameter "type" set to "performance" displays an overview of the performance of the corresponding factor in the form of equity curves. The equity curve for the factor is plotted along with, for long-short factors only, that of the long and short legs of the factor independently:
library(plotit) plot(object = factor, type = "performance")
Similarly, with the "type" parameter set to "positions", the plot function shows the proportion of time each asset leaves in the factor with a breakdown by leg for long-short factors:
plot(object = factor, type = "positions")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.