import numpy as np
import pandas as pd
'''圣彼得堡悖论:[1,2,4,8,16,...]'''
class StPetresburg(object):
def __init__(self, beitou=2, peilv=1.97):
self.peilv = peilv # 赔率
self.beitou = beitou # 倍投倍数
def set_option(self, *args, **kwargs):
self.__dict__.update(dict(zip(['beitou', 'peilv'][:len(args)], args)))
self.__dict__.update(kwargs)
@property
def df(self):
period = np.arange(10) # 期数(从0开始)
touzhu = self.beitou**period # 当期投注 = 倍投倍数 ^ 期数
prize = touzhu * self.peilv # 当期奖金 = 当期投注 × 赔率
earn = prize - np.cumsum(touzhu) # 累积收益 = 当期奖金 - 累积投注
yields = earn / np.cumsum(touzhu) # 收益率 = 累积收益 / 累积投注
#print(np.column_stack((period, touzhu, prize, np.cumsum(touzhu), earn, yields)))
df = pd.DataFrame({
'期数': pd.Series(period, dtype=int),
'当期投注': pd.Series(touzhu, dtype=int),
'当期奖金': pd.Series(prize, dtype=float),
'累积投注': pd.Series(np.cumsum(touzhu), dtype=int),
'累积收益': pd.Series(earn, dtype=float),
'收益率': pd.Series(yields, dtype=float)
})
df = df.reindex(columns=['期数','当期投注','当期奖金','累积投注','累积收益','收益率'])
return df
sp = StPetresburg()
print(sp.df)
sp.set_option(beitou=3)
print(sp.df)