summaryrefslogtreecommitdiff
path: root/frontend/lib/dcf.ts
blob: 04f1d6cf01886849ecfc52e88a02d1a2f48c37c0 (plain)
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
export type DcfInputs = {
  baseFcf: number;
  /** enterprise_value - equity_value; holds net_debt + preferred + minority interest */
  equityBridge: number;
  sharesOutstanding: number;
};

export type DcfParams = {
  wacc: number;
  terminalGrowth: number;
  projectionYears: number;
  growthRate: number;
};

export type DcfComputed = {
  intrinsicValuePerShare: number;
  enterpriseValue: number;
  equityValue: number;
  fcfPvSum: number;
  terminalValuePv: number;
};

export function computeDcf(
  inputs: DcfInputs,
  params: DcfParams
): DcfComputed | { error: string } {
  const { baseFcf, equityBridge, sharesOutstanding } = inputs;
  const { wacc, terminalGrowth, projectionYears, growthRate } = params;

  if (wacc <= 0) return { error: "WACC must be greater than 0%." };
  if (terminalGrowth >= wacc) return { error: "Terminal growth must be lower than WACC." };
  if (baseFcf <= 0) return { error: "DCF not meaningful with zero or negative base FCF." };

  const projected = Array.from(
    { length: projectionYears },
    (_, i) => baseFcf * (1 + growthRate) ** (i + 1)
  );
  const fcfPvSum = projected.reduce(
    (sum, fcf, i) => sum + fcf / (1 + wacc) ** (i + 1),
    0
  );

  const terminalFcf = projected[projected.length - 1] * (1 + terminalGrowth);
  const terminalValue = terminalFcf / (wacc - terminalGrowth);
  const terminalValuePv = terminalValue / (1 + wacc) ** projectionYears;

  const enterpriseValue = fcfPvSum + terminalValuePv;
  const equityValue = enterpriseValue - equityBridge;
  const intrinsicValuePerShare = equityValue / sharesOutstanding;

  return { intrinsicValuePerShare, enterpriseValue, equityValue, fcfPvSum, terminalValuePv };
}