当前位置: 首页>>代码示例>>Python>>正文


Python Simulator.out_of_sample方法代码示例

本文整理汇总了Python中simulator.Simulator.out_of_sample方法的典型用法代码示例。如果您正苦于以下问题:Python Simulator.out_of_sample方法的具体用法?Python Simulator.out_of_sample怎么用?Python Simulator.out_of_sample使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在simulator.Simulator的用法示例。


在下文中一共展示了Simulator.out_of_sample方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: run

# 需要导入模块: from simulator import Simulator [as 别名]
# 或者: from simulator.Simulator import out_of_sample [as 别名]
def run(s_option):
    """
    Run the agent for a finite number of trials.:
    :param s_option: string. The type of the test
    """
    i_idx = 15  # 15  # index of the start file to be used in simulations
    n_trials = 10  # number of repetitions of the same sessions
    n_sessions = 1  # number of different days traded
    # Set up environment
    s_fname = 'data/data_0725_0926.zip'
    e = Environment(s_fname=s_fname, i_idx=i_idx)
    # create agent
    if s_option in ['train_learner', 'test_learner', 'optimize_k',
                    'optimize_gamma']:
        a = e.create_agent(LearningAgent_k,
                           f_min_time=2.,
                           f_k=0.8,
                           f_gamma=0.5)
    elif s_option == 'test_random':
        a = e.create_agent(BasicAgent, f_min_time=2.)
    else:
        l_aux = ['train_learner', 'test_learner', 'test_random', 'optimize_k',
                 'optimize_gamma']
        s_err = 'Select an <OPTION> between: \n{}'.format(l_aux)
        raise InvalidOptionException(s_err)
    e.set_primary_agent(a)  # specify agent to track

    # set up the simulation object
    sim = Simulator(e, update_delay=1.00, display=False)

    if 'train' in s_option:
        # ==== IN-SAMPLE TEST ====
        # Training the agent
        s_print = 'run(): Starting training session ! In-Sample Test.'
        if DEBUG:
            root.debug(s_print)
        else:
            print s_print

        # run for a specified number of trials
        sim.train(n_trials=n_trials, n_sessions=n_sessions)

        # test the agent
        s_print = 'run(): Starting testing phase ! In-Sample Test.'
        if DEBUG:
            root.debug(s_print)
        else:
            print s_print
        # run for a specified number of trials. should have the same number of
        # trials and session of the training phase
        sim.in_sample_test(n_trials=n_trials, n_sessions=n_sessions)
    elif s_option in ['test_random', 'test_learner']:
        # ==== OUT-OF-SAMPLE TEST ====
        # test the agent
        s_print = 'run(): Starting testing phase ! Out-of-Sample Test.'
        if DEBUG:
            root.debug(s_print)
        else:
            print s_print
        # run for a specified number of trials
        s_qtable = 'log/qtable/LearningAgent_k_qtable_{}.log'.format(n_trials)
        if e.primary_agent.s_agent_name == 'BasicAgent':
            # run that if is the basicagent
            sim.out_of_sample(s_qtable=s_qtable,
                              n_start=n_sessions+i_idx,
                              n_trials=20,
                              n_sessions=1)
        else:
            # the learning agent, when the policy is freezed, will always take
            # the same actions. So there is no meaning on test multiple times
            sim.out_of_sample(s_qtable=s_qtable,
                              n_start=n_sessions+i_idx,
                              n_trials=1,
                              n_sessions=1)

    elif s_option == 'optimize_k':
        # test the agent
        s_print = 'run(): Starting training session ! Optimiza_K Test.'
        if DEBUG:
            root.debug(s_print)
        else:
            print s_print
        # k tests
        for f_k in [0.3, 0.8, 1.3, 2.]:
            e = Environment(s_fname=s_fname, i_idx=i_idx)
            a = e.create_agent(LearningAgent_k,
                               f_min_time=2.,
                               f_k=f_k,
                               f_gamma=0.5)
            e.set_primary_agent(a)
            sim = Simulator(e, update_delay=1.00, display=False)
            sim.train(n_trials=5, n_sessions=1)
    elif s_option == 'optimize_gamma':
        # test the agent
        s_print = 'run(): Starting training session ! Optimiza_gamma Test.'
        if DEBUG:
            root.debug(s_print)
        else:
            print s_print
        # gamma test
#.........这里部分代码省略.........
开发者ID:Tornadoofsoul,项目名称:QLearning_Trading,代码行数:103,代码来源:agent.py


注:本文中的simulator.Simulator.out_of_sample方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。