Create kelly_formula.py

This commit is contained in:
guanjihuan 2024-11-21 12:30:46 +08:00
parent 67d4c9e5a4
commit d2b76e2cd7

View File

@ -0,0 +1,49 @@
"""
This code is supported by the website: https://www.guanjihuan.com
The newest version of this code is on the web page: https://www.guanjihuan.com/archives/43508
"""
import numpy as np
import matplotlib.pyplot as plt
investment_ratio_array = np.arange(0.1, 1.1, 0.1)
investment_times = 1000
test_times = 100
# 几个例子https://www.guanjihuan.com/archives/43412
# 例子2的参数
p = 0.6 # 胜率
b = 1 # 收益
a = 1 # 损失
# # 例子3的参数
# p = 0.5
# b = 1
# a = 0.5
win_array = [] # 胜出的仓位
for i0 in range(test_times):
# print(i0)
capital_array = []
for f in investment_ratio_array:
capital = 1
for _ in range(investment_times):
investment = capital*f
if investment>0:
random_value = np.random.uniform(0, 1)
if random_value<p:
capital = capital+investment*b
else:
capital = capital-investment*a
capital_array.append(capital)
max_capital_index = capital_array.index(max(capital_array))
win_array.append(investment_ratio_array[max_capital_index])
def kelly_formula(p, b, a):
f=(p/a)-((1-p)/b)
return f
print(kelly_formula(p=p, b=b, a=a))
plt.hist(win_array, bins=100, color='skyblue')
plt.show()