Pharos
TAMath transform

EXP - 指数函数

函数说明

计算数组中每个元素的指数值(e的x次方)。

语法

python
result = TA.EXP(records)

参数

参数名类型说明
recordsarray数值数组

返回值

返回指数值数组

计算方法

对输入数组的每个元素x,返回 e^x(e ≈ 2.71828)

使用场景

  1. 对数收益率转价格(最常用)
  2. 指数增长模型
  3. 复利计算
  4. 对数变换的反变换

基础示例

python
import math

def main():
    records = exchange.GetRecords()
    if len(records) < 2:
        return
    
    # 从对数收益率恢复价格
    log_returns = [0.01, 0.02, -0.015, 0.03]  # 对数收益率
    
    # 累计对数收益率
    cumulative_log_return = sum(log_returns)
    
    # 转换为总收益率
    total_return = math.exp(cumulative_log_return) - 1
    Log(f"总收益率: {total_return * 100:.2f}%")

高级应用

1. 对数收益率转价格变化

python
def log_return_to_price():
    records = exchange.GetRecords()
    prices = [r['Close'] for r in records[-100:]]
    
    # 计算对数收益率
    log_returns = [math.log(prices[i] / prices[i-1]) 
                   for i in range(1, len(prices))]
    
    # 重建价格序列
    reconstructed_prices = [prices[0]]
    for log_ret in log_returns:
        # exp(log_return) = price_ratio
        next_price = reconstructed_prices[-1] * math.exp(log_ret)
        reconstructed_prices.append(next_price)
    
    # 验证重建准确性
    error = abs(reconstructed_prices[-1] - prices[-1])
    Log(f"重建误差: {error:.8f}")

2. 复利计算

python
def compound_interest():
    # 年化收益率5%,持有10年
    annual_rate = 0.05
    years = 10
    
    # 连续复利最终值
    # FV = PV * e^(r*t)
    initial_value = 10000
    final_value = initial_value * math.exp(annual_rate * years)
    
    Log(f"初始: {initial_value}, 最终: {final_value:.2f}")
    Log(f"收益: {(final_value/initial_value - 1) * 100:.2f}%")

3. 指数加权移动平均(手动实现)

python
def exponential_weighted_ma():
    records = exchange.GetRecords()
    prices = [r['Close'] for r in records[-100:]]
    
    # EMA的alpha参数
    period = 20
    alpha = 2 / (period + 1)
    
    # 使用指数衰减权重
    ema = prices[0]
    for price in prices[1:]:
        ema = alpha * price + (1 - alpha) * ema
    
    Log(f"EMA({period}): {ema:.2f}")
    return ema

4. 波动率预测(GARCH模型简化)

python
def volatility_forecast():
    records = exchange.GetRecords()
    prices = [r['Close'] for r in records[-252:]]
    
    # 对数收益率
    log_returns = [math.log(prices[i] / prices[i-1]) 
                   for i in range(1, len(prices))]
    
    # 简单GARCH:波动率持续性
    squared_returns = [r**2 for r in log_returns]
    
    # 指数加权
    alpha = 0.94
    ewma_variance = squared_returns[0]
    
    for sq_ret in squared_returns[1:]:
        ewma_variance = alpha * ewma_variance + (1 - alpha) * sq_ret
    
    # 预测波动率
    forecast_volatility = math.sqrt(ewma_variance) * math.sqrt(252)
    Log(f"预测年化波动率: {forecast_volatility * 100:.2f}%")

5. 期权定价(Black-Scholes简化)

python
def option_value_component():
    # 无风险利率
    r = 0.05
    # 到期时间(年)
    T = 0.5
    
    # 折现因子
    discount_factor = math.exp(-r * T)
    
    Log(f"半年期折现因子: {discount_factor:.4f}")
    
    # 如果期权内在价值为100
    intrinsic_value = 100
    present_value = intrinsic_value * discount_factor
    Log(f"现值: {present_value:.2f}")

注意事项

  • exp(0) = 1
  • exp(1) ≈ 2.71828
  • exp(ln(x)) = x(互为反函数)
  • 增长速度极快,注意数值溢出
  • Python中可使用 math.exp() 替代

相关函数

  • LN - 自然对数(exp的反函数)
  • LOG10 - 常用对数
  • EMA - 指数移动平均