use crate::mathx;
pub const NEUTRAL_DISCOUNT_RATE: f64 = 0.04;
pub const RATE_PE_SENSITIVITY: f64 = 1.5;
pub const RATE_ADJUSTMENT_FLOOR: f64 = 0.5;
pub const GROWTH_DURATION_SCALE: f64 = 2.0;
pub const LOSS_MAKING_PRICE_TO_BOOK: f64 = 1.2;
pub const FAIR_VALUE_FLOOR: f64 = 0.01;
pub const DEFAULT_SECTOR_ANCHOR_PE: f64 = 18.0;
#[derive(Debug, Clone, Copy, PartialEq, Default)]
#[non_exhaustive]
pub struct CompanyValuationInputs {
pub sector_avg_pe: Option<f64>,
pub eps: Option<f64>,
pub book_value_per_share: Option<f64>,
pub revenue_growth: Option<f64>,
}
#[derive(Debug, Clone, Copy, PartialEq)]
#[non_exhaustive]
pub struct EconomyValuationInputs {
pub corporate_bond_yield: Option<f64>,
pub federal_funds_rate: f64,
pub qe_pe_boost: Option<f64>,
pub qe_assets_ratio: Option<f64>,
}
impl EconomyValuationInputs {
pub fn new(federal_funds_rate: f64) -> Self {
EconomyValuationInputs {
corporate_bond_yield: None,
federal_funds_rate,
qe_pe_boost: None,
qe_assets_ratio: None,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
#[non_exhaustive]
pub struct FairValueBreakdown {
pub fair_value: f64,
pub target_pe: f64,
pub sector_anchor_pe: f64,
pub rate_adjustment: f64,
pub qe_adjustment: f64,
pub book_value_path: bool,
}
#[derive(Debug, Clone, Copy, PartialEq)]
#[non_exhaustive]
pub struct TargetPe {
pub target_pe: f64,
pub sector_anchor_pe: f64,
pub rate_adjustment: f64,
pub qe_adjustment: f64,
}
fn sector_anchor_pe(avg_pe: Option<f64>) -> f64 {
match avg_pe {
Some(pe) if pe != 0.0 && !pe.is_nan() => pe,
_ => DEFAULT_SECTOR_ANCHOR_PE,
}
}
fn discount_rate(economy: &EconomyValuationInputs) -> f64 {
let yield_pct = economy
.corporate_bond_yield
.unwrap_or(economy.federal_funds_rate);
yield_pct / 100.0
}
fn qe_stock_term(gain: f64, ratio: Option<f64>) -> f64 {
if gain == 0.0 {
return 0.0;
}
match ratio {
Some(r) => gain * mathx::log(mathx::max(r, 1e-6)),
None => 0.0,
}
}
pub fn compute_target_pe(
company: &CompanyValuationInputs,
economy: &EconomyValuationInputs,
qe_gain: f64,
qe_stock_gain: f64,
neutral_rate: f64,
) -> TargetPe {
compute_target_pe_at(company, economy, qe_gain, qe_stock_gain, neutral_rate, RATE_PE_SENSITIVITY)
}
pub fn compute_target_pe_at(
company: &CompanyValuationInputs,
economy: &EconomyValuationInputs,
qe_gain: f64,
qe_stock_gain: f64,
neutral_rate: f64,
rate_pe_sensitivity: f64,
) -> TargetPe {
let sector_anchor_pe = sector_anchor_pe(company.sector_avg_pe);
let discount = discount_rate(economy);
let revenue_growth = company.revenue_growth.unwrap_or(0.0);
let duration_multiplier = 1.0 + mathx::max(0.0, revenue_growth) * GROWTH_DURATION_SCALE;
let rate_adjustment = mathx::max(
RATE_ADJUSTMENT_FLOOR,
1.0 - (discount - neutral_rate) * rate_pe_sensitivity * duration_multiplier,
);
let qe_adjustment = 1.0 + qe_gain * economy.qe_pe_boost.unwrap_or(0.0)
+ qe_stock_term(qe_stock_gain, economy.qe_assets_ratio);
TargetPe {
target_pe: sector_anchor_pe * rate_adjustment * qe_adjustment,
sector_anchor_pe,
rate_adjustment,
qe_adjustment,
}
}
pub fn compute_fair_value(
company: &CompanyValuationInputs,
economy: &EconomyValuationInputs,
) -> FairValueBreakdown {
compute_fair_value_with(company, economy, 0.0, 1.0, 0.0, NEUTRAL_DISCOUNT_RATE)
}
pub fn compute_fair_value_with(
company: &CompanyValuationInputs,
economy: &EconomyValuationInputs,
book_floor: f64,
qe_gain: f64,
qe_stock_gain: f64,
neutral_rate: f64,
) -> FairValueBreakdown {
compute_fair_value_at(company, economy, book_floor, qe_gain, qe_stock_gain, neutral_rate,
RATE_PE_SENSITIVITY)
}
pub fn compute_fair_value_at(
company: &CompanyValuationInputs,
economy: &EconomyValuationInputs,
book_floor: f64,
qe_gain: f64,
qe_stock_gain: f64,
neutral_rate: f64,
rate_pe_sensitivity: f64,
) -> FairValueBreakdown {
let eps = company.eps.unwrap_or(0.0);
if eps > 0.0 {
let pe = compute_target_pe_at(company, economy, qe_gain, qe_stock_gain, neutral_rate,
rate_pe_sensitivity);
let earnings_value = mathx::max(FAIR_VALUE_FLOOR, eps * pe.target_pe);
let fair_value = if book_floor == 0.0 {
earnings_value
} else {
let floor = company.book_value_per_share.unwrap_or(0.0)
* LOSS_MAKING_PRICE_TO_BOOK
* book_floor;
mathx::max(earnings_value, floor)
};
return FairValueBreakdown {
fair_value,
target_pe: pe.target_pe,
sector_anchor_pe: pe.sector_anchor_pe,
rate_adjustment: pe.rate_adjustment,
qe_adjustment: pe.qe_adjustment,
book_value_path: false,
};
}
let book_value = company.book_value_per_share.unwrap_or(0.0);
FairValueBreakdown {
fair_value: mathx::max(FAIR_VALUE_FLOOR, book_value * LOSS_MAKING_PRICE_TO_BOOK),
target_pe: 0.0,
sector_anchor_pe: sector_anchor_pe(company.sector_avg_pe),
rate_adjustment: 1.0,
qe_adjustment: 1.0,
book_value_path: true,
}
}
#[cfg(test)]
mod tests {
use super::*;
fn econ(y: Option<f64>, fed: f64, qe: Option<f64>) -> EconomyValuationInputs {
EconomyValuationInputs {
corporate_bond_yield: y,
federal_funds_rate: fed,
qe_pe_boost: qe,
qe_assets_ratio: None,
}
}
fn co(
pe: Option<f64>,
eps: Option<f64>,
book: Option<f64>,
g: Option<f64>,
) -> CompanyValuationInputs {
CompanyValuationInputs {
sector_avg_pe: pe,
eps,
book_value_per_share: book,
revenue_growth: g,
}
}
#[test]
fn zero_sector_anchor_falls_back_to_default() {
assert_eq!(sector_anchor_pe(Some(0.0)), DEFAULT_SECTOR_ANCHOR_PE);
assert_eq!(sector_anchor_pe(None), DEFAULT_SECTOR_ANCHOR_PE);
assert_eq!(sector_anchor_pe(Some(f64::NAN)), DEFAULT_SECTOR_ANCHOR_PE);
assert_eq!(sector_anchor_pe(Some(32.0)), 32.0);
}
#[test]
fn zero_bond_yield_is_kept_not_replaced_by_the_policy_rate() {
let kept = discount_rate(&econ(Some(0.0), 9.0, None));
assert_eq!(kept, 0.0, "a 0% yield must be used, not treated as absent");
let fell_through = discount_rate(&econ(None, 9.0, None));
assert_eq!(
fell_through, 0.09,
"an absent yield falls through to fedRate"
);
}
#[test]
fn the_qe_gain_is_inert_at_one_and_scales_the_channel() {
let c = co(Some(20.0), Some(1.0), None, Some(0.1));
let mut e = econ(Some(10.0), 3.5, None);
e.qe_pe_boost = Some(0.20);
let shipped = compute_target_pe(&c, &e, 1.0, 0.0, NEUTRAL_DISCOUNT_RATE);
assert_eq!(shipped.qe_adjustment.to_bits(), (1.20f64).to_bits());
assert_eq!(compute_target_pe(&c, &e, 0.5, 0.0, NEUTRAL_DISCOUNT_RATE).qe_adjustment.to_bits(),
(1.10f64).to_bits());
let off = compute_target_pe(&c, &e, 0.0, 0.0, NEUTRAL_DISCOUNT_RATE);
assert_eq!(off.qe_adjustment.to_bits(), (1.0f64).to_bits());
assert!(off.target_pe > 0.0);
}
#[test]
fn qe_stock_channel_is_inert_off_and_concave_on() {
let base = econ(Some(10.0), 3.5, Some(0.05));
let mut high = base;
high.qe_assets_ratio = Some(2.2);
let c = co(Some(20.0), Some(4.0), None, Some(0.1));
let off_base = compute_target_pe(&c, &base, 1.0, 0.0, NEUTRAL_DISCOUNT_RATE);
let off_high = compute_target_pe(&c, &high, 1.0, 0.0, NEUTRAL_DISCOUNT_RATE);
assert_eq!(off_base.qe_adjustment.to_bits(), off_high.qe_adjustment.to_bits());
let mut neutral = base;
neutral.qe_assets_ratio = Some(1.0);
assert_eq!(
compute_target_pe(&c, &neutral, 1.0, 0.13, NEUTRAL_DISCOUNT_RATE).qe_adjustment.to_bits(),
off_base.qe_adjustment.to_bits(),
);
let term = |r: f64| {
let mut e = base;
e.qe_assets_ratio = Some(r);
compute_target_pe(&c, &e, 1.0, 0.13, NEUTRAL_DISCOUNT_RATE).qe_adjustment
};
let d1 = term(2.0) - term(1.0);
let d2 = term(3.0) - term(2.0);
assert!(d1 > d2 && d2 > 0.0, "equal level steps must add less: {d1} then {d2}");
}
#[test]
fn negative_growth_does_not_shrink_duration() {
let shrinking = compute_target_pe(
&co(Some(20.0), Some(1.0), None, Some(-0.9)),
&econ(Some(10.0), 3.5, None),
1.0,
0.0,
NEUTRAL_DISCOUNT_RATE,
);
let flat = compute_target_pe(
&co(Some(20.0), Some(1.0), None, Some(0.0)),
&econ(Some(10.0), 3.5, None),
1.0,
0.0,
NEUTRAL_DISCOUNT_RATE,
);
assert_eq!(
shrinking.rate_adjustment.to_bits(),
flat.rate_adjustment.to_bits()
);
}
#[test]
fn rate_adjustment_never_falls_below_its_floor() {
let extreme = compute_target_pe(
&co(Some(32.0), Some(4.0), None, Some(0.5)),
&econ(Some(40.0), 3.5, None),
1.0,
0.0,
NEUTRAL_DISCOUNT_RATE,
);
assert_eq!(extreme.rate_adjustment, RATE_ADJUSTMENT_FLOOR);
}
#[test]
fn loss_makers_take_the_book_path() {
for eps in [0.0, -0.01, -5.0] {
let fv = compute_fair_value(
&co(Some(24.0), Some(eps), Some(10.0), None),
&econ(Some(4.0), 3.5, None),
);
assert!(fv.book_value_path);
assert_eq!(fv.target_pe, 0.0);
assert_eq!(fv.fair_value, 12.0, "10 x 1.2");
}
}
#[test]
fn the_floor_binds_on_both_paths() {
let degenerate_book = compute_fair_value(
&co(Some(24.0), Some(-1.0), Some(-50.0), None),
&econ(Some(4.0), 3.5, None),
);
assert_eq!(degenerate_book.fair_value, FAIR_VALUE_FLOOR);
let tiny_eps = compute_fair_value(
&co(Some(14.0), Some(1e-12), Some(5.0), None),
&econ(Some(4.0), 3.5, None),
);
assert_eq!(tiny_eps.fair_value, FAIR_VALUE_FLOOR);
}
#[test]
fn is_a_pure_function() {
let c = co(Some(32.0), Some(4.2), Some(31.7), Some(0.15));
let e = econ(Some(4.0), 3.5, Some(0.05));
let first = compute_fair_value(&c, &e);
for _ in 0..100 {
assert_eq!(compute_fair_value(&c, &e), first);
}
}
}