聖彼得堡悖論之破解

2022-03-15 20:17:29 字數 1041 閱讀 9419

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()

df = df.reindex(columns=['期數','當期投注','當期獎金','累積投注','累積收益','收益率'])

return df

sp = stpetresburg()

print(sp.df)

sp.set_option(beitou=3)

print(sp.df)

聖彼得堡悖論

usr bin env python3 coding utf 8 created on fri jun 22 15 15 13 2018 author yunjinqi email yunjinqi qq.com sigature it takes significant discipline,re...