22 PetscFunctionBeginUser;
27 for (PetscInt i = 0; i < 8; ++i) {
33 "constant signal must give bitwise-zero centered second moment"));
35 "constant signal pair must give bitwise-zero co-moment"));
38 "constant signal variance must be exactly zero"));
39 PetscFunctionReturn(0);
47 const PetscReal samples[3] = {1.0, 2.0, 6.0};
49 PetscFunctionBeginUser;
56 "three-sample variance"));
58 "equal weights make effective count equal sample count"));
68 "unequal-weight variance"));
71 "unequal weights reduce effective count"));
72 PetscFunctionReturn(0);
80 PetscFunctionBeginUser;
89 "two-sample covariance"));
90 PetscFunctionReturn(0);
101 const PetscReal series[3][3] = {{1.0, 2.0, 3.0}, {3.0, 6.0, 5.0}, {5.0, 4.0, 7.0}};
102 const PetscInt first[6] = {0, 0, 0, 1, 1, 2};
103 const PetscInt second[6] = {0, 1, 2, 1, 2, 2};
104 const PetscReal expected_cm[6] = {8.0, 4.0, 8.0, 8.0, 4.0, 8.0};
105 const char *labels[6] = {
"xx",
"xy",
"xz",
"yy",
"yz",
"zz"};
110 PetscFunctionBeginUser;
113 for (PetscInt s = 0; s < 3; ++s) {
114 for (PetscInt c = 0; c < 6; ++c) {
116 series[s][first[c]], series[s][second[c]], 1.0));
121 for (PetscInt c = 0; c < 6; ++c) {
122 PetscCall(PetscSNPrintf(context,
sizeof(context),
"centered co-moment component %s", labels[c]));
131 PetscFunctionReturn(0);
139 const PetscReal samples[5] = {2.5, -1.75, 9.0, 0.25, 4.5};
140 const PetscReal weights[5] = {1.0, 0.5, 2.25, 3.0, 0.125};
142 PetscFunctionBeginUser;
145 for (PetscInt i = 0; i < 5; ++i) {
150 "co-moment of a signal with itself must equal its centered second moment"));
152 "co-moment self-pair means must equal the scalar mean"));
153 PetscFunctionReturn(0);
168 PetscFunctionBeginUser;
178 "high-mean low-fluctuation variance must not cancel"));
181 "high-mean anti-correlated covariance must not cancel"));
182 PetscFunctionReturn(0);
191 const PetscReal samples[8] = {1.5, -2.0, 7.25, 0.5, 3.0, -4.5, 6.0, 2.25};
192 const PetscReal weights[8] = {1.0, 2.0, 0.5, 1.25, 3.0, 0.75, 1.0, 2.5};
194 PetscFunctionBeginUser;
203 for (PetscInt i = 0; i < 8; ++i) {
229 "merging an empty partition on the right must not change the state"));
232 "merging an empty partition on the left must not change the state"));
233 PetscFunctionReturn(0);
241 PetscErrorCode zero_ierr = 0, negative_ierr = 0, co_zero_ierr = 0;
243 PetscFunctionBeginUser;
247 PetscCall(PetscPushErrorHandler(PetscIgnoreErrorHandler, NULL));
249 PetscCall(PetscPopErrorHandler());
251 "zero sample weight should be rejected"));
253 PetscCall(PetscPushErrorHandler(PetscIgnoreErrorHandler, NULL));
255 PetscCall(PetscPopErrorHandler());
257 "negative sample weight should be rejected"));
259 PetscCall(PetscPushErrorHandler(PetscIgnoreErrorHandler, NULL));
261 PetscCall(PetscPopErrorHandler());
263 "zero co-moment weight should be rejected"));
266 "a rejected sample must leave the accumulator untouched"));
267 PetscFunctionReturn(0);
287 ierr = PetscInitialize(&argc, &argv, NULL,
"PICurv centered-moment kernel tests");
292 ierr =
PicurvRunTests(
"unit-statistics", cases,
sizeof(cases) /
sizeof(cases[0]));
298 ierr = PetscFinalize();
Weighted centered-moment kernels for the field-statistics pipeline.
PetscErrorCode PicurvMomentStateMerge(PicurvMomentState *result, const PicurvMomentState *a, const PicurvMomentState *b)
Merges two independently accumulated scalar states.
PetscErrorCode PicurvCoMomentStateUpdate(PicurvCoMomentState *state, PetscReal value_x, PetscReal value_y, PetscReal weight)
Applies one weighted paired sample to a co-moment accumulator.
PetscReal mean_y
Weighted mean of the second member.
PetscReal weight
Total weight W.
PetscReal cm
Centered co-moment sum C.
PetscErrorCode PicurvCoMomentStateMerge(PicurvCoMomentState *result, const PicurvCoMomentState *a, const PicurvCoMomentState *b)
Merges two independently accumulated co-moment states.
PetscReal PicurvMomentStateEffectiveCount(const PicurvMomentState *state)
Returns Kish effective sample size W^2/W2, or zero when no weight accumulated.
void PicurvCoMomentStateReset(PicurvCoMomentState *state)
Resets a co-moment accumulator to the empty state.
PetscReal PicurvMomentStateVariance(const PicurvMomentState *state)
Returns the weighted variance M2/W, or zero when no weight accumulated.
PetscReal m2
Centered second-moment sum M2.
PetscReal PicurvCoMomentStateCovariance(const PicurvCoMomentState *state)
Returns the weighted covariance C/W, or zero when no weight accumulated.
void PicurvMomentStateReset(PicurvMomentState *state)
Resets a scalar moment accumulator to the empty state.
PetscReal mean
Weighted mean mu.
PetscReal mean_x
Weighted mean of the first member.
PetscErrorCode PicurvMomentStateUpdate(PicurvMomentState *state, PetscReal value, PetscReal weight)
Applies one weighted sample to a scalar moment accumulator.
PetscReal count
Number of accepted samples.
Weighted centered co-moment state for one ordered pair of quantities.
Weighted centered state for one scalar quantity at one point.
static PetscErrorCode TestConstantFieldHasExactlyZeroVariance(void)
A constant signal must produce exactly zero variance, not merely small.
static PetscErrorCode TestCoMomentOfSelfMatchesSecondMoment(void)
A co-moment of a signal with itself must reproduce the scalar second moment bitwise.
static PetscErrorCode TestKnownScalarMoments(void)
Known scalar moments for equal and unequal weights.
int main(int argc, char **argv)
Entry point for the centered-moment kernel suite.
static PetscErrorCode TestKnownTwoSampleCovariance(void)
Known two-sample covariance through the co-moment update.
static PetscErrorCode TestNonPositiveWeightRejected(void)
Non-positive weights are rejected rather than silently corrupting the accumulator.
static PetscErrorCode TestSixSymmetricVelocityComponents(void)
All six symmetric components of a three-sample vector self-product.
static PetscErrorCode TestMergeEqualsSequential(void)
Merging two partitions must reproduce a single sequential accumulation.
static PetscErrorCode TestHighMeanLowFluctuationPrecision(void)
High mean with small fluctuation, where a naive sum-of-squares cancels catastrophically.
PetscErrorCode PicurvAssertRealNear(PetscReal expected, PetscReal actual, PetscReal tol, const char *context)
Asserts that two real values agree within tolerance.
PetscErrorCode PicurvRunTests(const char *suite_name, const PicurvTestCase *cases, size_t case_count)
Runs a named C test suite and prints pass/fail progress markers.
PetscErrorCode PicurvAssertIntEqual(PetscInt expected, PetscInt actual, const char *context)
Asserts that two integer values are equal.
PetscErrorCode PicurvAssertBool(PetscBool value, const char *context)
Asserts that one boolean condition is true.
Shared declarations for the PICurv C test fixture and assertion layer.
Named test case descriptor consumed by PicurvRunTests.