Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 数据帧中的循环依赖_Python_Python 2.7_Pandas - Fatal编程技术网

Python 数据帧中的循环依赖

Python 数据帧中的循环依赖,python,python-2.7,pandas,Python,Python 2.7,Pandas,下面的代码在掷硬币结果上下注。你从100英镑开始,每次掷骰的风险为5%,但因为我的代码根据你的起始余额计算赌注大小,所以赌注始终为5英镑 import pandas import matplotlib.pyplot as plt start_bal = 100.0 #start off with £100 risk = 0.05 # risk 5% on each bet #create an empty data frame. a = pandas.DataFrame() #create

下面的代码在掷硬币结果上下注。你从100英镑开始,每次掷骰的风险为5%,但因为我的代码根据你的起始余额计算赌注大小,所以赌注始终为5英镑

import pandas
import matplotlib.pyplot as plt

start_bal = 100.0 #start off with £100
risk = 0.05 # risk 5% on each bet

#create an empty data frame.
a = pandas.DataFrame()

#create a list of coin toss results, 1 is win, -1 is lose
a['Result'] = [1,1,1,1,-1,-1,1,1,1,-1,-1,1,1,-1,-1,-1,1,1]

#your bet size is a % of your starting balance
a['bet'] = start_bal*risk
#record profit or loss based on coin toss
a['pnl'] = a.Result * a.bet
#increase/decrease balance
a['bal'] = start_bal + a.pnl.cumsum()

#plot balance
plt.plot(a.bal)
我想做的是在每次下注后根据你当时的余额重新计算下注大小,这样当你的余额增加时下注更多,当余额减少时下注更少。这意味着“bal”取决于“bet”,而“bet”又取决于“bal”,所以我最终得到了一个循环关系

这可能吗?我是否需要一次遍历一行数据帧,重新计算该特定索引的“bal”和“bet”

谢谢。

简单的一行:

results = start_bal * (1 + risk * a.Result).cumprod()
>>> results
0     105.000000
1     110.250000
2     115.762500
3     121.550625
4     115.473094
5     109.699439
6     115.184411
7     120.943632
8     126.990813
9     120.641272
10    114.609209
11    120.339669
12    126.356653
13    120.038820
14    114.036879
15    108.335035
16    113.751787
17    119.439376
Name: Result, dtype: float64

results.plot()

是的,这就是我想要的!谢谢