Municipal Grant Distribution Anomaly Detection Script Generator
Generates a complete Python audit script to flag geographic and allocation discrepancies in public grant disbursement data.
Use this template when municipal analysts need to verify whether local grant allocations align with statutory equity benchmarks and identify funding anomalies. It provides an automated, reproducible data auditing script with standardized reporting.
Role: Senior Public Sector Analytics Engineer specializing in municipal finance and compliance auditing.
Context
- Grant Initiative: {{grant_program_name}}
- Jurisdiction: {{municipality_name}}
- Statutory Disadvantaged Community Share Target: {{target_disadvantaged_communities_ratio}}
- Fiscal Evaluation Period: {{fiscal_year}}
- Input Data Schema: {{raw_disbursement_dataset}}
- Anomaly Deviation Margin: {{audit_threshold_variance}}
Task
Author a modular Python analytics script utilizing pandas and scipy to ingest municipal disbursement records, calculate geographic allocation distributions, test for statistically significant funding discrepancies, and output clean tabular summaries alongside flagged audit exceptions.
Method
- Ingest the structured tabular records defined in {{raw_disbursement_dataset}} and parse grant amounts, timestamp stamps, and census-tract or ward identifiers.
- Standardize column data types, imputing missing categorical identifiers with an explicit audit-flag value.
- Aggregate total awarded funding by tract designation and calculate the realized proportion allocated to prioritized zones relative to {{target_disadvantaged_communities_ratio}}.
- Compute allocation z-scores and variance metrics across regional tranches to detect distribution skew beyond {{audit_threshold_variance}}.
- Implement a robust filtering rule that isolates individual disbursements displaying outlier amounts or misaligned category classifications.
- Generate summary tables calculating aggregate spend, target variance percentage, and total variance counts.
- Format diagnostic logging and export cleaned audit outputs to both standard output and a structured CSV payload.
Constraints
- MUST write clean, PEP-8 compliant Python 3 code with complete type hints and docstrings for all functions.
- MUST NOT include mock network requests or placeholder credentials in the executable logic.
- MUST handle zero-division errors when baseline tract populations or allocations are zero.
- Execution dependencies must be strictly restricted to pandas, numpy, and scipy.
- Output code must be self-contained and ready for immediate execution.
Output format
Provide the deliverable in three discrete sections:
- Execution Prerequisites: Environment requirements and list of input file columns.
- Complete Python Script: The fully functional Python script block.
- Output Data Dictionary: A table detailing all generated audit output columns and anomaly trigger criteria.
Self-review
- Check that every variable from {{grant_program_name}} to {{audit_threshold_variance}} is actively utilized in logic or parameters.
- Verify that statistical anomaly calculations correctly handle small-sample edge cases.
- Confirm that no unhandled exceptions can arise from missing or null categorical values.
Explicit role, a named task, and discrete steps the model can follow.
Background, inputs and variables the model needs before it starts.
Hard boundaries — what the model must and must not do.
A named, field-level shape for the response.
Ordered work items that force analysis before an answer.
Length and structure that travel across frontier models.
Signal density — instruction weight without padding.
Documented variables so the scaffold adapts to new inputs.
Quality bar, assumptions and behaviour when inputs are thin.
How much real usage the template has behind it.