Spaces:
Configuration error
Configuration error
File size: 5,220 Bytes
f898792 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 | """
Typed Pydantic models for the Financial Statement Anomaly Detection environment.
Defines Observation, Action, Reward, and State schemas used across the OpenEnv API.
"""
from __future__ import annotations
from enum import Enum
from typing import Any, Dict, List, Optional
from pydantic import BaseModel, Field
# ---------------------------------------------------------------------------
# Enums
# ---------------------------------------------------------------------------
class AnomalyType(str, Enum):
"""All anomaly categories the agent can flag."""
ARITHMETIC_ERROR = "arithmetic_error"
DUPLICATE_ENTRY = "duplicate_entry"
NEGATIVE_VALUE = "negative_value"
IMPOSSIBLE_CHANGE = "impossible_change"
PERCENTAGE_ERROR = "percentage_error"
RECEIVABLES_DIVERGENCE = "receivables_divergence"
INVENTORY_TURNOVER = "inventory_turnover"
CASHFLOW_MISMATCH = "cashflow_mismatch"
MARGIN_SHIFT = "margin_shift"
DSO_SPIKE = "dso_spike"
CHANNEL_STUFFING = "channel_stuffing"
COOKIE_JAR = "cookie_jar"
ROUND_TRIPPING = "round_tripping"
EARLY_REVENUE = "early_revenue"
BENFORDS_LAW = "benfords_law"
class ActionType(str, Enum):
"""Possible agent actions per step."""
FLAG_ANOMALY = "flag_anomaly"
REQUEST_DETAIL = "request_detail"
SUBMIT_REPORT = "submit_report"
class Industry(str, Enum):
TECHNOLOGY = "technology"
MANUFACTURING = "manufacturing"
RETAIL = "retail"
HEALTHCARE = "healthcare"
FINANCIAL_SERVICES = "financial_services"
class CompanySize(str, Enum):
SMALL = "small" # $10M-50M revenue
MEDIUM = "medium" # $50M-500M
LARGE = "large" # $500M+
# ---------------------------------------------------------------------------
# Sub-models
# ---------------------------------------------------------------------------
class CompanyMetadata(BaseModel):
"""Static company information for an episode."""
name: str
industry: Industry
size: CompanySize
num_quarters: int
fiscal_year_start: int = Field(description="Starting fiscal year, e.g. 2022")
class QuarterData(BaseModel):
"""Financial data for a single quarter. All amounts in integer cents."""
quarter_label: str = Field(description="e.g. 'Q1 2023'")
# Balance sheet
cash: int = 0
receivables: int = 0
inventory: int = 0
other_current_assets: int = 0
total_current_assets: int = 0
fixed_assets: int = 0
total_assets: int = 0
payables: int = 0
short_term_debt: int = 0
total_current_liabilities: int = 0
long_term_debt: int = 0
total_liabilities: int = 0
equity: int = 0
# Income statement
revenue: int = 0
cogs: int = 0
gross_profit: int = 0
operating_expenses: int = 0
operating_income: int = 0
interest_expense: int = 0
tax_expense: int = 0
net_income: int = 0
# Cash flow statement
cf_operating: int = 0
cf_investing: int = 0
cf_financing: int = 0
net_cash_change: int = 0
class AnomalyFlag(BaseModel):
"""An anomaly flagged by the agent."""
line_item: str
quarter: str
severity: int = Field(ge=1, le=5)
anomaly_type: AnomalyType
explanation: str = ""
class GroundTruthAnomaly(BaseModel):
"""A known injected anomaly (used internally by the grader)."""
line_item: str
quarter: str
severity: int = Field(ge=1, le=5)
anomaly_type: AnomalyType
description: str = ""
depends_on: Optional[str] = Field(
default=None,
description="ID of parent anomaly this one depends on (for ordering bonus)",
)
anomaly_id: str = ""
# ---------------------------------------------------------------------------
# Top-level OpenEnv models
# ---------------------------------------------------------------------------
class Observation(BaseModel):
"""What the agent sees each step."""
company: CompanyMetadata
quarters: List[QuarterData]
footnotes: List[str] = Field(default_factory=list)
prior_flags: List[AnomalyFlag] = Field(default_factory=list)
detail_response: Optional[Dict[str, Any]] = Field(
default=None,
description="Sub-ledger detail returned after a request_detail action",
)
step_number: int = 0
max_steps: int = 20
class Action(BaseModel):
"""What the agent does each step."""
action_type: ActionType
# For flag_anomaly
flag: Optional[AnomalyFlag] = None
# For request_detail
detail_line_item: Optional[str] = None
detail_quarter: Optional[str] = None
class Reward(BaseModel):
"""Reward signal returned after each step."""
step_reward: float = 0.0
cumulative_reward: float = 0.0
breakdown: Dict[str, float] = Field(default_factory=dict)
class Info(BaseModel):
"""Extra info dict returned alongside reward."""
message: str = ""
flags_so_far: int = 0
steps_remaining: int = 0
done_reason: Optional[str] = None
class State(BaseModel):
"""Full internal state returned by state()."""
step_number: int = 0
max_steps: int = 20
flags_submitted: List[AnomalyFlag] = Field(default_factory=list)
cumulative_reward: float = 0.0
done: bool = False
task_id: str = ""
|