示例#1
0
def SimExp(baseline, adjacency, decays, num_clusters, data):
    hawkes = SimuHawkesExpKernels(adjacency=adjacency,
                                  decays=decays,
                                  baseline=baseline,
                                  verbose=False)

    #dt = 0.001 #millisecond granularity
    #hawkes.track_intensity(dt) # turning this on will eat up memory

    #need to compute and draw from the cluster length distrbution from the original data
    cluster_lengths = ComputeClusterLengths(data)

    multi = SimuHawkesMulti(hawkes, n_simulations=num_clusters)

    multi.end_time = np.random.choice(cluster_lengths,
                                      size=num_clusters,
                                      replace=True)
    multi.simulate()
    sim_inner_timestamps = multi.timestamps

    l = 0
    for realisation in sim_inner_timestamps:
        for series in realisation:
            l += len(series)
    print(f"Simulated {l} points")
    return sim_inner_timestamps
示例#2
0
    def _sim_single_exposures(self):
        if not self.sparse:
            raise ValueError(
                "'single_exposure' exposures can only be simulated"
                " as sparse feature matrices")

        if self.hawkes_exp_kernels is None:
            np.random.seed(self.seed)
            decays = .002 * np.ones((self.n_features, self.n_features))
            baseline = 4 * np.random.random(self.n_features) / self.n_intervals
            mult = np.random.random(self.n_features)
            adjacency = mult * np.eye(self.n_features)

            if self.n_correlations:
                comb = list(permutations(range(self.n_features), 2))
                if len(comb) > 1:
                    idx = itemgetter(
                        *np.random.choice(range(len(comb)),
                                          size=self.n_correlations,
                                          replace=False))
                    comb = idx(comb)

                for i, j in comb:
                    adjacency[i, j] = np.random.random(1)

            self._set(
                'hawkes_exp_kernels',
                SimuHawkesExpKernels(adjacency=adjacency,
                                     decays=decays,
                                     baseline=baseline,
                                     verbose=False,
                                     seed=self.seed))

        self.hawkes_exp_kernels.adjust_spectral_radius(
            .1)  # TODO later: allow to change this parameter
        hawkes = SimuHawkesMulti(self.hawkes_exp_kernels,
                                 n_simulations=self.n_cases)

        run_time = self.n_intervals
        hawkes.end_time = [1 * run_time for _ in range(self.n_cases)]
        dt = 1
        self.hawkes_exp_kernels.track_intensity(dt)
        hawkes.simulate()

        self.hawkes_obj = hawkes
        features = [[
            np.min(np.floor(f)) if len(f) > 0 else -1 for f in patient_events
        ] for patient_events in hawkes.timestamps]

        features = [
            self.to_coo(feat, (run_time, self.n_features)) for feat in features
        ]

        # Make sure patients have at least one exposure?
        exposures_filter = itemgetter(
            *[i for i, f in enumerate(features) if f.sum() > 0])
        features = exposures_filter(features)
        n_samples = len(features)

        return features, n_samples
示例#3
0
if __name__ == '__main__':
    end_time = 50000
    n_realizations = 1
    decay = 3
    baseline =np.ones(6) * .03
    adjacency = np.zeros((6, 6))
    adjacency[2:, 2:] = np.ones((4, 4)) * 0.1
    adjacency[:3, :3] = np.ones((3, 3)) * 0.15

    hawkes_exp_kernels = SimuHawkesExpKernels(adjacency=adjacency, decays=decay,
                                              baseline=baseline, end_time=end_time,
                                              verbose=False, seed=1039)

    multi = SimuHawkesMulti(hawkes_exp_kernels, n_simulations=n_realizations)
    multi.end_time = [(i + 1) / n_realizations * end_time for i in range(n_realizations)]
    multi.simulate()

    type_seq = []
    time_seq = []
    for i, d in enumerate(multi.timestamps[0]):
        for j in d:
            type_seq.append(i)
            time_seq.append(j)
    type_seq = np.array(type_seq)
    time_seq = np.array(time_seq)

    type_seq = type_seq[np.argsort(time_seq)]
    time_seq = time_seq[np.argsort(time_seq)]

    para = Parameters(decay=3, K=6)