在Python中实现方差



我对Python还很陌生,但我真的想用Python对我的论文进行分析,这样我就能学到更多。

我试图在Python中估计三种类型的随时间变化的已实现方差。使用我在VBA中整理的五分钟数据,并成功地将excel日期转换为Python日期,并用以下代码将其与返回值匹配:

import xlrd as xl
import math as m
import datetime as time
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

file_loc = "/Python/fivecrude.xlsx"
workbook = xl.open_workbook(file_loc)
sheet = workbook.sheet_by_index(0)
tot = sheet.nrows
data = [[sheet.cell_value(r, c) for c in range(sheet.ncols)] for r in range(sheet.nrows)]
P = []
T = []
price = []
time = []
for i in range(2, tot):        
    t = data[i][0]
    ret = data[i][2]
    t = xl.xldate_as_tuple(t, 0)
    P.append(ret)
    T.append(t)
r = np.asarray(P)
time = np.asarray(T)
matrix = {'Date': time, 'Price': r}

通过查看矩阵,我得到了日期和相应的回报。

Out[466]: 
{'Date': array([[2015,    2,   27,   18,   50,    2],
        [2015,    2,   27,   18,   45,    1],
        [2015,    2,   27,   18,   40,    1],
        ..., 
        [2014,    3,    3,    8,   20,    1],
        [2014,    3,    3,    8,   15,    3],
        [2014,    3,    3,    8,   10,    3]]),
 'Price': array([ 0.00096852,  0.00226354,  0.00145784, ...,  0.00090302,
         0.00189899,  0.00135863])}

我如何构建一个例程,连续地对一整天以及一周和一个月的相应回报进行求和和和平方?但如果我能在一天内得到一些关于如何做这件事的线索,我就能解决剩下的问题

在最新版本的python统计模块中添加了具有方差和其他一些有用功能的模块。请使用:import statistics

最新更新