-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Expand file tree
/
Copy pathtreynor_ratio.rs
More file actions
35 lines (32 loc) · 1.02 KB
/
treynor_ratio.rs
File metadata and controls
35 lines (32 loc) · 1.02 KB
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
/// Calculates the Treynor Ratio for a portfolio.
///
/// # Inputs
/// - `portfolio_return`: Portfolio return
/// - `risk_free_rate`: Risk-free rate
/// - `beta`: Portfolio beta
/// where Beta is a financial metric that measures the systematic risk of a security or portfolio compared to the overall market.
///
/// # Output
/// - Returns excess return per unit of market risk
pub fn treynor_ratio(portfolio_return: f64, risk_free_rate: f64, beta: f64) -> f64 {
if beta == 0.0 {
f64::NAN
} else {
(portfolio_return - risk_free_rate) / beta
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_treynor_ratio() {
// for portfolio_return = 0.10, risk_free_rate = 0.05, beta = 1.5
// expected result: (0.10 - 0.05) / 1.5 = 0.033333...
assert!((treynor_ratio(0.10, 0.05, 1.50) - 0.03333).abs() < 0.01);
}
#[test]
fn test_treynor_ratio_empty_beta() {
// test for zero beta (undefined ratio)
assert!(treynor_ratio(0.10, 0.05, 0.00).is_nan());
}
}