简体   繁体   English

绘制标准偏差与带宽平均值的比率

[英]Plot the ratio of the standard deviation to the mean over bandwidth

I wanted to reveal hard to see correlation in data via computing the ratio of the standard deviation to the mean over the given bandwidth.我想通过计算给定带宽上的标准偏差与平均值的比率来揭示难以看到的数据相关性。 The window would be shifted one frequency bin to the right, and the ratio is computed again, and so on.窗口将向右移动一个频率区间,然后再次计算比率,依此类推。 I thought it is possible with ready function from Matplotlib or scipy library?我认为可以使用 Matplotlib 或 scipy 库中的就绪函数吗? I would be very grateful for showing me the solution.我将非常感谢向我展示解决方案。

Solution解决方案

What you are trying to calculate is a rolling version of Relative Standard Deviation (RSD), which is also known as Coefficient of Variation (CV).您要计算的是相对标准偏差(RSD) 的滚动版本,也称为变异系数(CV)。 See Wikipedia andInvestopedia for more details on the definition.有关定义的更多详细信息,请参阅WikipediaInvestopedia

RSD = CV = SD/Mean

Let us make some time-series data first.让我们先制作一些时间序列数据。

import pandas as pd
import numpy as np

# some sample data
ts = pd.Series(np.random.randn(1000), 
               index=pd.date_range('1/1/2000', 
                                   periods=1000)).cumsum()

The following piece of code will give you what you need.以下代码将为您提供所需的内容。

Option-A选项-A

window = 60
rolling_rsd = ts.rolling(window=window).std()/ts.rolling(window=window).mean()

Option-B选项-B

Or, you could use this convenience function:或者,您可以使用此便利功能:

def rsd(ts, window = 60):
    """
    Returns the Relative Standard Deviation (RSD), 
    a.k.a Coefficient of Variation (CV) for a 
    given rolling window size on a time series data-column.
    
    ts = time series data
    window = window size to compute rolling mean, std, rsd
    Example:
       rolling_rsd, rolling_mean, rolling_std = rsd(ts, window = 60)
    """
    rolling_mean = ts.rolling(window=window).mean()
    rolling_std = ts.rolling(window=window).std()
    rolling_rsd = rolling_std/rolling_mean
    
    return (rolling_rsd, rolling_mean, rolling_std)

Detailed Example详细示例

I will use convenience function, rsd() for following example.我将在以下示例中使用便利函数rsd()

import pandas as pd
import numpy as np

# some sample data
ts = pd.Series(np.random.randn(1000), index=pd.date_range('1/1/2000', periods=1000)).cumsum()

#plot the time series
ts.plot(style='k--')

# Using convenience function: rsd()
# calculate rolling RSD, MEAN and STD with window = 60
(rolling_rsd, rolling_mean, rolling_std) = rsd(ts, window = 60)

# calculate a 60 day rolling mean and plot
rolling_mean.plot(style='k')

# add the 60 day rolling standard deviation (STD) to the plot
rolling_std.plot(style='b')

# add the 60 day rolling  relative standard deviation (RSD) to the plot
rolling_rsd.plot(style='r')

Note:笔记:

You could also just calculate this directly as follows (if you prefer not to use another function).您也可以直接按如下方式计算(如果您不想使用其他函数)。

# calculate a 60 day rolling standard deviation (rsd)

rolling_rsd = ts.rolling(window=60).std()/ts.rolling(window=60).mean()

Related Solution/Question :相关解决方案/问题

  1. How can I simply calculate the rolling/moving variance of a time series in python? 如何简单地计算python中时间序列的滚动/移动方差?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM