Example #1
0
def kalman4ARX_pipeline(data_type, configs, n_trial):
    """基于 Kalman 滤波器的各个算法的 pipeline

    types = ['linear', 'longlag_linear']

    Args:
        data_type: 数据类型
        configs (dict): 配置字典
        n_trial: 试验次数
    """

    config = configs[data_type]
    term_selector = Selector(
        f"{config['data_root']}{data_type}{config['term_path']}")
    terms_set = term_selector.make_terms()
    # get data
    normalized_signals = term_selector.make_selection()[0]
    fname = f"{config['data_root']}{data_type}_kalman4ARX100_{config['est_fname']}"
    A_coef0 = 0
    for trial in range(n_trial):
        print(f'data_type: {data_type}, trial: ### {trial+1}')
        # 构造 Kalman Filter
        kf = Kalman4ARX(normalized_signals, config['max_lag'], uc=config['uc'])
        # 估计系数
        y_coef, A_coef = kf.estimate_coef(config['threshold'])
        A_coef0 += A_coef
    # 计算模型表达式并保存
    est_model = make_linear_func(A_coef0 / n_trial, fname=fname)
    # 输出结果
    print(est_model)
Example #2
0
def kalman_pipeline(configs):
    """基于 Kalman 滤波器的各个算法的 pipeline

    Args:
        configs (dict): 配置字典
    """

    for data_type in configs.keys():
        config = configs[data_type]
        term_selector = Selector(
            f"{config['data_root']}{data_type}{config['term_path']}")
        terms_set = term_selector.make_terms()
        # get data
        normalized_signals, Kalman_H, candidate_terms, Kalman_S_No = term_selector.make_selection(
        )
        # 构造 Kalman Filter
        for algo_type in config['algorithm']:
            print(f"{data_type} {algo_type}")
            fname = f"{config['data_root']}{data_type}_{algo_type}_{config['est_fname']}"
            if algo_type == 'Kalman4ARX':
                kf = Kalman4ARX(normalized_signals,
                                config['max_lag'],
                                uc=config['uc'])
                # 估计系数
                y_coef, A_coef = kf.estimate_coef(config['threshold'])
                # 计算模型表达式并保存
                est_model = make_linear_func(A_coef, fname=fname)
                # 保存平均结果
            elif algo_type == 'Kalman4FROLS':
                kf = Kalman4FROLS(normalized_signals,
                                  Kalman_H=Kalman_H,
                                  uc=config['uc'])
                y_coef = kf.estimate_coef()
                est_model = make_func4K4FROLS(y_coef,
                                              candidate_terms,
                                              Kalman_S_No,
                                              fname=fname)
                # 保存平均结果
            elif algo_type == 'torch4FROLS':
                kf = torch4FROLS(normalized_signals,
                                 Kalman_H=Kalman_H,
                                 n_epoch=config['n_epoch'])
                y_coef = kf.estimate_coef()
                est_model = make_func4K4FROLS(y_coef,
                                              candidate_terms,
                                              Kalman_S_No,
                                              fname=fname)
                # 保存平均结果
            else:
                print('!Not Defined!')
            print(f"\n{data_type}_{algo_type} est model saved!\n")

            # 输出结果
            print(est_model)
Example #3
0
def kalman4ARX_pipeline(data_type, configs, n_trial):
    """基于 Kalman 滤波器的各个算法的 pipeline

    types = ['linear', 'longlag_linear']

    Args:
        data_type: 数据类型
        configs (dict): 配置字典
        n_trial: 试验次数
    """

    config = configs[data_type]
    if data_type == 'linear':
        y_coef100 = np.zeros((100, 5, 25))
    else:
        y_coef100 = np.zeros((100, 5, 50))
    for trial in range(n_trial):
        term_selector = Selector(
            f"{config['data_root']}{data_type}{config['term_path']}{trial+1}.mat"
        )
        terms_set = term_selector.make_terms()
        # get data
        normalized_signals = term_selector.make_selection()[0]
        fname = f"{config['data_root']}{data_type}_kalman4ARX100_{config['est_fname']}{trial+1}.txt"
        print(f'data_type: {data_type}, trial: ### {trial+1}')
        # 构造 Kalman Filter
        kf = Kalman4ARX(normalized_signals, config['max_lag'], uc=config['uc'])
        # 估计系数
        y_coef, A_coef = kf.estimate_coef(config['threshold'])
        y_coef100[trial] = y_coef
        # 计算模型表达式并保存
        est_model = make_linear_func(A_coef, fname=fname)
        # 输出结果
        # print(est_model)
    fname1 = f"{config['data_root']}{data_type}_kalman4ARX100_{config['est_fname']}log.txt"
    save_3Darray(fname1, y_coef100)
    mean_y = np.mean(y_coef100, 0)
    var_y = np.var(y_coef100, 0)
    print(mean_y, var_y, sep='\n')
    fname1 = f"{config['data_root']}{data_type}_kalman4ARX100_{config['est_fname']}log100.txt"
    save_3Darray(fname1, np.array([mean_y, var_y]))
Example #4
0
# get data 线性模型
file_path = './data/linear_signals5D_noise1.mat'
data = get_mat_data(file_path, 'linear_signals')

# 数据标准化
data = normalize(data)

# *构造 Kalman Filter
kf = Kalman4ARX(data, 5, uc=0.01)
y_coef, A_coef = kf.estimate_coef(0.1)
print(y_coef, A_coef)

# *估计模型生成
est_model = make_linear_func(A_coef,
                             var_name='x',
                             fname='./data/linear_est_model.txt')
print(est_model)

# !Kalman4FROLS 测试
# !Selector 测试
# !线性模型
terms_path = './data/nor_linear_terms.mat'
term = Selector(terms_path)
terms_repr = term.make_terms()

# *保存候选项集合
fname = './data/linear_candidate_terms.txt'
np.savetxt(fname, terms_repr, fmt='%s')

# *selection