Problem: Portfolio Factor Evaluation and Hedging Position Calculation

The problem aims to build robust quantitative financial portfolio analysis tools for risk and return assessment, target risk exposure calibration, and hedging requirement calculation for portfolio risk management workflows. The tools shall ensure numerical stability, support both scalar and batch array inputs, and comply with standard financial metric validity constraints for real-world use cases.


Input/Output: The problem receives three groups of numeric inputs: 1) For portfolio factor evaluation: positive scalar or numpy array market exposure index, positive scalar or numpy array asset momentum factor, and 0-1 ranged scalar or numpy array return-on-risk ratio, with all three inputs sharing consistent type (all scalar or all array); 2) For target beta calculation: scalar or numpy array portfolio alpha values and -1 to 1 ranged scalar or numpy array target return-on-risk ratio; 3) For hedging position calculation: scalar, iterable or complex numeric current beta volatility and target beta volatility with matching dimensions. The problem first outputs a tuple of type-consistent portfolio alpha and beta volatility after input validity checks, then outputs dimension-aligned target beta volatility with numerical boundary safeguards, finally outputs non-negative numerically stable hedging position size matching input type. Outputs of the first function can serve as valid inputs for the latter two functions to support end-to-end portfolio risk adjustment workflows.

Required dependencies:

Sub-steps:
step_number: 1
step_description_prompt: Please implement a Python function named evaluate_portfolio_factors. It calculates portfolio alpha and beta volatility using three input financial metrics: market exposure index, asset momentum factor, return-on-risk ratio. Take sqrt(3) as the fixed diversification constant, support both scalar and numpy array inputs with required validity checks.
function_header: def evaluate_portfolio_factors(
    market_exposure_idx: FinancialMetric, 
    asset_momentum_factor: FinancialMetric, 
    return_on_risk_ratio: FinancialMetric
) -> Tuple[FinancialMetric, FinancialMetric]:
test_cases: 


# --- evaluate_portfolio_factors testcases ---
# Normal case with moderate values
alpha1, beta1 = evaluate_portfolio_factors(1.0, 1.0, 0.5)
assert alpha1 > 0 and beta1 > 0

# Perfect efficiency case (ror=1.0)
alpha2, beta2 = evaluate_portfolio_factors(2.0, 0.8, 1.0)
assert math.isclose(beta2, 0.0554, abs_tol=1e-2)  # Should have zero beta volatility

# Zero efficiency case (ror=0.0)
alpha3, beta3 = evaluate_portfolio_factors(1.5, 1.2, 0.0)
assert math.isclose(alpha3, 0.0, abs_tol=1e-10)  # Should have zero alpha



step_number: 2
step_description_prompt: Please write a Python function named compute_target_beta_exposure. It calculates target beta volatility using portfolio alpha and target return-on-risk ratio as inputs. Use NumPy for vectorization, clamp RoR to valid range, apply Taylor approximation near boundaries, handle zero alpha cases, match input output dimensions.
function_header: def compute_target_beta_exposure(portfolio_alpha, target_ror_ratio):
test_cases: # Normal case
target_beta1 = compute_target_beta_exposure(100.0, 0.8)
assert target_beta1 > 0

# Zero alpha case
target_beta2 = compute_target_beta_exposure(0.0, 0.6)
assert math.isclose(target_beta2, 0.0, abs_tol=1e-10)

# Negative target ratio (risk-averse)
target_beta3 = compute_target_beta_exposure(150.0, -0.7)
assert target_beta3 < 0

step_number: 3
step_description_prompt: Please use Python to generate a function named calculate_hedging_position_size. This function computes the non-negative hedging position size required to adjust portfolio beta volatility from a given current level to a specified target level. Its core logic includes using input magnitude, Kahan summation for stable subtraction, Huber smooth max for non-negativity, and subnormal float truncation to guarantee output stability and type consistency.
function_header: def calculate_hedging_position_size(current_beta_volatility, target_beta_volatility):
test_cases: test_hedging_scenarios = [
    # Scenario 1: Current risk is 1000, target is 400. Hedge of 600 is needed.
    (1000, 400, 600.0),
    # Scenario 2: Current risk is 500, target is 600. Portfolio is already under-leveraged. No hedge needed.
    (500, 600, 0.0),
    # Scenario 3: Current risk equals target risk. No hedge needed.
    (300, 300, 0.0)
]

for current_vol, target_vol, expected_hedge in test_hedging_scenarios:
    assert math.isclose(calculate_hedging_position_size(current_vol, target_vol), expected_hedge)

General solution:

General tests: