|
|
|
@ -59,7 +59,7 @@ fn simple_bic() { |
|
|
|
|
); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fn check_compatibility_between_dataset_and_network<T: StructureLearningAlgorithm>(mut sl: T) { |
|
|
|
|
fn check_compatibility_between_dataset_and_network<T: StructureLearningAlgorithm>(sl: T) { |
|
|
|
|
let mut net = CtbnNetwork::new(); |
|
|
|
|
let n1 = net |
|
|
|
|
.add_node(generate_discrete_time_continous_node(String::from("n1"), 3)) |
|
|
|
@ -126,7 +126,7 @@ pub fn check_compatibility_between_dataset_and_network_hill_climbing() { |
|
|
|
|
check_compatibility_between_dataset_and_network(hl); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fn learn_ternary_net_2_nodes<T: StructureLearningAlgorithm>(mut sl: T) { |
|
|
|
|
fn learn_ternary_net_2_nodes<T: StructureLearningAlgorithm>(sl: T) { |
|
|
|
|
let mut net = CtbnNetwork::new(); |
|
|
|
|
let n1 = net |
|
|
|
|
.add_node(generate_discrete_time_continous_node(String::from("n1"), 3)) |
|
|
|
@ -321,7 +321,7 @@ fn get_mixed_discrete_net_3_nodes_with_data() -> (CtbnNetwork, Dataset) { |
|
|
|
|
return (net, data); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fn learn_mixed_discrete_net_3_nodes<T: StructureLearningAlgorithm>(mut sl: T) { |
|
|
|
|
fn learn_mixed_discrete_net_3_nodes<T: StructureLearningAlgorithm>(sl: T) { |
|
|
|
|
let (net, data) = get_mixed_discrete_net_3_nodes_with_data(); |
|
|
|
|
let net = sl.fit_transform(net, &data); |
|
|
|
|
assert_eq!(BTreeSet::new(), net.get_parent_set(0)); |
|
|
|
@ -343,7 +343,7 @@ pub fn learn_mixed_discrete_net_3_nodes_hill_climbing_bic() { |
|
|
|
|
learn_mixed_discrete_net_3_nodes(hl); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fn learn_mixed_discrete_net_3_nodes_1_parent_constraint<T: StructureLearningAlgorithm>(mut sl: T) { |
|
|
|
|
fn learn_mixed_discrete_net_3_nodes_1_parent_constraint<T: StructureLearningAlgorithm>(sl: T) { |
|
|
|
|
let (net, data) = get_mixed_discrete_net_3_nodes_with_data(); |
|
|
|
|
let net = sl.fit_transform(net, &data); |
|
|
|
|
assert_eq!(BTreeSet::new(), net.get_parent_set(0)); |
|
|
|
@ -393,7 +393,7 @@ pub fn chi_square_compare_matrices() { |
|
|
|
|
[ 700, 800, 0] |
|
|
|
|
], |
|
|
|
|
]); |
|
|
|
|
let chi_sq = ChiSquare::new(0.1); |
|
|
|
|
let chi_sq = ChiSquare::new(1e-4); |
|
|
|
|
assert!(!chi_sq.compare_matrices(i, &M1, j, &M2)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -423,7 +423,7 @@ pub fn chi_square_compare_matrices_2() { |
|
|
|
|
[ 400, 0, 600], |
|
|
|
|
[ 700, 800, 0]] |
|
|
|
|
]); |
|
|
|
|
let chi_sq = ChiSquare::new(0.1); |
|
|
|
|
let chi_sq = ChiSquare::new(1e-4); |
|
|
|
|
assert!(chi_sq.compare_matrices(i, &M1, j, &M2)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -455,7 +455,7 @@ pub fn chi_square_compare_matrices_3() { |
|
|
|
|
[ 700, 800, 0] |
|
|
|
|
], |
|
|
|
|
]); |
|
|
|
|
let chi_sq = ChiSquare::new(0.1); |
|
|
|
|
let chi_sq = ChiSquare::new(1e-4); |
|
|
|
|
assert!(chi_sq.compare_matrices(i, &M1, j, &M2)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -469,14 +469,14 @@ pub fn chi_square_call() { |
|
|
|
|
let N1: usize = 0; |
|
|
|
|
let mut separation_set = BTreeSet::new(); |
|
|
|
|
let parameter_learning = BayesianApproach { alpha: 1, tau:1.0 }; |
|
|
|
|
let mut cache = Cache::new(parameter_learning, data); |
|
|
|
|
let chi_sq = ChiSquare::new(0.0001); |
|
|
|
|
let mut cache = Cache::new(¶meter_learning); |
|
|
|
|
let chi_sq = ChiSquare::new(1e-4); |
|
|
|
|
|
|
|
|
|
assert!(chi_sq.call(&net, N1, N3, &separation_set, &mut cache)); |
|
|
|
|
assert!(!chi_sq.call(&net, N3, N1, &separation_set, &mut cache)); |
|
|
|
|
assert!(!chi_sq.call(&net, N3, N2, &separation_set, &mut cache)); |
|
|
|
|
assert!(chi_sq.call(&net, N1, N3, &separation_set, &data, &mut cache)); |
|
|
|
|
assert!(!chi_sq.call(&net, N3, N1, &separation_set, &data, &mut cache)); |
|
|
|
|
assert!(!chi_sq.call(&net, N3, N2, &separation_set, &data, &mut cache)); |
|
|
|
|
separation_set.insert(N1); |
|
|
|
|
assert!(chi_sq.call(&net, N2, N3, &separation_set, &mut cache)); |
|
|
|
|
assert!(chi_sq.call(&net, N2, N3, &separation_set, &data, &mut cache)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
#[test] |
|
|
|
@ -488,91 +488,31 @@ pub fn f_call() { |
|
|
|
|
let N1: usize = 0; |
|
|
|
|
let mut separation_set = BTreeSet::new(); |
|
|
|
|
let parameter_learning = BayesianApproach { alpha: 1, tau:1.0 }; |
|
|
|
|
let mut cache = Cache::new(parameter_learning, data); |
|
|
|
|
let f = F::new(0.000001); |
|
|
|
|
let mut cache = Cache::new(¶meter_learning); |
|
|
|
|
let f = F::new(1e-6); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
assert!(f.call(&net, N1, N3, &separation_set, &mut cache)); |
|
|
|
|
assert!(!f.call(&net, N3, N1, &separation_set, &mut cache)); |
|
|
|
|
assert!(!f.call(&net, N3, N2, &separation_set, &mut cache)); |
|
|
|
|
assert!(f.call(&net, N1, N3, &separation_set, &data, &mut cache)); |
|
|
|
|
assert!(!f.call(&net, N3, N1, &separation_set, &data, &mut cache)); |
|
|
|
|
assert!(!f.call(&net, N3, N2, &separation_set, &data, &mut cache)); |
|
|
|
|
separation_set.insert(N1); |
|
|
|
|
assert!(f.call(&net, N2, N3, &separation_set, &mut cache)); |
|
|
|
|
assert!(f.call(&net, N2, N3, &separation_set, &data, &mut cache)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
#[test] |
|
|
|
|
pub fn learn_ternary_net_2_nodes_ctpc() { |
|
|
|
|
let mut net = CtbnNetwork::new(); |
|
|
|
|
let n1 = net |
|
|
|
|
.add_node(generate_discrete_time_continous_node(String::from("n1"), 3)) |
|
|
|
|
.unwrap(); |
|
|
|
|
let n2 = net |
|
|
|
|
.add_node(generate_discrete_time_continous_node(String::from("n2"), 3)) |
|
|
|
|
.unwrap(); |
|
|
|
|
net.add_edge(n1, n2); |
|
|
|
|
|
|
|
|
|
match &mut net.get_node_mut(n1) { |
|
|
|
|
params::Params::DiscreteStatesContinousTime(param) => { |
|
|
|
|
assert_eq!( |
|
|
|
|
Ok(()), |
|
|
|
|
param.set_cim(arr3(&[ |
|
|
|
|
[ |
|
|
|
|
[-3.0, 2.0, 1.0], |
|
|
|
|
[1.5, -2.0, 0.5], |
|
|
|
|
[0.4, 0.6, -1.0] |
|
|
|
|
], |
|
|
|
|
])) |
|
|
|
|
); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
match &mut net.get_node_mut(n2) { |
|
|
|
|
params::Params::DiscreteStatesContinousTime(param) => { |
|
|
|
|
assert_eq!( |
|
|
|
|
Ok(()), |
|
|
|
|
param.set_cim(arr3(&[ |
|
|
|
|
[ |
|
|
|
|
[-1.0, 0.5, 0.5], |
|
|
|
|
[3.0, -4.0, 1.0], |
|
|
|
|
[0.9, 0.1, -1.0] |
|
|
|
|
], |
|
|
|
|
[ |
|
|
|
|
[-6.0, 2.0, 4.0], |
|
|
|
|
[1.5, -2.0, 0.5], |
|
|
|
|
[3.0, 1.0, -4.0] |
|
|
|
|
], |
|
|
|
|
[ |
|
|
|
|
[-1.0, 0.1, 0.9], |
|
|
|
|
[2.0, -2.5, 0.5], |
|
|
|
|
[0.9, 0.1, -1.0] |
|
|
|
|
], |
|
|
|
|
])) |
|
|
|
|
); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
let data = trajectory_generator(&net, 100, 20.0, Some(6347747169756259)); |
|
|
|
|
|
|
|
|
|
let f = F::new(0.000001); |
|
|
|
|
let chi_sq = ChiSquare::new(0.0001); |
|
|
|
|
let f = F::new(1e-6); |
|
|
|
|
let chi_sq = ChiSquare::new(1e-4); |
|
|
|
|
let parameter_learning = BayesianApproach { alpha: 1, tau:1.0 }; |
|
|
|
|
let cache = Cache::new(parameter_learning, data.clone()); |
|
|
|
|
let mut ctpc = CTPC::new(f, chi_sq, cache); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let net = ctpc.fit_transform(net, &data); |
|
|
|
|
assert_eq!(BTreeSet::from_iter(vec![n1]), net.get_parent_set(n2)); |
|
|
|
|
assert_eq!(BTreeSet::new(), net.get_parent_set(n1)); |
|
|
|
|
let ctpc = CTPC::new(parameter_learning, f, chi_sq); |
|
|
|
|
learn_ternary_net_2_nodes(ctpc); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
#[test] |
|
|
|
|
fn learn_mixed_discrete_net_3_nodes_ctpc() { |
|
|
|
|
let (_, data) = get_mixed_discrete_net_3_nodes_with_data(); |
|
|
|
|
|
|
|
|
|
let f = F::new(1e-24); |
|
|
|
|
let chi_sq = ChiSquare::new(1e-24); |
|
|
|
|
let f = F::new(1e-6); |
|
|
|
|
let chi_sq = ChiSquare::new(1e-4); |
|
|
|
|
let parameter_learning = BayesianApproach { alpha: 1, tau:1.0 }; |
|
|
|
|
let cache = Cache::new(parameter_learning, data); |
|
|
|
|
let ctpc = CTPC::new(f, chi_sq, cache); |
|
|
|
|
|
|
|
|
|
let ctpc = CTPC::new(parameter_learning, f, chi_sq); |
|
|
|
|
learn_mixed_discrete_net_3_nodes(ctpc); |
|
|
|
|
} |
|
|
|
|