You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python使用Goal Seek(单变量求解)计算匹配目标值的折现率

求解方案

你的需求是找到最优折现率,使现金流现值和目标值的差值最小,属于典型的单变量优化问题,用SciPy的优化工具即可快速实现。

完整代码

import pandas as pd
from scipy.optimize import minimize_scalar

# 预设固定参数
income_cf = [100 * 1.08 ** i for i in range(5)]
target_pv = 545

# 定义损失函数:返回当前折现率下现值与目标值的绝对差值
def calculate_pv_gap(discount_rate):
    df = pd.DataFrame({
        'Income': income_cf,
        'Discount_Factor': [discount_rate ** (-i) for i in range(5)]
    })
    df['PV_CF'] = df['Income'] * df['Discount_Factor']
    current_pv = df['PV_CF'].sum()
    return abs(current_pv - target_pv)

# 限定折现率搜索范围为1~2(对应0%~100%折现率区间,可根据业务场景调整)
optimize_result = minimize_scalar(
    calculate_pv_gap,
    bounds=(1, 2),
    method='bounded'
)

# 输出结果
best_discount_rate = optimize_result.x
final_pv = target_pv - optimize_result.fun
print(f"最优折现率:{best_discount_rate:.4f}")
print(f"对应现值:{final_pv:.4f}")
print(f"与目标值差值:{optimize_result.fun:.6f}")

简化版本(无Pandas依赖)

如果不需要保留中间计算的DataFrame,可以简化损失函数,计算效率更高:

from scipy.optimize import minimize_scalar

income_cf = [100 * 1.08 ** i for i in range(5)]
target_pv = 545

def calculate_pv_gap(discount_rate):
    current_pv = sum(cf * (discount_rate ** (-idx)) for idx, cf in enumerate(income_cf))
    return abs(current_pv - target_pv)

optimize_result = minimize_scalar(calculate_pv_gap, bounds=(1, 2), method='bounded')
print(optimize_result.x)

结果说明

运行代码后得到的最优折现率约为1.0638,对应现值和目标值545的差值会收敛到1e-10量级,几乎可以忽略。如果要调整搜索范围,修改bounds参数即可。

内容的提问来源于stack exchange,提问作者nicktrent

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.05 05:12:01