Add CRSP monthly characteristics pipeline - #4
Merged
Conversation
There was a problem hiding this comment.
Pull request overview
This PR introduces a new chars_ciz_monthly pipeline to compute, merge, and publish monthly CRSP/Compustat/IBES-based characteristics (including rolling-window market microstructure measures and quarterly-sourced “satellite” anomalies), with a companion WRDS download script targeting the crspm schema.
Changes:
- Adds monthly satellite characteristic computations (e.g., SUE, ABR, RE) and a Polars-based rolling window engine for daily→monthly rolling characteristics.
- Adds a merge + backfill stage to combine accounting characteristics with satellites and CRSP monthly returns/ME.
- Adds an impute + rank + output stage that preserves
size_grpmetadata in ranked outputs.
Reviewed changes
Copilot reviewed 10 out of 10 changed files in this pull request and generated 7 comments.
Show a summary per file
| File | Description |
|---|---|
| chars_ciz_monthly/sue.py | Computes quarterly SUE and populates it forward to monthly dates. |
| chars_ciz_monthly/rolling_chars.py | Computes rolling daily-window characteristics and maps them to monthly windows. |
| chars_ciz_monthly/myre.py | Computes IBES-based monthly forecast revisions (RE) using ICLINK + CRSP. |
| chars_ciz_monthly/merge_chars.py | Merges accounting + satellites + rolling chars and backfills CRSP return/ME fields. |
| chars_ciz_monthly/impute_rank_output.py | Reconciles annual/quarterly, shifts returns, imputes, ranks, and writes final outputs. |
| chars_ciz_monthly/iclink_ciz.sas | Includes SAS macro source for generating the IBES–CRSP link table. |
| chars_ciz_monthly/functions.py | Provides shared helpers (ffi codes, imputation, standardization, etc.). |
| chars_ciz_monthly/download_data.py | Downloads required WRDS tables (Compustat/CRSP/IBES/CCM) into local parquet. |
| chars_ciz_monthly/accounting.py | Computes accounting and momentum characteristics and produces monthly-aligned accounting outputs. |
| chars_ciz_monthly/abr.py | Computes ABR around earnings announcements and populates to monthly. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Comment on lines
+87
to
+97
| def std_with_zero_handling(cols, min_valid=6): | ||
| """Row-wise std of YoY EPS differences; returns None if < min_valid non-null, 0 if all equal.""" | ||
| concat_expr = pl.concat_list(cols) | ||
| valid_count = concat_expr.list.eval(pl.element().is_not_null().cast(pl.Int32).sum()).list.first() | ||
| all_equal = concat_expr.list.max() == concat_expr.list.min() | ||
| std_val = concat_expr.list.eval(pl.element().std()).list.first() | ||
| return ( | ||
| pl.when(valid_count < min_valid).then(pl.lit(None, dtype=pl.Float64)) | ||
| .when(all_equal).then(pl.lit(0.0)) | ||
| .otherwise(std_val) | ||
| ) |
Comment on lines
+172
to
+179
| df.group_by(["permno", "group_number"]) | ||
| .agg([ | ||
| ((col("vol") * col("prc").abs()).log()).std().alias("std_dolvol"), | ||
| pl.len().alias("n_obs"), | ||
| ]) | ||
| .filter(col("n_obs") >= min_obs) | ||
| .drop("n_obs") | ||
| ) |
Comment on lines
+210
to
+215
| .with_columns([ | ||
| ( | ||
| (col("zero_count") + (1.0 / col("turn_sum")) / 11000) | ||
| * 63.0 / col("n_obs") | ||
| ).alias("zerotrade") | ||
| ]) |
Comment on lines
+226
to
+230
| df.group_by(["permno", "group_number"]) | ||
| .agg([ | ||
| (col("ret").abs() / (col("prc").abs() * col("vol"))).mean().alias("ill"), | ||
| pl.len().alias("n_obs"), | ||
| ]) |
Comment on lines
+278
to
+280
| /* ********************************************************************************* */ | ||
| /* ************* Material Copyright Wharton Research Data Services *************** */ | ||
| /* ****************************** All Rights Reserved ****************************** */ |
Comment on lines
+147
to
+148
| # Keep first (most recent datadate) for each permno-date | ||
| df = df.unique(subset=["permno", "date"], keep="first") |
Comment on lines
+152
to
+156
| def _backfill_crsp(chars, crsp_fill): | ||
| """Fill missing ret/retx/retadj/me from CRSP backfill table.""" | ||
| chars = chars.join(crsp_fill, on=['permno', 'jdate'], how='left') | ||
| for col_name in ['ret', 'retx', 'me']: | ||
| fill_col = f'{col_name}_fill' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
chars_ciz_monthlypipelinecrspmschemasize_grpas metadata in ranked outputsValidation
crspm.msf_v2,crspm.dsf_v2, andcrspm.inddlyseriesdatasize_grpwithout creatingrank_size_grp