一只股票0.5的概率为1.1,0.5的概率为0.9,求期望收益。
假设本金10000,0.5的概率涨跌,本金100元退市停止模拟。
期望收益=0.51.1+0.50.9=0。
所以游戏好似能永远玩下去。
情况1:数学期望为0
半仓(h)变化类型 h *= 1.1 、h*=0.9
import numpy as np
import matplotlib.pyplot as plt
def simulate_stock(price_change_probability, initial_capital, rounds, stop_loss):
capital_values = [initial_capital]
current_capital = initial_capital
for _ in range(rounds):
# Randomly decide if the stock goes up or down
h=h2=current_capital/2
if np.random.choice([True, False], p=[price_change_probability, 1-price_change_probability]):
h *= 1.1 # Increase by 1%
else:
h*=0.9
# h *= 1/1.1+1e-1000 # Decrease by 1%
current_capital=h+h2
# Record the current capital
capital_values.append(current_capital)
# Check if we hit the stop loss
if current_capital <= stop_loss:
break
return capital_values
# Simulation parameters
price_change_probability = 0.5 # 50% chance of going up or down
initial_capital = 10000 # Starting capital
rounds = 10000 # Number of rounds to simulate
stop_loss = 100 # Stop loss at $100
# f=p/a-q/b p=q=0.5 a=b=0.1
# Run the simulation
capital_values = simulate_stock(price_change_probability, initial_capital, rounds, stop_loss)
# Plot the results
plt.figure(figsize=(14, 7))
plt.plot(capital_values, label='Capital Over Time')
plt.axhline(y=initial_capital, color='r', linestyle='--', label='Initial Capital')
plt.axhline(y=stop_loss, color='g', linestyle='--', label='Stop Loss')
plt.title('Capital Over Time with Random Stock Price Changes')
plt.xlabel('Round')
plt.ylabel('Capital')
plt.legend()
plt.grid(True)
plt.show()
# Print the final capital value
print(f"Final capital: ${capital_values[-1]:.2f}")
在时间足够长的情况下,模拟6场结局,都是退市。期望收益为0的情况下是凯利公式是不能存在永久玩下去的情况的。
情况2:先跌后涨可持平,期望约为1.0045.
期望收益=0.5*1.1+0.5*1/1.1=1.0045454545454546
半仓(h)变化类型 h *= 1.1 、h*=1/1.1
import numpy as np
import matplotlib.pyplot as plt
def simulate_stock(price_change_probability, initial_capital, rounds, stop_loss):
capital_values = [initial_capital]
current_capital = initial_capital
for _ in range(rounds):
# Randomly decide if the stock goes up or down
h=h2=current_capital/2
if np.random.choice([True, False], p=[price_change_probability, 1-price_change_probability]):
h *= 1.1 # Increase by 1%
else:
# h*=1/1.005
h *=1/1.1 # Decrease by 1%
current_capital=h+h2
# Record the current capital
capital_values.append(current_capital)
# Check if we hit the stop loss
if current_capital <= stop_loss:
break
return capital_values
# Simulation parameters
price_change_probability = 0.5 # 50% chance of going up or down
initial_capital = 1000 # Starting capital
rounds = 100000 # Number of rounds to simulate
stop_loss = 100 # Stop loss at $100
# f=p/a-q/b p=q=0.5 a=b=0.1
# Run the simulation
capital_values = simulate_stock(price_change_probability, initial_capital, rounds, stop_loss)
# Plot the results
plt.figure(figsize=(14, 7))
plt.plot(capital_values, label='Capital Over Time')
plt.axhline(y=initial_capital, color='r', linestyle='--', label='Initial Capital')
plt.axhline(y=stop_loss, color='g', linestyle='--', label='Stop Loss')
plt.title('Capital Over Time with Random Stock Price Changes')
plt.xlabel('Round')
plt.ylabel('Capital')
plt.legend()
plt.grid(True)
plt.show()
# Print the final capital value
print(f"Final capital: ${capital_values[-1]:.2f}")
结论:至少期望为先跌后涨可持平的情况下才能不亏钱。就是期望为正数才行,期望为0也亏钱