def inclination(self, data_i): """ 標準偏差の傾きを調べる。ボラティリティの広がりをパターンに分ける :param data_i: int :return: int: 定数、傾きのパターン """ pre_data = self._data.loc[data_i - self._last_data_num + 1:data_i, 'sigma'].values min_price = np.amin(pre_data) t = pre_data - np.full_like(a=pre_data, fill_value=min_price) x = np.arange(start=0, step=self._inclination_alpha, stop=self._inclination_alpha * len(t)) inclination = functions.linear_regression( x=x, t=t, basic_function=functions.Polynomial(dim=2))[1] if self.POSITIVE_INCLINATION < inclination: inclination_pattern = self.HYPER_EXPANSION elif (self.POSITIVE_MIDDLE_INCLINATION < inclination) and (inclination <= self.POSITIVE_INCLINATION): inclination_pattern = self.EXPANSION elif (self.NEGATIVE_MIDDLE_INCLINATION <= inclination) and ( inclination <= self.POSITIVE_MIDDLE_INCLINATION): inclination_pattern = self.FLAT elif (self.NEGATIVE_INCLINATION <= inclination) and ( inclination < self.NEGATIVE_MIDDLE_INCLINATION): inclination_pattern = self.SQUEEZE elif inclination < self.NEGATIVE_INCLINATION: inclination_pattern = self.HYPER_SQUEEZE else: inclination_pattern = None return inclination_pattern
def __inclination(self): min_sigma = np.amin(self.__recent_sigma) # 最小値との差分だけの行列を作る t = self.__recent_sigma - np.full_like(a=self.__recent_sigma, fill_value=min_sigma) t = t * 1000 x = np.arange(start=0, step=self.__inclination_alpha, stop=self.__inclination_alpha * len(t)) # 直線(1次多項式)の線形回帰 # その傾きを取り出す inclination = linear_regression(x=x, t=t, basic_function=Polynomial(dim=2))[1] if self.POSITIVE_INCLINATION < inclination: inclination_pattern = self.HYPER_EXPANSION elif (self.POSITIVE_MIDDLE_INCLINATION < inclination) and (inclination <= self.POSITIVE_INCLINATION): inclination_pattern = self.EXPANSION elif (self.NEGATIVE_MIDDLE_INCLINATION <= inclination) and ( inclination <= self.POSITIVE_MIDDLE_INCLINATION): inclination_pattern = self.FLAT elif (self.NEGATIVE_INCLINATION <= inclination) and ( inclination < self.NEGATIVE_MIDDLE_INCLINATION): inclination_pattern = self.SQUEEZE elif inclination < self.NEGATIVE_INCLINATION: inclination_pattern = self.HYPER_SQUEEZE else: raise TypeError('inclination is None') return inclination_pattern
def test_linear_regression(self): x = np.asarray([0, 20, 40]) t = np.asarray([100, 120, 140]) polynomial = functions.Polynomial(dim=2) w = functions.linear_regression(x=x, t=t, basic_function=polynomial) self.assertEqual(100, round(w[0])) self.assertEqual(1, round(w[1])) print(round(w[0]), w[0]) print(round(w[1]), w[1])
def inclination(self, data_i): """ 標準偏差の傾きを調べる。ボラティリティの広がりをパターンに分ける :param data_i: :return: """ pre_data = self.__data.loc[data_i - self.__stock_term + 1:data_i, self.__target].values min_price = np.amin(pre_data) t = pre_data - np.full_like(a=pre_data, fill_value=min_price) t = t * 1000 x = np.arange(start=0, step=self.__inclination_alpha, stop=self.__inclination_alpha * len(t)) inclination = functions.linear_regression( x=x, t=t, basic_function=functions.Polynomial(dim=2))[1] self.__inclination_list.append(inclination) self.inclination_pattern(inclination=inclination)