优化
均值-方差目标、风险平价以及带诊断信息的有效前沿构建。
面向任务的示例参见使用指南。以下签名由与本站点其余内容同一提交的公共头文件生成。
-
struct efficient_frontier_result
有效前沿点与端点组合。
公共成员
-
std::vector<double> frontier_returns
各点的年化预期收益,从最小方差收益到最大收益端点。
-
std::vector<double> frontier_volatilities
各点的年化波动率。
-
std::vector<double> frontier_sharpe
各点的夏普比率;波动率可忽略时取 0。
-
std::vector<std::vector<double>> frontier_weights
各点的资产权重,按 asset_names 顺序排列。
-
portfolio_point min_variance
最小方差端点组合。
-
portfolio_point max_sharpe
最大夏普组合。
-
double max_sharpe_ratio = 0.0
max_sharpe 的夏普比率;波动率可忽略时取 0。
-
std::vector<std::string> asset_names
资产名称,按收益率列顺序排列。
-
double periods_per_year = 252.0
从配置复制的年化因子。
-
frontier_point_diagnostics min_variance_diagnostics
最小方差端点的残差。
-
frontier_point_diagnostics max_sharpe_diagnostics
最大夏普组合的残差。
-
std::vector<frontier_point_diagnostics> frontier_diagnostics
逐点残差,与 frontier_returns 一一对应。
-
std::vector<double> frontier_returns
-
struct frontier_config
有效前沿计算设置。
-
struct frontier_point_diagnostics
针对单个前沿点测得的约束残差。
公共成员
-
frontier_solver solver = frontier_solver::projected_gradient
该点使用的求解器。
-
double sum_residual = 0.0
权重之和相对 1 的绝对偏差。
-
double lower_bound_residual = 0.0
最大的下界违反量;可行时为 0。
-
double upper_bound_residual = 0.0
最大的上界违反量;可行时为 0。
-
double target_return_residual = 0.0
目标收益的绝对偏差;未设定目标时为 0。
-
frontier_solver solver = frontier_solver::projected_gradient
-
struct portfolio_optimization_config
optimize_portfolio 的输入。
公共成员
-
portfolio_objective objective = portfolio_objective::max_sharpe
要优化的目标。
-
double risk_free_rate = 0.0
max_sharpe 与所报告夏普比率使用的年化无风险利率;必须为有限值。
-
std::optional<double> target_return
target_return 所需的年化预期收益;使用时必须为有限值。
-
std::optional<double> target_volatility
target_risk 所需的年化波动率;必须有限且可达。
-
bool short_allowed = false
当 min_weight 未设置时,允许权重下探至 -max_weight。
不带 sector_constraints 的 min_variance 在单位单纯形上求解,因此无论该标志如何设置,这一组合都会返回长仓书。
-
double max_weight = 1.0
单资产上界;必须有限且大于 0。
-
std::optional<double> min_weight
可选的单资产下界;不得超过 max_weight,并覆盖 short_allowed 默认值。
-
std::vector<sector_allocation_constraint> sector_constraints
行业权重上下界;需要 sector_map,未映射任何资产的行业将被忽略。
-
std::map<std::string, std::string> sector_map
sector_constraints 使用的资产名到行业名映射。
-
portfolio_objective objective = portfolio_objective::max_sharpe
-
struct portfolio_optimization_result
优化后的权重及其年化汇总统计。
公共成员
-
std::vector<double> weights
按 asset_names 顺序排列的资产权重;其和为 1。
-
double expected_return = 0.0
年化预期收益,按每年 252 个周期计算。
-
double volatility = 0.0
年化波动率,按每年 252 个周期计算。
-
double sharpe = 0.0
(expected_return - risk_free_rate) / volatility;波动率可忽略时取 0。
-
std::vector<std::string> asset_names
资产名称,按收益率列顺序排列。
-
portfolio_objective objective = portfolio_objective::max_sharpe
产生该结果的优化目标。
-
std::vector<double> weights
-
struct portfolio_point
有效前沿上的一个组合。
-
struct risk_parity_result
风险平价权重及其风险贡献。
-
struct sector_allocation_constraint
映射到同一行业的资产权重之和的上下界。
-
enum class fincore::frontier_solver
生成某个前沿点的求解器。
取值:
-
enumerator min_variance_degenerate
由于可达收益区间退化,每个点都复用最小方差权重。
-
enumerator min_variance
第一个点,按方差最小化求解。
-
enumerator projected_gradient
内部目标收益点或最大夏普组合,由投影梯度下降求解。
-
enumerator bounded_linear_max_return
最后一个点,按预期收益降序贪心填充至权重上界。
-
enumerator min_variance_degenerate
-
enum class fincore::portfolio_objective
组合优化目标。
取值:
-
enumerator max_sharpe
使用 risk_free_rate 最大化夏普比率。
-
enumerator min_variance
最小化组合方差。
-
enumerator target_return
在预期收益等于 target_return 的约束下最小化方差。
-
enumerator target_risk
在波动率等于 target_volatility 的约束下最大化预期收益。
-
enumerator max_sharpe
-
efficient_frontier_result fincore::efficient_frontier(const dataframe &returns, const frontier_config &config = {})
基于日简单收益率计算有效前沿。
各点在最小方差组合与最大收益端点之间按预期收益均匀分布;最大收益端点按平均收益降序将资产填充至权重上界构造。收益与波动率使用 config.periods_per_year 年化,夏普比率使用 config.risk_free_rate。求解器在 frontier_diagnostics 中报告约束残差,而不报告收敛状态。
- 参数:
returns -- Daily simple returns with one finite double column per asset and at least two observations.
config -- Frontier settings;
n_pointsmust be at least 2.
- 抛出:
input_error -- If
returnsis empty or too short, fewer than two assets are present, values are non-finite, or a configuration value is invalid.optimization_error -- If the covariance matrix is not positive semidefinite, the weight bounds cannot fund a fully invested portfolio, or a solver returns infeasible weights.
- 返回:
Frontier points, endpoint portfolios and per-point diagnostics.
-
portfolio_optimization_result fincore::optimize_portfolio(const dataframe &returns, const portfolio_optimization_config &config = {})
针对某一个受支持的目标优化组合权重。
收益率按固定 252 周期年化。行业约束仅适用于 sector_map 中列出的资产,且不可行的目标通过抛出异常报告,而不是通过状态字段。
- 参数:
returns -- Daily simple returns with one finite double column per asset and at least two observations.
config -- Objective, bounds and optional sector constraints.
- 抛出:
input_error -- If
returnsis empty, too short or non-finite, an objective-specific target is missing, weight bounds are invalid, sector constraints lack a sector map, or the objective is unknown.optimization_error -- If the weight bounds cannot produce a fully invested portfolio,
target_volatilitylies outside the attainable range, or the solver returns infeasible weights.
- 返回:
Optimized weights, annualized return, volatility, Sharpe ratio and asset names.
-
risk_parity_result fincore::risk_parity(const dataframe &returns, std::optional<std::vector<double>> risk_budget = std::nullopt, std::size_t max_iter = 1000U)
使用等权重或用户给定的风险预算计算风险平价权重。
求解器最小化实际风险贡献份额与归一化风险预算之间的平方偏差,然后重新归一化权重使其和为 1。收益率与波动率使用固定 252 周期年化。求解期间权重被限制在 [1e-6, 1.0]。
- 参数:
returns -- Daily simple returns with one finite double column per asset and at least two observations.
risk_budget -- Optional per-asset budget with finite, non-negative entries and a positive sum; it is normalized internally. A uniform budget is used when omitted.
max_iter -- Maximum projected-gradient iterations per penalty stage; must be at least 1.
- 抛出:
input_error -- If
returnsis empty, too short or non-finite,max_iteris zero, or the risk budget is malformed.optimization_error -- If the solved weights cannot be normalized to a positive sum.
- 返回:
Risk-parity weights, risk contributions, annualized volatility and asset names.