wor*_*ins 7 python numpy pandas
我想在投注系统中使用Python和Pandas实现经典的鞅.
假设这个DataFrame是这样定义的
df = pd.DataFrame(np.random.randint(0,2,100)*2-1, columns=['TossResults'])
Run Code Online (Sandbox Code Playgroud)
所以它包含折腾结果(-1 =输1 =赢)
我想使用经典鞅更改赌注(我打赌每次赌注的金额).
初始股权为1.
如果我输了赌注将是之前赌注的2倍(乘数= 2).
如果我赢得股权将是stake_initial
我做了一个功能
def stake_martingale_classical(stake_previous, result_previous, multiplier, stake_initial):
if (result_previous==-1): # lose
stake = stake_previous*multiplier
elif (result_previous==1):
stake = stake_initial
else:
raise(Exception('Error result_previous must be equal to 1 (win) or -1 (lose)'))
return(stake)
Run Code Online (Sandbox Code Playgroud)
但我不知道如何使用Pandas有效地实现它.我试过这个:
initial_stake = 1
df['Stake'] = None
df['Stake'][0] = initial_stake
df['TossResultsPrevious'] = self.df['TossResults'].shift(1) # shifting-lagging
df['StakePrevious'] = self.df['Stake'].shift(1) # shifting-lagging
Run Code Online (Sandbox Code Playgroud)
但是现在,我需要沿着0轴应用这个(多参数)函数.
我不知道该怎么办!
我曾经看过pandas.DataFrame.applymap
功能,但它似乎只是一个参数功能.
也许我错了,使用shift
功能不是一个好主意
一个轻微的解释变化是您需要将损失标记为a 1
,将胜利标记为a 0
。
第一步是找到输球路线的边缘(steps
+ edges
)。然后,您需要考虑步骤大小的差异,并将这些值推回原始数据中。当您选择时cumsum
,toss2
它会显示您当前的连败时间。那你的赌注是2 ** cumsum(toss2)
。
的numpy
版本比更快pandas
版本,但因子取决于N
(用于〜8 N=100
和〜2 N > 10000
)。
使用pandas.Series
:
import pandas as pd
toss = np.random.randint(0,2,100)
toss = pd.Series(toss)
steps = (toss.cumsum() * toss).diff() # mask out the cumsum where we won [0 1 2 3 0 0 4 5 6 ... ]
edges = steps < 0 # find where the cumsum steps down -> where we won
dsteps = steps[edges].diff() # find the length of each losing streak
dsteps[steps[edges].index[0]] = steps[edges][:1] # fix length of the first run which in now NaN
toss2 = toss.copy() # get a copy of the toss series
toss2[edges] = dsteps # insert the length of the losing streaks into the copy of the toss results
bets = 2 ** (toss2).cumsum() # compute the wagers
res = pd.DataFrame({'toss': toss,
'toss2': toss2,
'runs': toss2.cumsum(),
'next_bet': bets})
Run Code Online (Sandbox Code Playgroud)
这是纯numpy
版本(我的母语是)。需要进行一些细微的调整以使阵列排成一行,pandas
这对您有帮助
toss = np.random.randint(0,2,100)
steps = np.diff(np.cumsum(toss) * toss)
edges = steps < 0
edges_shift = np.append(False, edges[:-1])
init_step = steps[edges][0]
toss2 = np.array(toss)
toss2[edges_shift] = np.append(init_step, np.diff(steps[edges]))
bets = 2 ** np.cumsum(toss2)
fmt_dict = {1:'l', 0:'w'}
for t, b in zip(toss, bets):
print fmt_dict[t] + '-> {0:d}'.format(b)
Run Code Online (Sandbox Code Playgroud)
In [65]: res
Out[65]:
next_bet runs toss toss2
0 1 0 0 0
1 2 1 1 1
2 4 2 1 1
3 8 3 1 1
4 16 4 1 1
5 1 0 0 -4
6 1 0 0 0
7 2 1 1 1
8 4 2 1 1
9 1 0 0 -2
10 1 0 0 0
11 2 1 1 1
12 4 2 1 1
13 1 0 0 -2
14 1 0 0 0
15 2 1 1 1
16 1 0 0 -1
17 1 0 0 0
18 2 1 1 1
19 1 0 0 -1
20 1 0 0 0
21 1 0 0 0
22 2 1 1 1
23 1 0 0 -1
24 2 1 1 1
25 1 0 0 -1
26 1 0 0 0
27 1 0 0 0
28 2 1 1 1
29 4 2 1 1
30 1 0 0 -2
31 2 1 1 1
32 4 2 1 1
33 1 0 0 -2
34 1 0 0 0
35 1 0 0 0
36 1 0 0 0
37 2 1 1 1
38 4 2 1 1
39 1 0 0 -2
40 2 1 1 1
41 4 2 1 1
42 8 3 1 1
43 1 0 0 -3
44 1 0 0 0
45 1 0 0 0
46 1 0 0 0
47 2 1 1 1
48 1 0 0 -1
49 2 1 1 1
50 1 0 0 -1
51 1 0 0 0
52 1 0 0 0
53 1 0 0 0
54 1 0 0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> 1
l-> 2
l-> 4
l-> 8
w-> 1
w-> 1
l-> 2
l-> 4
l-> 8
w-> 1
l-> 2
l-> 4
w-> 1
l-> 2
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1719 次 |
最近记录: |