42 std::vector<int> resampledData;
43 resampledData.reserve(numSamples);
45 std::mt19937 rng(std::random_device{}());
46 std::uniform_int_distribution<int> dist(0, data.size() - 1);
48 for (
int i = 0; i < numSamples; ++i) {
49 int index = dist(rng);
50 resampledData.push_back(data[index]);
60 if (numSamples >
static_cast<int>(data.size())) {
61 throw std::invalid_argument(
62 "Number of samples cannot exceed data size");
65 std::vector<int> resampledData = data;
66 std::shuffle(resampledData.begin(),
68 std::mt19937(std::random_device()()));
70 resampledData.resize(numSamples);
76 std::vector<std::vector<int>>
79 std::vector<std::vector<int>> resampledDatasets;
80 resampledDatasets.reserve(n);
82 for (
int i = 0; i < n; ++i) {
83 std::vector<int> resampledData = data;
84 resampledData.erase(resampledData.begin() + i);
85 resampledDatasets.push_back(resampledData);
88 return resampledDatasets;
92 std::vector<std::vector<int>>
94 int numPermutations) {
95 std::vector<std::vector<int>> permutedDatasets;
96 permutedDatasets.reserve(numPermutations);
98 std::mt19937 rng(std::random_device{}());
100 for (
int i = 0; i < numPermutations; ++i) {
101 std::vector<int> permutedData = data;
102 std::shuffle(permutedData.begin(), permutedData.end(), rng);
103 permutedDatasets.push_back(permutedData);
106 return permutedDatasets;
113 std::vector<double> resampledMeans;
114 resampledMeans.reserve(numSamples);
116 std::mt19937 rng(std::random_device{}());
117 std::uniform_int_distribution<int> dist(0, data.size() - 1);
119 for (
int i = 0; i < numSamples; ++i) {
120 std::vector<double> resampledData;
121 resampledData.reserve(data.size());
122 for (
int j = 0; j < static_cast<int>(data.size()); ++j) {
123 int index = dist(rng);
124 resampledData.push_back(data[index]);
127 std::accumulate(resampledData.begin(), resampledData.end(), 0.0) /
128 resampledData.size();
129 resampledMeans.push_back(mean);
132 return resampledMeans;
136 std::pair<double, double>
140 std::vector<double> resampledMeans = bootstrap_t(data, numSamples);
141 std::sort(resampledMeans.begin(), resampledMeans.end());
142 int lowerIndex =
static_cast<int>((alpha / 2) * numSamples);
143 int upperIndex =
static_cast<int>((1 - alpha / 2) * numSamples) - 1;
144 return std::make_pair(resampledMeans[lowerIndex],
145 resampledMeans[upperIndex]);
152 std::vector<double> resampledData;
153 resampledData.reserve(numSamples);
155 std::mt19937 rng(std::random_device{}());
156 std::uniform_int_distribution<int> dist(0, data.size() - 1);
158 for (
int i = 0; i < numSamples; ++i) {
160 for (
int j = 0; j < static_cast<int>(data.size()); ++j) {
161 int index = dist(rng);
164 resampledData.push_back(sum / data.size());
167 return resampledData;
172 const std::vector<double> &data,
176 if (blockSize <= 0 || blockSize >
static_cast<int>(data.size())) {
177 throw std::invalid_argument(
"Invalid block size");
180 std::vector<double> resampledData;
181 resampledData.reserve(numSamples);
183 std::mt19937 rng(std::random_device{}());
184 std::uniform_int_distribution<int> dist(0, data.size() - 1);
186 for (
int i = 0; i < numSamples; ++i) {
187 std::vector<double> blockMeans;
188 blockMeans.reserve(data.size() / blockSize);
189 for (
int j = 0; j < static_cast<int>(data.size()) / blockSize; ++j) {
191 for (
int k = 0; k < blockSize; ++k) {
192 int index = dist(rng);
195 blockMeans.push_back(sum / blockSize);
197 std::shuffle(blockMeans.begin(), blockMeans.end(), rng);
198 resampledData.insert(resampledData.end(),
203 return resampledData;
210 std::vector<double> resampledData;
211 resampledData.reserve(numSamples);
213 std::mt19937 rng(std::random_device{}());
214 std::uniform_int_distribution<int> dist(0, data.size() - 1);
216 for (
int i = 0; i < numSamples; ++i) {
217 std::vector<double> resampledSequence;
218 resampledSequence.reserve(data.size());
219 int startIndex = dist(rng);
220 for (
int j = 0; j < static_cast<int>(data.size()); ++j) {
221 int index = (startIndex + j) % data.size();
222 resampledSequence.push_back(data[index]);
224 resampledData.insert(resampledData.end(),
225 resampledSequence.begin(),
226 resampledSequence.end());
229 return resampledData;
234 const std::vector<double> &weights,
236 std::vector<double> resampledData;
237 std::random_device rd;
238 std::mt19937 gen(rd());
239 std::discrete_distribution<> dis(weights.begin(), weights.end());
240 for (
int i = 0; i < size; ++i) {
241 resampledData.push_back(data[dis(gen)]);
243 return resampledData;
248 const std::vector<double> &data2,
249 double observedStatistic) {
251 std::vector<double> combinedData = data1;
252 combinedData.insert(combinedData.end(), data2.begin(), data2.end());
253 std::shuffle(combinedData.begin(),
255 std::mt19937(std::random_device()()));
256 std::vector<double> permutedData1(data1.begin(), data1.end());
257 std::vector<double> permutedData2(data2.begin(), data2.end());
258 for (
int i = 0; i < 1000; ++i) {
259 std::shuffle(combinedData.begin(),
261 std::mt19937(std::random_device()()));
262 auto permutedStatistic =
263 (std::accumulate(combinedData.begin(),
264 combinedData.begin() + data1.size(),
267 if (permutedStatistic >= observedStatistic) {
271 return count / 1000.0;
static std::vector< double > bootstrap_t(const std::vector< double > &data, int numSamples)
Perform bootstrap t-statistic resampling.
static std::vector< double > circular_block_bootstrap(const std::vector< double > &data, int blockSize, int numSamples)
Perform circular block bootstrap resampling.
static std::vector< double > smoothed_bootstrap(const std::vector< double > &data, int numSamples)
Perform smoothed bootstrap resampling.
static std::vector< double > weighted_bootstrap(const std::vector< double > &data, const std::vector< double > &weights, int size)
Perform weighted bootstrap resampling.
static std::vector< int > subsample(const std::vector< int > &data, int numSamples)
Perform subsampling.
static std::vector< double > time_series_bootstrap(const std::vector< double > &data, int numSamples)
Perform time series bootstrap resampling.
static double permutation_p_value(const std::vector< double > &data1, const std::vector< double > &data2, double observedStatistic)
Calculate the p-value using permutation test.
static std::vector< std::vector< int > > jackknife(const std::vector< int > &data)
Perform jackknife resampling.
static std::vector< std::vector< int > > permutation_test(const std::vector< int > &data, int numPermutations)
Perform permutation test.
static std::pair< double, double > bootstrap_ci(const std::vector< double > &data, double alpha, int numSamples)
Calculate confidence interval using bootstrap.
static std::vector< int > bootstrap(const std::vector< int > &data, int numSamples)
Perform bootstrap resampling.