使用R?的指数加权运动标准偏差的矢量化实现

时间:2019-02-08 01:49:06

标签: r time-series moving-average standard-deviation quantitative-finance

我正在尝试使用R实现矢量化指数加权移动标准偏差。这是正确的方法吗?

ewma <- function (x, alpha) {
  c(stats::filter(x * ratio, 1 - ratio, "recursive", init = x[1]))
}
ewmsd <- function(x, alpha) {
  sqerror <- na.omit((x - lag(ewma(x, ratio)))^2)
  ewmvar <- c(stats::filter(sqerror * ratio, 1 - ratio, "recursive", init = 0))
  c(NA, sqrt(ewmvar))
}

我猜不是,因为它的输出不同于Python的pandas.Series.ewm.std()函数。

我跑步时

ewmsd(x = 0:9, alpha = 0.96)

输出为

 [1]        NA 0.2236068 0.4874679 0.7953500 1.1353903 1.4993855 1.8812961 2.2764708 2.6812160 3.0925367

但是,与

pd.Series(range(10)).ewm(alpha = 0.96).std()

输出为

0         NaN
1    0.707107
2    0.746729
3    0.750825
4    0.751135
5    0.751155
6    0.751156
7    0.751157
8    0.751157
9    0.751157

1 个答案:

答案 0 :(得分:0)

根据documentation for PandasDB::transaction函数接收一个public function voucher($id) { DB::transaction(function () use ($id) { $roll = Roll::lockForUpdate()->find($id); if ($roll != null) { $roll->status = "V"; $roll->save(); $voucher = new Voucher(); $voucher->member_id = $roll->member_id; ... // Correct me if I am wrong, form your table structure it looks like you linked table voucher and setting with 'value' // I strongly recommend you to have setting_id as foreign key in table voucher // So you could do // $setting = Setting::where('Setting', 'weekly Subs')->where('Value', 10)->first(); // $voucher->setting_id = $setting->id; $voucher->save(); return redirect(action('RollController@index'))->with ('success', 'Member Paid with Vocuher'); } } return redirect(action('RollController@index')); } 参数,默认为pandas.Series.ewm()。当adjust时,来自TRUE的指数加权移动平均值是通过权重而非递归计算的。当然,这也会影响标准偏差的输出。有关更多信息,请参见this Github issuethis question

这是R中的向量化解决方案:

adjust == TRUE