1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
//! Calculate an empirical p-value for an unpermuted optimization result.
//!
//! Given a set of `OptimizationResult`s, where exactly one is expected to be
//! unpermuted, calculate a null distribution of p-values from the permuted set
//! and compare the unpermuted p-value to the null distribution to calculate
//! an empirical p-value. If only an unpermuted result is provided, the empirical
//! is set to 1.0 arbitrarily.
use crateOptimizationResult;
use cratefdr;
use json;
/// Calculates the empirical p-value based on an unpermuted optimization result
/// and a collection of permuted results.
///
/// The empirical p-value is computed as the proportion of permuted p-values that are
/// less than or equal to the p-value of the unpermuted result. The function also
/// returns additional fields, including thresholds, dataset sizes, and population data.
///
/// # Arguments
/// - `unpermuted_result`: The optimization result from the unpermuted data.
/// - `permuted_results`: A vector of optimization results from permuted data.
///
/// # Returns
/// A `serde_json::Value` containing:
/// - `rank1`: The first threshold from the unpermuted result.
/// - `rank2`: The second threshold from the unpermuted result.
/// - `set1_len`: Size of the first dataset.
/// - `set2_len`: Size of the second dataset.
/// - `population_size`: The total size of the population.
/// - `unpermuted_intersection_size`: Size of the intersection for the unpermuted result.
/// - `unpermuted_pvalue`: The p-value of the unpermuted result.
/// - `empirical_pvalue`: The calculated empirical p-value.
///
/// # Panics
/// - Panics if the `unpermuted_result` is not of type `OptimizationResult::Best`.
/// - Panics if any `OptimizationResult` in `permuted_results` is not of type `Best`.
///
/// # Examples
/// ```rust
/// use serde_json::json;
/// use dual_threshold_optimization::dto::{OptimizationResult, OptimizationResultRecord, FeatureSets};
/// use dual_threshold_optimization::stat_operations::empirical_pvalue;
///
/// let unpermuted_result = OptimizationResult::Best(OptimizationResultRecord {
/// rank1: 1,
/// rank2: 2,
/// pvalue: 0.05,
/// set1_len: 50,
/// set2_len: 40,
/// population_size: 100,
/// intersection_size: 10,
/// feature_sets: FeatureSets::None,
/// permuted: false,
/// });
///
/// let input_vec = vec![
/// unpermuted_result.clone(),
/// OptimizationResult::Best(OptimizationResultRecord {
/// rank1: 1,
/// rank2: 2,
/// pvalue: 0.10,
/// set1_len: 50,
/// set2_len: 40,
/// population_size: 100,
/// intersection_size: 8,
/// feature_sets: FeatureSets::None,
/// permuted: true,
/// }),
/// OptimizationResult::Best(OptimizationResultRecord {
/// rank1: 1,
/// rank2: 2,
/// pvalue: 0.03,
/// set1_len: 50,
/// set2_len: 40,
/// population_size: 100,
/// intersection_size: 9,
/// feature_sets: FeatureSets::None,
/// permuted: true,
/// }),
/// OptimizationResult::Best(OptimizationResultRecord {
/// rank1: 1,
/// rank2: 2,
/// pvalue: 0.07,
/// set1_len: 50,
/// set2_len: 40,
/// population_size: 100,
/// intersection_size: 7,
/// feature_sets: FeatureSets::None,
/// permuted: true,
/// }),
/// ];
///
/// let result = empirical_pvalue(input_vec);
/// assert_eq!(
/// result,
/// json!({
/// "rank1": 1,
/// "rank2": 2,
/// "set1_len": 50,
/// "set2_len": 40,
/// "population_size": 100,
/// "unpermuted_intersection_size": 10,
/// "unpermuted_pvalue": 0.05,
/// "empirical_pvalue": 0.3333333333333333, // 1/3 of permuted p-values <= 0.05
/// "fdr": 1.03125,
/// })
/// );
/// ```
// pub fn empirical_pvalue(
// unpermuted_result: OptimizationResult,
// permuted_results: Vec<OptimizationResult>,
// ) -> serde_json::Value {
// let (unpermuted_pvalue, unpermuted_rank1, unpermuted_rank2,
// set1_len, set2_len, population_size, unpermuted_intersect_size) = match unpermuted_result {
// OptimizationResult::Best(record) => (
// record.pvalue,
// record.rank1,
// record.rank2,
// record.set1_len,
// record.set2_len,
// record.population_size,
// record.intersection_size),
// _ => panic!("Unexpected result type for unpermuted optimization"),
// };
// let permuted_pvalues: Vec<f64> = permuted_results
// .into_iter()
// .filter_map(|result| match result {
// OptimizationResult::Best(record) => Some(record.pvalue),
// _ => None,
// })
// .collect();
// let empirical_pvalue = permuted_pvalues
// .iter()
// .filter(|&&p| p <= unpermuted_pvalue)
// .count() as f64
// / permuted_pvalues.len() as f64;
// json!({
// "rank1": unpermuted_rank1,
// "rank2": unpermuted_rank2,
// "set1_len": set1_len,
// "set2_len": set2_len,
// "population_size": population_size,
// "unpermuted_intersection_size": unpermuted_intersect_size,
// "unpermuted_pvalue": unpermuted_pvalue,
// "empirical_pvalue": empirical_pvalue,
// })
// }