-
Notifications
You must be signed in to change notification settings - Fork 107
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Hawkes cumulants - Tensorflow v1 #505
Changes from 6 commits
f7b17ea
e9ce6dd
6d6b943
bee8a54
55e8a2a
449fa52
11353f4
fa43f6d
37844c3
837331e
9cc6090
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,8 +44,7 @@ SArrayDoublePtr HawkesCumulant::compute_A_and_I_ij(ulong r, ulong i, ulong j, | |
if (abs_t_j_l_minus_t_i_k < width) { | ||
sub_res += width - abs_t_j_l_minus_t_i_k; | ||
|
||
if (abs_t_j_l_minus_t_i_k < integration_support) | ||
timestamps_in_interval++; | ||
if (abs_t_j_l_minus_t_i_k < integration_support) timestamps_in_interval++; | ||
} else { | ||
break; | ||
} | ||
|
@@ -64,8 +63,7 @@ SArrayDoublePtr HawkesCumulant::compute_A_and_I_ij(ulong r, ulong i, ulong j, | |
return return_array.as_sarray_ptr(); | ||
} | ||
|
||
double HawkesCumulant::compute_E_ijk(ulong r, ulong i, ulong j, ulong k, | ||
double mean_intensity_i, | ||
double HawkesCumulant::compute_E_ijk(ulong r, ulong i, ulong j, ulong k, double mean_intensity_i, | ||
double mean_intensity_j, double J_ij) { | ||
auto timestamps_i = timestamps_list[r][i]; | ||
auto timestamps_j = timestamps_list[r][j]; | ||
|
@@ -126,3 +124,66 @@ double HawkesCumulant::compute_E_ijk(ulong r, ulong i, ulong j, ulong k, | |
res /= (*end_times)[r]; | ||
return res; | ||
} | ||
|
||
HawkesTheoreticalCumulant::HawkesTheoreticalCumulant(int dim) : d(dim) { | ||
Lambda = SArrayDouble::new_ptr(dim); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The variables I agree that the upper case is uncommon. I can change the name as follows: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done here: 11353f4 |
||
C = SArrayDouble2d::new_ptr(dim, dim); | ||
Kc = SArrayDouble2d::new_ptr(dim, dim); | ||
R = SArrayDouble2d::new_ptr(dim, dim); | ||
} | ||
|
||
// Formulae from eq. 7 in the paper | ||
void HawkesTheoreticalCumulant::compute_mean_intensity() { | ||
for (int i = 0; i < d; ++i) { | ||
double lambda_i = 0; | ||
for (int m = 0; m < d; ++m) { | ||
int _im = i * d + m; | ||
double mu_m = (*mu)[m]; | ||
double R_im = (*R)[_im]; | ||
lambda_i += R_im * mu_m; | ||
} | ||
(*Lambda)[i] = lambda_i; | ||
} | ||
}; | ||
|
||
// Formulae from eq. 8 in the paper | ||
void HawkesTheoreticalCumulant::compute_covariance() { | ||
for (int i = 0; i < d; ++i) { | ||
for (int j = 0; j < d; ++j) { | ||
int _ij = i * d + j; | ||
double C_ij = 0; | ||
for (int m = 0; m < d; ++m) { | ||
int _im = i * d + m; | ||
int _jm = j * d + m; | ||
C_ij += (*Lambda)[m] * (*R)[_im] * (*R)[_jm]; | ||
} | ||
(*C)[_ij] = C_ij; | ||
} | ||
} | ||
}; | ||
|
||
// Formulae from eq. 9 in the paper | ||
void HawkesTheoreticalCumulant::compute_skewness() { | ||
for (int i = 0; i < d; ++i) { | ||
for (int k = 0; k < d; ++k) { | ||
int _ik = i * d + k; | ||
double Kc_ik = 0; | ||
for (int m = 0; m < d; ++m) { | ||
int _im = i * d + m; | ||
int _km = k * d + m; | ||
double R_im = (*R)[_im]; | ||
double R_km = (*R)[_km]; | ||
double C_km = (*C)[_km]; | ||
double C_im = (*C)[_im]; | ||
double Lambda_m = (*Lambda)[m]; | ||
Kc_ik += (R_im * R_im * C_km + 2 * R_im * R_km * C_im - 2 * Lambda_m * R_im * R_im * R_km); | ||
} | ||
(*Kc)[_ik] = Kc_ik; | ||
} | ||
} | ||
}; | ||
void HawkesTheoreticalCumulant::compute_cumulants() { | ||
compute_mean_intensity(); | ||
compute_covariance(); | ||
compute_skewness(); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,8 +13,7 @@ Hawkes::Hawkes(unsigned int n_nodes, int seed) | |
} | ||
} | ||
|
||
void Hawkes::init_intensity_(ArrayDouble &intensity, | ||
double *total_intensity_bound) { | ||
void Hawkes::init_intensity_(ArrayDouble &intensity, double *total_intensity_bound) { | ||
*total_intensity_bound = 0; | ||
for (unsigned int i = 0; i < n_nodes; i++) { | ||
intensity[i] = get_baseline(i, 0.); | ||
|
@@ -30,16 +29,14 @@ bool Hawkes::update_time_shift_(double delay, ArrayDouble &intensity, | |
// We loop on the contributions | ||
for (unsigned int i = 0; i < n_nodes; i++) { | ||
intensity[i] = get_baseline(i, get_time()); | ||
if (total_intensity_bound1) | ||
*total_intensity_bound1 += get_baseline_bound(i, get_time()); | ||
if (total_intensity_bound1) *total_intensity_bound1 += get_baseline_bound(i, get_time()); | ||
|
||
for (unsigned int j = 0; j < n_nodes; j++) { | ||
HawkesKernelPtr &k = kernels[i * n_nodes + j]; | ||
|
||
if (k->get_support() == 0) continue; | ||
double bound = 0; | ||
intensity[i] += | ||
k->get_convolution(get_time() + delay, *timestamps[j], &bound); | ||
intensity[i] += k->get_convolution(get_time() + delay, *timestamps[j], &bound); | ||
|
||
if (total_intensity_bound1) { | ||
*total_intensity_bound1 += bound; | ||
|
@@ -56,15 +53,13 @@ bool Hawkes::update_time_shift_(double delay, ArrayDouble &intensity, | |
void Hawkes::reset() { | ||
for (unsigned int i = 0; i < n_nodes; i++) { | ||
for (unsigned int j = 0; j < n_nodes; j++) { | ||
if (kernels[i * n_nodes + j] != nullptr) | ||
kernels[i * n_nodes + j]->rewind(); | ||
if (kernels[i * n_nodes + j] != nullptr) kernels[i * n_nodes + j]->rewind(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I know it was There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Understood... |
||
} | ||
} | ||
PP::reset(); | ||
} | ||
|
||
void Hawkes::set_kernel(unsigned int i, unsigned int j, | ||
HawkesKernelPtr &kernel) { | ||
void Hawkes::set_kernel(unsigned int i, unsigned int j, HawkesKernelPtr &kernel) { | ||
if (i >= n_nodes) TICK_BAD_INDEX(0, n_nodes, i); | ||
if (j >= n_nodes) TICK_BAD_INDEX(0, n_nodes, j); | ||
|
||
|
@@ -100,8 +95,7 @@ void Hawkes::set_baseline(unsigned int i, TimeFunction time_function) { | |
set_baseline(i, std::make_shared<HawkesTimeFunctionBaseline>(time_function)); | ||
} | ||
|
||
void Hawkes::set_baseline(unsigned int i, ArrayDouble ×, | ||
ArrayDouble &values) { | ||
void Hawkes::set_baseline(unsigned int i, ArrayDouble ×, ArrayDouble &values) { | ||
set_baseline(i, std::make_shared<HawkesTimeFunctionBaseline>(times, values)); | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,12 +13,10 @@ class DLL_PUBLIC HawkesCumulant : public ModelHawkesList { | |
public: | ||
explicit HawkesCumulant(double integration_support); | ||
|
||
SArrayDoublePtr compute_A_and_I_ij(ulong r, ulong i, ulong j, | ||
double mean_intensity_j); | ||
SArrayDoublePtr compute_A_and_I_ij(ulong r, ulong i, ulong j, double mean_intensity_j); | ||
|
||
double compute_E_ijk(ulong r, ulong i, ulong j, ulong k, | ||
double mean_intensity_i, double mean_intensity_j, | ||
double J_ij); | ||
double compute_E_ijk(ulong r, ulong i, ulong j, ulong k, double mean_intensity_i, | ||
double mean_intensity_j, double J_ij); | ||
|
||
double get_integration_support() const { return integration_support; } | ||
|
||
|
@@ -35,4 +33,29 @@ class DLL_PUBLIC HawkesCumulant : public ModelHawkesList { | |
} | ||
}; | ||
|
||
class DLL_PUBLIC HawkesTheoreticalCumulant { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a new c++ class that implements the theoretical formulae for mean intensity, covariance and skewness of hawkes processes. These are the formulae (7), (8), and (9) in the paper. |
||
private: | ||
int d; | ||
SArrayDoublePtr mu; | ||
SArrayDoublePtr Lambda; | ||
SArrayDouble2dPtr C; | ||
SArrayDouble2dPtr Kc; | ||
SArrayDouble2dPtr R; | ||
|
||
public: | ||
HawkesTheoreticalCumulant(int); | ||
int get_dimension() { return d; } | ||
void set_baseline(const SArrayDoublePtr mu) { this->mu = mu; } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We might not have it everywhere, but There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Changed: fa43f6d |
||
SArrayDoublePtr get_baseline() { return mu; } | ||
void set_R(const SArrayDouble2dPtr R) { this->R = R; } | ||
SArrayDouble2dPtr get_R() { return R; } | ||
void compute_mean_intensity(); | ||
void compute_covariance(); | ||
void compute_skewness(); | ||
void compute_cumulants(); | ||
SArrayDoublePtr mean_intensity() { return Lambda; } | ||
SArrayDouble2dPtr covariance() { return C; } | ||
SArrayDouble2dPtr skewness() { return Kc; } | ||
}; | ||
|
||
#endif // LIB_INCLUDE_TICK_HAWKES_INFERENCE_HAWKES_CUMULANT_H_ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can these be part of the constuctor setup arguments like
: d(dim)
?