Пример #1
0
def calibrate(event_type = 0):
    if type_of_preestim == 'nonparam':
        with open(path_mmodel+'_nonp', 'rb') as source:
            model=pickle.load(source)
    elif type_of_preestim == 'ordinary_hawkes':        
        with open(path_mdata,'rb') as source:
            data=pickle.load(source)
        assert symbol==data.symbol    
        assert date==data.date
        model=sd_hawkes_model.SDHawkes(
            number_of_event_types=data.number_of_event_types,  number_of_states = data.number_of_states,
            number_of_lob_levels=data.n_levels, volume_imbalance_upto_level = data.volume_enc.volume_imbalance_upto_level,
            list_of_n_states=data.state_enc.list_of_n_states, st1_deflationary=data.state_enc.st1_deflationary,
            st1_inflationary=data.state_enc.st1_inflationary, st1_stationary=data.state_enc.st1_stationary
        )
        model.get_input_data(data)
    else:
        print("type_of_preestim={}".format(type_of_preestim))
        raise ValueError("type_of_preestim not recognised")

    now=datetime.datetime.now()
    message='\ndate of run: {}-{:02d}-{:02d} at {}:{:02d}\n'.format(now.year,now.month,now.day, now.hour, now.minute)
    message+='I am calibrating on lobster\n'
    message+='symbol={}, date={}, time_window={}\n'.format(symbol,date,time_window)
    message+='event type: {}'.format(event_type)
    name_of_model_partial=name_of_model+'_partial{}'.format(event_type)
    path_mpartial=path_mmodel+'_partial{}'.format(event_type)
    path_readout=path_mpartial+'_readout.txt'
    fout,saveout=redirect_stdout(direction='from',message=message,path=path_readout)
    model.calibrate_on_input_data(
        e=event_type,
        name_of_model=name_of_model_partial,
        type_of_preestim=type_of_preestim,
        max_imp_coef = max_imp_coef,
        learning_rate = learning_rate, maxiter = maxiter, num_of_random_guesses=num_guesses,
        parallel=parallel,
        use_prange=use_prange,
        number_of_attempts = 2, num_processes = num_processes,
        batch_size = batch_size, num_run_per_minibatch = num_run_per_minibatch,
        store_trans_prob=False, store_dirichlet_param=False,
        dump_after_calibration=True
    )
    n=datetime.datetime.now()
    message='\nCalibration of event_type {} terminates on {}-{:02d}-{:02d} at {}:{:02d}\n'\
    .format(event_type, n.year, n.month, n.day, n.hour, n.minute)
    redirect_stdout(direction='to',message=message, fout=fout, saveout=saveout)
Пример #2
0
def nonparam_preestim():
    now=datetime.datetime.now()
    message='\ndate of run: {}-{:02d}-{:02d} at {}:{:02d}\n'.format(now.year,now.month,now.day, now.hour, now.minute)
    message+='I am pre-estimating the model using non-parametric procedure\n'
    message+='symbol={}, date={}, time_window={}'.format(symbol,date,time_window)
    name_of_model_nonp=name_of_model+'_nonp'
    path_mnonp=path_mmodel+'_nonp'
    path_readout=path_mnonp+'_readout.txt'
    fout,saveout=redirect_stdout(direction='from',path=path_readout,message=message)    
    with open(path_mdata,'rb') as source:
        data=pickle.load(source)
    assert symbol==data.symbol    
    assert date==data.date
   
    model=sd_hawkes_model.SDHawkes(
        number_of_event_types=data.number_of_event_types,  number_of_states = data.number_of_states,
        number_of_lob_levels=data.n_levels, volume_imbalance_upto_level = data.volume_enc.volume_imbalance_upto_level,
        list_of_n_states=data.state_enc.list_of_n_states, st1_deflationary=data.state_enc.st1_deflationary,
        st1_inflationary=data.state_enc.st1_inflationary, st1_stationary=data.state_enc.st1_stationary
    )
    model.get_input_data(data)
    model.create_nonparam_estim(type_of_input='empirical',
                                num_quadpnts = num_quadpnts,
                                quad_tmax = quad_tmax,
                                quad_tmin = quad_tmin,
                                num_gridpnts = num_gridpnts,
                                grid_tmax = grid_tmax,
                                grid_tmin = grid_tmin,
                                two_scales=True,
                                tol=1.0e-7
                               ) 
    run_time = -time.time()
    model.nonparam_estim.estimate_hawkes_kernel(
        parallel=True,
        store_L1_norm=False, use_filter=True, enforce_positive_g_hat=True,
        filter_cutoff=50.0, filter_scale=30.0, num_addpnts_filter=3000)
    model.nonparam_estim.fit_powerlaw(compute_L1_norm=True,ridge_param=1.0e-02, tol=1.0e-7)
    model.nonparam_estim.store_base_rates()
    run_time+=time.time()
    model.nonparam_estim.store_runtime(run_time)
    model.nonparam_estim.create_goodness_of_fit()
    model.dump(name=name_of_model_nonp,path=path_models+'/'+symbol)
    n=datetime.datetime.now()
    message='\nNon-parametric pre-estimation terminates on {}-{:02d}-{:02d} at {}:{:02d}\n'\
    .format(n.year, n.month, n.day, n.hour, n.minute)
    redirect_stdout(direction='to', message=message, fout=fout, saveout=saveout)
Пример #3
0
def instantiate_and_simulate():
    path_readout=path_saved_tests+'/'+this_test_model_name+'_simulation_readout.txt'
    now=datetime.datetime.now()
    message="I am executing {} --simulation".format(sys.argv[0])
    message+="\nDate of run: {}-{:02d}-{:02d} at {:02d}:{:02d}".format(now.year, now.month, now.day, now.hour, now.minute)
    fout,saveout=redirect_stdout(direction="from", message=message, path=path_readout)
    model = sd_hawkes_model.SDHawkes(
        number_of_event_types=n_events,
        number_of_lob_levels=n_levels,
        list_of_n_states=n_states,
        volume_imbalance_upto_level=upto_level,
        name_of_model=this_test_model_name)
    tot_n_states=model.state_enc.tot_n_states
    phis = model.state_enc.generate_random_transition_prob(n_events=n_events).astype(np.float)
    nus = 0.1*np.random.randint(low=15,high=20,size=n_events).astype(np.float)
    alphas = np.power(10,-np.random.uniform(low=1.0, high=1.2))*np.random.randint(low=0,high=4,size=(n_events, tot_n_states, n_events)).astype(np.float)
    betas = np.random.uniform(1.25025,2.1,size=(n_events, tot_n_states, n_events)).astype(np.float)
    gammas = np.random.uniform(low=1.25, high = 5.6,size=(tot_n_states,2*n_levels))

    model.set_hawkes_parameters(nus,alphas,betas)
    model.set_dirichlet_parameters(gammas)
    model.set_transition_probabilities(phis)

    print("\nSIMULATION\n")
    global time_start
    global time_end
    max_number_of_events = np.random.randint(low=7900, high=8010)
    times, events, states, volumes = model.simulate(
        time_start, time_end, max_number_of_events=max_number_of_events,
        add_initial_cond=True,
        store_results=True, report_full_volumes=False)
    time_end=float(times[-1])
    model.create_goodness_of_fit(type_of_input='simulated', parallel=True)
    model.goodness_of_fit.ks_test_on_residuals()
    model.goodness_of_fit.ad_test_on_residuals()
    model.dump(path=path_saved_tests)
    model.dump(name=this_test_model_name+"_simulation", path=path_saved_tests)
    now=datetime.datetime.now()
    message='\nSimulation terminates on {}-{:02d}-{:02d} at {:02d}:{:02d}\n'\
    .format(now.year, now.month, now.day, now.hour, now.minute)
    redirect_stdout(direction="to", message=message, fout=fout, saveout=saveout) 
Пример #4
0
def merge_from_partial(adjust_base_rates = False, leading_coef = 0.66):
    now=datetime.datetime.now()
    message='\ndate of run: {}-{:02d}-{:02d} at {}:{:02d}\n'.format(now.year,now.month,now.day, now.hour, now.minute)
    message+='I am merging from partial models\n'
    message+='symbol={}, date={}, time_window={}'.format(symbol,date,time_window)
    path_readout=path_mmodel+'_merge_readout.txt'
    fout,saveout=redirect_stdout(direction='from',path=path_readout,message=message)    
    list_of_partial_names=[name_of_model+'_partial{}'.format(e) for e in range(number_of_event_types)]
    partial_models=[]
    with open(path_mdata,'rb') as source:
        data=pickle.load(source)
        assert data.symbol == symbol
        assert data.date == date
        assert data.number_of_event_types==number_of_event_types
    MODEL=sd_hawkes_model.SDHawkes(
        number_of_event_types=data.number_of_event_types, number_of_states=data.number_of_states,
        number_of_lob_levels=data.n_levels,volume_imbalance_upto_level = data.volume_enc.volume_imbalance_upto_level,
        list_of_n_states=data.state_enc.list_of_n_states, st1_deflationary=data.state_enc.st1_deflationary,
        st1_inflationary=data.state_enc.st1_inflationary, st1_stationary=data.state_enc.st1_stationary
    )
    MODEL.get_input_data(data)    
    for mn in list_of_partial_names:
        try:
            with open(path_models+'/{}/{}_{}/'.format(symbol,symbol,date)+mn,'rb') as source:
                model=pickle.load(source)
        except:
            with open(path_models+'/{}/'.format(symbol,)+mn,'rb') as source:
                model=pickle.load(source)
            assert model.data.symbol == symbol
            assert model.data.date == date
            assert model.calibration.type_of_preestim == type_of_preestim
            partial_models.append(model)
    if type_of_preestim == 'nonparam':
        MODEL.store_nonparam_estim_class(model.nonparam_estim)
    MODEL.initialise_from_partial_calibration(partial_models, set_parameters=True, adjust_base_rates = True, dump_after_merging=True, name_of_model=name_of_model)       
    n=datetime.datetime.now()
    message='\nMerging has been completed  on {}-{:02d}-{:02d} at {}:{:02d}'.format(n.year,n.month,n.day,n.hour,n.minute)
    redirect_stdout(direction='to',message=message,fout=fout, saveout=saveout) 
Пример #5
0
def main():
    list_of_n_states = [3, 5]
    n_events = 4
    n_levels = 2

    sd_model = model.SDHawkes(number_of_event_types=n_events,
                              number_of_lob_levels=n_levels,
                              volume_imbalance_upto_level=2,
                              list_of_n_states=list_of_n_states)

    tot_n_states = sd_model.state_enc.tot_n_states

    # The base rates $\nu$
    nus = 0.05 * np.random.randint(low=2, high=6, size=n_events)
    # The impact coefficients $\alpha$
    alphas = np.random.uniform(0.0002,
                               0.4,
                               size=(n_events, tot_n_states,
                                     n_events)).astype(np.float)
    # The decay coefficients $\beta$
    betas = np.random.uniform(1.265,
                              2.805,
                              size=(n_events, tot_n_states,
                                    n_events)).astype(np.float)
    sd_model.set_hawkes_parameters(nus, alphas, betas)
    # The transition probabilities $\phi$
    phis = sd_model.state_enc.generate_random_transition_prob(
        n_events=n_events).astype(np.float)
    sd_model.set_transition_probabilities(phis)
    sd_model.enforce_symmetry_in_transition_probabilities()
    # The Dirichlet parameters $\kappa$
    kappas = np.random.lognormal(size=(tot_n_states, 2 * n_levels))
    sd_model.set_dirichlet_parameters(kappas)

    time_start = 0.0
    time_end = time_start + 1.0 * 60 * 60
    max_number_of_events = 2000

    print("\nSIMULATION\n")

    times, events, states, volumes = sd_model.simulate(
        time_start,
        time_end,
        max_number_of_events=max_number_of_events,
        add_initial_cond=True,
        store_results=True,
        report_full_volumes=False)
    time_end = float(times[-1])

    sd_model.create_goodness_of_fit(type_of_input='simulated')
    sd_model.goodness_of_fit.ks_test_on_residuals()
    sd_model.goodness_of_fit.ad_test_on_residuals()

    #     exit()

    print("\nMLE ESTIMATION\n")
    "Initialise the class"
    sd_model.create_mle_estim(type_of_input='simulated')
    d_E = sd_model.number_of_event_types
    d_S = sd_model.number_of_states
    "Fictious initial guess"
    list_init_guesses = []
    for k in range(3):
        nus = np.random.uniform(low=0.0, high=1.0, size=(d_E, ))
        alphas = np.random.uniform(low=0.0, high=1.0, size=(d_E, d_S, d_E))
        betas = np.random.uniform(low=1.1, high=3.0, size=(d_E, d_S, d_E))
        guess = computation.parameters_to_array(nus, alphas, betas)
        list_init_guesses.append(np.array(guess, copy=True))
    "Set the estimation"
    sd_model.mle_estim.set_estimation_of_hawkes_param(
        time_start,
        time_end,
        list_of_init_guesses=list_init_guesses,
        learning_rate=0.00005,
        maxiter=10,
        number_of_additional_guesses=3,
        parallel=True,
        pre_estim_ord_hawkes=True,
        pre_estim_parallel=True,
        number_of_attempts=2,
        num_processes=8)

    #     exit()

    "Launch estimation"
    sd_model.mle_estim.launch_estimation_of_hawkes_param(e=0)
Пример #6
0
def read(
    symbol="INTC",
    date="2019-01-23",
    time_window="41400-45000",
    simulate=False,
):
    now = datetime.datetime.now()
    message = '\ndate of run: {}-{:02d}-{:02d} at {}:{:02d}\n'.format(
        now.year, now.month, now.day, now.hour, now.minute)
    message += 'I am reading from saved models\n'
    message += 'symbol={}, date={}, time_window={}'.format(
        symbol, date, time_window)
    path_readout = path_impact + '/models/{}/{}_{}_{}_onesided_thesis_readout'.format(
        symbol, symbol, date, time_window)
    fout, saveout = redirect_stdout(direction='from',
                                    message=message,
                                    path=path_readout)
    try:
        with open(
                path_models + "/{}/{}_{}/{}_sdhawkes_{}_{}".format(
                    symbol, symbol, date, symbol, date, time_window),
                'rb') as source:
            calmodel = pickle.load(source)
    except FileNotFoundError:
        try:
            with open(
                    path_models + "/{}/{}_sdhawkes_{}_{}".format(
                        symbol, symbol, date, time_window), 'rb') as source:
                calmodel = pickle.load(source)
        except FileNotFoundError:
            print("File not found")
            raise FileNotFoundError
    model=sd_hawkes_model.SDHawkes(
        number_of_event_types=calmodel.number_of_event_types,
        list_of_n_states=calmodel.state_enc.list_of_n_states,
        number_of_lob_levels=calmodel.n_levels,
        volume_imbalance_upto_level=\
        calmodel.volume_enc.volume_imbalance_upto_level
    )
    model.get_configuration(calmodel)
    model.create_uq()
    model.set_base_rates(
        np.array([0.14008215, 0.14451349, 6.4689, 6.0295], dtype=np.float))
    #    target=computation.avg_rates(model.data.number_of_event_types,
    #                                model.data.observed_times,
    #                                model.data.observed_events, partial=False)
    #    model.uncertainty_quantification.adjust_baserates(
    #        target,
    #        adj_coef=5.0e-2,
    #        num_iter=15,
    #        max_number_of_events=30000
    #    )
    model.reduce_price_volatility(reduction_coef=0.7)
    #Remarkably, compared to the same function in bacrymuzy/main.py, here we are NOT enforcing price symmetry
    model.create_goodness_of_fit(type_of_input='empirical')
    if simulate:
        time_start = 0.0
        time_end = time_start + 0.15 * 60 * 60
        model.simulate(time_start,
                       time_end,
                       max_number_of_events=50000,
                       add_initial_cond=True,
                       store_results=True,
                       report_full_volumes=False)
        model.store_price_trajectory(
            type_of_input='simulated',
            initial_price=model.data.mid_price.iloc[0, 1],
            ticksize=model.data.ticksize)
    model.store_price_trajectory(type_of_input='empirical',
                                 initial_price=model.data.mid_price.iloc[0, 1],
                                 ticksize=model.data.ticksize)
    try:
        os.mkdir(
            path_impact +
            '/models/{}/{}_{}_{}'.format(symbol, symbol, date, time_window))
    except FileExistsError:
        pass
    model.set_name_of_model('{}_sdhawkes_{}_{}_onesided_thesis'.format(
        symbol, date, time_window))
    model.dump(path=path_impact +
               '/models/{}/{}_{}_{}'.format(symbol, symbol, date, time_window))
    now = datetime.datetime.now()
    message = '\nEnds on {}-{:02d}-{:02d} at {}:{:02d}\n'.format(
        now.year, now.month, now.day, now.hour, now.minute)
    redirect_stdout(direction='to',
                    message=message,
                    fout=fout,
                    saveout=saveout)
Пример #7
0
def main():
    global now
    name_of_model_nonp = name_of_model + '_nonp'
    path_mnonp = path_mmodel + '_nonp'
    with open(path_mdata, 'rb') as source:
        data = pickle.load(source)
    assert symbol == data.symbol
    assert date == data.date

    model = sd_hawkes_model.SDHawkes(
        number_of_event_types=data.number_of_event_types,
        number_of_states=data.number_of_states,
        number_of_lob_levels=data.n_levels,
        volume_imbalance_upto_level=data.volume_enc.
        volume_imbalance_upto_level,
        list_of_n_states=data.state_enc.list_of_n_states,
        st1_deflationary=data.state_enc.st1_deflationary,
        st1_inflationary=data.state_enc.st1_inflationary,
        st1_stationary=data.state_enc.st1_stationary)
    model.get_input_data(data)
    if type_of_preestim == 'nonparam':
        model.create_nonparam_estim(type_of_input='empirical',
                                    num_quadpnts=num_quadpnts,
                                    quad_tmax=quad_tmax,
                                    quad_tmin=quad_tmin,
                                    num_gridpnts=num_gridpnts,
                                    grid_tmax=grid_tmax,
                                    grid_tmin=grid_tmin,
                                    two_scales=True,
                                    tol=1.0e-7)
        run_time = -time.time()
        model.nonparam_estim.estimate_hawkes_kernel(
            store_L1_norm=False,
            use_filter=True,
            enforce_positive_g_hat=True,
            filter_cutoff=50.0,
            filter_scale=30.0,
            num_addpnts_filter=3000)
        model.nonparam_estim.fit_powerlaw(compute_L1_norm=True,
                                          ridge_param=1.0e-02,
                                          tol=1.0e-7)
        model.nonparam_estim.store_base_rates()
        run_time += time.time()
        model.nonparam_estim.store_runtime(run_time)
        model.nonparam_estim.create_goodness_of_fit()
        #         model.dump(name=name_of_model_nonp,path=path_models+'/'+symbol)
        n = datetime.datetime.now()
        message='\nNon-parametric pre-estimation terminates on {}-{:02d}-{:02d} at {}:{:02d}\n'\
        .format(n.year, n.month, n.day, n.hour, n.minute)
        print(message)

    print("\nMLE ESTIMATION\n")
    name_of_model_partial = name_of_model + '_partial{}'.format(event_type)
    path_mpartial = path_mmodel + '_partial{}'.format(event_type)
    now = datetime.datetime.now()
    message = '\ndate of run: {}-{:02d}-{:02d} at {}:{:02d}\n'.format(
        now.year, now.month, now.day, now.hour, now.minute)
    message += 'I am calibrating on lobster\n'
    message += 'symbol={}, date={}, time_window={}\n'.format(
        symbol, date, time_window)
    message += 'event type: {}'.format(event_type)
    print(message)
    model.calibrate_on_input_data(partial=True,
                                  e=event_type,
                                  name_of_model=name_of_model_partial,
                                  type_of_preestim=type_of_preestim,
                                  max_imp_coef=max_imp_coef,
                                  learning_rate=learning_rate,
                                  maxiter=maxiter,
                                  num_of_random_guesses=num_guesses,
                                  parallel=True,
                                  number_of_attempts=2,
                                  num_processes=num_processes,
                                  skip_estim_of_state_processes=True,
                                  dump_after_calibration=True)
    n = datetime.datetime.now()
    message='\nCalibration of event_type {} terminates on {}-{:02d}-{:02d} at {}:{:02d}\n'\
    .format(event_type, n.year, n.month, n.day, n.hour, n.minute)
    print(message)
Пример #8
0
def uncertainty_quantification(input_model_name=''):
    now=datetime.datetime.now()
    message='\ndate of run: {}-{:02d}-{:02d} at {}:{:02d}\n'.format(now.year,now.month,now.day, now.hour, now.minute)
    message+='I am performing uncertainty quantification\n'
    message+='symbol={}, date={}, time_window={}'.format(symbol,date,time_window)
    path_readout=path_mmodel+'_uq_readout.txt'
    fout,saveout=redirect_stdout(direction='from',path=path_readout,message=message)    
    if input_model_name=='':
        input_model_name='{}_sdhawkes_{}_{}'.format(symbol, date, time_window)
    try:
        with open(path_models+'/{}/{}_{}/'.format(symbol,symbol,date)+input_model_name,'rb') as source:
            input_model=pickle.load(source)
    except:
        with open(path_models+'/{}/'.format(symbol,)+input_model_name,'rb') as source:
            input_model=pickle.load(source)
    assert input_model.data.symbol == symbol
    assert input_model.data.date == date

    model=sd_hawkes_model.SDHawkes(
        name_of_model = input_model_name+'_uq',
        number_of_event_types=input_model.number_of_event_types,
        number_of_states=input_model.number_of_states,
        number_of_lob_levels=input_model.n_levels,
        volume_imbalance_upto_level = input_model.volume_enc.volume_imbalance_upto_level,
        list_of_n_states=input_model.state_enc.list_of_n_states,
        st1_deflationary=input_model.state_enc.st1_deflationary,
        st1_inflationary=input_model.state_enc.st1_inflationary,
        st1_stationary=input_model.state_enc.st1_stationary
    )
    model.get_input_data(input_model.data)
    try:
        model.store_nonparam_estim_class(input_model.nonparam_estim)
    except:
        pass
    model.store_mle_estim(input_model.mle_estim)
    model.store_calibration(input_model.calibration)
    model.store_goodness_of_fit(input_model.goodness_of_fit)
    model.set_hawkes_parameters(input_model.base_rates,
                                input_model.impact_coefficients,
                                input_model.decay_coefficients)
    model.set_transition_probabilities(input_model.transition_probabilities)
    model.set_dirichlet_parameters(input_model.dirichlet_param)
    model.create_uq()
    time_start=0.0
    time_end=3600.0
    max_events=40000
    model.uncertainty_quantification.simulate(
            time_start, time_end, max_number_of_events=max_events)
    model.uncertainty_quantification.create_goodness_of_fit()
    model.uncertainty_quantification.calibrate_on_simulated_data(
        type_of_preestim = type_of_preestim,
        max_imp_coef = max_imp_coef,
        learning_rate = learning_rate,
        maxiter = maxiter,
        num_of_random_guesses = num_guesses,
        parallel=parallel, use_prange = use_prange,
        number_of_attempts = number_of_attempts,
        num_processes = num_processes,
        batch_size = batch_size,
        num_run_per_minibatch = num_run_per_minibatch,
    )
    model.dump(path=path_models+'/{}'.format(symbol))  
    n=datetime.datetime.now()
    message='\nUncertainty quantification completed  on {}-{:02d}-{:02d} at {}:{:02d}'.format(n.year,n.month,n.day,n.hour,n.minute)
    redirect_stdout(direction='to',message=message,fout=fout, saveout=saveout) 
Пример #9
0
def main():    
    print("I am executing test_nonparam.py")
    global now
    print('\ndate of run: {}-{:02d}-{:02d} at {}:{:02d}\n'.format(now.year,now.month,now.day, now.hour, now.minute))
    n_states=[3,5]
    n_events = 4  # number of event types, $d_e$
    n_levels = 2
    upto_level = 2
    time_start=np.random.uniform()
    time_end=time_start+0.2*60*60
    model = sd_hawkes_model.SDHawkes(
        number_of_event_types=n_events,
        number_of_lob_levels=n_levels,
        list_of_n_states=n_states,
        volume_imbalance_upto_level=upto_level)
    tot_n_states=model.state_enc.tot_n_states
    # The transition probabilities $\phi$
    phis = model.state_enc.generate_random_transition_prob(n_events=n_events).astype(np.float)
    # The base rates $\nu$
    nus = 0.1*np.random.randint(low=15,high=20,size=n_events)
    # The impact coefficients $\alpha$
    alphas = np.power(10,-np.random.uniform(low=1.0, high=1.2))*np.random.randint(low=0,high=4,size=(n_events, tot_n_states, n_events)).astype(np.float)
    # The decay coefficients $\beta$
    betas = np.random.uniform(1.25025,2.1,size=(n_events, tot_n_states, n_events)).astype(np.float)
    # The Dirichlet parameters $\gamma$
    gammas = np.random.uniform(low=1.25, high = 5.6,size=(tot_n_states,2*n_levels))

    model.set_hawkes_parameters(nus,alphas,betas)
    model.set_dirichlet_parameters(gammas)
    model.set_transition_probabilities(phis)

    print("\nSIMULATION\n")

    times, events, states, volumes = model.simulate(
        time_start, time_end,max_number_of_events=12000,add_initial_cond=True,
        store_results=True,report_full_volumes=False)
    time_end=np.array(times[-1],copy=True)

    model.create_goodness_of_fit(type_of_input='simulated')
    model.goodness_of_fit.ks_test_on_residuals()
    model.goodness_of_fit.ad_test_on_residuals()

    print("\nNON-PARAMETRIC ESTIMATION\n")

    upperbound_of_support_of_kernel=1.0e+00
    lowerbound_of_support_of_kernel=1.0e-01
    num_quadpnts = 80
    num_gridpnts= 75
    run_time = -time.time()
    model.create_nonparam_estim(type_of_input='simulated',
                                num_quadpnts = num_quadpnts,
                                quad_tmax = upperbound_of_support_of_kernel,
                                quad_tmin = lowerbound_of_support_of_kernel,
                                num_gridpnts = num_gridpnts,
                                grid_tmax = upperbound_of_support_of_kernel,
                                grid_tmin = lowerbound_of_support_of_kernel,
                                two_scales=True,
                                tol=1.0e-6
                               )
    model.nonparam_estim.estimate_hawkes_kernel(store_L1_norm=False,
                               use_filter=True, enforce_positive_g_hat=True,
                               filter_cutoff=20.0, filter_scale=30.0, num_addpnts_filter=3000)
    model.nonparam_estim.fit_powerlaw(compute_L1_norm=True,ridge_param=1.0e-02, tol=1.0e-7)
    model.nonparam_estim.store_base_rates()
    run_time+=time.time()
    model.nonparam_estim.store_runtime(run_time)
    model.nonparam_estim.create_goodness_of_fit()
    now=datetime.datetime.now()
    print("Estimation terminates on {}-{:02d}-{:02d} at {:02d}:{:02d}".format(
        now.year,now.month,now.day,now.hour,now.minute
    ))
    return model
Пример #10
0
def main():
    list_of_n_states = [3, 5]
    n_events = 4
    n_levels = 2

    sd_model = model.SDHawkes(number_of_event_types=n_events,
                              number_of_lob_levels=n_levels,
                              volume_imbalance_upto_level=2,
                              list_of_n_states=list_of_n_states)

    tot_n_states = sd_model.state_enc.tot_n_states

    # The base rates $\nu$
    nus = 0.05 * np.random.randint(low=2, high=8, size=n_events)
    # The impact coefficients $\alpha$
    alphas = np.random.uniform(0.002,
                               0.2435,
                               size=(n_events, tot_n_states,
                                     n_events)).astype(np.float)
    # The decay coefficients $\beta$
    betas = np.random.uniform(1.265,
                              2.805,
                              size=(n_events, tot_n_states,
                                    n_events)).astype(np.float)
    sd_model.set_hawkes_parameters(nus, alphas, betas)
    # The transition probabilities $\phi$
    phis = sd_model.state_enc.generate_random_transition_prob(
        n_events=n_events).astype(np.float)
    sd_model.set_transition_probabilities(phis)
    #     sd_model.enforce_symmetry_in_transition_probabilities()
    # The Dirichlet parameters $\kappa$
    kappas = np.random.lognormal(size=(tot_n_states, 2 * n_levels))
    sd_model.set_dirichlet_parameters(kappas)

    time_start = 0.0
    time_end = time_start + 1.0 * 60 * 60
    max_number_of_events = 4000

    print("\nSIMULATION\n")

    times, events, states, volumes = sd_model.simulate(
        time_start,
        time_end,
        max_number_of_events=max_number_of_events,
        add_initial_cond=True,
        store_results=True,
        report_full_volumes=False)
    time_end = float(times[-1])

    sd_model.create_goodness_of_fit(type_of_input='simulated', parallel=False)
    sd_model.goodness_of_fit.ks_test_on_residuals()
    sd_model.goodness_of_fit.ad_test_on_residuals()

    print("\nMINIMISATION PROCEDURE\n")
    event_type = 0
    num_init_guesses = 9
    list_init_guesses = []
    for n in range(num_init_guesses):
        list_init_guesses.append(1.0 + np.random.lognormal(size=(
            1 +
            2 * sd_model.number_of_event_types * sd_model.number_of_states, )))
    MinimProc = minim_algo.MinimisationProcedure(
        times,
        events,
        states,
        time_start,
        time_end,
        sd_model.number_of_event_types,
        sd_model.number_of_states,
        event_type,
        list_init_guesses=list_init_guesses,
        learning_rate=0.0001,
        maxiter=6,
        tol=1.0e-7,
        number_of_attempts=2,
        batch_size=2000,
        num_run_per_minibatch=2,
    )
    #     MinimProc.prepare_batches_fake()
    MinimProc.prepare_batches()
    run_time = -time.time()
    MinimProc.launch_minimisation(parallel=False, num_processes=8)
    run_time += time.time()
    print("Minimisation terminates. run_time = {}".format(run_time))
    time.sleep(2)
    print("Now exiting")
Пример #11
0
def main(model_name=''):
    global time_start
    global time_end
    model = sd_hawkes_model.SDHawkes(number_of_event_types=n_events,
                                     number_of_lob_levels=n_levels,
                                     list_of_n_states=n_states,
                                     volume_imbalance_upto_level=upto_level,
                                     name_of_model=model_name)
    tot_n_states = model.state_enc.tot_n_states
    phis = model.state_enc.generate_random_transition_prob(
        n_events=n_events).astype(np.float)
    nus = 0.1 * np.random.randint(low=15, high=20, size=n_events)
    alphas = np.power(
        10, -np.random.uniform(low=1.0, high=1.2)) * np.random.randint(
            low=0, high=4, size=(n_events, tot_n_states, n_events)).astype(
                np.float)
    betas = np.random.uniform(1.25025,
                              2.1,
                              size=(n_events, tot_n_states,
                                    n_events)).astype(np.float)
    gammas = np.random.uniform(low=1.25,
                               high=5.6,
                               size=(tot_n_states, 2 * n_levels))

    model.set_hawkes_parameters(nus, alphas, betas)
    model.set_dirichlet_parameters(gammas)
    model.set_transition_probabilities(phis)

    print("\nSIMULATION\n")
    max_number_of_events = np.random.randint(low=5850, high=6000)
    times, events, states, volumes = model.simulate(
        time_start,
        time_end,
        max_number_of_events=max_number_of_events,
        add_initial_cond=True,
        store_results=True,
        report_full_volumes=False)
    time_end = float(times[-1])
    model.create_goodness_of_fit(type_of_input='simulated', parallel=False)
    model.goodness_of_fit.ks_test_on_residuals()
    model.goodness_of_fit.ad_test_on_residuals()
    if type_of_preestim == 'nonparam':
        print("\nNON-PARAMETRIC ESTIMATION\n")
        run_time = -time.time()
        model.create_nonparam_estim(type_of_input='simulated',
                                    num_quadpnts=num_quadpnts,
                                    quad_tmax=quad_tmax,
                                    quad_tmin=quad_tmin,
                                    num_gridpnts=num_gridpnts,
                                    grid_tmax=grid_tmax,
                                    grid_tmin=grid_tmin,
                                    two_scales=True,
                                    tol=1.0e-7)
        model.nonparam_estim.estimate_hawkes_kernel(
            store_L1_norm=False,
            use_filter=True,
            enforce_positive_g_hat=True,
            filter_cutoff=20.0,
            filter_scale=30.0,
            num_addpnts_filter=2000,
            parallel=False,
            parallel_prep=True)
        model.nonparam_estim.fit_powerlaw(compute_L1_norm=True,
                                          ridge_param=1.0e-01,
                                          tol=1.0e-7)
        model.nonparam_estim.store_base_rates()
        run_time += time.time()
        model.nonparam_estim.store_runtime(run_time)
        model.nonparam_estim.create_goodness_of_fit(parallel=False)

    print("\nMLE ESTIMATION\n")
    "Initialise the class"
    model.create_mle_estim(type_of_input='simulated', store_trans_prob=True)
    "Set the estimation"
    if type_of_preestim == 'nonparam':
        list_init_guesses = model.nonparam_estim.produce_list_init_guesses_for_mle_estimation(
            num_additional_random_guesses=2, max_imp_coef=50.0)
    else:
        list_init_guesses = []
    model.mle_estim.set_estimation_of_hawkes_param(
        time_start,
        time_end,
        list_of_init_guesses=list_init_guesses,
        max_imp_coef=max_imp_coef,
        learning_rate=learning_rate,
        maxiter=maxiter,
        number_of_additional_guesses=num_guesses,
        parallel=parallel,
        pre_estim_ord_hawkes=True,
        pre_estim_parallel=parallel,
        number_of_attempts=3,
        num_processes=num_processes,
        batch_size=batch_size,
        num_run_per_minibatch=num_run_per_minibatch,
    )
    #     exit()
    "Launch estimation"
    model.mle_estim.launch_estimation_of_hawkes_param(partial=True)
    model.mle_estim.store_hawkes_parameters()
    model.mle_estim.create_goodness_of_fit(parallel=False)
    model.dump(path=path_saved_tests)