本文通过通俗易懂的方式介绍凯利准则(Kelly Criterion)的核心思想及其在投资中的应用,包括理论基础、实现方法和实际案例。
如果想更加全面清晰地了解金融资产组合模型进化论的体系架构,可参考:
0. 金融资产组合模型进化全图鉴
理解凯利准则,需要从以下几个维度进行分析:
import numpy as np
import pandas as pd
class KellyCriterion:
def __init__(self):
pass
def calculate_kelly_fraction(self, win_prob, win_loss_ratio):
"""
计算单资产凯利比例
win_prob: 获胜概率
win_loss_ratio: 盈亏比
"""
# 凯利公式:f = p - (1-p)/R
# f: 投资比例
# p: 获胜概率
# R: 盈亏比
kelly_fraction = win_prob - (1 - win_prob) / win_loss_ratio
# 限制在[0,1]范围内
kelly_fraction = np.clip(kelly_fraction, 0, 1)
return kelly_fraction
def estimate_parameters(self, returns):
"""
从历史数据估计参数
"""
win_prob = np.mean(returns > 0)
positive_returns = returns[returns > 0]
negative_returns = np.abs(returns[returns < 0])
if len(negative_returns) == 0:
win_loss_ratio = np.inf
else:
win_loss_ratio = np.mean(positive_returns) / np.mean(negative_returns)
return {
'win_probability': win_prob,
'win_loss_ratio': win_loss_ratio
}
class MultiAssetKelly:
def __init__(self):
pass
def optimize_portfolio(self, returns_df, risk_free_rate=0):
"""
多资产凯利组合优化
"""
# 计算超额收益
excess_returns = returns_df - risk_free_rate
# 计算均值和协方差
mu = excess_returns.mean()
sigma = excess_returns.cov()
# 优化目标函数(最大化对数期望增长率)
def objective(weights):
portfolio_return = np.sum(weights * mu)
portfolio_variance = np.dot(weights.T, np.dot(sigma, weights))
# 对数期望增长率
growth_rate = portfolio_return - 0.5 * portfolio_variance
return -growth_rate # 最小化负增长率
# 约束条件
from scipy.optimize import minimize
constraints = [
{'type': 'eq', 'fun': lambda x: np.sum(x) - 1}, # 权重和为1
{'type': 'ineq', 'fun': lambda x: x} # 权重非负
]
# 初始猜测
n_assets = len(returns_df.columns)
x0 = np.ones(n_assets) / n_assets
# 优化
result = minimize(objective, x0, constraints=constraints)
return pd.Series(result.x, index=returns_df.columns)
class FractionalKelly:
def __init__(self, fraction=0.5):
"""
fraction: 完全凯利比例的分数,通常取0.5
"""
self.fraction = fraction
def calculate_position_size(self, kelly_fraction, capital):
"""
计算实际仓位大小
"""
# 使用分数凯利以降低风险
fractional_kelly = kelly_fraction * self.fraction
position_size = capital * fractional_kelly
return position_size
def simulate_strategy(self, returns, initial_capital, kelly_fraction):
"""
模拟分数凯利策略
"""
capital = np.zeros(len(returns) + 1)
capital[0] = initial_capital
for i in range(len(returns)):
# 计算仓位
position = self.calculate_position_size(
kelly_fraction, capital[i]
)
# 更新资金
capital[i+1] = capital[i] + position * returns[i]
return capital
class DynamicKelly:
def __init__(self, window_size=252):
self.window_size = window_size
def calculate_dynamic_fraction(self, returns, min_window=30):
"""
动态计算凯利比例
"""
kelly_fractions = np.zeros(len(returns))
kelly = KellyCriterion()
for i in range(len(returns)):
if i < min_window:
kelly_fractions[i] = 0
continue
# 使用滚动窗口
window = returns[max(0, i-self.window_size):i]
params = kelly.estimate_parameters(window)
kelly_fractions[i] = kelly.calculate_kelly_fraction(
params['win_probability'],
params['win_loss_ratio']
)
return kelly_fractions
def add_volatility_adjustment(self, kelly_fractions, returns,
target_vol=0.15):
"""
根据波动率调整凯利比例
"""
rolling_vol = pd.Series(returns).rolling(
self.window_size
).std() * np.sqrt(252)
# 波动率调整因子
vol_adjustment = target_vol / rolling_vol
# 调整凯利比例
adjusted_fractions = kelly_fractions * vol_adjustment
# 限制在合理范围内
adjusted_fractions = np.clip(adjusted_fractions, 0, 1)
return adjusted_fractions
class KellyStrategyEvaluator:
def __init__(self):
pass
def evaluate_performance(self, capital_history):
"""
评估策略表现
"""
returns = np.diff(capital_history) / capital_history[:-1]
metrics = {
'total_return': capital_history[-1] / capital_history[0] - 1,
'annual_return': np.mean(returns) * 252,
'annual_volatility': np.std(returns) * np.sqrt(252),
'sharpe_ratio': np.mean(returns) / np.std(returns) * np.sqrt(252),
'max_drawdown': self.calculate_max_drawdown(capital_history)
}
return metrics
def calculate_max_drawdown(self, capital_history):
"""
计算最大回撤
"""
peaks = np.maximum.accumulate(capital_history)
drawdowns = (peaks - capital_history) / peaks
return np.max(drawdowns)
凯利准则的核心思想是通过最大化财富的期望增长率来确定最优投资比例。可以把它想象成:
关键技术点:
实践建议:
通过合理运用凯利准则,我们可以在追求高收益的同时,实现科学的风险控制,构建更稳健的投资策略。但要注意,实际应用中需要考虑估计误差、市场流动性等现实因素。