Spaces:
Configuration error

File size: 3,946 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
"""
Utility functions for financial ratio calculations, Benford's Law analysis,
and cross-statement validation.
"""

from __future__ import annotations

import math
from typing import Dict, List

from .models import QuarterData


# ---------------------------------------------------------------------------
# Benford's Law
# ---------------------------------------------------------------------------

BENFORD_EXPECTED = {
    1: 0.301, 2: 0.176, 3: 0.125, 4: 0.097,
    5: 0.079, 6: 0.067, 7: 0.058, 8: 0.051, 9: 0.046,
}


def leading_digit(value: int) -> int:
    """Return the leading digit of an integer (ignoring sign and zero)."""
    v = abs(value)
    if v == 0:
        return 0
    while v >= 10:
        v //= 10
    return v


def benfords_chi_squared(values: List[int]) -> float:
    """
    Compute Benford's Law chi-squared statistic for a list of integer values.
    Higher values indicate more deviation from expected distribution.
    """
    counts: Dict[int, int] = {d: 0 for d in range(1, 10)}
    total = 0
    for v in values:
        d = leading_digit(v)
        if d >= 1:
            counts[d] += 1
            total += 1
    if total == 0:
        return 0.0
    chi2 = 0.0
    for digit in range(1, 10):
        observed = counts[digit]
        expected = BENFORD_EXPECTED[digit] * total
        if expected > 0:
            chi2 += (observed - expected) ** 2 / expected
    return chi2


# ---------------------------------------------------------------------------
# Financial ratios
# ---------------------------------------------------------------------------

def gross_margin(q: QuarterData) -> float:
    """Gross profit / Revenue."""
    if q.revenue == 0:
        return 0.0
    return q.gross_profit / q.revenue


def current_ratio(q: QuarterData) -> float:
    """Total current assets / Total current liabilities."""
    if q.total_current_liabilities == 0:
        return 0.0
    return q.total_current_assets / q.total_current_liabilities


def days_sales_outstanding(q: QuarterData) -> float:
    """DSO = (Receivables / Revenue) * 90 (for a quarter)."""
    if q.revenue == 0:
        return 0.0
    return (q.receivables / q.revenue) * 90


def ocf_to_ni_ratio(q: QuarterData) -> float:
    """Operating cash flow / Net income."""
    if q.net_income == 0:
        return 0.0
    return q.cf_operating / q.net_income


def inventory_turnover(q: QuarterData) -> float:
    """COGS / Inventory (annualized from quarterly = multiply by 4)."""
    if q.inventory == 0:
        return 0.0
    return (q.cogs * 4) / q.inventory


def debt_to_equity(q: QuarterData) -> float:
    """Total liabilities / Equity."""
    if q.equity == 0:
        return 0.0
    return q.total_liabilities / q.equity


# ---------------------------------------------------------------------------
# Cross-statement validation helpers
# ---------------------------------------------------------------------------

def balance_sheet_balanced(q: QuarterData) -> bool:
    """Check assets == liabilities + equity."""
    return q.total_assets == q.total_liabilities + q.equity


def cash_flow_reconciles(q: QuarterData) -> bool:
    """Check net cash change == sum of CF components."""
    return q.net_cash_change == q.cf_operating + q.cf_investing + q.cf_financing


def current_assets_sum_check(q: QuarterData) -> bool:
    """Check total_current_assets == sum of components."""
    expected = q.cash + q.receivables + q.inventory + q.other_current_assets
    return q.total_current_assets == expected


def income_statement_check(q: QuarterData) -> bool:
    """Check gross_profit == revenue - cogs."""
    return q.gross_profit == q.revenue - q.cogs


def cents_to_dollars(cents: int) -> float:
    """Convert integer cents to dollar float for display."""
    return cents / 100.0


def dollars_to_cents(dollars: float) -> int:
    """Convert dollar float to integer cents for storage."""
    return int(round(dollars * 100))