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
|
"""Exact-math verification tests for the DCF engine.
Each scenario independently recomputes the expected projection, discounting,
terminal value, enterprise value, equity value, and per-share intrinsic value,
then asserts the implementation matches. This makes the test itself auditable.
"""
import pandas as pd
import pytest
from app.services.data_service import _run_dcf
def _fcf_series(prior: float, base: float) -> pd.Series:
"""Return a two-point historical FCF series: prior -> base (most recent)."""
return pd.Series([prior, base], index=pd.to_datetime(["2023-09-30", "2024-09-30"]))
def _expected_dcf(
base_fcf: float,
growth_rate: float,
wacc: float,
terminal_growth: float,
projection_years: int,
shares_outstanding: float,
total_debt: float = 0.0,
cash_and_equivalents: float = 0.0,
preferred_equity: float = 0.0,
minority_interest: float = 0.0,
) -> dict:
"""Independent reference implementation of the DCF math used by `_run_dcf`."""
projected = [base_fcf * ((1 + growth_rate) ** yr) for yr in range(1, projection_years + 1)]
discounted = [fcf / ((1 + wacc) ** (year - 0.5)) for year, fcf in enumerate(projected, start=1)]
fcf_pv_sum = sum(discounted)
terminal_fcf = projected[-1] * (1 + terminal_growth)
terminal_value = terminal_fcf / (wacc - terminal_growth)
terminal_value_pv = terminal_value / ((1 + wacc) ** (projection_years - 0.5))
enterprise_value = fcf_pv_sum + terminal_value_pv
net_debt = total_debt - cash_and_equivalents
equity_value = enterprise_value - net_debt - preferred_equity - minority_interest
intrinsic_value_per_share = equity_value / shares_outstanding
return {
"fcf_pv_sum": fcf_pv_sum,
"terminal_value_pv": terminal_value_pv,
"enterprise_value": enterprise_value,
"net_debt": net_debt,
"equity_value": equity_value,
"intrinsic_value_per_share": intrinsic_value_per_share,
}
_APPROX = pytest.approx
def test_run_dcf_one_year_with_equity_bridge() -> None:
"""1-year projection with debt, cash, preferred, and minority claims."""
fcf = _fcf_series(100.0, 105.0) # exact 5% growth
expected = _expected_dcf(
base_fcf=105.0,
growth_rate=0.05,
wacc=0.10,
terminal_growth=0.03,
projection_years=1,
shares_outstanding=10.0,
total_debt=20.0,
cash_and_equivalents=5.0,
preferred_equity=2.0,
minority_interest=3.0,
)
result = _run_dcf(
fcf_series=fcf,
shares_outstanding=10.0,
wacc=0.10,
terminal_growth=0.03,
projection_years=1,
total_debt=20.0,
cash_and_equivalents=5.0,
preferred_equity=2.0,
minority_interest=3.0,
)
assert result["base_fcf"] == 105.0
assert result["growth_rate_used"] == _APPROX(0.05)
assert result["fcf_pv_sum"] == _APPROX(expected["fcf_pv_sum"])
assert result["terminal_value_pv"] == _APPROX(expected["terminal_value_pv"])
assert result["enterprise_value"] == _APPROX(expected["enterprise_value"])
assert result["net_debt"] == expected["net_debt"]
assert result["equity_value"] == _APPROX(expected["equity_value"])
assert result["intrinsic_value_per_share"] == _APPROX(expected["intrinsic_value_per_share"])
def test_run_dcf_three_years_no_claims() -> None:
"""3-year projection with a clean capital structure."""
fcf = _fcf_series(100.0, 105.0)
expected = _expected_dcf(
base_fcf=105.0,
growth_rate=0.05,
wacc=0.10,
terminal_growth=0.03,
projection_years=3,
shares_outstanding=10.0,
)
result = _run_dcf(
fcf_series=fcf,
shares_outstanding=10.0,
wacc=0.10,
terminal_growth=0.03,
projection_years=3,
)
assert result["base_fcf"] == 105.0
assert result["fcf_pv_sum"] == _APPROX(expected["fcf_pv_sum"])
assert result["terminal_value_pv"] == _APPROX(expected["terminal_value_pv"])
assert result["enterprise_value"] == _APPROX(expected["enterprise_value"])
assert result["equity_value"] == _APPROX(expected["equity_value"])
assert result["intrinsic_value_per_share"] == _APPROX(expected["intrinsic_value_per_share"])
assert result["net_debt"] == 0.0
def test_run_dcf_five_years_default_horizon() -> None:
"""5-year projection matching the API default horizon."""
fcf = _fcf_series(100.0, 105.0)
expected = _expected_dcf(
base_fcf=105.0,
growth_rate=0.05,
wacc=0.10,
terminal_growth=0.03,
projection_years=5,
shares_outstanding=10.0,
)
result = _run_dcf(
fcf_series=fcf,
shares_outstanding=10.0,
wacc=0.10,
terminal_growth=0.03,
projection_years=5,
)
assert result["base_fcf"] == 105.0
assert result["fcf_pv_sum"] == _APPROX(expected["fcf_pv_sum"])
assert result["terminal_value_pv"] == _APPROX(expected["terminal_value_pv"])
assert result["enterprise_value"] == _APPROX(expected["enterprise_value"])
assert result["equity_value"] == _APPROX(expected["equity_value"])
assert result["intrinsic_value_per_share"] == _APPROX(expected["intrinsic_value_per_share"])
def test_run_dcf_zero_growth_flat_fcf() -> None:
"""Edge of the model: zero growth projects a flat FCF stream."""
fcf = _fcf_series(100.0, 100.0) # exact 0% growth
expected = _expected_dcf(
base_fcf=100.0,
growth_rate=0.0,
wacc=0.10,
terminal_growth=0.02,
projection_years=5,
shares_outstanding=10.0,
)
result = _run_dcf(
fcf_series=fcf,
shares_outstanding=10.0,
wacc=0.10,
terminal_growth=0.02,
projection_years=5,
)
assert result["growth_rate_used"] == _APPROX(0.0)
assert result["fcf_pv_sum"] == _APPROX(expected["fcf_pv_sum"])
assert result["terminal_value_pv"] == _APPROX(expected["terminal_value_pv"])
assert result["enterprise_value"] == _APPROX(expected["enterprise_value"])
assert result["intrinsic_value_per_share"] == _APPROX(expected["intrinsic_value_per_share"])
def test_run_dcf_uses_historical_growth_rate() -> None:
"""End-to-end check that the growth-rate estimator feeds the projection.
A series rising exactly 5% per year should produce the same 5-year result as
the hand-computed 5-year scenario (within float tolerance).
"""
fcf = _fcf_series(100.0, 105.0)
expected = _expected_dcf(
base_fcf=105.0,
growth_rate=0.05,
wacc=0.10,
terminal_growth=0.03,
projection_years=5,
shares_outstanding=10.0,
)
result = _run_dcf(
fcf_series=fcf,
shares_outstanding=10.0,
wacc=0.10,
terminal_growth=0.03,
projection_years=5,
)
assert result["growth_rate_used"] == _APPROX(0.05)
assert result["intrinsic_value_per_share"] == _APPROX(expected["intrinsic_value_per_share"])
|