Statistical learning for sectoral equity allocation #

This notebook offers the necessary code to replicate the research findings discussed in the Macrosynergy research post “Statistical learning for sectoral equity allocation”. Its primary objective is to inspire readers to explore and conduct additional investigations whilst also providing a foundation for testing their own unique ideas.

On the back of our initial exploration of the relationship between macroeconomic trends and sectoral equity indices , we continue the analysis of sectoral rotation signals using macro trends as well as macro and financial conditions. We take advantage of previous data ingestion and transformations of JPMaQS macro quantamental indicators, as shown in Sectoral equity indicators , structuring the notebook as follows:

  • Get Packages and JPMaQS Data: This section is responsible for installing and importing the necessary Python packages used throughout the analysis, as well as loading all the required data

  • Feature filtering and imputation: We apply common sense in determining which categories should be part of the panel in the learning exercise. We look for consistency of feature availability across cross sections, as well as maximum coverage. Therefore we opt to discard some categories while imputing others in specific cross sections in the early period.

  • Sectoral signals and naive PnLs: We proceed with statistical learning on vol-targeted relative returns, using common pipeline across sectors. This combines a feature selection mechanism based upon LARS and linear models, as we opt for simplicity.

It is important to note that while the notebook covers a selection of indicators and strategies used for the post’s main findings, users can explore countless other possible indicators and approaches. Users can modify the code to test different hypotheses and strategies based on their research and ideas. Best of luck with your research!

Get packages and JPMaQS data #

Packages #

import os
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

from macrosynergy.download import JPMaQSDownload
import macrosynergy.management as msm
import macrosynergy.panel as msp
import macrosynergy.pnl as msn
import macrosynergy.signal as mss
import macrosynergy.learning as msl
import macrosynergy.visuals as msv

from sklearn.linear_model import LinearRegression
from sklearn.pipeline import Pipeline
from sklearn.metrics import make_scorer

from timeit import default_timer as timer
from datetime import timedelta, date, datetime

import warnings

from IPython.display import HTML

warnings.filterwarnings("ignore")

Previously prepared quantamental categories #

# Import data from csv file created preparation notebook
# https://macrosynergy.com/academy/notebooks/sectoral-equity-indicators/

INPUT_PATH = os.path.join(os.getcwd(), r"../../../equity_sectoral_notebook_data.csv")

df_csv = pd.read_csv(INPUT_PATH, index_col=0)
df_csv["real_date"] = pd.to_datetime(df_csv["real_date"]).dt.date

df_csv = msm.utils.standardise_dataframe(df_csv)
df_csv = df_csv.sort_values(["cid", "xcat", "real_date"])
# Equity sector labels and cross sections

sector_labels = {
    "ALL": "All sectors",
    "COD": "Cons. discretionary",
    "COS": "Cons. staples",
    "CSR": "Communication services",
    "ENR": "Energy",
    "FIN": "Financials",
    "HLC": "Healthcare",
    "IND": "Industrials",
    "ITE": "Information tech",
    "MAT": "Materials",
    "REL": "Real estate",
    "UTL": "Utilities",
}
cids_secs = list(sector_labels.keys())

# Equity countries cross sections

cids_eq = [
    "AUD",
    "CAD",
    "CHF",
    "EUR",
    "GBP",
    "ILS",
    "JPY",
    "NOK",
    "NZD",
    "SEK",
    "SGD",
    "USD",
]
# Base category tickes of quantamental categories created by data preparation notebook:
# https://macrosynergy.com/academy/notebooks/sectoral-equity-indicators/

output_growth = [
    # industrial prod
    "XIP_SA_P1M1ML12_3MMA",
    "XIP_SA_P1M1ML12_3MMA_WG",
    # construction
    "XCSTR_SA_P1M1ML12_3MMA",
    "XCSTR_SA_P1M1ML12_3MMA_WG",
    # Excess GDP growth
    "XRGDPTECH_SA_P1M1ML12_3MMA",
    "XRGDPTECH_SA_P1M1ML12_3MMA_WG",
]
private_consumption = [
    # Consumer surveys
    "CCSCORE_SA",
    "CCSCORE_SA_D3M3ML3",
    "CCSCORE_SA_WG",
    "CCSCORE_SA_D3M3ML3_WG",
    
    "XNRSALES_SA_P1M1ML12_3MMA",
    "XRRSALES_SA_P1M1ML12_3MMA",
    "XNRSALES_SA_P1M1ML12_3MMA_WG",
    "XRRSALES_SA_P1M1ML12_3MMA_WG",
    "XRPCONS_SA_P1M1ML12_3MMA",
    "XRPCONS_SA_P1M1ML12_3MMA_WG",    
]
export = [
    "XEXPORTS_SA_P1M1ML12_3MMA",
]
labour_market = [
    "UNEMPLRATE_NSA_3MMA_D1M1ML12",
    "UNEMPLRATE_SA_3MMAv5YMA",
    "UNEMPLRATE_NSA_3MMA_D1M1ML12_WG",
    "UNEMPLRATE_SA_3MMAv5YMA_WG",
    "XEMPL_NSA_P1M1ML12_3MMA",
    "XEMPL_NSA_P1M1ML12_3MMA_WG",
    "XRWAGES_NSA_P1M1ML12",
]
business_surveys = [
    # Manufacturing
    "MBCSCORE_SA",
    "MBCSCORE_SA_D3M3ML3",
    "MBCSCORE_SA_WG",
    "MBCSCORE_SA_D3M3ML3_WG",
    # Services
    "SBCSCORE_SA",
    "SBCSCORE_SA_D3M3ML3",
    "SBCSCORE_SA_WG",
    "SBCSCORE_SA_D3M3ML3_WG",
    # Construction
    "CBCSCORE_SA",
    "CBCSCORE_SA_D3M3ML3",
    "CBCSCORE_SA_WG",
    "CBCSCORE_SA_D3M3ML3_WG",
]
private_credit = [
    "XPCREDITBN_SJA_P1M1ML12",
    "XPCREDITBN_SJA_P1M1ML12_WG",
    # liquidity conditions
    "INTLIQGDP_NSA_D1M1ML1",
    "INTLIQGDP_NSA_D1M1ML6",
]
broad_inflation = [
    # Inflation
    "XCPIC_SA_P1M1ML12",
    "XCPIH_SA_P1M1ML12",
    "XPPIH_NSA_P1M1ML12",    
]
specific_inflation = [
    "XCPIE_SA_P1M1ML12",
    "XCPIF_SA_P1M1ML12",
    "XCPIE_SA_P1M1ML12_WG",
    "XCPIF_SA_P1M1ML12_WG",
]
private_and_public_debt = [
    "HHINTNETGDP_SA_D1M1ML12",
    "HHINTNETGDP_SA_D1M1ML12_WG",
    "CORPINTNETGDP_SA_D1Q1QL4",
    "CORPINTNETGDP_SA_D1Q1QL4_WG",
    "XGGDGDPRATIOX10_NSA",
]
commodity_inventories = [
    "BMLXINVCSCORE_SA",
    "REFIXINVCSCORE_SA",
    "BASEXINVCSCORE_SA",
]
commodity_markets = [
    "BMLCOCRY_SAVT10_21DMA",
    "COXR_VT10vWTI_21DMA"    
]
real_appreciation_tot = [
    "CXPI_NSA_P1M12ML1",
    "CMPI_NSA_P1M12ML1",
    "CTOT_NSA_P1M12ML1",
    "REEROADJ_NSA_P1M12ML1",
]
interest_rates = [
    "RIR_NSA",
    "RYLDIRS02Y_NSA",
    "RYLDIRS05Y_NSA",
    "RSLOPEMIDDLE_NSA",
]

# All economic categories
ecos = output_growth + private_consumption + export + labour_market + business_surveys + private_credit + broad_inflation + specific_inflation + private_and_public_debt + commodity_inventories + commodity_markets + real_appreciation_tot + interest_rates

# Equity categories
eqrets = [
    "EQC" + sec + ret for sec in cids_secs for ret in ["XR_NSA", "R_NSAvALL", "R_VT10vALL"]
]

# All categories
all_xcats = [x + suff for x in ecos + ecos for suff in ["_ZN", "_ZN_NEG"]] + eqrets 

# Resultant tickers

tickers = [cid + "_" + xcat for cid in cids_eq for xcat in all_xcats]
print(f"Maximum number of tickers is {len(tickers)}")
Maximum number of tickers is 3552

Download additional data from JPMaQS #

# Additional tickers for download from JPMaQS

untradeable = [
    "EQCCODUNTRADABLE_NSA",
    "EQCCOSUNTRADABLE_NSA",
    "EQCCSRUNTRADABLE_NSA",
    "EQCENRUNTRADABLE_NSA",
    "EQCFINUNTRADABLE_NSA",
    "EQCHLCUNTRADABLE_NSA",
    "EQCINDUNTRADABLE_NSA",
    "EQCITEUNTRADABLE_NSA",
    "EQCMATUNTRADABLE_NSA",
    "EQCRELUNTRADABLE_NSA",
    "EQCUTLUNTRADABLE_NSA",   
]  # dummy variables for dates where certain sectors were untradeable

bmrs = [
    "USD_EQXR_NSA",
    "USD_EQXR_VT10"
]  # U.S. equity returns for correlation analysis

xtickers = [cid + "_" + xcat for cid in cids_eq for xcat in untradeable] + bmrs
print(f"Maximum number of tickers is {len(xtickers)}")
Maximum number of tickers is 134
# Download series from J.P. Morgan DataQuery by tickers
start_date = "2000-01-01"

# Retrieve credentials

client_id: str = os.getenv("DQ_CLIENT_ID")
client_secret: str = os.getenv("DQ_CLIENT_SECRET")

# Download from DataQuery
with JPMaQSDownload(client_id=client_id, client_secret=client_secret) as downloader:
    start = timer()
    assert downloader.check_connection()
    df_jpmaqs = downloader.download(
        tickers=xtickers,
        start_date=start_date,
        metrics=["value"],
        suppress_warning=True,
        show_progress=True,
    )
    end = timer()

print("Download time from DQ: " + str(timedelta(seconds=end - start)))
Downloading data from JPMaQS.
Timestamp UTC:  2024-09-03 14:44:19
Connection successful!
Requesting data: 100%|███████████████████████████████████████████████████████████████████| 7/7 [00:01<00:00,  4.70it/s]
Downloading data: 100%|██████████████████████████████████████████████████████████████████| 7/7 [00:07<00:00,  1.09s/it]
Some expressions are missing from the downloaded data. Check logger output for complete list.
1 out of 134 expressions are missing. To download the catalogue of all available expressions and filter the unavailable expressions, set `get_catalogue=True` in the call to `JPMaQSDownload.download()`.
Some dates are missing from the downloaded data. 
3 out of 6439 dates are missing.
Download time from DQ: 0:00:11.832393
df = msm.update_df(df_csv, df_jpmaqs)
# Dictionary of featire category labels

cat_labels = {
    "BASEXINVCSCORE_SA_ZN": {
        "Group": "Commodity inventories",
        "Label": "Excess crude inventory score",
        "Description": "Crude oil excess inventory z-score, seasonally adjusted",
        "Geography": "global",
    },
    "BMLCOCRY_SAVT10_21DMA_ZN": {
        "Group": "Market metrics",
        "Label": "Base metals carry",
        "Description": "Nominal carry for base metals basket, seasonally and vol-adjusted, 21 days moving average",
        "Geography": "global",
    },
    "BMLXINVCSCORE_SA_ZN": {
        "Group": "Commodity inventories",
        "Label": "Excess metal inventory score",
        "Description": "Base metal excess inventory z-score, seasonally adjusted",
        "Geography": "global",
    },
    "CBCSCORE_SA_D3M3ML3_WG_ZN": {
        "Group": "Business surveys",
        "Label": "Construction confidence, q/q",
        "Description": "Construction business confidence score, seas. adjusted, change q/q",
        "Geography": "weighted",
    },
    "CBCSCORE_SA_D3M3ML3_ZN": {
        "Group": "Business surveys",
        "Label": "Construction confidence, q/q",
        "Description": "Construction business confidence score, seas. adjusted, change q/q",
        "Geography": "local",
    },
    "CBCSCORE_SA_WG_ZN": {
        "Group": "Business surveys",
        "Label": "Construction confidence",
        "Description": "Construction business confidence score, seas. adjusted",
        "Geography": "weighted",
    },
    "CBCSCORE_SA_ZN": {
        "Group": "Business surveys",
        "Label": "Construction confidence",
        "Description": "Construction business confidence score, seas. adjusted",
        "Geography": "local",
    },
    "CCSCORE_SA_D3M3ML3_WG_ZN": {
        "Group": "Private consumption",
        "Label": "Consumer confidence, q/q",
        "Description": "Consumer confidence score, seasonally adjusted, change q/q",
        "Geography": "weighted",
    },
    "CCSCORE_SA_D3M3ML3_ZN": {
        "Group": "Private consumption",
        "Label": "Consumer confidence, q/q",
        "Description": "Consumer confidence score, seasonally adjusted, change q/q",
        "Geography": "local",
    },
    "CCSCORE_SA_WG_ZN": {
        "Group": "Private consumption",
        "Label": "Consumer confidence",
        "Description": "Consumer confidence score, seasonally adjusted",
        "Geography": "weighted",
    },
    "CCSCORE_SA_ZN": {
        "Group": "Private consumption",
        "Label": "Consumer confidence",
        "Description": "Consumer confidence score, seasonally adjusted",
        "Geography": "local",
    },
    "CMPI_NSA_P1M12ML1_ZN": {
        "Group": "Real appreciation",
        "Label": "Import prices, %oya",
        "Description": "Commodity-based import price index, %oya",
        "Geography": "local",
    },
    "CTOT_NSA_P1M12ML1_ZN": {
        "Group": "Real appreciation",
        "Label": "Terms-of-trade, %oya",
        "Description": "Commodity-based terms-of-trade, %oya",
        "Geography": "local",
    },
    "CXPI_NSA_P1M12ML1_ZN": {
        "Group": "Real appreciation",
        "Label": "Export prices, %oya",
        "Description": "Commodity-based export price index, %oya",
        "Geography": "local",
    },
    "COXR_VT10vWTI_21DMA_ZN": {
        "Group": "Market metrics",
        "Label": "Refined vs crude oil returns",
        "Description": "Refined oil products vs crude oil vol-targeted return differential, 21 days moving average",
        "Geography": "global",
    },
    "INTLIQGDP_NSA_D1M1ML1_ZN": {
        "Group": "Private credit",
        "Label": "Intervention liquidity, diff m/m",
        "Description": "Intervention liquidity to GDP ratio, change over the last month",
        "Geography": "local",
    },
    "INTLIQGDP_NSA_D1M1ML6_ZN": {
        "Group": "Private credit",
        "Label": "Intervention liquidity, diff 6m",
        "Description": "Intervention liquidity to GDP ratio, change overlast 6 months",
        "Geography": "local",
    },
    "MBCSCORE_SA_D3M3ML3_WG_ZN": {
        "Group": "Business surveys",
        "Label": "Manufacturing confidence, q/q",
        "Description": "Manufacturing business confidence score, seas. adj., change q/q",
        "Geography": "weighted",
    },
    "MBCSCORE_SA_D3M3ML3_ZN": {
        "Group": "Business surveys",
        "Label": "Manufacturing confidence, q/q",
        "Description": "Manufacturing business confidence score, seas. adj., change q/q",
        "Geography": "local",
    },
    "MBCSCORE_SA_WG_ZN": {
        "Group": "Business surveys",
        "Label": "Manufacturing confidence",
        "Description": "Manufacturing business confidence score, seasonally adjusted",
        "Geography": "weighted",
    },
    "MBCSCORE_SA_ZN": {
        "Group": "Business surveys",
        "Label": "Manufacturing confidence",
        "Description": "Manufacturing business confidence score, seasonally adjusted",
        "Geography": "local",
    },
    "REEROADJ_NSA_P1M12ML1_ZN": {
        "Group": "Real appreciation",
        "Label": "Open-adj REER, %oya",
        "Description": "Openness-adjusted real effective exchange rate, %oya",
        "Geography": "local",
    },
    "REFIXINVCSCORE_SA_ZN": {
        "Group": "Commodity inventories",
        "Label": "Excess refined oil inventory score",
        "Description": "Refined oil product excess inventory z-score, seas. adjusted",
        "Geography": "global",
    },
    "RIR_NSA_ZN": {
        "Group": "Market metrics",
        "Label": "Real 1-month rate",
        "Description": "Real 1-month interest rate",
        "Geography": "local",
    },
    "RSLOPEMIDDLE_NSA_ZN": {
        "Group": "Market metrics",
        "Label": "Real 5y-2y yield",
        "Description": "Real IRS yield differentials, 5-years versus 2-years",
        "Geography": "local",
    },
    "RYLDIRS02Y_NSA_ZN": {
        "Group": "Market metrics",
        "Label": "Real 2-year yield",
        "Description": "Real 2-year IRS yield",
        "Geography": "local",
    },
    "RYLDIRS05Y_NSA_ZN": {
        "Group": "Market metrics",
        "Label": "Real 5-year yield",
        "Description": "Real 5-year IRS yield",
        "Geography": "local",
    },
    "SBCSCORE_SA_D3M3ML3_WG_ZN": {
        "Group": "Business surveys",
        "Label": "Service confidence, q/q",
        "Description": "Services business confidence score, seas. adjusted, change q/q",
        "Geography": "weighted",
    },
    "SBCSCORE_SA_D3M3ML3_ZN": {
        "Group": "Business surveys",
        "Label": "Service confidence, q/q",
        "Description": "Services business confidence score, seas. adjusted, change q/q",
        "Geography": "local",
    },
    "SBCSCORE_SA_WG_ZN": {
        "Group": "Business surveys",
        "Label": "Service confidence",
        "Description": "Services business confidence score, seasonally adjusted",
        "Geography": "weighted",
    },
    "SBCSCORE_SA_ZN": {
        "Group": "Business surveys",
        "Label": "Service confidence",
        "Description": "Services business confidence score, seasonally adjusted",
        "Geography": "local",
    },
    "UNEMPLRATE_NSA_3MMA_D1M1ML12_WG_ZN": {
        "Group": "Labour market",
        "Label": "Unemployment rate, diff oya",
        "Description": "Unemployment rate, change oya",
        "Geography": "weighted",
    },
    "UNEMPLRATE_NSA_3MMA_D1M1ML12_ZN": {
        "Group": "Labour market",
        "Label": "Unemployment rate, diff oya",
        "Description": "Unemployment rate, change oya",
        "Geography": "local",
    },
    "UNEMPLRATE_SA_3MMAv5YMA_WG_ZN": {
        "Group": "Labour market",
        "Label": "Unemployment rate, diff vs 5yma",
        "Description": "Unemployment rate, difference vs 5-year moving average",
        "Geography": "weighted",
    },
    "UNEMPLRATE_SA_3MMAv5YMA_ZN": {
        "Group": "Labour market",
        "Label": "Unemployment rate, diff vs 5yma",
        "Description": "Unemployment rate, difference vs 5-year moving average",
        "Geography": "local",
    },
    "XCPIC_SA_P1M1ML12_ZN": {
        "Group": "Inflation - broad",
        "Label": "Excess core CPI, %oya",
        "Description": "Core CPI, %oya, in excess of effective inflation target",
        "Geography": "local",
    },
    "XCPIE_SA_P1M1ML12_WG_ZN": {
        "Group": "Inflation - specific",
        "Label": "Excess energy CPI, %oya",
        "Description": "Energy CPI, %oya, in excess of effective inflation target",
        "Geography": "weighted",
    },
    "XCPIE_SA_P1M1ML12_ZN": {
        "Group": "Inflation - specific",
        "Label": "Excess energy CPI, %oya",
        "Description": "Energy CPI, %oya, in excess of effective inflation target",
        "Geography": "local",
    },
    "XCPIF_SA_P1M1ML12_WG_ZN": {
        "Group": "Inflation - specific",
        "Label": "Excess food CPI, %oya",
        "Description": "Food CPI, %oya, in excess of effective inflation target",
        "Geography": "weighted",
    },
    "XCPIF_SA_P1M1ML12_ZN": {
        "Group": "Inflation - specific",
        "Label": "Excess food CPI, %oya",
        "Description": "Food CPI, %oya, in excess of effective inflation target",
        "Geography": "local",
    },
    "XCPIH_SA_P1M1ML12_ZN": {
        "Group": "Inflation - broad",
        "Label": "Excess headline CPI, %oya",
        "Description": "Headline CPI, %oya, in excess of effective inflation target",
        "Geography": "local",
    },
    "XCSTR_SA_P1M1ML12_3MMA_WG_ZN": {
        "Group": "Output growth",
        "Label": "Excess construction growth",
        "Description": "Construction output, %oya, 3mma, in excess of 5-y median GDP growth",
        "Geography": "weighted",
    },
    "XCSTR_SA_P1M1ML12_3MMA_ZN": {
        "Group": "Output growth",
        "Label": "Excess construction growth",
        "Description": "Construction output, %oya, 3mma, in excess of 5-y median GDP growth",
        "Geography": "local",
    },
    "XEMPL_NSA_P1M1ML12_3MMA_WG_ZN": {
        "Group": "Labour market",
        "Label": "Excess employment growth",
        "Description": "Employment growth, %oya, 3mma, in excess of population growth",
        "Geography": "weighted",
    },
    "XEMPL_NSA_P1M1ML12_3MMA_ZN": {
        "Group": "Labour market",
        "Label": "Excess employment growth",
        "Description": "Employment growth, %oya, 3mma, in excess of population growth",
        "Geography": "local",
    },
    "XEXPORTS_SA_P1M1ML12_3MMA_ZN": {
        "Group": "Exports",
        "Label": "Excess export growth",
        "Description": "Exports growth, %oya, 3mma, in excess of 5-year median GDP growth",
        "Geography": "local",
    },
    "XGGDGDPRATIOX10_NSA_ZN": {
        "Group": "Debt",
        "Label": "Excess projected gov. debt",
        "Description": "Government debt-to-GDP ratio proj. in 10 years, in excess of 100%",
        "Geography": "local",
    },
    "CORPINTNETGDP_SA_D1Q1QL4_WG_ZN": {
        "Group": "Debt",
        "Label": "Corporate debt servicing, %oya",
        "Description": "Corporate net debt servicing-to-GDP ratio, seasonally-adjusted, %oya",
        "Geography": "weighted",
    },
    "CORPINTNETGDP_SA_D1Q1QL4_ZN": {
        "Group": "Debt",
        "Label": "Corporate debt servicing, %oya",
        "Description": "Corporate net debt servicing-to-GDP ratio, seasonally-adjusted, %oya",
        "Geography": "local",
    },
    "HHINTNETGDP_SA_D1M1ML12_WG_ZN": {
        "Group": "Debt",
        "Label": "Households debt servicing, %oya",
        "Description": "Households net debt servicing-to-GDP ratio, seasonally-adjusted, %oya",
        "Geography": "weighted",
    },
    "HHINTNETGDP_SA_D1M1ML12_ZN": {
        "Group": "Debt",
        "Label": "Households debt servicing, %oya",
        "Description": "Households net debt servicing-to-GDP ratio, seasonally-adjusted, %oya",
        "Geography": "local",
    },
    "XIP_SA_P1M1ML12_3MMA_WG_ZN": {
        "Group": "Output growth",
        "Label": "Excess industry growth",
        "Description": "Industrial output, %oya, 3mma, in excess of 5-y median GDP growth",
        "Geography": "weighted",
    },
    "XIP_SA_P1M1ML12_3MMA_ZN": {
        "Group": "Output growth",
        "Label": "Excess industry growth",
        "Description": "Industrial output, %oya, 3mma, in excess of 5-y median GDP growth",
        "Geography": "local",
    },
    "XNRSALES_SA_P1M1ML12_3MMA_WG_ZN": {
        "Group": "Private consumption",
        "Label": "Excess retail sales growth",
        "Description": "Nominal retail sales, %oya, 3mma, in excess of 5-y median GDP growth",
        "Geography": "weighted",
    },
    "XNRSALES_SA_P1M1ML12_3MMA_ZN": {
        "Group": "Private consumption",
        "Label": "Excess retail sales growth",
        "Description": "Nominal retail sales, %oya, 3mma, in excess of 5-y median GDP growth",
        "Geography": "local",
    },
    "XRRSALES_SA_P1M1ML12_3MMA_WG_ZN": {
        "Group": "Private consumption",
        "Label": "Excess real retail growth",
        "Description": "Real retail sales, %oya, 3mma, in excess of 5-y median GDP growth",
        "Geography": "weighted",
    },
    "XRRSALES_SA_P1M1ML12_3MMA_ZN": {
        "Group": "Private consumption",
        "Label": "Excess real retail growth",
        "Description": "Real retail sales, %oya, 3mma, in excess of 5-y median GDP growth",
        "Geography": "local",
    },
    "XPCREDITBN_SJA_P1M1ML12_WG_ZN": {
        "Group": "Private credit",
        "Label": "Excess credit growth",
        "Description": "Private credit, %oya, 3mma, in excess of 5-y median GDP growth",
        "Geography": "weighted",
    },
    "XPCREDITBN_SJA_P1M1ML12_ZN": {
        "Group": "Private credit",
        "Label": "Excess credit growth",
        "Description": "Private credit, %oya, 3mma, in excess of 5-y median GDP growth",
        "Geography": "local",
    },
    "XPPIH_NSA_P1M1ML12_ZN": {
        "Group": "Inflation - broad",
        "Label": "Excess PPI, %oya",
        "Description": "Producer price inflation, %oya, in excess of eff. inflation target",
        "Geography": "local",
    },
    "XRGDPTECH_SA_P1M1ML12_3MMA_WG_ZN": {
        "Group": "Output growth",
        "Label": "Excess GDP growth",
        "Description": "Real GDP, %oya, 3mma, using HF data, in excess of 5-y med. GDP growth",
        "Geography": "weighted",
    },
    "XRGDPTECH_SA_P1M1ML12_3MMA_ZN": {
        "Group": "Output growth",
        "Label": "Excess GDP growth",
        "Description": "Real GDP, %oya, 3mma, using HF data, in excess of 5-y med. GDP growth",
        "Geography": "local",
    },
    "XRPCONS_SA_P1M1ML12_3MMA_WG_ZN": {
        "Group": "Private consumption",
        "Label": "Excess consumption growth",
        "Description": "Real private consumption, %oya, 3mma, in excess of 5-y median GDP growth",
        "Geography": "weighted",
    },
    "XRPCONS_SA_P1M1ML12_3MMA_ZN": {
        "Group": "Private consumption",
        "Label": "Excess real consum growth",
        "Description": "Real private consumption, %oya, 3mma, in excess of 5-y median GDP growth",
        "Geography": "local",
    },
    "XRWAGES_NSA_P1M1ML12_ZN": {
        "Group": "Labour market",
        "Label": "Excess real wage growth",
        "Description": "Real wage growth, %oya, in excess of medium-term productivity growth",
        "Geography": "local",
    },
}

cat_labels = pd.DataFrame(cat_labels).T
cat_alllabel_dict = cat_labels[["Label", "Geography"]].agg(", ".join, axis=1).to_dict()

cat_labels = (
    cat_labels
    .reset_index(drop=False)
    .rename(columns={"index": "Category"})
    .set_index(["Group", "Category"])
    .sort_index()
)
cat_groups_count = (
    cat_labels.index.to_frame()
    .reset_index(drop=True)
    .groupby("Group")["Category"].count()
    .sort_values(ascending=True)
)

fig = cat_groups_count.plot.barh(
    ylabel="",
    fontsize=11
)
fig.set_title(label="Number of categories by aggregate macro group", pad=20)
fig.title.set_size(16)

plt.plot()
[]
https://macrosynergy.com/notebooks.build/data-science/statistical-learning-for-sectoral-equity-allocation/_images/fd4605034092a435bd5c236330c9c9992b0abc2ae26b1e53479b1a93a9160f09.png

Feature filtering and imputation #

Cross-section availability requirement #

# All normalized macroeconomic categories
all_macroz = [x + "_ZN" for x in ecos] 

# Identify categories with less than 10 cross sections
df_macro = df[df["xcat"].isin(all_macroz)]
cid_counts = df_macro.groupby('xcat')['cid'].nunique()
xcatx_low_cid = cid_counts[cid_counts < 10].index.tolist()
print("Categories with less than 10 cross sections:\n")
for xcat in xcatx_low_cid:
    print(xcat)

# Remove categories with less than 10 cross sections
macroz = [x for x in all_macroz if not x in xcatx_low_cid]

# Identify categories that have short history

df_macro = df[df["xcat"].isin(macroz)]
cutoff_date = pd.Timestamp("2003-01-01")
min_dates = df_macro.groupby('xcat')['real_date'].min()
xcatx_late_start = min_dates[min_dates >= cutoff_date].index.tolist()
print("\nCategories that start after 2002:\n")
for xcat in xcatx_late_start:
    print(xcat)

# Remove categories that start late
macroz = [x for x in macroz if not x in xcatx_late_start]
Categories with less than 10 cross sections:

CBCSCORE_SA_D3M3ML3_WG_ZN
CBCSCORE_SA_D3M3ML3_ZN
CBCSCORE_SA_WG_ZN
CBCSCORE_SA_ZN
CORPINTNETGDP_SA_D1Q1QL4_WG_ZN
CORPINTNETGDP_SA_D1Q1QL4_ZN
HHINTNETGDP_SA_D1M1ML12_WG_ZN
HHINTNETGDP_SA_D1M1ML12_ZN

Categories that start after 2002:

COXR_VT10vWTI_21DMA_ZN
# Reduce label dictionary

cat_label_dict = {k:v for k, v in cat_alllabel_dict.items() if k in macroz}
# Visualize remaining macroeconomic categories
msm.check_availability(df, xcats=macroz, cids=cids_eq, missing_recent=False)
https://macrosynergy.com/notebooks.build/data-science/statistical-learning-for-sectoral-equity-allocation/_images/889efc76b305d17110a0778d4ae7d80ce414935f7417e764e4c6a5cea2bec6cf.png

Conditional imputation of missing cross-sections #

# Impute cross-sectional values if majority of cross sections are available

# Set parameters
impute_missing_cids = True
min_ratio_cids = 0.4

# Exclude categories than cannot logically be imputed
non_imputables = [
    "CXPI_NSA_P1M12ML1_ZN",
    "CMPI_NSA_P1M12ML1_ZN",
    "CTOT_NSA_P1M12ML1_ZN",
    "REEROADJ_NSA_P1M12ML1_ZN",
]
imputables = list(set(macroz) - set(non_imputables))

if impute_missing_cids:
    df_impute = msp.impute_panel(
        df=df, xcats=imputables, cids=cids_eq, threshold=min_ratio_cids
    )
    dfx = msm.update_df(df, df_impute)
else:
    dfx = df.copy()
# Visualize imputed macroeconomic categories
msm.check_availability(dfx, xcats=macroz, cids=cids_eq, missing_recent=False)
https://macrosynergy.com/notebooks.build/data-science/statistical-learning-for-sectoral-equity-allocation/_images/59b2de0156de60c44f7f0361e2e3dd542be282bd63a52fe003cd0ccd1267d82d.png

Equity sectoral return blacklisting #

sector_blacklist = {}

for sec in list(set(cids_secs) - {"ALL"}):
    
    dfb = df[df["xcat"] == f"EQC{sec}UNTRADABLE_NSA"].loc[:, ["cid", "xcat", "real_date", "value"]]
    dfba = (
        dfb.groupby(["cid", "real_date"])
        .aggregate(value=pd.NamedAgg(column="value", aggfunc="max"))
        .reset_index()
    )
    dfba["xcat"] = f"EQC{sec}BLACK"
    
    sector_blacklist[sec] = msp.make_blacklist(dfba, f"EQC{sec}BLACK")

Visualize target availability #

targets = [
    x for x in eqrets if x.endswith(("R_NSAvALL", "R_VT10vALL"))
]
msm.check_availability(dfx, targets, missing_recent=False)
https://macrosynergy.com/notebooks.build/data-science/statistical-learning-for-sectoral-equity-allocation/_images/081d3ab854548a5ba605778c123d2a026debed25008abcfc145388c13497cfc7.png

Sectoral signals and naive PnLs #

Common pipeline for all sectors #

default_target_type = "R_VT10vALL"

default_splitter = msl.ExpandingKFoldPanelSplit()
default_metric = make_scorer(msl.sharpe_ratio, greater_is_better=True)

# Model dictionary
default_models = {
        "ols": Pipeline(
          [
              ("selector", msl.LarsSelector()),
              ("predictor", msl.ModifiedLinearRegression(method = "analytic")),
          ]
        ),
        "swls": Pipeline(
          [
              ("selector", msl.LarsSelector()),
              ("predictor", msl.ModifiedSignWeightedLinearRegression(method = "analytic")),
          ]
        ),
        "twls": Pipeline(
          [
              ("selector", msl.LarsSelector()),
              ("predictor", msl.ModifiedTimeWeightedLinearRegression(method = "analytic")),
          ]
        ),
    }

# Hyperparameter grid   
default_hparam_grid = {
    "ols": {
        "selector__n_factors": [3, 5, 10],
        "predictor__fit_intercept": [True, False]
    },
    "swls": {
        "selector__n_factors": [3, 5, 10],
        "predictor__fit_intercept": [True, False]
    },
    "twls": {
       "selector__n_factors": [3, 5, 10],
       "predictor__fit_intercept": [True, False],
       "predictor__half_life": [12, 24, 36, 60],
    },
}


# Default parameters
default_test_size = 3  # retraining interval in months
default_min_cids = 2  # minimum number of cids to start predicting
default_min_periods = 24  # minimum number of periods to start predicting
default_threshold_ndates = 24 # number of rebalancing dates to pass before incrementing the number of CV splits
default_initial_nsplits = 3 # initial number of cross-validation splits
default_start_date = "2003-01-31"  # start date for the analysis

Energy #

Factor selection and signal generation #

sector = "ENR"

enr_dict = {
    "sector_name": sector_labels[sector],
    "signal_name": f"{sector}SOL",
    "pnl_name": f"{sector_labels[sector]} learning-based signal",
    "xcatx": macroz,
    "cidx": list(set(cids_eq)-set(["CHF"])), # CHF has no energy companies
    "ret": f"EQC{sector}{default_target_type}",
    "freq": "M",
    "black": sector_blacklist[sector],
    "srr": None,
    "pnls": None,
}
# Converted selected features and targets to scikit-learn format
xcatx = enr_dict["xcatx"] + [enr_dict["ret"]]
cidx = enr_dict["cidx"]

dfw_enr = msm.categories_df(
    df=dfx,
    xcats=xcatx,
    cids=cidx,
    freq=enr_dict["freq"],
    blacklist=enr_dict["black"],
    lag=1,
    xcat_aggs=["last", "sum"],
).sort_index().dropna()

X_enr = dfw_enr.iloc[:, :-1]
y_enr = dfw_enr.iloc[:, -1]
secname = enr_dict["sector_name"]
signal_name = enr_dict["signal_name"]

so_enr = msl.SignalOptimizer(
    inner_splitter=default_splitter,
    X=X_enr,
    y=y_enr,
    threshold_ndates = default_threshold_ndates,
    initial_nsplits = default_initial_nsplits,
)

so_enr.calculate_predictions(
    name=signal_name,
    models=default_models,
    metric=default_metric,
    hparam_grid=default_hparam_grid,
    test_size=default_test_size,
    min_cids=default_min_cids, 
    min_periods=default_min_periods,
    n_jobs=-1,
)
so_enr.models_heatmap(
    signal_name,
    cap=10,
    title=f"{secname} sector: model selection heatmap",
)

# Store signals
dfa = so_enr.get_optimized_signals()
dfx = msm.update_df(dfx, dfa)
https://macrosynergy.com/notebooks.build/data-science/statistical-learning-for-sectoral-equity-allocation/_images/9020bf54b73e21f7250fbfcb6488035906910f38646ce68d67f2c4bf50cbe5db.png
xcatx = enr_dict["signal_name"]
secname = enr_dict["sector_name"]

so_enr.feature_selection_heatmap(
    xcatx,
    remove_blanks=True,
    title=f"{secname} sector: feature selection heatmap",
    ftrs_renamed=cat_label_dict,
    figsize=(12, 10),
)
https://macrosynergy.com/notebooks.build/data-science/statistical-learning-for-sectoral-equity-allocation/_images/6d147ba621e8b98da4a57d5a44bf69a336136a775463b3a0c5c19743295aec5a.png
xcatx = enr_dict["signal_name"]
secname = enr_dict["sector_name"]

so_enr.coefs_stackedbarplot(
    name=xcatx,
    ftrs_renamed=cat_label_dict,
    title=f"{secname} sector: annual averages of most important feature coefficients",
)
https://macrosynergy.com/notebooks.build/data-science/statistical-learning-for-sectoral-equity-allocation/_images/c69b671f041955a7b9fdfe3cc4205019a58c0ae9768cfa3d2a165290bbfec6ab.png

Signal quality check #

xcatx = [enr_dict["signal_name"], enr_dict["ret"]]
cidx = enr_dict["cidx"]
secname = enr_dict["sector_name"]

cr_enr = msp.CategoryRelations(
    df=dfx,
    xcats=xcatx,
    cids=cidx,
    freq=enr_dict["freq"],
    lag=1,
    blacklist = enr_dict["black"],
    xcat_aggs=["last", "sum"],
    slip=1,
    xcat_trims=[30, 30],  # trim dodgy data point
)

cr_enr.reg_scatter(
    title=f"{secname} sector: learning-based signal and subsequent returns",
    labels=False,
    prob_est="map",
    xlab=f"{secname} signal, end-of-month, based on concurrent best model",
    ylab=f"Relative return of {secname.lower()} sector (vol-targeted), next month, %",
    coef_box="upper left",
    size=(12, 8),
)
https://macrosynergy.com/notebooks.build/data-science/statistical-learning-for-sectoral-equity-allocation/_images/056e14e3196ebc3e2849785bd7919b960be757556b8b662cc9f84889ccbb3795.png
xcatx = [enr_dict["signal_name"]]
cidx = enr_dict["cidx"]
secname = enr_dict["sector_name"]

pnl_enr = msn.NaivePnL(
    df=dfx,
    ret=enr_dict["ret"],
    sigs=xcatx,
    cids=cidx,
    start=default_start_date,
    blacklist=enr_dict["black"],
    bms=["USD_EQXR_NSA"],
)

for xcat in xcatx:
    pnl_enr.make_pnl(
        sig=xcat,
        sig_op="zn_score_pan",
        rebal_freq="monthly",
        neutral="zero",
        rebal_slip=1,
        vol_scale = None,
        thresh=3,
        pnl_name=enr_dict["pnl_name"],
    )
pnl_enr.make_long_pnl(
    vol_scale=None, label=f"{secname} always long versus all-sector basket"
)

pnl_enr.plot_pnls(
    pnl_cats=pnl_enr.pnl_names,
    title=f"{secname} sector: naive PnLs of positions versus all-sector basket",
    title_fontsize=14
)

enr_dict["pnls"] = pnl_enr
pnl_enr.evaluate_pnls(pnl_cats=pnl_enr.pnl_names)
https://macrosynergy.com/notebooks.build/data-science/statistical-learning-for-sectoral-equity-allocation/_images/18c690d086d1fbca831f913416e8107c2d624c22eb09e719284bd0fce7f9f097.png
xcat Energy always long versus all-sector basket Energy learning-based signal
Return % -17.329838 45.240855
St. Dev. % 53.003341 69.428418
Sharpe Ratio -0.326957 0.651619
Sortino Ratio -0.450023 0.974416
Max 21-Day Draw % -78.404808 -134.900713
Max 6-Month Draw % -186.69396 -113.815095
Peak to Trough Draw % -837.214078 -174.119478
Top 5% Monthly PnL Share -1.452358 0.822625
USD_EQXR_NSA correl -0.046569 -0.096442
Traded Months 259 259
secname = enr_dict["sector_name"]

pnl_enr.signal_heatmap(
    pnl_name=enr_dict["pnl_name"],
    figsize=(12, 3),
    title=f"{secname} sector: signal heatmap",
)
https://macrosynergy.com/notebooks.build/data-science/statistical-learning-for-sectoral-equity-allocation/_images/8601d5dba8e96888ca39b6596627f2c6127e9c511fb4b97b0e33b2144ba20201.png

Materials #

Factor selection and signal generation #

sector = "MAT"

mat_dict = {
    "sector_name": sector_labels[sector],
    "signal_name": f"{sector}SOL",
    "pnl_name": f"{sector_labels[sector]} learning-based signal",
    "xcatx": macroz,
    "cidx": cids_eq, 
    "ret": f"EQC{sector}{default_target_type}",
    "freq": "M",
    "black": sector_blacklist[sector],
    "srr": None,
    "pnls": None,
}
# Converted selected features and targets to scikit-learn format
xcatx = mat_dict["xcatx"] + [mat_dict["ret"]]
cidx = mat_dict["cidx"]

dfw_mat = msm.categories_df(
    df=dfx,
    xcats=xcatx,
    cids=cidx,
    freq=mat_dict["freq"],
    blacklist=mat_dict["black"],
    lag=1,
    xcat_aggs=["last", "sum"],
).dropna()

X_mat = dfw_mat.iloc[:, :-1]
y_mat = dfw_mat.iloc[:, -1]
secname = mat_dict["sector_name"]
signal_name = mat_dict["signal_name"]

so_mat = msl.SignalOptimizer(
    inner_splitter=default_splitter,
    X=X_mat,
    y=y_mat,
    threshold_ndates=default_threshold_ndates,
    initial_nsplits=default_initial_nsplits,
)

so_mat.calculate_predictions(
    name=signal_name,
    models=default_models,
    metric=default_metric,
    hparam_grid=default_hparam_grid,
    test_size=default_test_size,
    min_cids=default_min_cids,
    min_periods=default_min_periods,
    n_jobs=-1,
)
so_mat.models_heatmap(
    signal_name,
    cap=10,
    title=f"{secname} sector: model selection heatmap",
)

# Store signals
dfa = so_mat.get_optimized_signals()
dfx = msm.update_df(dfx, dfa)
https://macrosynergy.com/notebooks.build/data-science/statistical-learning-for-sectoral-equity-allocation/_images/6db10404b37529b3cd0715af7dd129afc7e6796df7e5e9b465557a00624d9fb7.png
xcatx = mat_dict["signal_name"]
secname = mat_dict["sector_name"]

so_mat.feature_selection_heatmap(
    xcatx,
    remove_blanks=True,
    title=f"{secname} sector: feature selection heatmap",
    ftrs_renamed=cat_label_dict,
    figsize=(12, 10),
)
https://macrosynergy.com/notebooks.build/data-science/statistical-learning-for-sectoral-equity-allocation/_images/1912fcf1da16b14d111197cc5dfa5ef67a4efd4d187d0e9a3d6616569b3342a1.png
xcatx = mat_dict["signal_name"]
secname = mat_dict["sector_name"]

so_mat.coefs_stackedbarplot(
    name=xcatx,
    ftrs_renamed=cat_label_dict,
    title=f"{secname} sector: annual averages of most important feature coefficients",
)
https://macrosynergy.com/notebooks.build/data-science/statistical-learning-for-sectoral-equity-allocation/_images/049071d6eb8fc7f68390246bee8dc72a3ddc9bd21e9cfd53661b5a2334103fc0.png

Signal quality check #

xcatx = [mat_dict["signal_name"], mat_dict["ret"]]
cidx = mat_dict["cidx"]
secname = mat_dict["sector_name"]

cr_mat = msp.CategoryRelations(
    df=dfx,
    xcats=xcatx,
    cids=cidx,
    freq=mat_dict["freq"],
    blacklist=mat_dict["black"],
    lag=1,
    xcat_aggs=["last", "sum"],
    slip=1,
)

cr_mat.reg_scatter(
    title=f"{secname} sector: learning-based signal and subsequent returns",
    labels=False,
    prob_est="map",
    xlab=f"{secname} signal, end-of-month, based on concurrent best model",
    ylab=f"Relative return of {secname.lower()} sector (vol-targeted), next month, %",
    coef_box="upper left",
    size=(12, 8),
)
https://macrosynergy.com/notebooks.build/data-science/statistical-learning-for-sectoral-equity-allocation/_images/caaa15608cdcc58d94b7075cf7a179643a17112deac17bddb1612b2df85f77fa.png
xcatx = [mat_dict["signal_name"]]
cidx = mat_dict["cidx"]
secname = mat_dict["sector_name"]
pnl_name=mat_dict["pnl_name"]

pnl_mat = msn.NaivePnL(
    df=dfx,
    ret=mat_dict["ret"],
    sigs=xcatx,
    cids=cidx,
    start=default_start_date,
    blacklist=mat_dict["black"],
    bms=["USD_EQXR_NSA"],
)

for xcat in xcatx:
    pnl_mat.make_pnl(
        sig=xcat,
        sig_op="zn_score_pan",
        rebal_freq="monthly",
        neutral="zero",
        rebal_slip=1,
        vol_scale=None,
        thresh=3,
        pnl_name=pnl_name,
    )
pnl_mat.make_long_pnl(
    vol_scale=None, label=f"{secname} always long versus all-sector basket"
)

pnl_mat.plot_pnls(
    pnl_cats=pnl_mat.pnl_names,
    title=f"{secname} sector: naive PnLs of positions versus all-sector basket",
    title_fontsize=14
)

mat_dict["pnls"] = pnl_mat
pnl_mat.evaluate_pnls(pnl_cats=pnl_mat.pnl_names)
https://macrosynergy.com/notebooks.build/data-science/statistical-learning-for-sectoral-equity-allocation/_images/166a9a3763fa6eb4063c5d29ec1f46f404f0a48fe1ba0bf2d54faca7d0900719.png
xcat Materials always long versus all-sector basket Materials learning-based signal
Return % -22.344101 28.04184
St. Dev. % 39.907352 46.623586
Sharpe Ratio -0.559899 0.601452
Sortino Ratio -0.767954 0.852735
Max 21-Day Draw % -61.657388 -95.041086
Max 6-Month Draw % -152.838949 -70.278055
Peak to Trough Draw % -689.542867 -131.513106
Top 5% Monthly PnL Share -0.700612 0.877869
USD_EQXR_NSA correl 0.023979 0.002989
Traded Months 259 259
secname = mat_dict["sector_name"]
xcatx = mat_dict["signal_name"]

pnl_mat.signal_heatmap(
    pnl_name=f"{secname} learning-based signal",
    figsize=(12, 3),
    title=f"{secname} sector: signal heatmap",
)
https://macrosynergy.com/notebooks.build/data-science/statistical-learning-for-sectoral-equity-allocation/_images/08df4fe76ce1e4235c065f060cdd8b03683fe456fa02844583518c6cfaf2c4ab.png

Industrials #

Factor selection and signal generation #

sector = "IND"

ind_dict = {
    "sector_name": sector_labels[sector],
    "signal_name": f"{sector}SOL",
    "pnl_name": f"{sector_labels[sector]} learning-based signal",
    "xcatx": macroz,
    "cidx": cids_eq,
    "ret": f"EQC{sector}{default_target_type}",
    "freq": "M",
    "black": sector_blacklist[sector],
    "srr": None,
    "pnls": None,
}
# Converted selected features and targets to scikit-learn format
xcatx = ind_dict["xcatx"] + [ind_dict["ret"]]
cidx = ind_dict["cidx"]

dfw_ind = msm.categories_df(
    df=dfx,
    xcats=xcatx,
    cids=cidx,
    freq=ind_dict["freq"],
    blacklist=ind_dict["black"],
    lag=1,
    xcat_aggs=["last", "sum"],
).sort_index().dropna()

X_ind = dfw_ind.iloc[:, :-1]
y_ind = dfw_ind.iloc[:, -1]
secname = ind_dict["sector_name"]
signal_name = ind_dict["signal_name"]

so_ind = msl.SignalOptimizer(
    inner_splitter=default_splitter,
    X=X_ind,
    y=y_ind,
    threshold_ndates = default_threshold_ndates,
    initial_nsplits = default_initial_nsplits,
)

so_ind.calculate_predictions(
    name=signal_name,
    models=default_models,
    metric=default_metric,
    hparam_grid=default_hparam_grid,
    test_size=default_test_size,
    min_cids=default_min_cids, 
    min_periods=default_min_periods,
    n_jobs=-1,
)
so_ind.models_heatmap(
    signal_name,
    cap=10,
    title=f"{secname} sector: model selection heatmap",
)

# Store signals
dfa = so_ind.get_optimized_signals()
dfx = msm.update_df(dfx, dfa)
https://macrosynergy.com/notebooks.build/data-science/statistical-learning-for-sectoral-equity-allocation/_images/27d717664d6006a1d2659726d9e16a27b6cec3e41b2ef5734f02428affedabfd.png
secname = ind_dict["sector_name"]
xcatx = ind_dict["signal_name"]

so_ind.feature_selection_heatmap(
    xcatx,
    remove_blanks=True,
    title=f"{secname} sector: feature selection heatmap",
    ftrs_renamed=cat_label_dict,
    figsize=(12, 10),
)
https://macrosynergy.com/notebooks.build/data-science/statistical-learning-for-sectoral-equity-allocation/_images/9439cf02d148ff66be7d27c49be9e13c05fb2bb1dfbf08c778ebabaeb469051f.png
secname = ind_dict["sector_name"]
xcatx = ind_dict["signal_name"]

so_ind.coefs_stackedbarplot(
    name=xcatx,
    ftrs_renamed=cat_label_dict,
    title=f"{secname} sector: annual averages of most important feature coefficients",
)
https://macrosynergy.com/notebooks.build/data-science/statistical-learning-for-sectoral-equity-allocation/_images/4ce0eb87ef36369d12185a03f9c01f41d1c61173591c835bd794563c489f953d.png

Signal quality check #

xcatx = [ind_dict["signal_name"], ind_dict["ret"]]
cidx = ind_dict["cidx"]
secname = ind_dict["sector_name"]

cr_ind = msp.CategoryRelations(
    df=dfx,
    xcats=xcatx,
    cids=cidx,
    freq=ind_dict["freq"],
    blacklist=ind_dict["black"],
    lag=1,
    xcat_aggs=["last", "sum"],
    slip=1,
)

cr_ind.reg_scatter(
    title=f"{secname} sector: learning-based signal and subsequent returns",
    labels=False,
    prob_est="map",
    xlab=f"{secname} signal, end-of-month, based on concurrent best model",
    ylab=f"Relative return of {secname.lower()} sector (vol-targeted), next month, %",
    coef_box="upper left",
    size=(12, 8),
)
https://macrosynergy.com/notebooks.build/data-science/statistical-learning-for-sectoral-equity-allocation/_images/b699cd4eb554be85f4fbefc04b6f5755fcbde96cb5f7314250c2cb9886e2a1d5.png
xcatx = [ind_dict["signal_name"]]
cidx = ind_dict["cidx"]
secname = ind_dict["sector_name"]
pnl_name = ind_dict["pnl_name"]

pnl_ind = msn.NaivePnL(
    df=dfx,
    ret=ind_dict["ret"],
    sigs=xcatx,
    cids=cidx,
    start=default_start_date,
    bms=["USD_EQXR_NSA"],
    blacklist = ind_dict["black"],
)

for xcat in xcatx:
    pnl_ind.make_pnl(
        sig=xcat,
        sig_op="zn_score_pan",
        rebal_freq="monthly",
        neutral="zero",
        rebal_slip=1,
        vol_scale = None,
        thresh=3,
        pnl_name=pnl_name,
    )

    pnl_ind.make_long_pnl(
    vol_scale=None, label=f"{secname} always long versus all-sector basket"
)

pnl_ind.plot_pnls(
    pnl_cats=pnl_ind.pnl_names,
    title=f"{secname} sector: naive PnLs of positions versus all-sector basket",
    title_fontsize=14
)

ind_dict["pnls"] = pnl_ind
pnl_ind.evaluate_pnls(pnl_cats=pnl_ind.pnl_names)
https://macrosynergy.com/notebooks.build/data-science/statistical-learning-for-sectoral-equity-allocation/_images/e936add49e63e3d71a4f31c1a976558dc3fa49df4d70a14b4749b2f93aff2bb2.png
xcat Industrials always long versus all-sector basket Industrials learning-based signal
Return % 16.094063 8.127747
St. Dev. % 31.542136 29.801776
Sharpe Ratio 0.51024 0.272727
Sortino Ratio 0.724523 0.392091
Max 21-Day Draw % -54.172009 -41.230626
Max 6-Month Draw % -63.203195 -54.192114
Peak to Trough Draw % -91.886853 -90.747758
Top 5% Monthly PnL Share 0.877142 1.918922
USD_EQXR_NSA correl 0.264486 0.016377
Traded Months 259 259
xcatx = ind_dict["signal_name"]
pnl_ind.signal_heatmap(
    pnl_name=f"{secname} learning-based signal",
    figsize=(12, 3),
    title=f"{secname} sector: signal heatmap",
)
https://macrosynergy.com/notebooks.build/data-science/statistical-learning-for-sectoral-equity-allocation/_images/a42a2fb047370a70d3d82f39f74969bc6d1eea2bd1a1449ff39f011db492d01b.png

Consumer discretionary #

Factor selection and signal generation #

sector = "COD"

cod_dict = {
    "sector_name": sector_labels[sector],
    "signal_name": f"{sector}SOL",
    "pnl_name": f"{sector_labels[sector]} learning-based signal",
    "xcatx": macroz,
    "cidx": cids_eq,
    "ret": f"EQC{sector}{default_target_type}",
    "freq": "M",
    "black": sector_blacklist[sector],
    "srr": None,
    "pnls": None,
}
# Converted selected features and targets to scikit-learn format
xcatx = cod_dict["xcatx"] + [cod_dict["ret"]]
cidx = cod_dict["cidx"]
dfw_cod = msm.categories_df(
    df=dfx,
    xcats=xcatx,
    cids=cidx,
    freq=cod_dict["freq"],
    blacklist=cod_dict["black"],
    lag=1,
    xcat_aggs=["last", "sum"],
).sort_index().dropna()

X_cod = dfw_cod.iloc[:, :-1]
y_cod = dfw_cod.iloc[:, -1]
secname = cod_dict["sector_name"]
signal_name = cod_dict["signal_name"]

so_cod = msl.SignalOptimizer(
    inner_splitter=default_splitter,
    X=X_cod,
    y=y_cod,
    threshold_ndates = default_threshold_ndates,
    initial_nsplits = default_initial_nsplits,
)

so_cod.calculate_predictions(
    name=signal_name,
    models=default_models,
    metric=default_metric,
    hparam_grid=default_hparam_grid,
    test_size=default_test_size,
    min_cids=default_min_cids, 
    min_periods=default_min_periods,
    n_jobs=-1,
)
so_cod.models_heatmap(
    signal_name,
    cap=10,
    title=f"{secname} sector: model selection heatmap",
)

# Store signals
dfa = so_cod.get_optimized_signals()
dfx = msm.update_df(dfx, dfa)
https://macrosynergy.com/notebooks.build/data-science/statistical-learning-for-sectoral-equity-allocation/_images/53433e9f00f4248f15a52c49c99ba41ea0c30acecb08130ca7290e24b69d1e24.png
xcatx = cod_dict["signal_name"]
secname = cod_dict["sector_name"]

so_cod.feature_selection_heatmap(
    xcatx,
    remove_blanks=True,
    title=f"{secname} sector: feature selection heatmap",
    ftrs_renamed=cat_label_dict,
    figsize=(12, 10),
)
https://macrosynergy.com/notebooks.build/data-science/statistical-learning-for-sectoral-equity-allocation/_images/257e6755a855f36c1295610a44aebe7687831f4ebafc04acc1930fe97377aada.png
xcatx = cod_dict["signal_name"]
secname = cod_dict["sector_name"]

so_cod.coefs_stackedbarplot(
    name=xcatx,
    ftrs_renamed=cat_label_dict,
    title=f"{secname} sector: annual averages of most important feature coefficients",
)
https://macrosynergy.com/notebooks.build/data-science/statistical-learning-for-sectoral-equity-allocation/_images/77602e175491196d85ac23e025418b2222014a7dd3914eeb47e68ee72fb4a3ff.png

Signal quality check #

xcatx = [cod_dict["signal_name"], cod_dict["ret"]]
cidx = cod_dict["cidx"]
signal_name = cod_dict["signal_name"]

cr_cod = msp.CategoryRelations(
    df=dfx,
    xcats=xcatx,
    cids=cidx,
    freq=cod_dict["freq"],
    blacklist = cod_dict["black"],
    lag=1,
    xcat_aggs=["last", "sum"],
    slip=1,
)

cr_cod.reg_scatter(
    title=f"{secname} sector: learning-based signal and subsequent returns",
    labels=False,
    prob_est="map",
    xlab=f"{secname} signal, end-of-month, based on concurrent best model",
    ylab=f"Relative return of {secname.lower()} sector (vol-targeted), next month, %",
    coef_box="upper left",
    size=(12, 8),
)
https://macrosynergy.com/notebooks.build/data-science/statistical-learning-for-sectoral-equity-allocation/_images/76e8df1cb830543243623bdff84ef0db16ea26b4b870ecc63a93485cb221d1e5.png
xcatx = [cod_dict["signal_name"]]
cidx = cod_dict["cidx"]
secname = cod_dict["sector_name"]
signal_name = cod_dict["signal_name"]
pnl_name = cod_dict["pnl_name"]

pnl_cod = msn.NaivePnL(
    df=dfx,
    ret=cod_dict["ret"],
    sigs=xcatx,
    cids=cidx,
    start=default_start_date,
    blacklist = cod_dict["black"],
    bms=["USD_EQXR_NSA"],
)

for xcat in xcatx:
    pnl_cod.make_pnl(
        sig=xcat,
        sig_op="zn_score_pan",
        rebal_freq="monthly",
        neutral="zero",
        rebal_slip=1,
        vol_scale = None,
        thresh=3,
        pnl_name=pnl_name,

    )
pnl_cod.make_long_pnl(
    vol_scale=None, label=f"{secname} always long versus all-sector basket"
)

pnl_cod.plot_pnls(
    pnl_cats=pnl_cod.pnl_names,
    title=f"{secname} sector: naive PnLs of positions versus all-sector basket",
    title_fontsize=14
)

cod_dict["pnls"] = pnl_cod
pnl_cod.evaluate_pnls(pnl_cats=pnl_cod.pnl_names)
https://macrosynergy.com/notebooks.build/data-science/statistical-learning-for-sectoral-equity-allocation/_images/e25d44a3cd6abfefd542da48f2a9518da91501101f1f8418e692ab4cea16bfcc.png
xcat Cons. discretionary always long versus all-sector basket Cons. discretionary learning-based signal
Return % -15.31235 15.463861
St. Dev. % 31.222662 32.068285
Sharpe Ratio -0.490424 0.482217
Sortino Ratio -0.677743 0.700718
Max 21-Day Draw % -68.480798 -46.080215
Max 6-Month Draw % -96.076731 -56.362704
Peak to Trough Draw % -348.545916 -80.91694
Top 5% Monthly PnL Share -0.735885 0.95454
USD_EQXR_NSA correl 0.095412 -0.025281
Traded Months 259 259
pnl_name = cod_dict["pnl_name"]
secname = cod_dict["sector_name"]

pnl_cod.signal_heatmap(
    pnl_name=f"{secname} learning-based signal",
    figsize=(12, 3),
    title=f"{secname} sector: signal heatmap",
)
https://macrosynergy.com/notebooks.build/data-science/statistical-learning-for-sectoral-equity-allocation/_images/51f78402c5e43f1543e69cb7059386989691627529fe3cb48ed03f6c8ca32e39.png

Consumer staples #

Factor selection and signal generation #

sector = "COS"

cos_dict = {
    "sector_name": sector_labels[sector],
    "signal_name": f"{sector}SOL",
    "pnl_name": f"{sector_labels[sector]} learning-based signal",
    "xcatx": macroz,
    "cidx": cids_eq,
    "ret": f"EQC{sector}{default_target_type}",
    "freq": "M",
    "black": sector_blacklist[sector],
    "srr": None,
    "pnls": None,
}
# Converted selected features and targets to scikit-learn format
xcatx = cos_dict["xcatx"] + [cos_dict["ret"]]
cidx = cos_dict["cidx"]

dfw_cos = msm.categories_df(
    df=dfx,
    xcats=xcatx,
    cids=cidx,
    freq=cos_dict["freq"],
    blacklist = cos_dict["black"],
    lag=1,
    xcat_aggs=["last", "sum"],
).sort_index().dropna()

X_cos = dfw_cos.iloc[:, :-1]
y_cos = dfw_cos.iloc[:, -1]
secname = cos_dict["sector_name"]
signal_name = cos_dict["signal_name"]

so_cos = msl.SignalOptimizer(
    inner_splitter=default_splitter,
    X=X_cos,
    y=y_cos,
    threshold_ndates = default_threshold_ndates,
    initial_nsplits = default_initial_nsplits,
)

so_cos.calculate_predictions(
    name=signal_name,
    models=default_models,
    metric=default_metric,
    hparam_grid=default_hparam_grid,
    test_size=default_test_size,
    min_cids=default_min_cids, 
    min_periods=default_min_periods,
    n_jobs=-1,
)
so_cos.models_heatmap(
    signal_name,
    cap=10,
    title=f"{secname} sector: model selection heatmap",
)

# Store signals
dfa = so_cos.get_optimized_signals()
dfx = msm.update_df(dfx, dfa)
https://macrosynergy.com/notebooks.build/data-science/statistical-learning-for-sectoral-equity-allocation/_images/cba52ab91b9bb8194cc039ff17498abb9afa14b0b8d6311abe4432ae6e8958b4.png
xcatx = cos_dict["signal_name"]
secname = cos_dict["sector_name"]

so_cos.feature_selection_heatmap(
    xcatx,
    remove_blanks=True,
    title=f"{secname} sector: feature selection heatmap",
    ftrs_renamed=cat_label_dict,
    figsize=(12, 10),
)
https://macrosynergy.com/notebooks.build/data-science/statistical-learning-for-sectoral-equity-allocation/_images/6ed0ebc57f36af4373a3642a4f82ca740c2b89fefebf30f1e2eb9df05a8a1e60.png
xcatx = cos_dict["signal_name"]
secname = cos_dict["sector_name"]

so_cos.coefs_stackedbarplot(
    name=xcatx,
    ftrs_renamed=cat_label_dict,
    title=f"{secname} sector: annual averages of most important feature coefficients",
)
https://macrosynergy.com/notebooks.build/data-science/statistical-learning-for-sectoral-equity-allocation/_images/cc93a04be06ef20f1fbbaec0155efb67f63a129447b1c975f0209c7716a37db9.png

Signal quality check #

xcatx = [cos_dict["signal_name"], cos_dict["ret"]]
cidx = cos_dict["cidx"]
secname = cos_dict["sector_name"]

cr_cos = msp.CategoryRelations(
    df=dfx,
    xcats=xcatx,
    cids=cidx,
    freq=cos_dict["freq"],
    blacklist = cos_dict["black"],
    lag=1,
    xcat_aggs=["last", "sum"],
    slip=1,
)

cr_cos.reg_scatter(
    title=f"{secname} sector: learning-based signal and subsequent returns",
    labels=False,
    prob_est="map",
    xlab=f"{secname} signal, end-of-month, based on concurrent best model",
    ylab=f"Relative return of {secname.lower()} sector (vol-targeted), next month, %",
    coef_box="upper left",
    size=(12, 8),
)
https://macrosynergy.com/notebooks.build/data-science/statistical-learning-for-sectoral-equity-allocation/_images/acb7ce91774ab77c44ab3512a3e153e52b9bc2a388bd8085ca4a983d5b36f400.png
xcatx = [cos_dict["signal_name"]]
cidx = cos_dict["cidx"]
secname = cos_dict["sector_name"]
signal_name = cos_dict["signal_name"]
pnl_name = cos_dict["pnl_name"]

pnl_cos = msn.NaivePnL(
    df=dfx,
    ret=cos_dict["ret"],
    sigs=xcatx,
    cids=cidx,
    start=default_start_date,
    blacklist = cos_dict["black"],
    bms=["USD_EQXR_NSA"],
)

for xcat in xcatx:
    pnl_cos.make_pnl(
        sig=xcat,
        sig_op="zn_score_pan",
        rebal_freq="monthly",
        neutral="zero",
        rebal_slip=1,
        vol_scale = None,
        thresh=3,
        pnl_name=pnl_name,
    )
pnl_cos.make_long_pnl(
    vol_scale=None, label=f"{secname} always long versus all-sector basket"
)

pnl_cos.plot_pnls(
    pnl_cats=pnl_cos.pnl_names, 
    title=f"{secname} sector: naive PnLs of positions versus all-sector basket",
    title_fontsize=14
)

cos_dict["pnls"] = pnl_cos
pnl_cos.evaluate_pnls(pnl_cats=pnl_cos.pnl_names)
https://macrosynergy.com/notebooks.build/data-science/statistical-learning-for-sectoral-equity-allocation/_images/3472efa0b9b0bf3c80562ff5da81d53ca13a67eb3261116e948e7286999f32be.png
xcat Cons. staples always long versus all-sector basket Cons. staples learning-based signal
Return % 7.593452 19.203292
St. Dev. % 37.489304 34.446072
Sharpe Ratio 0.20255 0.557489
Sortino Ratio 0.290393 0.823897
Max 21-Day Draw % -33.681565 -51.713239
Max 6-Month Draw % -93.828486 -59.198678
Peak to Trough Draw % -188.904198 -97.037594
Top 5% Monthly PnL Share 2.193531 0.999179
USD_EQXR_NSA correl -0.132772 -0.027091
Traded Months 259 259
pnl_name = cos_dict["pnl_name"]
secname = cos_dict["sector_name"]

pnl_cos.signal_heatmap(
    pnl_name=pnl_name,
    figsize=(12, 3),
    title=f"{secname} sector: signal heatmap",
)
https://macrosynergy.com/notebooks.build/data-science/statistical-learning-for-sectoral-equity-allocation/_images/dc2ad43949e193e1ebe183fa9f281a2275775f0dc929617d1f6a457334e274e6.png

Healthcare #

Factor selection and signal generation #

sector = "HLC"

hlc_dict = {
    "sector_name": sector_labels[sector],
    "signal_name": f"{sector}SOL",
    "pnl_name": f"{sector_labels[sector]} learning-based signal",
    "xcatx": macroz,
    "cidx": cids_eq,
    "ret": f"EQC{sector}{default_target_type}",
    "freq": "M",
    "black": sector_blacklist[sector],
    "srr": None,
    "pnls": None,
}
# Converted selected features and targets to scikit-learn format
xcatx = hlc_dict["xcatx"] + [hlc_dict["ret"]]
cidx = hlc_dict["cidx"]

dfw_hlc = msm.categories_df(
    df=dfx,
    xcats=xcatx,
    cids=cidx,
    freq=hlc_dict["freq"],
    blacklist=hlc_dict["black"],
    lag=1,
    xcat_aggs=["last", "sum"],
).sort_index().dropna()

X_hlc = dfw_hlc.iloc[:, :-1]
y_hlc = dfw_hlc.iloc[:, -1]
secname = hlc_dict["sector_name"]
signal_name = hlc_dict["signal_name"]

so_hlc = msl.SignalOptimizer(
    inner_splitter=default_splitter,
    X=X_hlc,
    y=y_hlc,
    threshold_ndates = default_threshold_ndates,
    initial_nsplits = default_initial_nsplits,
)

so_hlc.calculate_predictions(
    name=signal_name,
    models=default_models,
    metric=default_metric,
    hparam_grid=default_hparam_grid,
    test_size=default_test_size,
    min_cids=default_min_cids, 
    min_periods=default_min_periods,
    n_jobs=-1,
)
so_hlc.models_heatmap(
    signal_name,
    cap=10,
    title=f"{secname} sector: model selection heatmap",
)

# Store signals
dfa = so_hlc.get_optimized_signals()
dfx = msm.update_df(dfx, dfa)
https://macrosynergy.com/notebooks.build/data-science/statistical-learning-for-sectoral-equity-allocation/_images/214d306bec98f594b991cf646f7aa10a4f6a8267b051beb1f3f1058ffc6e3e5d.png
xcatx = hlc_dict["signal_name"]

so_hlc.feature_selection_heatmap(
    xcatx,
    remove_blanks=True,
    title="Feature selection heatmap, healthcare sector",
    ftrs_renamed=cat_label_dict,
    figsize=(12, 10),
)
https://macrosynergy.com/notebooks.build/data-science/statistical-learning-for-sectoral-equity-allocation/_images/76bea868829edecf0ad5cacc49cdb013050fbd4e296c35c1eca12e96ef13e558.png
xcatx = hlc_dict["signal_name"]
signal_name = hlc_dict["signal_name"]

so_hlc.coefs_stackedbarplot(
    name=xcatx,
    ftrs_renamed=cat_label_dict,
    title=f"{secname} sector: annual averages of most important feature coefficients",
)
https://macrosynergy.com/notebooks.build/data-science/statistical-learning-for-sectoral-equity-allocation/_images/eb6aaf13e220a35f6b1aacd1f3c3d62d365046e4e82b5bab42f1a744e860a2bb.png

Signal quality check #

xcatx = [hlc_dict["signal_name"], hlc_dict["ret"]]
cidx = hlc_dict["cidx"]
secname = hlc_dict["sector_name"]

cr_hlc = msp.CategoryRelations(
    df=dfx,
    xcats=xcatx,
    cids=cidx,
    freq=hlc_dict["freq"],
    blacklist = hlc_dict["black"],
    lag=1,
    xcat_aggs=["last", "sum"],
    slip=1,
)

cr_hlc.reg_scatter(
    title=f"{secname} sector: learning-based signal and subsequent returns",
    labels=False,
    prob_est="map",
    xlab=f"{secname} signal, end-of-month, based on concurrent best model",
    ylab=f"Relative return of {secname.lower()} sector (vol-targeted), next month, %",
    coef_box="upper left",
    size=(12, 8),
)
https://macrosynergy.com/notebooks.build/data-science/statistical-learning-for-sectoral-equity-allocation/_images/3590bbfdaa0afef87a2986f63cc49e109732962e5b126da4b297fb9f2f0472be.png
xcatx = [hlc_dict["signal_name"]]
cidx = hlc_dict["cidx"]
secname = hlc_dict["sector_name"]
signal_name = hlc_dict["signal_name"]
pnl_name = hlc_dict["pnl_name"]

pnl_hlc = msn.NaivePnL(
    df=dfx,
    ret=hlc_dict["ret"],
    sigs=xcatx,
    cids=cidx,
    start=default_start_date,
    blacklist = hlc_dict["black"],
    bms=["USD_EQXR_NSA"],
)

for xcat in xcatx:
    pnl_hlc.make_pnl(
        sig=xcat,
        sig_op="zn_score_pan",
        rebal_freq="monthly",
        neutral="zero",
        rebal_slip=1,
        vol_scale = None,
        thresh=3,
        pnl_name=pnl_name,
    )
pnl_hlc.make_long_pnl(
    vol_scale=None, label=f"{secname} always long versus all-sector basket"
)

pnl_hlc.plot_pnls(
    pnl_cats=pnl_hlc.pnl_names,
    title=f"{secname} sector: naive PnLs of positions versus all-sector basket",
    title_fontsize=14
)

hlc_dict["pnls"] = pnl_hlc
pnl_hlc.evaluate_pnls(pnl_cats=pnl_hlc.pnl_names)
https://macrosynergy.com/notebooks.build/data-science/statistical-learning-for-sectoral-equity-allocation/_images/4a1493545aa84f05995442d6b26a653c11b741dcaeae1c5167d26a898244ba6a.png
xcat Healthcare always long versus all-sector basket Healthcare learning-based signal
Return % -2.636272 6.570015
St. Dev. % 38.515166 40.975564
Sharpe Ratio -0.068448 0.16034
Sortino Ratio -0.09828 0.227905
Max 21-Day Draw % -47.409795 -88.317844
Max 6-Month Draw % -93.207315 -90.456598
Peak to Trough Draw % -263.887278 -198.493241
Top 5% Monthly PnL Share -6.221331 2.783057
USD_EQXR_NSA correl -0.159854 0.046437
Traded Months 259 259
pnl_name = hlc_dict["pnl_name"]
secname = hlc_dict["sector_name"]

pnl_hlc.signal_heatmap(
    pnl_name=pnl_name,
    figsize=(12, 3),
    title=f"{secname} sector: signal heatmap",
)
https://macrosynergy.com/notebooks.build/data-science/statistical-learning-for-sectoral-equity-allocation/_images/c901da419700265b515f36643fd78066b0577d6d04abd0fd18dbfa87394d2693.png

Financials #

Factor selection and signal generation #

sector = "FIN"

fin_dict = {
    "sector_name": sector_labels[sector],
    "signal_name": f"{sector}SOL",
    "pnl_name": f"{sector_labels[sector]} learning-based signal",
    "xcatx": macroz,
    "cidx": cids_eq,
    "ret": f"EQC{sector}{default_target_type}",
    "freq": "M",
    "black": sector_blacklist[sector],
    "srr": None,
    "pnls": None,
}
# Converted selected features and targets to scikit-learn format
xcatx = fin_dict["xcatx"] + [fin_dict["ret"]]
cidx = fin_dict["cidx"]

dfw_fin = msm.categories_df(
    df=dfx,
    xcats=xcatx,
    cids=cidx,
    freq=fin_dict["freq"],
    blacklist=fin_dict["black"],
    lag=1,
    xcat_aggs=["last", "sum"],
).sort_index().dropna()

X_fin = dfw_fin.iloc[:, :-1]
y_fin = dfw_fin.iloc[:, -1]
secname = fin_dict["sector_name"]
signal_name = fin_dict["signal_name"]

so_fin = msl.SignalOptimizer(
    inner_splitter=default_splitter,
    X=X_fin,
    y=y_fin,
    threshold_ndates = default_threshold_ndates,
    initial_nsplits = default_initial_nsplits,
)

so_fin.calculate_predictions(
    name=signal_name,
    models=default_models,
    metric=default_metric,
    hparam_grid=default_hparam_grid,
    test_size=default_test_size,
    min_cids=default_min_cids, 
    min_periods=default_min_periods,
    n_jobs=-1,
)
so_fin.models_heatmap(
    signal_name,
    cap=10,
    title=f"{secname} sector: model selection heatmap",
)

# Store signals
dfa = so_fin.get_optimized_signals()
dfx = msm.update_df(dfx, dfa)
https://macrosynergy.com/notebooks.build/data-science/statistical-learning-for-sectoral-equity-allocation/_images/8a4bb0527f07b568d232681fe8bb4d2d94e66dd0b6f8d1768a59fc508e754b88.png
xcatx = fin_dict["signal_name"]
secname = fin_dict["sector_name"]

so_fin.feature_selection_heatmap(
    xcatx,
    remove_blanks=True,
    title=f"{secname} sector: feature selection heatmap",
    ftrs_renamed=cat_label_dict,
    figsize=(12, 10),
)
https://macrosynergy.com/notebooks.build/data-science/statistical-learning-for-sectoral-equity-allocation/_images/cc6b6a60855d86ba024ccc32f64ed4e48ae80caa9da1a5b13d70ceb4511527e1.png
xcatx = fin_dict["signal_name"]
secname = fin_dict["sector_name"]

so_fin.coefs_stackedbarplot(
    name=xcatx,
    ftrs_renamed=cat_label_dict,
    title=f"{secname} sector: annual averages of most important feature coefficients",
)
https://macrosynergy.com/notebooks.build/data-science/statistical-learning-for-sectoral-equity-allocation/_images/280d38db76815e94a8cb89f0d8f0807b91f05c533f19239b6d811a81a5c3d50d.png

Signal quality check #

xcatx = [fin_dict["signal_name"], fin_dict["ret"]]
cidx = fin_dict["cidx"]
secname = fin_dict["sector_name"]

cr_fin = msp.CategoryRelations(
    df=dfx,
    xcats=xcatx,
    cids=cidx,
    freq=fin_dict["freq"],
    blacklist = fin_dict["black"],
    lag=1,
    xcat_aggs=["last", "sum"],
    slip=1,
)

cr_fin.reg_scatter(
    title=f"{secname} sector: learning-based signal and subsequent returns",
    labels=False,
    prob_est="map",
    xlab=f"{secname} signal, end-of-month, based on concurrent best model",
    ylab=f"Relative return of {secname.lower()} sector (vol-targeted), next month, %",
    coef_box="upper left",
    size=(12, 8),
)
https://macrosynergy.com/notebooks.build/data-science/statistical-learning-for-sectoral-equity-allocation/_images/ccd88e939cbfdbbefa30df17b525def77a16b7a302fbce2cdf79130c2287f8cd.png
xcatx = [fin_dict["signal_name"]]
cidx = fin_dict["cidx"]
secname = fin_dict["sector_name"]
signal_name = fin_dict["signal_name"]
pnl_name = fin_dict["pnl_name"]

pnl_fin = msn.NaivePnL(
    df=dfx,
    ret=fin_dict["ret"],
    sigs=xcatx,
    cids=cidx,
    start=default_start_date,
    blacklist = fin_dict["black"],
    bms=["USD_EQXR_NSA"],
)

for xcat in xcatx:
    pnl_fin.make_pnl(
        sig=xcat,
        sig_op="zn_score_pan",
        rebal_freq="monthly",
        neutral="zero",
        rebal_slip=1,
        vol_scale = None,
        thresh=3,
        pnl_name=pnl_name,
    )
pnl_fin.make_long_pnl(
    vol_scale=None, label=f"{secname} always long versus all-sector basket"
)

pnl_fin.plot_pnls(
    pnl_cats=pnl_fin.pnl_names,
    title=f"{secname} sector: naive PnLs of positions versus all-sector basket",
    title_fontsize=14
)

fin_dict["pnls"] = pnl_fin
pnl_fin.evaluate_pnls(pnl_cats=pnl_fin.pnl_names)
https://macrosynergy.com/notebooks.build/data-science/statistical-learning-for-sectoral-equity-allocation/_images/7dd7db43d3dfff2b806d40994fc6dbc30276f8e352e9c037fea283dd0631fe38.png
xcat Financials always long versus all-sector basket Financials learning-based signal
Return % 0.356038 1.348195
St. Dev. % 37.914126 44.050302
Sharpe Ratio 0.009391 0.030606
Sortino Ratio 0.013681 0.044084
Max 21-Day Draw % -76.126375 -98.35711
Max 6-Month Draw % -96.122679 -178.935643
Peak to Trough Draw % -332.744644 -260.907163
Top 5% Monthly PnL Share 51.811272 18.061715
USD_EQXR_NSA correl 0.216566 -0.115434
Traded Months 259 259
xcatx = fin_dict["signal_name"]
pnl_name = fin_dict["pnl_name"]
secname = fin_dict["sector_name"]

pnl_fin.signal_heatmap(
    pnl_name=pnl_name,
    figsize=(12, 3),
    title=f"{secname} sector: signal heatmap",
)
https://macrosynergy.com/notebooks.build/data-science/statistical-learning-for-sectoral-equity-allocation/_images/dfb1744420da430659e4ecbb97d130a19524301a51804891136c2147c46f33eb.png

Technology #

Factor selection and signal generation #

sector = "ITE"

ite_dict = {
    "sector_name": sector_labels[sector],
    "signal_name": f"{sector}SOL",
    "pnl_name": f"{sector_labels[sector]} learning-based signal",
    "xcatx": macroz,
    "cidx": cids_eq,
    "ret": f"EQC{sector}{default_target_type}",
    "freq": "M",
    "black": sector_blacklist[sector],
    "srr": None,
    "pnls": None,
}
# Converted selected features and targets to scikit-learn format
xcatx = ite_dict["xcatx"] + [ite_dict["ret"]]
cidx = ite_dict["cidx"]

dfw_ite = msm.categories_df(
    df=dfx,
    xcats=xcatx,
    cids=cidx,
    freq=ite_dict["freq"],
    blacklist = ite_dict["black"],
    lag=1,
    xcat_aggs=["last", "sum"],
).sort_index().dropna()

X_ite = dfw_ite.iloc[:, :-1]
y_ite = dfw_ite.iloc[:, -1]
secname = ite_dict["sector_name"]
signal_name = ite_dict["signal_name"]

so_ite = msl.SignalOptimizer(
    inner_splitter=default_splitter,
    X=X_ite,
    y=y_ite,
    threshold_ndates = default_threshold_ndates,
    initial_nsplits = default_initial_nsplits,
)

so_ite.calculate_predictions(
    name=signal_name,
    models=default_models,
    metric=default_metric,
    hparam_grid=default_hparam_grid,
    test_size=default_test_size,
    min_cids=default_min_cids, 
    min_periods=default_min_periods,
    n_jobs=-1,
)
so_ite.models_heatmap(
    signal_name,
    cap=10,
    title=f"{secname} sector: model selection heatmap",
)

# Store signals
dfa = so_ite.get_optimized_signals()
dfx = msm.update_df(dfx, dfa)
https://macrosynergy.com/notebooks.build/data-science/statistical-learning-for-sectoral-equity-allocation/_images/1cbc20eff4f03f2dc494e23f39268fae90fa440047f76743d258931a6db56f43.png
xcatx = ite_dict["signal_name"]
secname = ite_dict["sector_name"]

so_ite.feature_selection_heatmap(
    xcatx,
    remove_blanks=True,
    title=f"{secname} sector: feature selection heatmap",
    ftrs_renamed=cat_label_dict,
    figsize=(12, 10),
)
https://macrosynergy.com/notebooks.build/data-science/statistical-learning-for-sectoral-equity-allocation/_images/f973c35132421a49fd8b12ac56714882d76993cc85d77479495810126a220583.png
xcatx = ite_dict["signal_name"]
secname = ite_dict["sector_name"]

so_ite.coefs_stackedbarplot(
    name=xcatx,
    ftrs_renamed=cat_label_dict,
    title=f"{secname} sector: annual averages of most important feature coefficients",
)
https://macrosynergy.com/notebooks.build/data-science/statistical-learning-for-sectoral-equity-allocation/_images/2e83b30fe61e9d0eb77b1aa299b3a8a41761d4a6dcfb23b66d22255f1010d13b.png

Signal quality check #

xcatx = [ite_dict["signal_name"], ite_dict["ret"]]
cidx = ite_dict["cidx"]
secname = ite_dict["sector_name"]

cr_ite = msp.CategoryRelations(
    df=dfx,
    xcats=xcatx,
    cids=cidx,
    freq=ite_dict["freq"],
    blacklist = ite_dict["black"],
    lag=1,
    xcat_aggs=["last", "sum"],
    slip=1,
)

cr_ite.reg_scatter(
    title=f"{secname} sector: learning-based signal and subsequent returns",
    labels=False,
    prob_est="map",
    xlab=f"{secname} signal, end-of-month, based on concurrent best model",
    ylab=f"Relative return of {secname.lower()} sector (vol-targeted), next month, %",
    coef_box="upper left",
    size=(12, 8),
)
https://macrosynergy.com/notebooks.build/data-science/statistical-learning-for-sectoral-equity-allocation/_images/9b701e90908c19da617ff97127821ad4a7fd4d94dd6b5d82b1427aecc5054e00.png
xcatx = [ite_dict["signal_name"]]
cidx = ite_dict["cidx"]
secname = ite_dict["sector_name"]
signal_name = ite_dict["signal_name"]
pnl_name = ite_dict["pnl_name"]

pnl_ite = msn.NaivePnL(
    df=dfx,
    ret=ite_dict["ret"],
    sigs=xcatx,
    cids=cidx,
    start=default_start_date,
    blacklist=ite_dict["black"],
    bms=["USD_EQXR_NSA"],
)

for xcat in xcatx:
    pnl_ite.make_pnl(
        sig=xcat,
        sig_op="zn_score_pan",
        rebal_freq="monthly",
        neutral="zero",
        rebal_slip=1,
        vol_scale = None,
        thresh=3,
        pnl_name=pnl_name,
    )
pnl_ite.make_long_pnl(
    vol_scale=None, label=f"{secname} always long versus all-sector basket"
)

pnl_ite.plot_pnls(
    pnl_cats=pnl_ite.pnl_names,
    title=f"{secname} sector: naive PnLs of positions versus all-sector basket",
    title_fontsize=14,
)

ite_dict["pnls"] = pnl_ite
pnl_ite.evaluate_pnls(pnl_cats=pnl_ite.pnl_names)
https://macrosynergy.com/notebooks.build/data-science/statistical-learning-for-sectoral-equity-allocation/_images/83c709a85d2d05bd940d3df0978a025dd8052eb814d398792a5d9038d3ba39c7.png
xcat Information tech always long versus all-sector basket Information tech learning-based signal
Return % -6.037478 8.042235
St. Dev. % 37.37468 37.787246
Sharpe Ratio -0.161539 0.212829
Sortino Ratio -0.223487 0.299029
Max 21-Day Draw % -43.579705 -53.583944
Max 6-Month Draw % -116.166222 -79.099934
Peak to Trough Draw % -404.663283 -207.638985
Top 5% Monthly PnL Share -2.636402 2.500706
USD_EQXR_NSA correl 0.025743 -0.001081
Traded Months 259 259
pnl_name = ite_dict["pnl_name"]
secname = ite_dict["sector_name"]

pnl_ite.signal_heatmap(
    pnl_name=pnl_name,
    figsize=(12, 3),
    title=f"{secname} sector: signal heatmap",
)
https://macrosynergy.com/notebooks.build/data-science/statistical-learning-for-sectoral-equity-allocation/_images/ac584f23bdfd170e8b20c154d2711682b5cedecdf848969689320013d1604404.png

Communication #

Factor selection and signal generation #

sector = "CSR"

csr_dict = {
    "sector_name": sector_labels[sector],
    "signal_name": f"{sector}SOL",
    "pnl_name": f"{sector_labels[sector]} learning-based signal",
    "xcatx": macroz,
    "cidx": cids_eq,
    "ret": f"EQC{sector}{default_target_type}",
    "freq": "M",
    "black": sector_blacklist[sector],
    "srr": None,
    "pnls": None,
}
# Converted selected features and targets to scikit-learn format
xcatx = csr_dict["xcatx"] + [csr_dict["ret"]]
cidx = csr_dict["cidx"]

dfw_csr = msm.categories_df(
    df=dfx,
    xcats=xcatx,
    cids=cidx,
    freq=csr_dict["freq"],
    blacklist=csr_dict["black"],
    lag=1,
    xcat_aggs=["last", "sum"],
).sort_index().dropna()

X_csr = dfw_csr.iloc[:, :-1]
y_csr = dfw_csr.iloc[:, -1]
secname = csr_dict["sector_name"]
signal_name = csr_dict["signal_name"]

so_csr = msl.SignalOptimizer(
    inner_splitter=default_splitter,
    X=X_csr,
    y=y_csr,
    threshold_ndates = default_threshold_ndates,
    initial_nsplits = default_initial_nsplits,
)

so_csr.calculate_predictions(
    name=signal_name,
    models=default_models,
    metric=default_metric,
    hparam_grid=default_hparam_grid,
    test_size=default_test_size,
    min_cids=default_min_cids, 
    min_periods=default_min_periods,
    n_jobs=-1,
)
so_csr.models_heatmap(
    signal_name,
    cap=10,
    title=f"{secname} sector: model selection heatmap",
)

# Store signals
dfa = so_csr.get_optimized_signals()
dfx = msm.update_df(dfx, dfa)
https://macrosynergy.com/notebooks.build/data-science/statistical-learning-for-sectoral-equity-allocation/_images/f0f12f2db7ac2853f79adb0c55c1514b18fddd6949cd890545ff73aa3cbf3140.png
xcatx = csr_dict["signal_name"]
secname = csr_dict["sector_name"]

so_csr.feature_selection_heatmap(
    xcatx,
    remove_blanks=True,
    title=f"{secname} sector: feature selection heatmap",
    ftrs_renamed=cat_label_dict,
    figsize=(12, 10),
)
https://macrosynergy.com/notebooks.build/data-science/statistical-learning-for-sectoral-equity-allocation/_images/9bcdb18077ae31cd9a27035d61ee200fb03042f49ba4e3017e3a8f696bf94b88.png
xcatx = csr_dict["signal_name"]
secname = csr_dict["sector_name"]

so_csr.coefs_stackedbarplot(
    name=xcatx,
    ftrs_renamed=cat_label_dict,
    title=f"{secname} sector: annual averages of most important feature coefficients",
)
https://macrosynergy.com/notebooks.build/data-science/statistical-learning-for-sectoral-equity-allocation/_images/5e6b4e1dc900ca6a31c92d4c769b4cecf5a2e1f5a74db818477f38efd8fee8a8.png

Signal quality check #

xcatx = [csr_dict["signal_name"], csr_dict["ret"]]
cidx = csr_dict["cidx"]
secname = csr_dict["sector_name"]

cr_csr = msp.CategoryRelations(
    df=dfx,
    xcats=xcatx,
    cids=cidx,
    freq=csr_dict["freq"],
    blacklist = csr_dict["black"],
    lag=1,
    xcat_aggs=["last", "sum"],
    slip=1,
)

cr_csr.reg_scatter(
    title=f"{secname} sector: learning-based signal and subsequent returns",
    labels=False,
    prob_est="map",
    xlab=f"{secname} signal, end-of-month, based on concurrent best model",
    ylab=f"Relative return of {secname.lower()} sector (vol-targeted), next month, %",
    coef_box="upper left",
    size=(12, 8),
)
https://macrosynergy.com/notebooks.build/data-science/statistical-learning-for-sectoral-equity-allocation/_images/2074cb271b6aaf121c74ebfd90b99691eb62cc82289d684edd9056d4bc9089d2.png
xcatx = [csr_dict["signal_name"]]
cidx = csr_dict["cidx"]
secname = csr_dict["sector_name"]
signal_name = csr_dict["signal_name"]
pnl_name = csr_dict["pnl_name"]

pnl_csr = msn.NaivePnL(
    df=dfx,
    ret=csr_dict["ret"],
    sigs=xcatx,
    cids=cidx,
    start=default_start_date,
    blacklist = csr_dict["black"],
    bms=["USD_EQXR_NSA"],
)

for xcat in xcatx:
    pnl_csr.make_pnl(
        sig=xcat,
        sig_op="zn_score_pan",
        rebal_freq="monthly",
        neutral="zero",
        rebal_slip=1,
        vol_scale = None,
        thresh=3,
        pnl_name=pnl_name,
    )
pnl_csr.make_long_pnl(
    vol_scale=None, label=f"{secname} always long versus all-sector basket"
)

pnl_csr.plot_pnls(
    pnl_cats=pnl_csr.pnl_names,
    title=f"{secname} sector: naive PnLs of positions versus all-sector basket",
    title_fontsize=14
)

csr_dict["pnls"] = pnl_csr
pnl_csr.evaluate_pnls(pnl_cats=pnl_csr.pnl_names)
https://macrosynergy.com/notebooks.build/data-science/statistical-learning-for-sectoral-equity-allocation/_images/f7fe541a3b1d54daae2d1b83632115aa17716b26251777f0e11c11463cba5df9.png
xcat Communication services always long versus all-sector basket Communication services learning-based signal
Return % -12.009305 10.040176
St. Dev. % 35.601175 31.47881
Sharpe Ratio -0.337329 0.31895
Sortino Ratio -0.477247 0.446419
Max 21-Day Draw % -41.34344 -95.403352
Max 6-Month Draw % -110.518234 -159.358749
Peak to Trough Draw % -322.986005 -213.150624
Top 5% Monthly PnL Share -1.199733 1.71775
USD_EQXR_NSA correl -0.036372 0.01053
Traded Months 259 259
pnl_name = csr_dict["pnl_name"]
secname = csr_dict["sector_name"]

pnl_csr.signal_heatmap(
    pnl_name=pnl_name,
    figsize=(12, 3),
    title=f"{secname} sector: signal heatmap",
)
https://macrosynergy.com/notebooks.build/data-science/statistical-learning-for-sectoral-equity-allocation/_images/50915da1a1459bec12c8696c43331eb066662283f1b3245fdb57e4851723646b.png

Utilities #

Factor selection and signal generation #

sector = "UTL"

utl_dict = {
    "sector_name": sector_labels[sector],
    "signal_name": f"{sector}SOL",
    "pnl_name": f"{sector_labels[sector]} learning-based signal",
    "xcatx": macroz,
    "cidx": cids_eq,
    "ret": f"EQC{sector}{default_target_type}",
    "freq": "M",
    "black": sector_blacklist[sector],
    "srr": None,
    "pnls": None,
}
# Converted selected features and targets to scikit-learn format
xcatx = utl_dict["xcatx"] + [utl_dict["ret"]]
cidx = utl_dict["cidx"]

dfw_utl = msm.categories_df(
    df=dfx,
    xcats=xcatx,
    cids=cidx,
    freq=utl_dict["freq"],
    blacklist=utl_dict["black"],
    lag=1,
    xcat_aggs=["last", "sum"],
).sort_index().dropna()

X_utl = dfw_utl.iloc[:, :-1]
y_utl = dfw_utl.iloc[:, -1]
secname = utl_dict["sector_name"]
signal_name = utl_dict["signal_name"]

so_utl = msl.SignalOptimizer(
    inner_splitter=default_splitter,
    X=X_utl,
    y=y_utl,
    threshold_ndates = default_threshold_ndates,
    initial_nsplits = default_initial_nsplits,
)

so_utl.calculate_predictions(
    name=signal_name,
    models=default_models,
    metric=default_metric,
    hparam_grid=default_hparam_grid,
    test_size=default_test_size,
    min_cids=default_min_cids, 
    min_periods=default_min_periods,
    n_jobs=-1,
)
so_utl.models_heatmap(
    signal_name,
    cap=10,
    title=f"{secname} sector: model selection heatmap",
)

# Store signals
dfa = so_utl.get_optimized_signals()
dfx = msm.update_df(dfx, dfa)
https://macrosynergy.com/notebooks.build/data-science/statistical-learning-for-sectoral-equity-allocation/_images/7665254813b32c7062a095be63a10c19ee2aab515f545156bffbb7331b3c4543.png
xcatx = utl_dict["signal_name"]
secname = utl_dict["sector_name"]

so_utl.feature_selection_heatmap(
    xcatx,
    remove_blanks=True,
    title=f"{secname} sector: feature selection heatmap",
    ftrs_renamed=cat_label_dict,
    figsize=(12, 10),
)
https://macrosynergy.com/notebooks.build/data-science/statistical-learning-for-sectoral-equity-allocation/_images/bc3eb793b40797903d5532b4d78c9b043e37fbec3c6363aba3a70c15256a4274.png
xcatx = utl_dict["signal_name"]
secname = utl_dict["sector_name"]

so_utl.coefs_stackedbarplot(
    name=xcatx,
    ftrs_renamed=cat_label_dict,
    title=f"{secname} sector: annual averages of most important feature coefficients",
)
https://macrosynergy.com/notebooks.build/data-science/statistical-learning-for-sectoral-equity-allocation/_images/5a7e8367d78be9235f209de3d006dcf6da96f47d6e8a0f844e51d4e3136be0f5.png

Signal quality check #

xcatx = [utl_dict["signal_name"], utl_dict["ret"]]
cidx = utl_dict["cidx"]
secname = utl_dict["sector_name"]

cr_utl = msp.CategoryRelations(
    df=dfx,
    xcats=xcatx,
    cids=cidx,
    freq=utl_dict["freq"],
    blacklist=utl_dict["black"],
    lag=1,
    xcat_aggs=["last", "sum"],
    slip=1,
)

cr_utl.reg_scatter(
    title=f"{secname} sector: learning-based signal and subsequent returns",
    labels=False,
    prob_est="map",
    xlab=f"{secname} signal, end-of-month, based on concurrent best model",
    ylab=f"Relative return of {secname.lower()} sector (vol-targeted), next month, %",
    coef_box="upper left",
    size=(12, 8),
)
https://macrosynergy.com/notebooks.build/data-science/statistical-learning-for-sectoral-equity-allocation/_images/cc9d52642249bf497d25952c51b89eb055e14da9cc8b1deb101387945a17da78.png
xcatx = [utl_dict["signal_name"]]
cidx = utl_dict["cidx"]
secname = utl_dict["sector_name"]
signal_name = utl_dict["signal_name"]
pnl_name = utl_dict["pnl_name"]

pnl_utl = msn.NaivePnL(
    df=dfx,
    ret=utl_dict["ret"],
    sigs=xcatx,
    cids=cidx,
    start=default_start_date,
    blacklist = utl_dict["black"],
    bms=["USD_EQXR_NSA"],
)

for xcat in xcatx:
    pnl_utl.make_pnl(
        sig=xcat,
        sig_op="zn_score_pan",
        rebal_freq="monthly",
        neutral="zero",
        rebal_slip=1,
        vol_scale = None,
        thresh=3,
        pnl_name=pnl_name,
    )
pnl_utl.make_long_pnl(
    vol_scale=None, label=f"{secname} always long versus all-sector basket"
)

pnl_utl.plot_pnls(
    pnl_cats=pnl_utl.pnl_names,
    title=f"{secname} sector: naive PnLs of positions versus all-sector basket",
    title_fontsize=14
)

utl_dict["pnls"] = pnl_utl
pnl_utl.evaluate_pnls(pnl_cats=pnl_utl.pnl_names)
https://macrosynergy.com/notebooks.build/data-science/statistical-learning-for-sectoral-equity-allocation/_images/b69522cb477de31d0b15ed601d41eec064bd0f02944694a7e0760d0488acf176.png
xcat Utilities always long versus all-sector basket Utilities learning-based signal
Return % 11.253423 18.179652
St. Dev. % 39.290727 41.618768
Sharpe Ratio 0.286414 0.436814
Sortino Ratio 0.419205 0.639079
Max 21-Day Draw % -45.235513 -102.238062
Max 6-Month Draw % -93.980448 -112.403374
Peak to Trough Draw % -272.563373 -145.008647
Top 5% Monthly PnL Share 1.634651 1.13979
USD_EQXR_NSA correl -0.194839 -0.037821
Traded Months 259 259
pnl_name = utl_dict["pnl_name"]
secname = utl_dict["sector_name"]

pnl_utl.signal_heatmap(
    pnl_name=pnl_name,
    figsize=(12, 3),
    title=f"{secname} sector: signal heatmap",
)
https://macrosynergy.com/notebooks.build/data-science/statistical-learning-for-sectoral-equity-allocation/_images/21e1d3e9ca9c25b698967946cf86cf2ea1d8fef78312d6c1fcd0fd4d1c463ce3.png

Real estate #

Factor selection and signal generation #

sector = "REL"

rel_dict = {
    "sector_name": sector_labels[sector],
    "signal_name": f"{sector}SOL",
    "pnl_name": f"{sector_labels[sector]} learning-based signal",
    "xcatx": macroz,
    "cidx": cids_eq,
    "ret": f"EQC{sector}{default_target_type}",
    "freq": "M",
    "black": sector_blacklist[sector],
    "srr": None,
    "pnls": None,
}
# Converted selected features and targets to scikit-learn format
xcatx = rel_dict["xcatx"] + [rel_dict["ret"]]
cidx = rel_dict["cidx"]

dfw_rel = msm.categories_df(
    df=dfx,
    xcats=xcatx,
    cids=cidx,
    freq=rel_dict["freq"],
    blacklist=rel_dict["black"],
    lag=1,
    xcat_aggs=["last", "sum"],
).sort_index().dropna()

X_rel = dfw_rel.iloc[:, :-1]
y_rel = dfw_rel.iloc[:, -1]
secname = rel_dict["sector_name"]
signal_name = rel_dict["signal_name"]

so_rel = msl.SignalOptimizer(
    inner_splitter=default_splitter,
    X=X_rel,
    y=y_rel,
    threshold_ndates = default_threshold_ndates,
    initial_nsplits = default_initial_nsplits,
)

so_rel.calculate_predictions(
    name=signal_name,
    models=default_models,
    metric=default_metric,
    hparam_grid=default_hparam_grid,
    test_size=default_test_size,
    min_cids=default_min_cids, 
    min_periods=default_min_periods,
    n_jobs=-1,
)
so_rel.models_heatmap(
    signal_name,
    cap=10,
    title=f"{secname} sector: model selection heatmap",
)

# Store signals
dfa = so_rel.get_optimized_signals()
dfx = msm.update_df(dfx, dfa)
https://macrosynergy.com/notebooks.build/data-science/statistical-learning-for-sectoral-equity-allocation/_images/ae8100b38ce53a5ef522a17999a3558c88ede08148b22817e956f50baede3640.png
xcatx = rel_dict["signal_name"]
secname = rel_dict["sector_name"]

so_rel.feature_selection_heatmap(
    xcatx,
    remove_blanks=True,
    title=f"{secname} sector: feature selection heatmap",
    ftrs_renamed=cat_label_dict,
    figsize=(12, 10),
)
https://macrosynergy.com/notebooks.build/data-science/statistical-learning-for-sectoral-equity-allocation/_images/378cccc43c7099891786f3d50dfc3f9953c8a79708b17aa235661a0141172f4b.png
xcatx = rel_dict["signal_name"]
secname = rel_dict["sector_name"]

so_rel.coefs_stackedbarplot(
    name=xcatx,
    ftrs_renamed=cat_label_dict,
    title=f"{secname} sector: annual averages of most important feature coefficients",
)
https://macrosynergy.com/notebooks.build/data-science/statistical-learning-for-sectoral-equity-allocation/_images/0cbbc5e039194a1ea35397211096518a50d79230968372d4f52ae2cc71a067de.png

Signal quality check #

xcatx = [rel_dict["signal_name"], rel_dict["ret"]]
cidx = rel_dict["cidx"]
secname = rel_dict["sector_name"]

cr_rel = msp.CategoryRelations(
    df=dfx,
    xcats=xcatx,
    cids=cidx,
    freq=rel_dict["freq"],
    blacklist=rel_dict["black"],
    lag=1,
    xcat_aggs=["last", "sum"],
    slip=1,
)

cr_rel.reg_scatter(
    title=f"{secname} sector: learning-based signal and subsequent returns",
    labels=False,
    prob_est="map",
    xlab=f"{secname} signal, end-of-month, based on concurrent best model",
    ylab=f"Relative return of {secname.lower()} sector (vol-targeted), next month, %",
    coef_box="upper left",
    size=(12, 8),
)
https://macrosynergy.com/notebooks.build/data-science/statistical-learning-for-sectoral-equity-allocation/_images/66f151b6aebcdfcfa6570e3ff6e6b22eb4b05c31dd75d59a28f53f3e69258077.png
xcatx = [rel_dict["signal_name"]]
cidx = rel_dict["cidx"]
secname = rel_dict["sector_name"]
signal_name = rel_dict["signal_name"]
pnl_name = rel_dict["pnl_name"]

pnl_rel = msn.NaivePnL(
    df=dfx,
    ret=rel_dict["ret"],
    sigs=xcatx,
    cids=cidx,
    start=default_start_date,
    blacklist = rel_dict["black"],
    bms=["USD_EQXR_NSA"],
)

for xcat in xcatx:
    pnl_rel.make_pnl(
        sig=xcat,
        sig_op="zn_score_pan",
        rebal_freq="monthly",
        neutral="zero",
        rebal_slip=1,
        vol_scale = None,
        thresh=3,
        pnl_name=pnl_name,
    )
pnl_rel.make_long_pnl(vol_scale=None, label=f"{secname} always long versus all-sector basket")

pnl_rel.plot_pnls(
    pnl_cats=pnl_rel.pnl_names, 
    title=f"{secname} sector: naive PnLs of positions versus all-sector basket",
    title_fontsize=14
)

rel_dict["pnls"] = pnl_rel
pnl_rel.evaluate_pnls(pnl_cats=pnl_rel.pnl_names)
https://macrosynergy.com/notebooks.build/data-science/statistical-learning-for-sectoral-equity-allocation/_images/bf402d92bb1df3dd6205cef5f7d6b3e5cdcbea72d80f7c4e49ebe08dd0044606.png
xcat Real estate always long versus all-sector basket Real estate learning-based signal
Return % 26.176755 25.443448
St. Dev. % 42.426817 43.198466
Sharpe Ratio 0.616986 0.58899
Sortino Ratio 0.880151 0.827925
Max 21-Day Draw % -107.121602 -149.320882
Max 6-Month Draw % -123.928869 -129.817585
Peak to Trough Draw % -228.650393 -210.989102
Top 5% Monthly PnL Share 0.855974 1.028567
USD_EQXR_NSA correl -0.052294 -0.038074
Traded Months 259 259
pnl_name = rel_dict["pnl_name"]
secname = rel_dict["sector_name"]

pnl_rel.signal_heatmap(
    pnl_name=pnl_name,
    figsize=(12, 3),
    title=f"{secname} sector: signal heatmap",
)
https://macrosynergy.com/notebooks.build/data-science/statistical-learning-for-sectoral-equity-allocation/_images/235f9a1b3595d387e35f54a4892bf09a03f22bafc6d2f4d6eefa842a2e027d19.png

Summary #

Sector-specific signals and returns #

sec_catregs = {
    "enr": cr_enr,
    "mat": cr_mat,
    "ind": cr_ind,
    "cod": cr_cod, 
    "cos": cr_cos,
    "hlc": cr_hlc,
    "fin": cr_fin,
    "ite": cr_ite, 
    "csr": cr_csr,
    "utl": cr_utl, 
    "rel": cr_rel,
}


msv.multiple_reg_scatter(
    cat_rels=list(sec_catregs.values()),
    ncol=3,
    nrow=4,
    figsize=(15, 15),
    title="Statistical macro signals and subsequent sectoral equity returns, 11 currency areas, since 2003",
    title_xadj=0.5,
    title_yadj=0.99,
    title_fontsize=20,
    xlab="Sector-specific statistical signal",
    ylab="Sector return versus equal weighted local index (all vol-targeted), next month %",
    coef_box="lower right",
    prob_est="map",
    single_chart=True,
    subplot_titles=[sector_labels[sector.upper()] for sector in sec_catregs.keys()],
)
https://macrosynergy.com/notebooks.build/data-science/statistical-learning-for-sectoral-equity-allocation/_images/bc092bad92a7555e9fc3a8fec63581df655e3f57f8cfb38d9f96afc619398aeb.png

Combined cross-sector trading PnL #

sec_pnls = {
    "enr": pnl_enr,
    "mat": pnl_mat,
    "ind": pnl_ind,
    "cod": pnl_cod, 
    "cos": pnl_cos,
    "hlc": pnl_hlc,
    "fin": pnl_fin,
    "ite": pnl_ite, 
    "csr": pnl_csr,
    "utl": pnl_utl, 
    "rel": pnl_rel,
}

ma_pnl = msn.MultiPnL()
for sec, pnl in sec_pnls.items():
    ma_pnl.add_pnl(pnl, pnl_xcats=[f"{sector_labels[sec.upper()]} learning-based signal"])
ma_pnl.plot_pnls(
    pnl_xcats=[
        f"{sector_labels[sec.upper()]} learning-based signal" for sec in sec_pnls.keys()
    ],
    title="Naive PnLs for relative sector strategies",
    xcat_labels=[sector_labels[sec.upper()] for sec in sec_pnls.keys()],
)
https://macrosynergy.com/notebooks.build/data-science/statistical-learning-for-sectoral-equity-allocation/_images/d597dd7069fe690114c0ec931a9d1adeda79c5e38b3d2e68350ca370dc58d0b1.png
cpname = "Simple average PnL of relative sector strategies based on statistical learning and macro signals"

macro_sector_pnl = ma_pnl.combine_pnls(
    pnl_xcats=[f"{sector_labels[sec.upper()]} learning-based signal" for sec in sec_pnls.keys()],
    composite_pnl_xcat=cpname,
    weights=None,
)
ma_pnl.plot_pnls(
    [cpname],
    title="Cumulative naive PnL value of cross-sectoral equity allocation",
)
https://macrosynergy.com/notebooks.build/data-science/statistical-learning-for-sectoral-equity-allocation/_images/56b096484028fa00706a9677fbe4779533d2b6cfe78250bd3c5d77a42f1830d9.png
tbr = ma_pnl.evaluate_pnls()
tbr = tbr.rename(columns={
    **{
        f"{sector_labels[sec.upper()]} learning-based signal/EQC{sec.upper()}R_VT10vALL": f"{sector_labels[sec.upper()]}" 
        for sec in sec_pnls.keys()
    }, 
    **{
        "Simple average PnL of relative sector strategies based on statistical learning and macro signals": "Simple average"
    }
})

selected_rows = ["Return %", "St. Dev. %", "Sharpe Ratio", "Sortino Ratio", "USD_EQXR_NSA correl"]
selected_columns = ["Simple average"] + [sector_labels[sec.upper()] for sec in sec_pnls.keys()] 
selected_pnl_stats = tbr.loc[selected_rows, selected_columns].T
display(selected_pnl_stats.style.format("{:.2f}"))
Return % St. Dev. % Sharpe Ratio Sortino Ratio USD_EQXR_NSA correl
Simple average 19.06 16.01 1.19 1.76 nan
Energy 45.24 69.43 0.65 0.97 -0.10
Materials 28.04 46.62 0.60 0.85 0.00
Industrials 8.13 29.80 0.27 0.39 0.02
Cons. discretionary 15.46 32.07 0.48 0.70 -0.03
Cons. staples 19.20 34.45 0.56 0.82 -0.03
Healthcare 6.57 40.98 0.16 0.23 0.05
Financials 1.35 44.05 0.03 0.04 -0.12
Information tech 8.04 37.79 0.21 0.30 -0.00
Communication services 10.04 31.48 0.32 0.45 0.01
Utilities 18.18 41.62 0.44 0.64 -0.04
Real estate 25.44 43.20 0.59 0.83 -0.04

Appendix #

Appendix 1 - Macro quantamental indicators description #

# Convert the dictionary to an HTML table with custom inline CSS
html_table = cat_labels.to_html(index=True, table_id="custom_table")

# Inject CSS to align text to the left and reduce font size
css = """
<style>
#custom_table th, #custom_table td {
    text-align: left;
    font-size: 12px; /* Adjust the font size as needed */
}
</style>
"""
# Display the styled HTML table
HTML(css + html_table)
Label Description Geography
Group Category
Business surveys CBCSCORE_SA_D3M3ML3_WG_ZN Construction confidence, q/q Construction business confidence score, seas. adjusted, change q/q weighted
CBCSCORE_SA_D3M3ML3_ZN Construction confidence, q/q Construction business confidence score, seas. adjusted, change q/q local
CBCSCORE_SA_WG_ZN Construction confidence Construction business confidence score, seas. adjusted weighted
CBCSCORE_SA_ZN Construction confidence Construction business confidence score, seas. adjusted local
MBCSCORE_SA_D3M3ML3_WG_ZN Manufacturing confidence, q/q Manufacturing business confidence score, seas. adj., change q/q weighted
MBCSCORE_SA_D3M3ML3_ZN Manufacturing confidence, q/q Manufacturing business confidence score, seas. adj., change q/q local
MBCSCORE_SA_WG_ZN Manufacturing confidence Manufacturing business confidence score, seasonally adjusted weighted
MBCSCORE_SA_ZN Manufacturing confidence Manufacturing business confidence score, seasonally adjusted local
SBCSCORE_SA_D3M3ML3_WG_ZN Service confidence, q/q Services business confidence score, seas. adjusted, change q/q weighted
SBCSCORE_SA_D3M3ML3_ZN Service confidence, q/q Services business confidence score, seas. adjusted, change q/q local
SBCSCORE_SA_WG_ZN Service confidence Services business confidence score, seasonally adjusted weighted
SBCSCORE_SA_ZN Service confidence Services business confidence score, seasonally adjusted local
Commodity inventories BASEXINVCSCORE_SA_ZN Excess crude inventory score Crude oil excess inventory z-score, seasonally adjusted global
BMLXINVCSCORE_SA_ZN Excess metal inventory score Base metal excess inventory z-score, seasonally adjusted global
REFIXINVCSCORE_SA_ZN Excess refined oil inventory score Refined oil product excess inventory z-score, seas. adjusted global
Debt CORPINTNETGDP_SA_D1Q1QL4_WG_ZN Corporate debt servicing, %oya Corporate net debt servicing-to-GDP ratio, seasonally-adjusted, %oya weighted
CORPINTNETGDP_SA_D1Q1QL4_ZN Corporate debt servicing, %oya Corporate net debt servicing-to-GDP ratio, seasonally-adjusted, %oya local
HHINTNETGDP_SA_D1M1ML12_WG_ZN Households debt servicing, %oya Households net debt servicing-to-GDP ratio, seasonally-adjusted, %oya weighted
HHINTNETGDP_SA_D1M1ML12_ZN Households debt servicing, %oya Households net debt servicing-to-GDP ratio, seasonally-adjusted, %oya local
XGGDGDPRATIOX10_NSA_ZN Excess projected gov. debt Government debt-to-GDP ratio proj. in 10 years, in excess of 100% local
Exports XEXPORTS_SA_P1M1ML12_3MMA_ZN Excess export growth Exports growth, %oya, 3mma, in excess of 5-year median GDP growth local
Inflation - broad XCPIC_SA_P1M1ML12_ZN Excess core CPI, %oya Core CPI, %oya, in excess of effective inflation target local
XCPIH_SA_P1M1ML12_ZN Excess headline CPI, %oya Headline CPI, %oya, in excess of effective inflation target local
XPPIH_NSA_P1M1ML12_ZN Excess PPI, %oya Producer price inflation, %oya, in excess of eff. inflation target local
Inflation - specific XCPIE_SA_P1M1ML12_WG_ZN Excess energy CPI, %oya Energy CPI, %oya, in excess of effective inflation target weighted
XCPIE_SA_P1M1ML12_ZN Excess energy CPI, %oya Energy CPI, %oya, in excess of effective inflation target local
XCPIF_SA_P1M1ML12_WG_ZN Excess food CPI, %oya Food CPI, %oya, in excess of effective inflation target weighted
XCPIF_SA_P1M1ML12_ZN Excess food CPI, %oya Food CPI, %oya, in excess of effective inflation target local
Labour market UNEMPLRATE_NSA_3MMA_D1M1ML12_WG_ZN Unemployment rate, diff oya Unemployment rate, change oya weighted
UNEMPLRATE_NSA_3MMA_D1M1ML12_ZN Unemployment rate, diff oya Unemployment rate, change oya local
UNEMPLRATE_SA_3MMAv5YMA_WG_ZN Unemployment rate, diff vs 5yma Unemployment rate, difference vs 5-year moving average weighted
UNEMPLRATE_SA_3MMAv5YMA_ZN Unemployment rate, diff vs 5yma Unemployment rate, difference vs 5-year moving average local
XEMPL_NSA_P1M1ML12_3MMA_WG_ZN Excess employment growth Employment growth, %oya, 3mma, in excess of population growth weighted
XEMPL_NSA_P1M1ML12_3MMA_ZN Excess employment growth Employment growth, %oya, 3mma, in excess of population growth local
XRWAGES_NSA_P1M1ML12_ZN Excess real wage growth Real wage growth, %oya, in excess of medium-term productivity growth local
Market metrics BMLCOCRY_SAVT10_21DMA_ZN Base metals carry Nominal carry for base metals basket, seasonally and vol-adjusted, 21 days moving average global
COXR_VT10vWTI_21DMA_ZN Refined vs crude oil returns Refined oil products vs crude oil vol-targeted return differential, 21 days moving average global
RIR_NSA_ZN Real 1-month rate Real 1-month interest rate local
RSLOPEMIDDLE_NSA_ZN Real 5y-2y yield Real IRS yield differentials, 5-years versus 2-years local
RYLDIRS02Y_NSA_ZN Real 2-year yield Real 2-year IRS yield local
RYLDIRS05Y_NSA_ZN Real 5-year yield Real 5-year IRS yield local
Output growth XCSTR_SA_P1M1ML12_3MMA_WG_ZN Excess construction growth Construction output, %oya, 3mma, in excess of 5-y median GDP growth weighted
XCSTR_SA_P1M1ML12_3MMA_ZN Excess construction growth Construction output, %oya, 3mma, in excess of 5-y median GDP growth local
XIP_SA_P1M1ML12_3MMA_WG_ZN Excess industry growth Industrial output, %oya, 3mma, in excess of 5-y median GDP growth weighted
XIP_SA_P1M1ML12_3MMA_ZN Excess industry growth Industrial output, %oya, 3mma, in excess of 5-y median GDP growth local
XRGDPTECH_SA_P1M1ML12_3MMA_WG_ZN Excess GDP growth Real GDP, %oya, 3mma, using HF data, in excess of 5-y med. GDP growth weighted
XRGDPTECH_SA_P1M1ML12_3MMA_ZN Excess GDP growth Real GDP, %oya, 3mma, using HF data, in excess of 5-y med. GDP growth local
Private consumption CCSCORE_SA_D3M3ML3_WG_ZN Consumer confidence, q/q Consumer confidence score, seasonally adjusted, change q/q weighted
CCSCORE_SA_D3M3ML3_ZN Consumer confidence, q/q Consumer confidence score, seasonally adjusted, change q/q local
CCSCORE_SA_WG_ZN Consumer confidence Consumer confidence score, seasonally adjusted weighted
CCSCORE_SA_ZN Consumer confidence Consumer confidence score, seasonally adjusted local
XNRSALES_SA_P1M1ML12_3MMA_WG_ZN Excess retail sales growth Nominal retail sales, %oya, 3mma, in excess of 5-y median GDP growth weighted
XNRSALES_SA_P1M1ML12_3MMA_ZN Excess retail sales growth Nominal retail sales, %oya, 3mma, in excess of 5-y median GDP growth local
XRPCONS_SA_P1M1ML12_3MMA_WG_ZN Excess consumption growth Real private consumption, %oya, 3mma, in excess of 5-y median GDP growth weighted
XRPCONS_SA_P1M1ML12_3MMA_ZN Excess real consum growth Real private consumption, %oya, 3mma, in excess of 5-y median GDP growth local
XRRSALES_SA_P1M1ML12_3MMA_WG_ZN Excess real retail growth Real retail sales, %oya, 3mma, in excess of 5-y median GDP growth weighted
XRRSALES_SA_P1M1ML12_3MMA_ZN Excess real retail growth Real retail sales, %oya, 3mma, in excess of 5-y median GDP growth local
Private credit INTLIQGDP_NSA_D1M1ML1_ZN Intervention liquidity, diff m/m Intervention liquidity to GDP ratio, change over the last month local
INTLIQGDP_NSA_D1M1ML6_ZN Intervention liquidity, diff 6m Intervention liquidity to GDP ratio, change overlast 6 months local
XPCREDITBN_SJA_P1M1ML12_WG_ZN Excess credit growth Private credit, %oya, 3mma, in excess of 5-y median GDP growth weighted
XPCREDITBN_SJA_P1M1ML12_ZN Excess credit growth Private credit, %oya, 3mma, in excess of 5-y median GDP growth local
Real appreciation CMPI_NSA_P1M12ML1_ZN Import prices, %oya Commodity-based import price index, %oya local
CTOT_NSA_P1M12ML1_ZN Terms-of-trade, %oya Commodity-based terms-of-trade, %oya local
CXPI_NSA_P1M12ML1_ZN Export prices, %oya Commodity-based export price index, %oya local
REEROADJ_NSA_P1M12ML1_ZN Open-adj REER, %oya Openness-adjusted real effective exchange rate, %oya local