Calculating returns from a dataframe with financial data

后端 未结 4 1098
不知归路
不知归路 2021-02-02 11:44

I have a dataframe with monthly financial data:

In [89]: vfiax_monthly.head()
Out[89]: 
            year  month  day       d   open  close   high    low  volume          


        
4条回答
  •  悲&欢浪女
    2021-02-02 12:36

    The best way to calculate forward looking returns without any chance of bias is to use the built in function pd.DataFrame.pct_change(). In your case all you need to use is this function since you have monthly data, and you are looking for the monthly return.

    If, for example, you wanted to look at the 6 month return, you would just set the param df.pct_change(periods = 6) and that will give you the 6 month percent return.

    Because you have a relatively small data set, the easiest way is to resample on the parameters that you need to calculate the data on then use the pct_change() function again.

    However because of the nice properties of log it is common to use the formula for calculating returns (if you plan on computing statistics on the return series):

    Which you would implement as such:

    log_return = np.log(vfiax_monthly.open / vfiax_monthly.open.shift())

提交回复
热议问题