【python】凯利公式 收益什么时候才不会亏钱

一只股票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也亏钱

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值