Пример #1
0
def demo(seed=None):
    """
    Create a discrete time Hawkes model and generate from it.

    :return:
    """
    raise NotImplementedError("This example needs to be updated.")

    if seed is None:
        seed = np.random.randint(2**32)

    print("Setting seed to ", seed)
    np.random.seed(seed)

    C = 1  # Number of clusters in the true data
    K = 10  # Number of nodes
    T = 1000  # Number of time bins to simulate
    dt = 0.02  # Time bin size
    dt_max = 0.08
    B = 3  # Number of basis functions

    # Sample from a sparse network Hawkes model
    S, true_model = sample_from_network_hawkes(C, K, T, dt, dt_max, B)

    # Make a new model for inference
    test_basis = IdentityBasis(dt, dt_max, allow_instantaneous=False)
    test_model = DiscreteTimeStandardHawkesModel(K=K,
                                                 dt=dt,
                                                 dt_max=dt_max + dt,
                                                 beta=1.0,
                                                 basis=test_basis,
                                                 allow_self_connections=True)
    test_model.add_data(S)

    # DEBUG: Initialize with the true parameters of the network Hawkes model
    # test_model.initialize_with_gibbs_model(true_model)

    test_model.fit_with_bfgs()

    print("W true:        ",
          true_model.weight_model.A * true_model.weight_model.W)
    print("lambda0 true:  ", true_model.bias_model.lambda0)
    print("ll true:       ", true_model.log_likelihood())
    print("")
    print("W test:        ", test_model.W)
    print("lambda0 test   ", test_model.bias)
    print("ll test:       ", test_model.log_likelihood())

    plot_network(np.ones((K, K)), test_model.W, vmax=0.5)

    # Plot the rates
    plt.figure()
    for k in range(3):
        plt.subplot(3, 1, k + 1)
        plt.plot(np.arange(T) * dt, true_model.compute_rate(proc=k), '-b')
        plt.plot(np.arange(T) * dt, test_model.compute_rate(ks=k), '-r')

    plt.ioff()
    plt.show()
Пример #2
0
def demo(seed=None):
    """
    Create a discrete time Hawkes model and generate from it.

    :return:
    """
    if seed is None:
        seed = np.random.randint(2**32)

    print("Setting seed to ", seed)
    np.random.seed(seed)

    K = 5       # Number of nodes
    T = 10000     # Number of time bins to simulate
    dt = 1       # Time bin size
    dt_max = 50  # Impulse response length
    B = 1        # Number of basis functions

    # Sample from a sparse network Hawkes model
    S, true_model = sample_from_network_hawkes(K, T, dt, dt_max, B)

    # Make a new model for inference
    # test_basis = IdentityBasis(dt, dt_max, allow_instantaneous=False)
    test_basis = true_model.basis
    test_model = DiscreteTimeStandardHawkesModel(K=K, dt=dt, dt_max=dt_max+dt,
                                                 beta=1.0,
                                                 basis=test_basis,
                                                 allow_self_connections=True)
    test_model.add_data(S)

    # DEBUG: Initialize with the true parameters of the network Hawkes model
    # test_model.initialize_with_gibbs_model(true_model)

    test_model.fit_with_bfgs()

    print("lambda0 true:  ", true_model.bias_model.lambda0)
    print("lambda0 test   ", test_model.bias)

    print("")
    print("W true:        ", true_model.weight_model.A * true_model.weight_model.W)
    print("W test:        ", test_model.W)

    print("")
    print("ll true:       ", true_model.log_likelihood())
    print("ll test:       ", test_model.log_likelihood())

    # test_model.plot_network()

    # Plot the rates
    plt.figure()
    for k in range(3):
        plt.subplot(3,1,k+1)
        plt.plot(np.arange(T) * dt, true_model.compute_rate(proc=k), '-b')
        plt.plot(np.arange(T) * dt, test_model.compute_rate(ks=k), '-r')
        lim = plt.ylim()
        plt.ylim(0, 1.25*lim[1])

    plt.ioff()
    plt.show()
Пример #3
0
def demo(seed=None):
    """
    Create a discrete time Hawkes model and generate from it.

    :return:
    """
    raise NotImplementedError("This example needs to be updated.")

    if seed is None:
        seed = np.random.randint(2**32)

    print "Setting seed to ", seed
    np.random.seed(seed)

    C = 1       # Number of clusters in the true data
    K = 10      # Number of nodes
    T = 1000    # Number of time bins to simulate
    dt = 0.02   # Time bin size
    dt_max = 0.08
    B = 3       # Number of basis functions

    # Sample from a sparse network Hawkes model
    S, true_model = sample_from_network_hawkes(C, K, T, dt, dt_max, B)

    # Make a new model for inference
    test_basis = IdentityBasis(dt, dt_max, allow_instantaneous=False)
    test_model = DiscreteTimeStandardHawkesModel(K=K, dt=dt, dt_max=dt_max+dt,
                                                 beta=1.0,
                                                 basis=test_basis,
                                                 allow_self_connections=True)
    test_model.add_data(S)

    # DEBUG: Initialize with the true parameters of the network Hawkes model
    # test_model.initialize_with_gibbs_model(true_model)

    test_model.fit_with_bfgs()

    print "W true:        ", true_model.weight_model.A * true_model.weight_model.W
    print "lambda0 true:  ", true_model.bias_model.lambda0
    print "ll true:       ", true_model.log_likelihood()
    print ""
    print "W test:        ", test_model.W
    print "lambda0 test   ", test_model.bias
    print "ll test:       ", test_model.log_likelihood()

    plot_network(np.ones((K,K)), test_model.W, vmax=0.5)

    # Plot the rates
    plt.figure()
    for k in xrange(3):
        plt.subplot(3,1,k+1)
        plt.plot(np.arange(T) * dt, true_model.compute_rate(proc=k), '-b')
        plt.plot(np.arange(T) * dt, test_model.compute_rate(ks=k), '-r')

    plt.ioff()
    plt.show()
Пример #4
0
def demo(seed=None):
    """
    Suppose we have a very long recording such that computing gradients of
    the log likelihood is quite expensive. Here we explore the use of
    stochastic gradient descent to fit the standard Hawkes model, which has
    a convex log likelihood. We first initialize the parameters using BFGS
    on a manageable subset of the data. Then we use SGD to refine the parameters
    on the entire dataset.

    :return:
    """
    if seed is None:
        seed = np.random.randint(2**32)

    print "Setting seed to ", seed
    np.random.seed(seed)

    C = 1       # Number of clusters in the true data
    K = 10      # Number of nodes
    T = 10000   # Number of time bins to simulate
    dt = 1.0    # Time bin size
    B = 3       # Number of basis functions

    # Sample from the network Hawkes model
    S, R, true_model = sample_from_network_hawkes(C, K, T, dt, B)

    # Make a model to initialize the parameters
    init_len   = 256
    init_model = DiscreteTimeStandardHawkesModel(K=K, dt=dt, B=B, beta=1.0)
    init_model.add_data(S[:init_len, :])

    print "Initializing with BFGS on first ", init_len, " time bins."
    init_model.fit_with_bfgs()

    # Make another model for inference
    test_model = DiscreteTimeStandardHawkesModel(K=K, dt=dt, B=B, beta=1.0)
    # Initialize with the BFGS parameters
    test_model.weights = init_model.weights
    # Add the data in minibatches
    test_model.add_data(S, minibatchsize=256)

    # Plot the true and inferred firing rate
    kplt = 0
    plt.figure()
    plt.plot(np.arange(256), R[:256,kplt], '-k', lw=2)
    plt.ion()
    ln = plt.plot(np.arange(256), test_model.compute_rate(ks=kplt)[:256], '-r')[0]
    plt.show()

    # Gradient descent
    N_steps = 10000
    lls = []
    learning_rate = 0.01 * np.ones(N_steps)
    momentum = 0.8 * np.ones(N_steps)
    prev_velocity = None
    for itr in xrange(N_steps):
        W,ll,prev_velocity = test_model.sgd_step(prev_velocity, learning_rate[itr], momentum[itr])
        lls.append(ll)

        # Update plot
        if itr % 5 == 0:
            ln.set_data(np.arange(256), test_model.compute_rate(ks=kplt))
            plt.title("Iteration %d" % itr)
            plt.pause(0.001)

    plt.ioff()

    print "W true:        ", true_model.weight_model.A * true_model.weight_model.W
    print "lambda0 true:  ", true_model.bias_model.lambda0
    print ""
    print "W test:        ", test_model.W
    print "lambda0 test   ", test_model.bias

    plt.figure()
    plt.plot(np.arange(N_steps), lls)
    plt.xlabel("Iteration")
    plt.ylabel("Log likelihood")

    plot_network(np.ones((K,K)), test_model.W)
    plt.show()
def demo(seed=None):
    """
    Suppose we have a very long recording such that computing gradients of
    the log likelihood is quite expensive. Here we explore the use of
    stochastic gradient descent to fit the standard Hawkes model, which has
    a convex log likelihood. We first initialize the parameters using BFGS
    on a manageable subset of the data. Then we use SGD to refine the parameters
    on the entire dataset.

    :return:
    """
    raise NotImplementedError("This example needs to be updated.")

    if seed is None:
        seed = np.random.randint(2**32)

    print("Setting seed to ", seed)
    np.random.seed(seed)

    C = 1  # Number of clusters in the true data
    K = 10  # Number of nodes
    T = 10000  # Number of time bins to simulate
    dt = 1.0  # Time bin size
    B = 3  # Number of basis functions

    # Sample from the network Hawkes model
    S, R, true_model = sample_from_network_hawkes(C, K, T, dt, B)

    # Make a model to initialize the parameters
    init_len = 256
    init_model = DiscreteTimeStandardHawkesModel(K=K, dt=dt, B=B, beta=1.0)
    init_model.add_data(S[:init_len, :])

    print("Initializing with BFGS on first ", init_len, " time bins.")
    init_model.fit_with_bfgs()

    # Make another model for inference
    test_model = DiscreteTimeStandardHawkesModel(K=K, dt=dt, B=B, beta=1.0)
    # Initialize with the BFGS parameters
    test_model.weights = init_model.weights
    # Add the data in minibatches
    test_model.add_data(S, minibatchsize=256)

    # Plot the true and inferred firing rate
    kplt = 0
    plt.figure()
    plt.plot(np.arange(256), R[:256, kplt], '-k', lw=2)
    plt.ion()
    ln = plt.plot(np.arange(256),
                  test_model.compute_rate(ks=kplt)[:256], '-r')[0]
    plt.show()

    # Gradient descent
    N_steps = 10000
    lls = []
    learning_rate = 0.01 * np.ones(N_steps)
    momentum = 0.8 * np.ones(N_steps)
    prev_velocity = None
    for itr in range(N_steps):
        W, ll, prev_velocity = test_model.sgd_step(prev_velocity,
                                                   learning_rate[itr],
                                                   momentum[itr])
        lls.append(ll)

        # Update plot
        if itr % 5 == 0:
            ln.set_data(np.arange(256), test_model.compute_rate(ks=kplt))
            plt.title("Iteration %d" % itr)
            plt.pause(0.001)

    plt.ioff()

    print("W true:        ",
          true_model.weight_model.A * true_model.weight_model.W)
    print("lambda0 true:  ", true_model.bias_model.lambda0)
    print("")
    print("W test:        ", test_model.W)
    print("lambda0 test   ", test_model.bias)

    plt.figure()
    plt.plot(np.arange(N_steps), lls)
    plt.xlabel("Iteration")
    plt.ylabel("Log likelihood")

    plot_network(np.ones((K, K)), test_model.W)
    plt.show()
Пример #6
0
def demo(seed=None):
    """
    Create a discrete time Hawkes model and generate from it.

    :return:
    """
    raise NotImplementedError("This example needs to be updated.")

    if seed is None:
        seed = np.random.randint(2**32)

    print "Setting seed to ", seed
    np.random.seed(seed)

    C = 1
    K = 10
    T = 1000
    dt = 1.0
    B = 3

    # Create a true model
    p = 0.8 * np.eye(C)
    v = 10.0 * np.eye(C) + 20.0 * (1-np.eye(C))
    # m = 0.5 * np.ones(C)
    c = (0.0 * (np.arange(K) < 10) + 1.0 * (np.arange(K)  >= 10)).astype(np.int)
    true_model = DiscreteTimeNetworkHawkesModelSpikeAndSlab(C=C, K=K, dt=dt, B=B, c=c, p=p, v=v)

    # Plot the true network
    plt.ion()
    plot_network(true_model.weight_model.A,
                 true_model.weight_model.W,
                 vmax=0.5)
    plt.pause(0.001)

    # Sample from the true model
    S,R = true_model.generate(T=T)


    # Make a new model for inference
    test_model = DiscreteTimeStandardHawkesModel(K=K, dt=dt, B=B, beta=1.0)
    test_model.add_data(S)

    # Plot the true and inferred firing rate
    kplt = 0
    plt.figure()
    plt.plot(np.arange(T), R[:,kplt], '-k', lw=2)
    plt.ion()
    ln = plt.plot(np.arange(T), test_model.compute_rate(ks=kplt), '-r')[0]
    plt.show()

    # Gradient descent
    N_steps = 10000
    lls = []
    for itr in xrange(N_steps):
        W,ll,grad = test_model.gradient_descent_step(stepsz=0.001)
        lls.append(ll)

        # Update plot
        if itr % 5 == 0:
            ln.set_data(np.arange(T), test_model.compute_rate(ks=kplt))
            plt.title("Iteration %d" % itr)
            plt.pause(0.001)

    plt.ioff()

    print "W true:        ", true_model.weight_model.A * true_model.weight_model.W
    print "lambda0 true:  ", true_model.bias_model.lambda0
    print "ll true:       ", true_model.log_likelihood()
    print ""
    print "W test:        ", test_model.W
    print "lambda0 test   ", test_model.bias
    print "ll test:       ", test_model.log_likelihood()


    plt.figure()
    plt.plot(np.arange(N_steps), lls)
    plt.xlabel("Iteration")
    plt.ylabel("Log likelihood")

    plot_network(np.ones((K,K)), test_model.W, vmax=0.5)
    plt.show()
Пример #7
0
def demo(seed=None):
    """
    Create a discrete time Hawkes model and generate from it.

    :return:
    """
    if seed is None:
        seed = np.random.randint(2**32)

    print "Setting seed to ", seed
    np.random.seed(seed)

    C = 1
    K = 10
    T = 1000
    dt = 1.0
    B = 3

    # Create a true model
    p = 0.8 * np.eye(C)
    v = 10.0 * np.eye(C) + 20.0 * (1 - np.eye(C))
    # m = 0.5 * np.ones(C)
    c = (0.0 * (np.arange(K) < 10) + 1.0 * (np.arange(K) >= 10)).astype(np.int)
    true_model = DiscreteTimeNetworkHawkesModelSpikeAndSlab(C=C,
                                                            K=K,
                                                            dt=dt,
                                                            B=B,
                                                            c=c,
                                                            p=p,
                                                            v=v)

    # Plot the true network
    plt.ion()
    plot_network(true_model.weight_model.A,
                 true_model.weight_model.W,
                 vmax=0.5)
    plt.pause(0.001)

    # Sample from the true model
    S, R = true_model.generate(T=T)

    # Make a new model for inference
    test_model = DiscreteTimeStandardHawkesModel(K=K, dt=dt, B=B, beta=1.0)
    test_model.add_data(S)

    # Plot the true and inferred firing rate
    kplt = 0
    plt.figure()
    plt.plot(np.arange(T), R[:, kplt], '-k', lw=2)
    plt.ion()
    ln = plt.plot(np.arange(T), test_model.compute_rate(ks=kplt), '-r')[0]
    plt.show()

    # Gradient descent
    N_steps = 10000
    lls = []
    for itr in xrange(N_steps):
        W, ll, grad = test_model.gradient_descent_step(stepsz=0.001)
        lls.append(ll)

        # Update plot
        if itr % 5 == 0:
            ln.set_data(np.arange(T), test_model.compute_rate(ks=kplt))
            plt.title("Iteration %d" % itr)
            plt.pause(0.001)

    plt.ioff()

    print "W true:        ", true_model.weight_model.A * true_model.weight_model.W
    print "lambda0 true:  ", true_model.bias_model.lambda0
    print "ll true:       ", true_model.log_likelihood()
    print ""
    print "W test:        ", test_model.W
    print "lambda0 test   ", test_model.bias
    print "ll test:       ", test_model.log_likelihood()

    plt.figure()
    plt.plot(np.arange(N_steps), lls)
    plt.xlabel("Iteration")
    plt.ylabel("Log likelihood")

    plot_network(np.ones((K, K)), test_model.W, vmax=0.5)
    plt.show()