assayinganomalies — a function-by-function tutorial¶
A detailed walkthrough of the package: how the research database is built from CRSP and Compustat, how the analytics work, and what the anomaly-testing protocol of Novy-Marx and Velikov (2026) computes at each step.
Audience: a finance PhD student or researcher who wants to use the package and know exactly what it computes. The finance is assumed; the Python is explained. For installation and a quickstart, see Get started; Chapter 2 is the end-to-end tour.
The package began as a port of a MATLAB toolkit. The chapters describe the Python package on its own; if you know the MATLAB toolkit, the appendix maps its functions to this package's and lists the differences that matter.
20 chapters and an appendix (22 files including 09b), 78,385 words.
Terminology (canonical)¶
These two choices are used throughout the chapters:
| Term | Meaning | Do not use |
|---|---|---|
| panel | Long (date, permno) DataFrame (DuckDB ccm_panel and friends). Contrast with a wide / matrix layout (nMonths × nStocks). |
"long table" |
| tcosts | Stock-level and factor-level trading costs: module transforms/tcosts.py, table tcosts, column tcost, config tcosts_type. |
Switching freely with "transaction costs" in body text |
Chapter 9's title still says "Transaction costs" as the English name of that chapter; body text uses tcosts. The storage translation lives in Ch 3.
Table of contents¶
Part I — Orientation¶
| Ch | Chapter | Words |
|---|---|---|
| 1 | Architecture, install, config, CLI | 4,577 |
| 2 | End-to-end tour: build, load, assay | 3,063 |
Part II — The data layer¶
| Ch | Chapter | Words |
|---|---|---|
| 3 | The DuckDB storage layer | 2,939 |
| 4 | Pipeline orchestration and scoped rebuilds | 4,660 |
| 5 | Ingest: WRDS, CRSP, COMPUSTAT, FF, Chen-Zimmerman | 6,224 |
| 6 | CRSP monthly transforms | 2,970 |
| 7 | COMPUSTAT derived variables and the CCM merge | 2,850 |
| 8 | CRSP daily transforms | 3,416 |
| 9 | Transaction costs + FF factor tcosts (09b) | 2,246 + 1,248 |
| 10 | Betas | 3,331 |
| 11 | The 23 Novy-Marx/Velikov anomalies | 2,659 |
Part III — Analytics¶
| Ch | Chapter | Words |
|---|---|---|
| 12 | Data access and signal I/O | 2,222 |
| 13 | Portfolio sorts and factor regressions | 3,009 |
| 14 | Fama-MacBeth and the OLS utilities | 4,643 |
| 15 | Portfolio costs, generalized alpha, anomaly decay | 3,931 |
Part IV — The assay protocol¶
| Ch | Chapter | Words |
|---|---|---|
| 16 | The assay protocol orchestrator | 3,738 |
| 17 | Results builders I: basic sorts, anomaly zoo, FMB | 4,962 |
| 18 | Results builders II: close anomalies, dendrogram, combination strategies, ML | 5,377 |
| 19 | Reporting: PrintResults, LaTeX, figures |
6,813 |
Part V — Wrap-up¶
| Ch | Chapter | Words |
|---|---|---|
| 20 | Validation and reproducibility | 1,828 |
| — | Appendix: coming from the MATLAB toolkit | 1,679 |
Coverage: the chapters account for every
top-level name in the package modules, except three added or split out after
the tutorial was written: session_cache.py, plotting.py and
logging_utils.py. Package __init__.py barrels only re-export; those names
are documented in the owning module's chapter.
Function index¶
Every documented function, class, and named helper (a ## / ### heading
that starts with a backticked identifier), mapped to its chapter and heading
anchor. Copies that exist in more than one module (for example _matlab_prctile)
list every chapter that has a subsection. Grouped one-liners that never got
their own heading are not listed here.
| Name | Chapter |
|---|---|
_add_crsp_derived_signals |
Ch 5 |
_apply_exchange_exclusions |
Ch 9 |
_apply_matlab_delisting_adjustment |
Ch 6 |
_as_regressor |
Ch 14 |
_assign_bins |
Ch 13 |
_assign_ff_buckets |
Ch 6; Ch 6 |
_assign_to_ptf |
Ch 9b |
_batched_ols_beta_rbar |
Ch 14 |
_batched_wls_beta_rbar |
Ch 14 |
_bin_by_cross_section |
Ch 13 |
_calendar_lag_panel |
Ch 14 |
_calendar_lag_positions |
Ch 13 |
_check_response |
Ch 5 |
_coerce_anoms_to_dict |
Ch 17 |
_coerce_series |
Ch 16 |
_compute_ff49_industry_returns |
Ch 11 |
_conditional_bin_by_cross_section |
Ch 13 |
_configure_setup_logging |
Ch 4 |
_cross_rank |
Ch 11 |
_cross_section_fits |
Ch 14 |
_cs_spread |
Ch 9 |
_cumret_window |
Ch 6 |
_cumret_window_global_months |
Ch 6 |
_cz_file_for_duckdb |
Ch 5 |
_cz_read_sql |
Ch 5 |
_date_yyyymm |
Ch 6 |
_dedupe_names |
Ch 14; Ch 14 |
_df_brief |
Ch 4 |
_drop_highly_correlated |
Ch 17 |
_expand_gibbs_to_monthly |
Ch 9 |
_expand_plan |
Ch 4 |
_factor_details |
Ch 13 |
_ff_tcosts_or_zeros |
Ch 9b |
_filter_labels |
Ch 16 |
_find_input_file |
Ch 4 |
_fmt_bytes |
Ch 4 |
_get_breakpoints |
Ch 13 |
_grs_test_like_matlab |
Ch 13 |
_infer_time_period |
Ch 16 |
_insert_matlab_dlret_rows |
Ch 10 |
_is_valid_zip |
Ch 5 |
_load_cz_zoo |
Ch 16 |
_load_daily_delist |
Ch 4 |
_load_library_panel |
Ch 16 |
_log_stage |
Ch 16 |
_lstsq_full_rank |
Ch 14 |
_make_biv_sort_ind |
Ch 9b |
_make_univ_sort_ind |
Ch 9b |
_matlab_linkage_to_scipy |
Ch 19 |
_matlab_mean_tstat |
Ch 13 |
_matlab_prctile |
Ch 11; Ch 13 |
_matlab_prctile_rows |
Ch 9; Ch 9b |
_matlab_prctile_rows_finite |
Ch 14; Ch 14 |
_matlab_std |
Ch 8; Ch 8 |
_matlab_style_sort_outputs |
Ch 13 |
_maybe_fill_low_frequency |
Ch 14 |
_merge_on_interval |
Ch 6 |
_month_end |
Ch 6 |
_month_end_expr |
Ch 3 |
_month_index |
Ch 7 |
_month_key |
Ch 11 |
_nan_eret_like |
Ch 18 |
_month_rows |
Ch 9b |
_nanmatsum |
Ch 9b |
_nanols |
Ch 10 |
_nanols_resid_rms |
Ch 8 |
_nanols_span |
Ch 18 |
_nanols_univariate_beta |
Ch 18 |
_nansum |
Ch 7 |
_normalize_daily |
Ch 8 |
_nw_se_of_mean |
Ch 14 |
_ols |
Ch 13 |
_ols_beta_rbar |
Ch 14 |
_pad_cond_size_panel_a |
Ch 19 |
_panel_correls |
Ch 17 |
_panel_to_date_stock |
Ch 14 |
_parse_form_csv |
Ch 12 |
_plot_bivariate_results_matlab_like |
Ch 13 |
_print_bivariate_results_matlab_like |
Ch 13 |
_print_prt_sort_results_like_matlab |
Ch 13 |
_rbar_from_packed |
Ch 14 |
_regressor_frame |
Ch 14; Ch 14 |
_resolve_factor_model |
Ch 13 |
_resolve_test_signal |
Ch 16 |
_ret_col |
Ch 8 |
_row_lag |
Ch 9b |
_run_factor_regs |
Ch 13 |
_safe_col |
Ch 11 |
_scale_to_unit |
Ch 11 |
_signal_distress |
Ch 11 |
_signal_high_freq_combo |
Ch 11 |
_signal_industry_momentum |
Ch 11 |
_signal_industry_relative_reversal |
Ch 11 |
_signal_irr_low_vol |
Ch 11 |
_signal_pead_sue |
Ch 11 |
_signal_piotroski |
Ch 11 |
_signal_seasonality |
Ch 11 |
_signal_val_mom_prof |
Ch 11 |
_slice_panel_by_time |
Ch 17 |
_slice_time_period |
Ch 17 |
_stage_validate_bm_hml |
Ch 4 |
_take_lagged |
Ch 13 |
_tiedrank |
Ch 18; Ch 18 |
_to_month_end |
Ch 5; Ch 18 |
_to_wide |
Ch 15 |
_two_leg_sort |
Ch 15 |
_validate_columns |
Ch 5 |
_wide |
Ch 9b |
_winsor_or_trim_by_date |
Ch 14 |
_winsorize |
Ch 9 |
_winsorize_xs_1pct |
Ch 17 |
_wls_beta_rbar |
Ch 14 |
_ym_int_to_period |
Ch 8 |
add_comma |
Ch 19 |
adjust_nasdaq_volume |
Ch 8; Ch 8 |
aggregate_betas_to_monthly |
Ch 10 |
aggregate_daily_to_monthly |
Ch 8 |
align_annual_to_monthly |
Ch 7 |
AnomBenchmarkResults |
Ch 17 |
AnomStratResults |
Ch 17 |
AssayClient |
Ch 1 |
AssayConfig |
Ch 1 |
BasicSortsResults |
Ch 17 |
BivariateSortResult |
Ch 13 |
build_basic_sort_panels |
Ch 19 |
build_june_annual_panel |
Ch 7 |
build_placeholder_panel |
Ch 7 |
build_quarterly_monthly_panel |
Ch 7 |
build_select_statement |
Ch 3 |
cache_path |
Ch 3 |
calc_gen_alpha |
Ch 15 |
calc_mve |
Ch 15 |
calc_net_mve |
Ch 15 |
calc_ptf_rets |
Ch 13 |
calc_tcosts |
Ch 15; Ch 15 |
check_fill_anomalies |
Ch 11 |
CloseAnomResult |
Ch 18 |
CloseAnomResults |
Ch 18 |
coerce_nullable_dtypes |
Ch 8 |
combine_lf_spreads |
Ch 9 |
CombStratResult |
Ch 18 |
compute_abdi_ranaldi |
Ch 9 |
compute_amihud |
Ch 8 |
compute_betas |
Ch 10 |
compute_book_equity |
Ch 7 |
compute_car3 |
Ch 8 |
compute_cfo |
Ch 7 |
compute_corwin_schultz |
Ch 9 |
compute_daily_delisting_adj |
Ch 8 |
compute_derived_variables |
Ch 7 |
compute_dividends |
Ch 7 |
compute_droe |
Ch 7 |
compute_fp_beta |
Ch 10 |
compute_free_cash_flow |
Ch 7 |
compute_idiovol |
Ch 8 |
compute_kyle_obizhaeva |
Ch 9 |
compute_maxret |
Ch 8 |
compute_monthly_rvol |
Ch 8 |
compute_nmv_anomalies |
Ch 11 |
compute_ols_betas |
Ch 10 |
compute_operating_costs |
Ch 7 |
compute_quarterly_book_equity |
Ch 7 |
compute_quarterly_derived |
Ch 7 |
compute_quarterly_report_month |
Ch 7 |
compute_quarterly_roa |
Ch 7 |
compute_realized_volatility |
Ch 8 |
compute_sue |
Ch 7 |
compute_sue2 |
Ch 7 |
compute_working_capital |
Ch 7 |
connect |
Ch 5 |
CZ_RELEASES |
Ch 5 |
CZReleaseChoice |
Ch 5 |
database_is_demo |
Ch 20 |
database_manifest |
Ch 20 |
dates_to_pdates |
Ch 19 |
default_mpe_signal_info |
Ch 12 |
DendrogramResults |
Ch 18 |
describe_cz_signal |
Ch 12 |
downstream_of |
Ch 4 |
DuckDBManager |
Ch 3 |
emit_all_figures |
Ch 19 |
emit_alpha_prctl_figure |
Ch 19 |
emit_comb_strats_figure |
Ch 19 |
emit_cond_strats_figure |
Ch 19 |
emit_correl_figure |
Ch 19 |
emit_dendrogram_figure |
Ch 19 |
emit_ibbots_figure |
Ch 19 |
emit_sharpe_dist_figure |
Ch 19 |
emit_signal_description_figure |
Ch 19 |
ensure_config_dir |
Ch 1 |
ensure_credentials |
Ch 5 |
est_factor_regs |
Ch 13 |
factor_matrix_for_gen_alpha |
Ch 15 |
FACTOR_MODEL_COLS |
Ch 13 |
fetch_compustat_annual |
Ch 5 |
fetch_crsp_aux_table |
Ch 5 |
fetch_crsp_daily |
Ch 5 |
fetch_crsp_monthly |
Ch 5 |
fetch_cz_anomalies |
Ch 5 |
fetch_cz_signal_info |
Ch 5 |
fetch_ff_daily |
Ch 5 |
fetch_ff_historical_be |
Ch 7 |
fetch_ff_monthly |
Ch 5 |
FF10_RANGES |
Ch 6 |
FF25_URL |
Ch 5 |
FF_TC_COLS |
Ch 9b |
ff_tc_columns |
Ch 9b |
ff_tcosts |
Ch 9b |
ff_tcosts_from_matrices |
Ch 9b |
FieldCatalog |
Ch 3 |
fill_missing_tcosts |
Ch 9 |
fill_var |
Ch 11 |
FMBRes |
Ch 17 |
GEN_ALPHA_FACTOR_COLS |
Ch 15 |
generate_placeholder_annual |
Ch 5 |
generate_placeholder_cz_anomalies |
Ch 5 |
generate_placeholder_daily |
Ch 5 |
generate_placeholder_monthly |
Ch 5 |
get_anomaly_signals |
Ch 12 |
get_cz_signal_info |
Ch 12 |
get_signal_info |
Ch 12 |
ibbots |
Ch 19 |
ibbots_cumret |
Ch 19 |
invprctile |
Ch 19 |
IPCARes |
Ch 18 |
label_title |
Ch 20 |
LassoRes |
Ch 18 |
link_compustat_to_crsp |
Ch 7 |
list_cz_signals |
Ch 12 |
load |
Ch 12 |
load_config |
Ch 1 |
load_cpi_aucns |
Ch 9 |
load_cz_anomalies |
Ch 12 |
load_factors |
Ch 13 |
load_field_catalog |
Ch 3 |
load_fields |
Ch 1 |
load_gibbs |
Ch 9 |
load_hf_spreads |
Ch 9 |
load_nmv_anomalies |
Ch 12 |
load_signal_csv |
Ch 12 |
log |
Ch 14 |
main |
Ch 1 |
make_anom_benchmark_results |
Ch 17; Ch 17 |
make_anom_strat_results |
Ch 17; Ch 17 |
make_avg_rank |
Ch 18 |
make_basic_sorts_results |
Ch 17; Ch 17 |
make_close_anom_results |
Ch 18 |
make_comb_strats |
Ch 18 |
make_dendrogram_results |
Ch 18 |
make_ff_tcosts |
Ch 9b; Ch 15 |
make_ff_tcosts_table |
Ch 9b |
make_fmb_res |
Ch 17 |
make_ipca_res |
Ch 18 |
make_lasso_res |
Ch 18 |
make_pls_res |
Ch 18 |
make_res_to_print |
Ch 19 |
make_trading_costs |
Ch 9 |
make_weighted_avg_rank |
Ch 18 |
mark_database_demo |
Ch 20 |
mat_to_tex |
Ch 19 |
merge_compustat_crsp |
Ch 7 |
migrate_ts_data_monthly_dates_to_month_end |
Ch 3 |
month_end_to_yyyymm |
Ch 12 |
moving |
Ch 19 |
MPE_DEFAULT_SIGNAL_INFO |
Ch 12 |
nanols |
Ch 14 |
num_to_words |
Ch 19 |
OlsRes |
Ch 14 |
PipelineContext |
Ch 4 |
PipelineStage |
Ch 4 |
plot_anomaly_decay |
Ch 15 |
plot_name_histogram |
Ch 19 |
PLSRes |
Ch 18 |
print_basic_sort_table |
Ch 19 |
print_bibliography_file |
Ch 19 |
print_close_anoms_fmb_table |
Ch 19 |
print_close_anoms_span_table |
Ch 19 |
print_cond_size_sort_table |
Ch 19 |
print_robustness_to_sorting_table |
Ch 19 |
print_tex_file |
Ch 19 |
print_title_page |
Ch 19 |
PrintResults |
Ch 19 |
PrintText |
Ch 19 |
protocol_manifest |
Ch 20 |
ProtocolResult |
Ch 16 |
provenance_payload |
Ch 20 |
prt |
Ch 14 |
read_manifest |
Ch 20 |
rebuild |
Ch 4 |
RegressionResult |
Ch 14 |
require_real_database |
Ch 20 |
resolve_cz_signals_source |
Ch 5 |
resolve_project_dir |
Ch 1 |
run_anomaly_decay |
Ch 15 |
run_bivariate_sort |
Ch 13 |
run_fama_macbeth |
Ch 14 |
run_test_signal |
Ch 16 |
run_univariate_sort |
Ch 13 |
RVOL_WINDOWS_MONTHS |
Ch 8 |
save_config |
Ch 1 |
semantic_tex_diff |
Ch 19 |
set_params |
Ch 1 |
set_params_cli |
Ch 1 |
set_project_dir |
Ch 1 |
setup_database |
Ch 4; Ch 4 |
SignalInfo |
Ch 12 |
SignalTester |
Ch 16 |
SortResult |
Ch 13; Ch 13 |
stage_writes |
Ch 4 |
StageResult |
Ch 4 |
stages_for_columns |
Ch 4 |
stages_in_order |
Ch 4 |
strip_date_today |
Ch 19 |
test_signal |
Ch 16 |
transform_daily |
Ch 8 |
transform_monthly |
Ch 6 |
verify_manifest |
Ch 20 |
WRDSClient |
Ch 5 |
write_cz_anomalies_table |
Ch 5 |
write_manifest |
Ch 20 |
write_table |
Ch 3 |
yyyymm_to_month_end |
Ch 12 |
yyyymm_to_pdate |
Ch 19 |
Related documents¶
| Document | Use |
|---|---|
| ../ccm_panel_schema.md | Derived monthly panel column dictionary |
| ../analytics_api.md | Public analytics API reference |
| ../quickstart.md | The quickstart notebook as a page |
| ../trading_cost_inputs.md | tcosts_type choices, the Gibbs file, building hf_monthly.csv |