Spaces:
Configuration error

File size: 9,350 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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
"""
Generates synthetic but realistic financial statements for fictional companies.
Uses industry baselines from public financial databases and applies controlled
random variation to produce 4-8 quarters of clean data.
"""

from __future__ import annotations

import random
from typing import List, Tuple

from .models import (
    CompanyMetadata,
    CompanySize,
    Industry,
    QuarterData,
)
from .utils import dollars_to_cents

# ---------------------------------------------------------------------------
# Industry baseline profiles (ratios sourced from Damodaran Online aggregates)
# ---------------------------------------------------------------------------

INDUSTRY_PROFILES = {
    Industry.TECHNOLOGY: {
        "gross_margin": (0.65, 0.75),
        "opex_ratio": (0.35, 0.45),       # opex / revenue
        "current_ratio": (2.5, 4.0),
        "dso_days": (45, 75),
        "inventory_ratio": (0.02, 0.08),   # inventory / revenue
        "debt_equity": (0.2, 0.6),
        "ocf_ni": (1.2, 1.8),
        "capex_ratio": (0.05, 0.12),
        "seasonal_q4_boost": 0.08,
    },
    Industry.MANUFACTURING: {
        "gross_margin": (0.25, 0.35),
        "opex_ratio": (0.12, 0.20),
        "current_ratio": (1.5, 2.5),
        "dso_days": (35, 55),
        "inventory_ratio": (0.15, 0.30),
        "debt_equity": (0.5, 1.2),
        "ocf_ni": (1.0, 1.5),
        "capex_ratio": (0.08, 0.18),
        "seasonal_q4_boost": 0.04,
    },
    Industry.RETAIL: {
        "gross_margin": (0.30, 0.45),
        "opex_ratio": (0.20, 0.30),
        "current_ratio": (1.0, 1.8),
        "dso_days": (5, 15),
        "inventory_ratio": (0.20, 0.35),
        "debt_equity": (0.6, 1.5),
        "ocf_ni": (1.1, 1.6),
        "capex_ratio": (0.04, 0.10),
        "seasonal_q4_boost": 0.18,  # holiday season
    },
    Industry.HEALTHCARE: {
        "gross_margin": (0.55, 0.70),
        "opex_ratio": (0.30, 0.40),
        "current_ratio": (1.5, 3.0),
        "dso_days": (40, 65),
        "inventory_ratio": (0.05, 0.12),
        "debt_equity": (0.3, 0.8),
        "ocf_ni": (1.0, 1.4),
        "capex_ratio": (0.06, 0.14),
        "seasonal_q4_boost": 0.03,
    },
    Industry.FINANCIAL_SERVICES: {
        "gross_margin": (0.50, 0.65),
        "opex_ratio": (0.25, 0.38),
        "current_ratio": (1.2, 2.0),
        "dso_days": (30, 50),
        "inventory_ratio": (0.0, 0.02),
        "debt_equity": (1.0, 3.0),
        "ocf_ni": (0.9, 1.3),
        "capex_ratio": (0.03, 0.08),
        "seasonal_q4_boost": 0.05,
    },
}

REVENUE_RANGES = {
    CompanySize.SMALL: (10_000_000, 50_000_000),
    CompanySize.MEDIUM: (50_000_000, 500_000_000),
    CompanySize.LARGE: (500_000_000, 2_000_000_000),
}

COMPANY_NAMES = [
    "Apex Dynamics Corp", "Veriton Industries", "Crestline Holdings",
    "NovaEdge Solutions", "Pinnacle Systems Inc", "Meridian Analytics",
    "Stratos Group Ltd", "Caliber Technologies", "Summit Financial Corp",
    "Prism Data Services", "BluePeak Manufacturing", "Orion Healthcare Inc",
    "Trident Retail Group", "Cypher Software Ltd", "Atlas Infrastructure",
]


def _rand_in(low: float, high: float, rng: random.Random) -> float:
    return low + rng.random() * (high - low)


def generate_company_data(
    seed: int,
    num_quarters: int = 6,
    industry: Industry | None = None,
    size: CompanySize | None = None,
) -> Tuple[CompanyMetadata, List[QuarterData]]:
    """
    Generate a complete set of clean financial statements for a fictional company.

    Args:
        seed: RNG seed for reproducibility.
        num_quarters: Number of quarters to generate (4-8).
        industry: Force a specific industry, or random if None.
        size: Force a specific size, or random if None.

    Returns:
        Tuple of (CompanyMetadata, list of QuarterData).
    """
    rng = random.Random(seed)

    # Pick industry & size
    if industry is None:
        industry = rng.choice(list(Industry))
    if size is None:
        size = rng.choice(list(CompanySize))

    profile = INDUSTRY_PROFILES[industry]
    company_name = rng.choice(COMPANY_NAMES)

    # Base annual revenue
    rev_low, rev_high = REVENUE_RANGES[size]
    annual_revenue = rng.uniform(rev_low, rev_high)
    quarterly_revenue_base = annual_revenue / 4

    # Pick ratio targets
    gm = _rand_in(*profile["gross_margin"], rng)
    opex_r = _rand_in(*profile["opex_ratio"], rng)
    cr = _rand_in(*profile["current_ratio"], rng)
    dso = _rand_in(*profile["dso_days"], rng)
    inv_r = _rand_in(*profile["inventory_ratio"], rng)
    de = _rand_in(*profile["debt_equity"], rng)
    ocf_ni_r = _rand_in(*profile["ocf_ni"], rng)
    capex_r = _rand_in(*profile["capex_ratio"], rng)
    seasonal_boost = profile["seasonal_q4_boost"]

    # Growth rate per quarter
    qoq_growth = rng.uniform(-0.02, 0.06)

    fiscal_year_start = rng.choice([2021, 2022, 2023])
    metadata = CompanyMetadata(
        name=company_name,
        industry=industry,
        size=size,
        num_quarters=num_quarters,
        fiscal_year_start=fiscal_year_start,
    )

    quarters: List[QuarterData] = []
    prev_cash = dollars_to_cents(rng.uniform(0.05, 0.15) * annual_revenue)

    for i in range(num_quarters):
        year = fiscal_year_start + i // 4
        q_num = (i % 4) + 1
        label = f"Q{q_num} {year}"

        # Revenue with growth + seasonal variation
        growth_factor = (1 + qoq_growth) ** i
        seasonal = 1.0 + (seasonal_boost if q_num == 4 else -seasonal_boost / 3)
        variation = rng.uniform(0.95, 1.05)
        revenue = quarterly_revenue_base * growth_factor * seasonal * variation
        revenue_c = dollars_to_cents(revenue)

        # Income statement
        cogs_c = dollars_to_cents(revenue * (1 - gm) * rng.uniform(0.97, 1.03))
        gross_profit_c = revenue_c - cogs_c
        opex_c = dollars_to_cents(revenue * opex_r * rng.uniform(0.95, 1.05))
        operating_income_c = gross_profit_c - opex_c
        interest_c = dollars_to_cents(revenue * rng.uniform(0.005, 0.02))
        pretax = operating_income_c - interest_c
        tax_c = dollars_to_cents(max(0, pretax * 0.01) * rng.uniform(0.20, 0.28))
        net_income_c = pretax - tax_c

        # Balance sheet
        receivables_c = dollars_to_cents(revenue * (dso / 90) * rng.uniform(0.92, 1.08))
        inventory_c = dollars_to_cents(revenue * inv_r * rng.uniform(0.90, 1.10))
        other_ca_c = dollars_to_cents(revenue * rng.uniform(0.02, 0.06))

        # Cash flow
        depreciation = dollars_to_cents(revenue * rng.uniform(0.02, 0.05))
        wc_change = dollars_to_cents(revenue * rng.uniform(-0.03, 0.03))
        cf_operating_c = dollars_to_cents(
            (net_income_c + depreciation - wc_change) * 0.01
            * ocf_ni_r * rng.uniform(0.90, 1.10)
        )
        # Simpler: just derive from net income * ocf ratio
        cf_operating_c = int(net_income_c * ocf_ni_r * rng.uniform(0.90, 1.10))

        capex = dollars_to_cents(revenue * capex_r * rng.uniform(0.85, 1.15))
        cf_investing_c = -capex
        cf_financing_c = dollars_to_cents(revenue * rng.uniform(-0.05, 0.03))

        net_cash_change_c = cf_operating_c + cf_investing_c + cf_financing_c
        cash_c = prev_cash + net_cash_change_c
        if cash_c < 0:
            # Inject financing to keep cash positive
            injection = abs(cash_c) + dollars_to_cents(revenue * 0.02)
            cf_financing_c += injection
            net_cash_change_c = cf_operating_c + cf_investing_c + cf_financing_c
            cash_c = prev_cash + net_cash_change_c

        total_ca_c = cash_c + receivables_c + inventory_c + other_ca_c
        fixed_assets_c = dollars_to_cents(annual_revenue * rng.uniform(0.3, 0.8))
        total_assets_c = total_ca_c + fixed_assets_c

        # Liabilities
        payables_c = dollars_to_cents(revenue * rng.uniform(0.05, 0.12))
        short_debt_c = dollars_to_cents(revenue * rng.uniform(0.02, 0.08))
        total_cl_c = payables_c + short_debt_c
        long_debt_c = dollars_to_cents(total_assets_c * rng.uniform(0.10, 0.30))
        total_liab_c = total_cl_c + long_debt_c

        # Equity = assets - liabilities (forced balance)
        equity_c = total_assets_c - total_liab_c

        q = QuarterData(
            quarter_label=label,
            cash=cash_c,
            receivables=receivables_c,
            inventory=inventory_c,
            other_current_assets=other_ca_c,
            total_current_assets=total_ca_c,
            fixed_assets=fixed_assets_c,
            total_assets=total_assets_c,
            payables=payables_c,
            short_term_debt=short_debt_c,
            total_current_liabilities=total_cl_c,
            long_term_debt=long_debt_c,
            total_liabilities=total_liab_c,
            equity=equity_c,
            revenue=revenue_c,
            cogs=cogs_c,
            gross_profit=gross_profit_c,
            operating_expenses=opex_c,
            operating_income=operating_income_c,
            interest_expense=interest_c,
            tax_expense=tax_c,
            net_income=net_income_c,
            cf_operating=cf_operating_c,
            cf_investing=cf_investing_c,
            cf_financing=cf_financing_c,
            net_cash_change=net_cash_change_c,
        )
        quarters.append(q)
        prev_cash = cash_c

    return metadata, quarters