def smooth_xy(x, y): x = np.squeeze(x) y = np.squeeze(y) #v = lowess(y, x, frac=.05) kernel_reg = KernelReg(y, x, var_type='c', reg_type='lc') kernel_reg.bw = np.asarray([.01]) y = kernel_reg.fit(x)[0] return x, y
def calc_smooth(prices: pd.Series, *, bw: Union[np.ndarray, str] = 'cv_ls', a: float = None, use_array: bool = True) -> Union[pd.Series, np.ndarray]: """计算Nadaraya-Watson核估计后的价格数据 Args: prices (pd.Series): 价格数据 bw (Union[np.ndarray,str]): Either a user-specified bandwidth or the method for bandwidth selection. Defaults to cv_ls. a (float, optional): 论文中所说的比例数据. Defaults to None. use_array (bool, optional): 为True返回ndarray,False返回为pd.Series. Defaults to True. Returns: Union[pd.Series,np.ndarry] """ if not isinstance(prices, pd.Series): raise ValueError('prices必须为pd.Series') idx = np.arange(len(prices)) kr = KernelReg(prices.values, idx, var_type='c', reg_type='ll', bw=bw) if a is None: f = kr.fit(idx)[0] else: kr.bw = a * kr.bw # 论文用的0.3 * h f = kr.fit(idx)[0] if use_array: return f else: return pd.Series(data=f, index=prices.index)