The FactorExposures class enables you to create an exposure object to which different factor time series data can be attached.
To give some examples of how this object can then be used:
View a detailed report on a strategy's exposures to given factors on a given date and through time.
Create a replicating basket for a given strategy with a defined replicating universe, by mimicking factor exposure.
Enable factor exposure to be constrained in optimisation problems.
Create Equity Factor Basket building blocks: a strategy driven by factor data.
This page details case 1 above. Cases 2 & 3 are detailed in the FactorOptimisation page, whilst 4 is explained in the EquityFactorBasket show.
Import the relevant internal and external libraries
Configure the environment parameters
Initialise the environment
import sigtech.framework as sig# import method to build equity ReinvestmentStrategy objectsfrom sigtech.framework.strategies.equity_factor_basket import get_single_stock_strategy# import AnalystIndex class to get an overview of built-in factor time series datafrom sigtech.framework.instruments.indices import AnalystIndex# import SchedulePeriodic to build custom schedule belowfrom sigtech.framework.schedules import SchedulePeriodicimport pandas as pdimport seaborn as snsimport datetime as dtmfrom uuid import uuid4sns.set(rc={'figure.figsize':(18, 6)})ifnot sig.config.is_initialised(): env = sig.init()
The FactorExposures class can be used with all asset classes. Equities are probably the most common use case. The FactorExposures class implements estimation methods for exposures to factors for a selection of instruments, such as performing a series of multivariate time series regressions based on a configuration.
The configuration is created by adding steps to the object:
A fit() method executes the steps on the data provided, calculating the factor loadings across the instruments.
A report() method is available to generate a report summarising the results.
Example
This short example will showcase how to apply the FactorExposure to a number of ReinvetmentStrategy objects.
Where the exposure to factor is, by default, the OLS estimate βi and the residual at time t is ε(t). Here R(t) denotes the return of the strategy and Ri(t) denotes the return of factor i , both at time t .
rs_returns ={rs: rs.history().pct_change().dropna()for rs in all_rs}exposures, residuals = factor_exposures.fit(rs_returns)exposures
residuals.head()
residuals.plot();
Recompute residuals manually for one strategy and compare to output:
# add a cross_score for the `smb` factor called `smb_rank`factor_exposures.add_cross_score('smb_rank', 'smb')
The updated exposures now contain the row smb_rank, indicating the percentile score for each assets exposure to the smb factor. For example, the reinvestment strategy 1001489.SINGLE_STOCK.TRADABLE REINVSTRAT STRATEGY has an smb_rank score of 0.25 since it had the lowest exposure to the smb factor out of the four assets in the universe.
e2, _ = factor_exposures.fit(rs_returns)e2
Fit regressions
As seen above, after regression factors have been added to the FactorExposure object, it is possible to fit a regression from a dictionary of input returns onto those factors.
More granular control over the fitting process can be gained by adding individual steps to the fitting configuration.
Compute exposure of portfolios to factors through time:
ew_exp = factor_exposures.rolling_exposures(ew_portfolio)perf_exp = factor_exposures.rolling_exposures(perf_weighted_portfolio)ew_exp.columns = [f'ew_{c}'for c in ew_exp]perf_exp.columns = [f'perf_{c}'for c in perf_exp]df = pd.concat([ew_exp, perf_exp], axis=1)
(len(all_rs)* df).tail()
Fixed portfolio report
View the report for an individual portfolio, including an equally weights combination of securities within the portfolio for comparison. In addition to factor exposure, there is also variance decomposition and density plots of the weighted exposures of securities within the portfolio.