const QE_ASSETS_BASELINE_B: f64 = 5000.0;
use super::state::*;
use crate::mathx::{self, clamp_via_min_max as clamp};
use crate::rng::Rng;
const ANNOUNCEMENT_VARIANTS: f64 = 6.0;
pub const CORPORATE_SPREAD_FLOOR: f64 = 0.8;
pub fn cycle_spread_multiplier(phase: CyclePhase) -> f64 {
spread_multiplier_of(phase)
}
pub const MORTGAGE_SPREAD_FLOOR: f64 = 0.5;
pub const FED_PUT_INFLATION_CEILING: f64 = 4.0;
pub const FED_PUT_HOLD_VIX: f64 = 30.0;
pub fn fed_put_ask(gain: f64, threshold: f64, intermeeting_return: f64) -> f64 {
gain * mathx::max(0.0, -intermeeting_return - threshold)
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum Decision {
AggressiveHike,
Hike,
Cut,
EmergencyCut,
StagflationHike,
LaborEmergencyCut,
Hold,
}
impl Decision {
pub fn as_str(self) -> &'static str {
match self {
Decision::AggressiveHike => "aggressive_hike",
Decision::Hike => "hike",
Decision::Cut => "cut",
Decision::EmergencyCut => "emergency_cut",
Decision::StagflationHike => "stagflation_hike",
Decision::LaborEmergencyCut => "labor_emergency_cut",
Decision::Hold => "hold",
}
}
}
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub struct MeetingOutcome {
pub central_bank: CentralBankState,
pub economy: EconomyState,
pub decision: Option<Decision>,
pub announcement_variant: Option<usize>,
}
#[derive(Debug, Clone, Copy, PartialEq)]
#[non_exhaustive]
pub struct PolicyOptions {
pub calendar: MacroCalendar,
pub liftoff: f64,
pub spread_multiplier: Option<f64>,
pub growth_cut: f64,
pub stress_cut: f64,
pub stress_vix: f64,
pub stress_inflation_gap: f64,
pub stress_level: f64,
pub hold_rate: bool,
pub put_gain: f64,
pub put_threshold: f64,
pub put_pricing: f64,
pub haven_gain: f64,
pub put_carry: f64,
pub stress_hold: bool,
pub path_gain: f64,
pub path_before: f64,
pub rate_damping: f64,
pub spread_vix_cut: f64,
pub spread_equity_gain: f64,
pub spread_priced_gap: bool,
pub spread_priced_gain: f64,
pub treasury_equity_gain: f64,
pub spread_noise: Option<f64>,
pub term_premium: Option<f64>,
pub policy_inflation: Option<f64>,
pub curve_inflation: Option<f64>,
pub zlb_path: f64,
pub zlb_haven_gain: f64,
pub neutral_real_rate: f64,
pub two_year_carry: f64,
pub path_carry: Option<f64>,
pub eve_change: Option<f64>,
pub path_owed_excluded: bool,
}
impl Default for PolicyOptions {
fn default() -> Self {
PolicyOptions::shipped()
}
}
impl PolicyOptions {
pub const fn shipped() -> Self {
PolicyOptions {
calendar: MacroCalendar::shipped(),
liftoff: 0.0,
spread_multiplier: None,
growth_cut: 0.0,
stress_cut: 0.0,
stress_vix: 30.0,
stress_inflation_gap: 1.0,
stress_level: 0.0,
hold_rate: false,
put_gain: 0.0,
put_threshold: 0.0,
put_pricing: 0.0,
haven_gain: 0.0,
put_carry: 0.0,
stress_hold: false,
path_gain: 0.0,
path_before: 0.0,
rate_damping: 0.0,
spread_vix_cut: 0.0,
spread_equity_gain: 0.0,
spread_priced_gap: false,
spread_priced_gain: 0.0,
treasury_equity_gain: 0.0,
spread_noise: None,
term_premium: None,
policy_inflation: None,
curve_inflation: None,
zlb_path: 0.0,
zlb_haven_gain: 0.0,
neutral_real_rate: 0.0,
two_year_carry: 0.0,
path_carry: None,
eve_change: None,
path_owed_excluded: false,
}
}
}
pub fn spread_multiplier_of(phase: CyclePhase) -> f64 {
match phase {
CyclePhase::Contraction => 2.8,
CyclePhase::Trough => 3.5,
CyclePhase::Recovery => 1.4,
CyclePhase::Peak => 1.1,
CyclePhase::Expansion => 1.0,
}
}
pub fn spread_formula(vix: f64, multiplier: f64) -> f64 {
clamp((1.0 + (vix - 12.0) * 0.02) * multiplier, CORPORATE_SPREAD_FLOOR, 6.0)
}
pub fn spread_formula_with(vix: f64, multiplier: f64, vix_cut: f64, equity: f64) -> f64 {
clamp(
(1.0 + (vix - 12.0) * 0.02 * (1.0 - vix_cut) + equity) * multiplier,
CORPORATE_SPREAD_FLOOR,
6.0,
)
}
pub fn update_central_bank(
central_bank: &CentralBankState,
economy: &EconomyState,
current_timestamp: i64,
rng: &mut impl Rng,
) -> MeetingOutcome {
update_central_bank_with(central_bank, economy, current_timestamp, rng, &PolicyOptions::shipped())
}
pub fn update_central_bank_with(
central_bank: &CentralBankState,
economy: &EconomyState,
current_timestamp: i64,
rng: &mut impl Rng,
options: &PolicyOptions,
) -> MeetingOutcome {
if let Some(pi) = options.policy_inflation {
let mut view = economy.clone();
view.inflation_rate = pi;
let inner = PolicyOptions {
policy_inflation: None,
curve_inflation: Some(options.curve_inflation.unwrap_or(economy.inflation_rate)),
..*options
};
let mut outcome = update_central_bank_with(central_bank, &view, current_timestamp, rng, &inner);
outcome.economy.inflation_rate = economy.inflation_rate;
return outcome;
}
let curve_inflation = options.curve_inflation.unwrap_or(economy.inflation_rate);
let inflation_rate_gap = economy.inflation_rate - economy.federal_funds_rate;
let is_emergency_meeting = inflation_rate_gap > 4.0 && economy.inflation_rate > 4.0;
if !is_emergency_meeting && current_timestamp < central_bank.next_meeting_date {
return MeetingOutcome {
central_bank: *central_bank,
economy: economy.clone(),
decision: None,
announcement_variant: None,
};
}
let mut new_cb = *central_bank;
let mut new_economy = economy.clone();
let neutral_rate = 2.0 + 0.5 * (economy.inflation_rate / central_bank.target_inflation - 1.0);
let neutral_rate = if options.neutral_real_rate != 0.0 {
neutral_rate + options.neutral_real_rate
} else {
neutral_rate
};
let inflation_gap_taylor = economy.inflation_rate - central_bank.target_inflation;
let output_gap_taylor = central_bank.target_unemployment - economy.unemployment_rate;
let taylor_rate = neutral_rate
+ 0.5 * inflation_gap_taylor
+ 0.5 * output_gap_taylor
+ 0.1 * central_bank.hawkish_dovish_score;
let put_on = options.put_gain != 0.0;
let taylor_rate = if put_on { taylor_rate - economy.fed_put_owed } else { taylor_rate };
let current_rate = economy.federal_funds_rate;
let rate_diff = taylor_rate - current_rate;
let inflation_gap = (economy.inflation_rate - central_bank.target_inflation).abs();
let urgency = if inflation_gap > 4.0 {
mathx::max(2.0, inflation_gap / 1.5)
} else {
mathx::max(1.0, inflation_gap / 2.0)
};
let mut rate_change = 0.0;
let mut decision = Decision::Hold;
if economy.gdp_growth < -2.0 && economy.unemployment_rate > 10.0 {
rate_change = -1.0 - rng.next_f64() * 0.5;
decision = Decision::EmergencyCut;
new_cb.hawkish_dovish_score = clamp(central_bank.hawkish_dovish_score - 0.5, -1.0, 1.0);
} else if economy.gdp_growth < 0.0 && economy.unemployment_rate > 8.0 {
rate_change = -0.5 - rng.next_f64() * 0.5;
decision = Decision::EmergencyCut;
new_cb.hawkish_dovish_score = clamp(central_bank.hawkish_dovish_score - 0.4, -1.0, 1.0);
} else if economy.unemployment_rate > 8.0 && economy.inflation_rate < 3.0 {
rate_change = -0.75;
decision = Decision::LaborEmergencyCut;
new_cb.hawkish_dovish_score = clamp(central_bank.hawkish_dovish_score - 0.4, -1.0, 1.0);
} else if (economy.cycle_phase == CyclePhase::Contraction
|| economy.cycle_phase == CyclePhase::Trough)
&& economy.gdp_growth < 0.0
&& economy.unemployment_rate > 7.0
{
if economy.inflation_rate < economy.federal_funds_rate {
rate_change = -0.25;
decision = Decision::Cut;
new_cb.hawkish_dovish_score = clamp(central_bank.hawkish_dovish_score - 0.2, -1.0, 1.0);
} else {
rate_change = 0.0;
decision = Decision::Hold;
}
} else if (economy.cycle_phase == CyclePhase::Contraction
|| economy.cycle_phase == CyclePhase::Trough)
&& current_rate > 5.0
{
rate_change = -0.25;
decision = Decision::Cut;
new_cb.hawkish_dovish_score = clamp(central_bank.hawkish_dovish_score - 0.15, -1.0, 1.0);
} else if rate_diff > 2.0
&& economy.inflation_rate > central_bank.target_inflation + 4.0
&& current_rate < economy.inflation_rate
{
rate_change = if inflation_rate_gap > 4.0 { 1.0 } else { 0.75 };
decision = Decision::AggressiveHike;
new_cb.hawkish_dovish_score = clamp(central_bank.hawkish_dovish_score + 0.4, -1.0, 1.0);
} else if inflation_rate_gap > 2.0 && economy.inflation_rate > 4.0 {
rate_change = 0.75;
decision = Decision::AggressiveHike;
new_cb.hawkish_dovish_score = clamp(central_bank.hawkish_dovish_score + 0.35, -1.0, 1.0);
} else if rate_diff > 1.0 && economy.inflation_rate > central_bank.target_inflation + 2.0 {
rate_change = 0.5;
decision = Decision::AggressiveHike;
new_cb.hawkish_dovish_score = clamp(central_bank.hawkish_dovish_score + 0.3, -1.0, 1.0);
} else if rate_diff > 0.5 && economy.inflation_rate > central_bank.target_inflation + 1.0 {
rate_change = 0.25;
decision = Decision::Hike;
new_cb.hawkish_dovish_score = clamp(central_bank.hawkish_dovish_score + 0.2, -1.0, 1.0);
} else if rate_diff < -0.5
&& economy.unemployment_rate > central_bank.target_unemployment + 1.0
&& economy.inflation_rate < central_bank.target_inflation + 2.0
{
rate_change = -0.25;
decision = Decision::Cut;
new_cb.hawkish_dovish_score = clamp(central_bank.hawkish_dovish_score - 0.2, -1.0, 1.0);
} else if rate_diff < -1.0
&& (economy.cycle_phase == CyclePhase::Contraction
|| economy.cycle_phase == CyclePhase::Trough)
&& economy.inflation_rate < central_bank.target_inflation + 1.5
{
rate_change = -0.5;
decision = Decision::EmergencyCut;
new_cb.hawkish_dovish_score = clamp(central_bank.hawkish_dovish_score - 0.3, -1.0, 1.0);
} else if economy.inflation_rate > central_bank.target_inflation + 1.5
&& current_rate < economy.inflation_rate
&& economy.unemployment_rate < 8.0
{
let rate_deficit = economy.inflation_rate - current_rate;
rate_change = if rate_deficit > 3.0 { 0.50 } else { 0.25 };
decision = Decision::StagflationHike;
new_cb.hawkish_dovish_score = clamp(central_bank.hawkish_dovish_score + 0.15, -1.0, 1.0);
} else if options.growth_cut != 0.0
&& economy.gdp_growth < options.growth_cut
&& economy.inflation_rate < central_bank.target_inflation + 1.5
&& current_rate > 0.0
{
rate_change = -0.25;
decision = Decision::Cut;
new_cb.hawkish_dovish_score = clamp(central_bank.hawkish_dovish_score - 0.2, -1.0, 1.0);
} else if options.liftoff != 0.0
&& rate_diff > 0.5
&& economy.unemployment_rate <= central_bank.target_unemployment + 1.0
&& economy.cycle_phase != CyclePhase::Contraction
&& economy.cycle_phase != CyclePhase::Trough
{
rate_change = 0.25;
decision = Decision::Hike;
new_cb.hawkish_dovish_score = clamp(central_bank.hawkish_dovish_score + 0.2, -1.0, 1.0);
}
if options.stress_cut != 0.0
&& options.stress_level >= options.stress_vix
&& economy.inflation_rate < central_bank.target_inflation + options.stress_inflation_gap
&& current_rate > 0.0
{
let steps = mathx::min(
4.0, 1.0 + ((options.stress_level - options.stress_vix) / 10.0).floor());
let cut = mathx::min(current_rate, options.stress_cut * steps);
if rate_change > -cut {
rate_change = -cut;
decision = if cut > 0.25 { Decision::EmergencyCut } else { Decision::Cut };
new_cb.hawkish_dovish_score = clamp(central_bank.hawkish_dovish_score - 0.2, -1.0, 1.0);
}
}
if options.stress_hold
&& rate_change > 0.0
&& economy.inflation_rate < central_bank.target_inflation + options.stress_inflation_gap
{
rate_change = 0.0;
decision = Decision::Hold;
new_cb.hawkish_dovish_score = central_bank.hawkish_dovish_score;
}
if options.hold_rate {
rate_change = 0.0;
decision = Decision::Hold;
new_cb.hawkish_dovish_score = central_bank.hawkish_dovish_score;
}
if rate_change > 0.0 {
rate_change *= urgency;
}
let ladder_change = rate_change;
let mut asked = 0.0;
let mut put_cut = 0.0;
let mut unwind = 0.0;
if put_on && !options.hold_rate && economy.inflation_rate < FED_PUT_INFLATION_CEILING {
asked = fed_put_ask(options.put_gain, options.put_threshold, economy.intermeeting_return);
put_cut = mathx::min((asked / 0.25).round() * 0.25, mathx::max(0.0, current_rate));
if put_cut > 0.0 && rate_change > -put_cut {
rate_change = -put_cut;
decision = Decision::Cut;
new_cb.hawkish_dovish_score = clamp(central_bank.hawkish_dovish_score - 0.2, -1.0, 1.0);
}
if economy.vix >= FED_PUT_HOLD_VIX && rate_change > 0.0 {
rate_change = 0.0;
decision = Decision::Hold;
new_cb.hawkish_dovish_score = central_bank.hawkish_dovish_score;
}
}
if put_on
&& !options.hold_rate
&& put_cut == 0.0
&& rate_change >= 0.0
&& ladder_change >= 0.0
&& economy.vix < FED_PUT_HOLD_VIX
&& economy.fed_put_owed - economy.fed_put > 0.125
&& !(options.stress_hold
&& economy.inflation_rate
< central_bank.target_inflation + options.stress_inflation_gap)
{
unwind = mathx::min(0.25, economy.fed_put_owed);
rate_change += unwind;
if decision == Decision::Hold {
decision = Decision::Hike;
}
}
if let Some(c) = options.eve_change {
if !is_emergency_meeting {
rate_change = c;
decision = if c > 0.0 {
Decision::Hike
} else if c < 0.0 {
Decision::Cut
} else {
Decision::Hold
};
}
}
let announcement_variant = (rng.next_f64() * ANNOUNCEMENT_VARIANTS).floor() as usize;
new_economy.federal_funds_rate = clamp(current_rate + rate_change, 0.0, 8.0);
new_economy.prime_rate = new_economy.federal_funds_rate + 3.0;
if put_on {
let ladder_rate = clamp(current_rate + ladder_change, 0.0, 8.0);
let taken = mathx::max(0.0, ladder_rate - new_economy.federal_funds_rate);
if unwind > 0.0 {
new_economy.fed_put_owed = mathx::max(0.0, economy.fed_put_owed - unwind);
} else if taken > 0.0 {
new_economy.fed_put_owed = economy.fed_put_owed + taken;
new_economy.fed_put = economy.fed_put + taken;
}
new_economy.intermeeting_return = if options.put_carry != 0.0
&& !options.hold_rate
&& economy.inflation_rate < FED_PUT_INFLATION_CEILING
&& new_economy.federal_funds_rate > 0.0
{
let cut = mathx::max(0.0, current_rate - new_economy.federal_funds_rate);
options.put_carry
* mathx::min(0.0, economy.intermeeting_return + cut / options.put_gain)
} else {
0.0
};
}
let surprise = if put_on && options.put_pricing != 0.0 {
rate_change + options.put_pricing * mathx::min(asked, mathx::max(0.0, current_rate))
} else {
rate_change
};
let path_on = options.path_gain != 0.0;
let owed_moved = if put_on { new_economy.fed_put_owed - economy.fed_put_owed } else { 0.0 };
let path_after = if path_on {
options.path_before
+ options.path_gain * match options.path_carry {
Some(carry) if options.path_owed_excluded => carry,
Some(carry) => carry + owed_moved,
None => new_economy.federal_funds_rate - current_rate + owed_moved,
}
} else {
0.0
};
let surprise = if path_on { surprise + (path_after - options.path_before) } else { surprise };
let news_2y = surprise;
let damp = options.rate_damping;
let surprise = if damp != 0.0 {
let ladder = (new_economy.federal_funds_rate - current_rate) + owed_moved
+ (path_after - options.path_before);
surprise - damp * ladder
} else {
surprise
};
let treasury_target_10y = new_economy.federal_funds_rate
+ 1.0
+ mathx::max(0.0, (curve_inflation - 2.0) * 0.3);
let treasury_target_10y = if path_on { treasury_target_10y + path_after } else { treasury_target_10y };
let treasury_target_10y = if damp != 0.0 {
let owed = if put_on { new_economy.fed_put_owed } else { 0.0 };
treasury_target_10y
- damp * (new_economy.federal_funds_rate + owed + path_after
- super::daily::TREASURY_NEUTRAL_RATE)
} else {
treasury_target_10y
};
let treasury_target_10y = if options.haven_gain != 0.0
&& curve_inflation < FED_PUT_INFLATION_CEILING
{
treasury_target_10y - super::daily::haven_term_cut(options.haven_gain, economy.vix)
} else {
treasury_target_10y
};
let treasury_target_10y = if options.zlb_path != 0.0 {
treasury_target_10y - options.zlb_path
} else {
treasury_target_10y
};
let treasury_target_10y = if options.zlb_haven_gain != 0.0
&& new_economy.federal_funds_rate <= super::daily::ZLB_RATE
&& curve_inflation < FED_PUT_INFLATION_CEILING
{
treasury_target_10y - super::daily::haven_term_cut(options.zlb_haven_gain, economy.vix)
} else {
treasury_target_10y
};
let treasury_target_10y = if let Some(x) = options.term_premium {
treasury_target_10y + x
} else {
treasury_target_10y
};
let treasury_target_10y = if options.treasury_equity_gain != 0.0 {
treasury_target_10y - options.treasury_equity_gain * economy.treasury_equity_priced_gap
} else {
treasury_target_10y
};
new_economy.treasury_yield_10y = clamp(
economy.treasury_yield_10y
+ (treasury_target_10y - economy.treasury_yield_10y) * 0.50
+ surprise * 0.5,
0.5,
12.0,
);
new_economy.treasury_yield_2y = if path_on {
(new_economy.federal_funds_rate + path_after) * 0.85 + new_economy.treasury_yield_10y * 0.15
} else {
new_economy.federal_funds_rate * 0.85 + new_economy.treasury_yield_10y * 0.15
};
if options.two_year_carry != 0.0 {
let legacy = new_economy.treasury_yield_2y;
let carried = economy.treasury_yield_2y
+ 0.85 * news_2y
+ 0.15 * (new_economy.treasury_yield_10y - economy.treasury_yield_10y);
new_economy.treasury_yield_2y =
clamp(legacy + options.two_year_carry * (carried - legacy), 0.0, 12.0);
}
let mortgage_spread = 1.5 + (economy.vix - 12.0) * 0.015;
let calculated_mortgage = new_economy.treasury_yield_10y + clamp(mortgage_spread, 1.5, 2.8);
new_economy.mortgage_rate_30y = mathx::max(new_economy.prime_rate + 0.5, calculated_mortgage);
new_economy.mortgage_rate_30y = mathx::max(
new_economy.treasury_yield_10y + 0.5,
new_economy.mortgage_rate_30y,
);
let final_mortgage_spread = new_economy.mortgage_rate_30y - new_economy.treasury_yield_10y;
if final_mortgage_spread > 3.5 {
new_economy.mortgage_rate_30y = new_economy.treasury_yield_10y + 3.5;
}
let spread_gap = if options.spread_priced_gap && options.spread_priced_gain != 0.0 {
economy.spread_equity_gap + options.spread_priced_gain / options.spread_equity_gain * economy.spread_priced_gap
} else if options.spread_priced_gap {
economy.spread_priced_gap
} else {
economy.spread_equity_gap
};
let base_corporate_spread = if let Some(x) = options.spread_noise {
1.0 + (economy.vix - 12.0) * 0.02 * (1.0 - options.spread_vix_cut)
+ (options.spread_equity_gain * spread_gap + x)
} else if options.spread_vix_cut != 0.0 || options.spread_equity_gain != 0.0 {
1.0 + (economy.vix - 12.0) * 0.02 * (1.0 - options.spread_vix_cut)
+ options.spread_equity_gain * spread_gap
} else {
1.0 + (economy.vix - 12.0) * 0.02
};
let cycle_spread_multiplier = match options.spread_multiplier {
Some(m) => m,
None => spread_multiplier_of(economy.cycle_phase),
};
let corporate_spread = base_corporate_spread * cycle_spread_multiplier;
let calculated_corp_yield = new_economy.treasury_yield_10y + clamp(corporate_spread, CORPORATE_SPREAD_FLOOR, 6.0);
new_economy.corporate_bond_yield = mathx::max(
new_economy.treasury_yield_10y + 0.3,
mathx::max(
new_economy.treasury_yield_10y + CORPORATE_SPREAD_FLOOR,
calculated_corp_yield,
),
);
if new_economy.federal_funds_rate <= 0.25 && economy.cycle_phase == CyclePhase::Contraction {
new_cb.qe_active = true;
new_cb.qe_monthly_purchases = 120.0;
} else if central_bank.qe_active && economy.cycle_phase == CyclePhase::Expansion {
new_cb.qe_monthly_purchases = mathx::max(0.0, central_bank.qe_monthly_purchases - 15.0);
if new_cb.qe_monthly_purchases <= 0.0 {
new_cb.qe_active = false;
}
}
if new_cb.qe_active && new_cb.qe_monthly_purchases > 0.0 {
let qe_suppression = (new_cb.qe_monthly_purchases / 120.0) * 0.50;
new_economy.treasury_yield_10y =
mathx::max(0.1, new_economy.treasury_yield_10y - qe_suppression / 252.0);
}
if new_cb.qe_active && new_cb.qe_monthly_purchases > 0.0 {
new_economy.qe_pe_boost = 0.10 * (new_cb.qe_monthly_purchases / 120.0);
new_economy.qe_assets_ratio +=
(new_cb.qe_monthly_purchases / 21.0) / QE_ASSETS_BASELINE_B;
} else {
new_economy.qe_pe_boost = 0.0;
}
new_cb.forward_guidance = if new_cb.hawkish_dovish_score > 0.3 {
ForwardGuidance::OngoingIncreases
} else if new_cb.hawkish_dovish_score < -0.3 {
ForwardGuidance::Accommodative
} else {
ForwardGuidance::AsAppropriate
};
new_cb.last_meeting_date = current_timestamp;
let inflation_crisis = new_economy.inflation_rate > new_economy.federal_funds_rate + 2.0
&& new_economy.inflation_rate > 4.0;
let cal = options.calendar;
new_cb.next_meeting_date = if inflation_crisis {
current_timestamp + cal.scale_days(21 + (rng.next_f64() * 10.0).floor() as i64) * 24 * 60
} else {
current_timestamp + cal.scale_days(42 + (rng.next_f64() * 14.0).floor() as i64) * 24 * 60
};
MeetingOutcome {
central_bank: new_cb,
economy: new_economy,
decision: Some(decision),
announcement_variant: Some(announcement_variant),
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::rng::GameRng;
fn meeting(growth: f64, rate: f64, inflation: f64, options: &PolicyOptions) -> Option<Decision> {
let bank = create_initial_central_bank_state(0);
let mut economy = create_initial_economy_state(&InitialEconomyOptions::default());
economy.cycle_phase = CyclePhase::Expansion;
economy.gdp_growth = growth;
economy.federal_funds_rate = rate;
economy.inflation_rate = inflation;
economy.unemployment_rate = bank.target_unemployment;
let mut rng = GameRng::new(1, 1);
update_central_bank_with(&bank, &economy, bank.next_meeting_date, &mut rng, options).decision
}
#[test]
fn the_core_reading_meeting_is_the_meeting_at_core() {
let bank = create_initial_central_bank_state(0);
let mut economy = create_initial_economy_state(&InitialEconomyOptions::default());
economy.cycle_phase = CyclePhase::Expansion;
economy.gdp_growth = 1.0;
economy.federal_funds_rate = 2.0;
economy.unemployment_rate = bank.target_unemployment;
economy.inflation_rate = 3.6;
let on = PolicyOptions { growth_cut: 2.0, policy_inflation: Some(2.0), ..PolicyOptions::shipped() };
let mut at_core = economy.clone();
at_core.inflation_rate = 2.0;
let plain = PolicyOptions { growth_cut: 2.0, curve_inflation: Some(3.6), ..PolicyOptions::shipped() };
let a = update_central_bank_with(&bank, &economy, bank.next_meeting_date, &mut GameRng::new(1, 1), &on);
let b = update_central_bank_with(&bank, &at_core, bank.next_meeting_date, &mut GameRng::new(1, 1), &plain);
assert_eq!(a.decision, Some(Decision::Cut));
assert_eq!(a.decision, b.decision);
assert_eq!(a.economy.federal_funds_rate.to_bits(), b.economy.federal_funds_rate.to_bits());
assert_eq!(a.economy.treasury_yield_10y.to_bits(), b.economy.treasury_yield_10y.to_bits());
assert_eq!(a.economy.inflation_rate.to_bits(), 3.6f64.to_bits());
let off = PolicyOptions { growth_cut: 2.0, ..PolicyOptions::shipped() };
let c = update_central_bank_with(&bank, &economy, bank.next_meeting_date, &mut GameRng::new(1, 1), &off);
assert_ne!(c.decision, Some(Decision::Cut));
}
#[test]
fn the_two_year_carry_keeps_the_gap_a_meeting_did_not_announce() {
let bank = create_initial_central_bank_state(0);
let mut economy = create_initial_economy_state(&InitialEconomyOptions::default());
economy.cycle_phase = CyclePhase::Expansion;
economy.gdp_growth = 2.5;
economy.federal_funds_rate = 2.0;
economy.inflation_rate = 2.0;
economy.unemployment_rate = bank.target_unemployment;
economy.treasury_yield_2y = 0.85 * 2.0 + 0.15 * economy.treasury_yield_10y + 0.4;
let run = |c: f64| {
let o = PolicyOptions { two_year_carry: c, ..PolicyOptions::shipped() };
let mut rng = GameRng::new(1, 1);
update_central_bank_with(&bank, &economy, bank.next_meeting_date, &mut rng, &o)
};
let (off, on, half) = (run(0.0), run(1.0), run(0.5));
assert_eq!(off.decision, Some(Decision::Hold));
assert_eq!(on.decision, off.decision);
assert_eq!(on.economy.federal_funds_rate.to_bits(), off.economy.federal_funds_rate.to_bits());
assert_eq!(on.economy.treasury_yield_10y.to_bits(), off.economy.treasury_yield_10y.to_bits());
let legacy = off.economy.federal_funds_rate * 0.85 + off.economy.treasury_yield_10y * 0.15;
assert_eq!(off.economy.treasury_yield_2y.to_bits(), legacy.to_bits());
let carried = economy.treasury_yield_2y
+ 0.15 * (on.economy.treasury_yield_10y - economy.treasury_yield_10y);
assert!((on.economy.treasury_yield_2y - carried).abs() < 1e-12);
assert!((half.economy.treasury_yield_2y - 0.5 * (legacy + carried)).abs() < 1e-12);
}
#[test]
fn the_neutral_real_rate_moves_the_rule_by_itself() {
let lift = PolicyOptions { liftoff: 1.0, ..PolicyOptions::shipped() };
let moved = PolicyOptions { neutral_real_rate: 2.0, ..lift };
let zero = PolicyOptions { neutral_real_rate: 0.0, ..lift };
assert_eq!(meeting(2.5, 2.0, 2.0, &lift), Some(Decision::Hold));
assert_eq!(meeting(2.5, 2.0, 2.0, &zero), Some(Decision::Hold));
assert_eq!(meeting(2.5, 2.0, 2.0, &moved), Some(Decision::Hike));
assert_eq!(meeting(2.5, 4.0, 2.0, &moved), Some(Decision::Hold));
}
#[test]
fn the_growth_cut_fires_under_its_dial_and_before_lift_off() {
let off = PolicyOptions::shipped();
let on = PolicyOptions { growth_cut: 2.0, ..PolicyOptions::shipped() };
assert_eq!(meeting(1.0, 2.0, 2.0, &off), Some(Decision::Hold));
assert_eq!(meeting(1.0, 2.0, 2.0, &on), Some(Decision::Cut));
{
let bank = create_initial_central_bank_state(0);
let mut economy = create_initial_economy_state(&InitialEconomyOptions::default());
economy.cycle_phase = CyclePhase::Expansion;
economy.gdp_growth = 1.0;
economy.federal_funds_rate = 2.0;
economy.inflation_rate = 2.0;
economy.unemployment_rate = bank.target_unemployment;
let mut rng = GameRng::new(1, 1);
let out = update_central_bank_with(&bank, &economy, bank.next_meeting_date, &mut rng, &on);
assert!((out.economy.federal_funds_rate - 1.75).abs() < 1e-12);
assert!((out.central_bank.hawkish_dovish_score + 0.2).abs() < 1e-12);
}
assert_eq!(meeting(2.0, 2.0, 2.0, &on), Some(Decision::Hold));
assert_eq!(meeting(2.5, 2.0, 2.0, &on), Some(Decision::Hold));
assert_eq!(meeting(1.0, 0.0, 2.0, &on), Some(Decision::Hold));
assert_ne!(meeting(1.0, 2.0, 3.6, &on), Some(Decision::Cut));
let deep = PolicyOptions { growth_cut: -0.5, ..PolicyOptions::shipped() };
assert_eq!(meeting(0.0, 2.0, 2.0, &deep), Some(Decision::Hold));
assert_eq!(meeting(-1.0, 2.0, 2.0, &deep), Some(Decision::Cut));
let liftoff = PolicyOptions { liftoff: 1.0, ..PolicyOptions::shipped() };
let both = PolicyOptions { liftoff: 1.0, growth_cut: 2.0, ..PolicyOptions::shipped() };
assert_eq!(meeting(1.0, 1.4, 2.0, &liftoff), Some(Decision::Hike));
assert_eq!(meeting(1.0, 1.4, 2.0, &both), Some(Decision::Cut));
assert_eq!(meeting(3.0, 1.4, 2.0, &both), Some(Decision::Hike));
}
}
#[cfg(test)]
mod stress_cut {
use super::*;
use crate::economy::state::{
create_initial_central_bank_state, create_initial_economy_state, InitialEconomyOptions,
};
struct Silent;
impl Rng for Silent {
fn next_f64(&mut self) -> f64 {
0.5
}
fn next_normal(&mut self) -> f64 {
0.0
}
}
fn meet(economy: &EconomyState, options: &PolicyOptions) -> MeetingOutcome {
let mut cb = create_initial_central_bank_state(0);
cb.next_meeting_date = -1;
update_central_bank_with(&cb, economy, 1000, &mut Silent, options)
}
fn calm() -> EconomyState {
let mut e = create_initial_economy_state(&InitialEconomyOptions::default());
e.inflation_rate = 2.0;
e.unemployment_rate = 4.0;
e.federal_funds_rate = 2.0;
e.gdp_growth = 2.0;
e
}
fn with(level: f64) -> PolicyOptions {
PolicyOptions { stress_cut: 0.25, stress_vix: 30.0, stress_level: level, ..PolicyOptions::shipped() }
}
#[test]
fn off_the_cut_reads_nothing_it_is_given() {
let e = calm();
let shipped = meet(&e, &PolicyOptions::shipped());
let off = PolicyOptions { stress_level: 80.0, stress_vix: 10.0, stress_inflation_gap: 9.0,
..PolicyOptions::shipped() };
assert_eq!(meet(&e, &off), shipped);
}
#[test]
fn the_cut_fires_at_the_stress_level_and_steps_every_ten_points() {
let e = calm();
let hold = meet(&e, &PolicyOptions::shipped());
assert_eq!(hold.decision, Some(Decision::Hold), "the calm economy holds");
assert_eq!(meet(&e, &with(29.99)), hold);
let cases = [(30.0, 0.25, Decision::Cut), (39.9, 0.25, Decision::Cut),
(40.0, 0.5, Decision::EmergencyCut), (49.9, 0.5, Decision::EmergencyCut),
(50.0, 0.75, Decision::EmergencyCut), (60.0, 1.0, Decision::EmergencyCut),
(200.0, 1.0, Decision::EmergencyCut)];
for (level, cut, decision) in cases {
let out = meet(&e, &with(level));
assert_eq!(out.decision, Some(decision), "level {level}");
assert!((out.economy.federal_funds_rate - (2.0 - cut)).abs() < 1e-12,
"level {level}: rate {}", out.economy.federal_funds_rate);
assert!((out.central_bank.hawkish_dovish_score - (-0.2)).abs() < 1e-12);
}
}
#[test]
fn the_cut_never_takes_the_rate_below_zero_and_waits_on_inflation() {
let mut e = calm();
e.federal_funds_rate = 0.3;
let out = meet(&e, &PolicyOptions { stress_cut: 0.5, ..with(60.0) });
assert_eq!(out.economy.federal_funds_rate, 0.0);
e.federal_funds_rate = 0.0;
assert_ne!(meet(&e, &with(60.0)).decision, Some(Decision::EmergencyCut));
let mut hot = calm();
hot.inflation_rate = 3.0;
let gated = meet(&hot, &with(45.0));
assert_eq!(gated, meet(&hot, &PolicyOptions::shipped()));
let wide = meet(&hot, &PolicyOptions { stress_inflation_gap: 2.0, ..with(45.0) });
assert!(wide.economy.federal_funds_rate < hot.federal_funds_rate);
}
#[test]
fn the_cut_replaces_a_hike_and_a_smaller_cut_but_not_a_deeper_one() {
let mut e = calm();
e.inflation_rate = 3.2;
e.federal_funds_rate = 1.0;
let ladder = meet(&e, &PolicyOptions::shipped());
assert!(ladder.economy.federal_funds_rate > 1.0, "the ladder hikes here");
let stressed = meet(&e, &PolicyOptions { stress_inflation_gap: 1.5, ..with(35.0) });
assert_eq!(stressed.decision, Some(Decision::Cut));
assert!((stressed.economy.federal_funds_rate - 0.75).abs() < 1e-12);
let mut slump = calm();
slump.gdp_growth = -3.0;
slump.unemployment_rate = 11.0;
slump.federal_funds_rate = 4.0;
let deep = meet(&slump, &PolicyOptions::shipped());
assert_eq!(meet(&slump, &with(35.0)), deep);
}
}
#[cfg(test)]
mod spread_dials {
use super::*;
use crate::economy::state::{
create_initial_central_bank_state, create_initial_economy_state, InitialEconomyOptions,
};
struct Silent;
impl Rng for Silent {
fn next_f64(&mut self) -> f64 {
0.5
}
fn next_normal(&mut self) -> f64 {
0.0
}
}
fn meet(economy: &EconomyState, options: &PolicyOptions) -> MeetingOutcome {
let mut cb = create_initial_central_bank_state(0);
cb.next_meeting_date = -1;
update_central_bank_with(&cb, economy, 1000, &mut Silent, options)
}
#[test]
fn the_formula_with_nothing_set_is_the_formula_bit_for_bit() {
for vix in [9.0, 12.0, 17.3, 31.0, 80.0] {
for m in [1.0, 1.1, 1.4, 2.8, 3.5] {
assert_eq!(spread_formula_with(vix, m, 0.0, 0.0).to_bits(),
spread_formula(vix, m).to_bits());
}
}
}
#[test]
fn the_cut_scales_the_slope_and_the_equity_term_joins_the_base() {
assert_eq!(spread_formula_with(45.0, 2.8, 1.0, 0.25), (1.25f64 * 2.8).min(6.0));
assert!((spread_formula_with(32.0, 1.4, 0.5, 0.1) - 1.3 * 1.4).abs() < 1e-12);
assert_eq!(spread_formula_with(12.0, 1.0, 0.0, -0.9), CORPORATE_SPREAD_FLOOR);
assert_eq!(spread_formula_with(80.0, 3.5, 0.0, 2.0), 6.0);
}
#[test]
fn the_meeting_re_anchors_to_the_gap_and_off_it_reads_nothing() {
let mut e = create_initial_economy_state(&InitialEconomyOptions::default());
e.inflation_rate = 2.0;
e.unemployment_rate = 4.0;
e.federal_funds_rate = 2.0;
e.vix = 30.0;
e.spread_equity_gap = 0.2;
let shipped = meet(&e, &PolicyOptions::shipped());
let mut no_gap = e.clone();
no_gap.spread_equity_gap = 0.0;
let mut off = meet(&no_gap, &PolicyOptions::shipped());
off.economy.spread_equity_gap = 0.2;
assert_eq!(off, shipped);
let on = PolicyOptions { spread_vix_cut: 1.0, spread_equity_gain: 2.0, ..PolicyOptions::shipped() };
let out = meet(&e, &on);
let m = spread_multiplier_of(e.cycle_phase);
let want = clamp((1.0 + 2.0 * 0.2) * m, CORPORATE_SPREAD_FLOOR, 6.0);
let got = out.economy.corporate_bond_yield - out.economy.treasury_yield_10y;
assert!((got - want).abs() < 1e-12, "spread {got} against {want}");
assert_eq!(out.economy.treasury_yield_10y, shipped.economy.treasury_yield_10y);
assert_eq!(out.economy.federal_funds_rate, shipped.economy.federal_funds_rate);
}
}
#[cfg(test)]
mod put_carry {
use super::*;
use crate::economy::state::{
create_initial_central_bank_state, create_initial_economy_state, InitialEconomyOptions,
};
struct Silent;
impl Rng for Silent {
fn next_f64(&mut self) -> f64 {
0.5
}
fn next_normal(&mut self) -> f64 {
0.0
}
}
fn meet(economy: &EconomyState, options: &PolicyOptions) -> MeetingOutcome {
let mut cb = create_initial_central_bank_state(0);
cb.next_meeting_date = -1;
update_central_bank_with(&cb, economy, 1000, &mut Silent, options)
}
fn calm(intermeeting: f64) -> EconomyState {
let mut e = create_initial_economy_state(&InitialEconomyOptions::default());
e.inflation_rate = 2.0;
e.unemployment_rate = 4.0;
e.federal_funds_rate = 2.0;
e.gdp_growth = 2.0;
e.intermeeting_return = intermeeting;
e
}
fn put(carry: f64) -> PolicyOptions {
PolicyOptions { put_gain: 3.0, put_carry: carry, ..PolicyOptions::shipped() }
}
#[test]
fn off_the_clock_restarts_at_zero() {
for i in [-0.03, -0.06, -0.2, 0.04] {
let out = meet(&calm(i), &put(0.0));
assert_eq!(out.economy.intermeeting_return, 0.0, "I {i}");
}
}
#[test]
fn an_unanswered_fall_is_carried_and_a_cut_answers_its_share() {
let first = meet(&calm(-0.03), &put(1.0));
assert_eq!(first.economy.federal_funds_rate, 2.0);
assert!((first.economy.intermeeting_return + 0.03).abs() < 1e-12);
let second = meet(&calm(first.economy.intermeeting_return - 0.03), &put(1.0));
assert!((second.economy.federal_funds_rate - 1.75).abs() < 1e-12);
assert_eq!(second.economy.intermeeting_return, 0.0);
let big = meet(&calm(-0.10), &put(1.0));
assert!((big.economy.federal_funds_rate - 1.75).abs() < 1e-12);
assert!((big.economy.intermeeting_return - (-0.10 + 0.25 / 3.0)).abs() < 1e-12);
let half = meet(&calm(-0.03), &put(0.5));
assert!((half.economy.intermeeting_return + 0.015).abs() < 1e-12);
assert_eq!(meet(&calm(0.04), &put(1.0)).economy.intermeeting_return, 0.0);
}
#[test]
fn the_carry_stops_at_the_floor_the_ceiling_and_a_pin() {
let mut floor = calm(-0.5);
floor.federal_funds_rate = 0.25;
let out = meet(&floor, &put(1.0));
assert_eq!(out.economy.federal_funds_rate, 0.0);
assert_eq!(out.economy.intermeeting_return, 0.0);
let mut hot = calm(-0.03);
hot.inflation_rate = FED_PUT_INFLATION_CEILING;
assert_eq!(meet(&hot, &put(1.0)).economy.intermeeting_return, 0.0);
let pinned = PolicyOptions { hold_rate: true, ..put(1.0) };
assert_eq!(meet(&calm(-0.03), &pinned).economy.intermeeting_return, 0.0);
for i in [-0.03, -0.1, 0.02] {
let mut on = meet(&calm(i), &put(1.0));
let off = meet(&calm(i), &put(0.0));
on.economy.intermeeting_return = 0.0;
assert_eq!(on, off, "I {i}");
}
}
}