一次操作后值 returns 为零
Value returns zero after one operation
from numpy import std
import csv
data = []
with open('Data.csv') as file:
reader = csv.reader(file)
for column in zip(*reader):
data.append(column)
dates = list(reversed(open('Dates.csv').read().split('\n')))
stock_value = [int(x) for x in open('stock_value.csv').read().split(',')]
companies = open('companies.csv').read().split(',')
stock_change = {}
with open('Data.csv') as file:
reader = list(csv.reader(file))
for i, j in enumerate(dates):
stock_change[j] = map(float, reader[i])
company_value = dict(zip(companies, stock_value))
def change(invested, date):
"""Will return the change of invested stocks at the given date."""
sum_product = sum([value[0] * value[1] * data for value, data
in zip(invested, stock_change[date])])
_sum = sum([value[0] * value[1] for value in invested])
return sum_product / _sum
def total_change(invested):
"""Will return the total change associated with an investment."""
total_changes = []
for date in dates:
total_changes.append(change(list(zip(stock_value, invested)), date))
return total_changes
def volatility(invested):
"""Will return the std deviation from the total_change of the invested."""
return std(total_change(invested), ddof=1)
def tuner(invested):
"""Will return a weight list."""
weights = []
for i in range(465):
temp = invested[:]
temp1 = temp[:]
print(stock_value)
while True:
temp[i] = temp[i] + 1
if volatility(temp) < volatility(temp1):
temp1 = temp[:]
else:
temp[i] = temp[i] - 1
break
weights.append(temp[i])
return weights
invested = [0] * 465
invested[0] = 1
print(tuner(invested))
Data.csv 文件包含 881 行数据如下:
1.7529880478,2.8552887735,2.2606138577,1.7495626093,0.9274873524,0.6702840728,0.2543720191,2.1072796935,2.2385449458,2.2860610965,0.2590673575,...
每行对应一个日期。
companies.csv 是一个包含 465 个以逗号分隔的条目以及所有公司名称的文件,stock_value.csv 包含 465 个以逗号分隔的条目,其中每个条目都是公司股票的价值与它的索引相同。
在调谐器函数中,我打印了 temp 的波动率、temp1 = 0
的波动率,然后在下一个循环中 temp = 0
的波动率,temp1
的波动率也是如此。有谁知道为什么我的值变成零?
哦等等。我知道。抱歉,刚刚仔细看了代码
你有这个(我改变了它以使其更清晰):
def change(vs):
product = sum([v[0] * v[1] * d for v, d in zip(vs, ds)])
sums = sum([v[0] * v[1] for v in vs])
return product / sums
现在,使用基础数学,我们得到:
(v1 * v2 * d) / (v1 * v2)
(v1 * v2) * d / (v1 * v2)
d
所以,这个等式总是等于:
def change(vs):
return sum(ds)
如果您注意到,您的输出不依赖于您的输入,所以它是一个自变量,因此 return 一个常数值,将所有值清零。
这有点过于简单了,因为您确实传递了日期,然后获取了库存变化。但是由于传递了相同的日期数组,您将从 total_change
.
得到一个常量值数组 returned
from numpy import std
import csv
data = []
with open('Data.csv') as file:
reader = csv.reader(file)
for column in zip(*reader):
data.append(column)
dates = list(reversed(open('Dates.csv').read().split('\n')))
stock_value = [int(x) for x in open('stock_value.csv').read().split(',')]
companies = open('companies.csv').read().split(',')
stock_change = {}
with open('Data.csv') as file:
reader = list(csv.reader(file))
for i, j in enumerate(dates):
stock_change[j] = map(float, reader[i])
company_value = dict(zip(companies, stock_value))
def change(invested, date):
"""Will return the change of invested stocks at the given date."""
sum_product = sum([value[0] * value[1] * data for value, data
in zip(invested, stock_change[date])])
_sum = sum([value[0] * value[1] for value in invested])
return sum_product / _sum
def total_change(invested):
"""Will return the total change associated with an investment."""
total_changes = []
for date in dates:
total_changes.append(change(list(zip(stock_value, invested)), date))
return total_changes
def volatility(invested):
"""Will return the std deviation from the total_change of the invested."""
return std(total_change(invested), ddof=1)
def tuner(invested):
"""Will return a weight list."""
weights = []
for i in range(465):
temp = invested[:]
temp1 = temp[:]
print(stock_value)
while True:
temp[i] = temp[i] + 1
if volatility(temp) < volatility(temp1):
temp1 = temp[:]
else:
temp[i] = temp[i] - 1
break
weights.append(temp[i])
return weights
invested = [0] * 465
invested[0] = 1
print(tuner(invested))
Data.csv 文件包含 881 行数据如下:
1.7529880478,2.8552887735,2.2606138577,1.7495626093,0.9274873524,0.6702840728,0.2543720191,2.1072796935,2.2385449458,2.2860610965,0.2590673575,...
每行对应一个日期。 companies.csv 是一个包含 465 个以逗号分隔的条目以及所有公司名称的文件,stock_value.csv 包含 465 个以逗号分隔的条目,其中每个条目都是公司股票的价值与它的索引相同。
在调谐器函数中,我打印了 temp 的波动率、temp1 = 0
的波动率,然后在下一个循环中 temp = 0
的波动率,temp1
的波动率也是如此。有谁知道为什么我的值变成零?
哦等等。我知道。抱歉,刚刚仔细看了代码
你有这个(我改变了它以使其更清晰):
def change(vs):
product = sum([v[0] * v[1] * d for v, d in zip(vs, ds)])
sums = sum([v[0] * v[1] for v in vs])
return product / sums
现在,使用基础数学,我们得到:
(v1 * v2 * d) / (v1 * v2)
(v1 * v2) * d / (v1 * v2)
d
所以,这个等式总是等于:
def change(vs):
return sum(ds)
如果您注意到,您的输出不依赖于您的输入,所以它是一个自变量,因此 return 一个常数值,将所有值清零。
这有点过于简单了,因为您确实传递了日期,然后获取了库存变化。但是由于传递了相同的日期数组,您将从 total_change
.