21 PetscFunctionBeginUser;
26 for (PetscInt i = 0; i < 8; ++i) {
32 "constant signal must give bitwise-zero centered second moment"));
34 "constant signal pair must give bitwise-zero co-moment"));
37 "constant signal variance must be exactly zero"));
38 PetscFunctionReturn(0);
46 const PetscReal samples[3] = {1.0, 2.0, 6.0};
48 PetscFunctionBeginUser;
55 "three-sample variance"));
57 "equal weights make effective count equal sample count"));
67 "unequal-weight variance"));
70 "unequal weights reduce effective count"));
71 PetscFunctionReturn(0);
79 PetscFunctionBeginUser;
88 "two-sample covariance"));
89 PetscFunctionReturn(0);
100 const PetscReal series[3][3] = {{1.0, 2.0, 3.0}, {3.0, 6.0, 5.0}, {5.0, 4.0, 7.0}};
101 const PetscInt first[6] = {0, 0, 0, 1, 1, 2};
102 const PetscInt second[6] = {0, 1, 2, 1, 2, 2};
103 const PetscReal expected_cm[6] = {8.0, 4.0, 8.0, 8.0, 4.0, 8.0};
104 const char *labels[6] = {
"xx",
"xy",
"xz",
"yy",
"yz",
"zz"};
109 PetscFunctionBeginUser;
112 for (PetscInt s = 0; s < 3; ++s) {
113 for (PetscInt c = 0; c < 6; ++c) {
115 series[s][first[c]], series[s][second[c]], 1.0));
120 for (PetscInt c = 0; c < 6; ++c) {
121 PetscCall(PetscSNPrintf(context,
sizeof(context),
"centered co-moment component %s", labels[c]));
130 PetscFunctionReturn(0);
138 const PetscReal samples[5] = {2.5, -1.75, 9.0, 0.25, 4.5};
139 const PetscReal weights[5] = {1.0, 0.5, 2.25, 3.0, 0.125};
141 PetscFunctionBeginUser;
144 for (PetscInt i = 0; i < 5; ++i) {
149 "co-moment of a signal with itself must equal its centered second moment"));
151 "co-moment self-pair means must equal the scalar mean"));
152 PetscFunctionReturn(0);
167 PetscFunctionBeginUser;
177 "high-mean low-fluctuation variance must not cancel"));
180 "high-mean anti-correlated covariance must not cancel"));
181 PetscFunctionReturn(0);
190 const PetscReal samples[8] = {1.5, -2.0, 7.25, 0.5, 3.0, -4.5, 6.0, 2.25};
191 const PetscReal weights[8] = {1.0, 2.0, 0.5, 1.25, 3.0, 0.75, 1.0, 2.5};
193 PetscFunctionBeginUser;
202 for (PetscInt i = 0; i < 8; ++i) {
228 "merging an empty partition on the right must not change the state"));
231 "merging an empty partition on the left must not change the state"));
232 PetscFunctionReturn(0);
240 PetscErrorCode zero_ierr = 0, negative_ierr = 0, co_zero_ierr = 0;
242 PetscFunctionBeginUser;
246 PetscCall(PetscPushErrorHandler(PetscIgnoreErrorHandler, NULL));
248 PetscCall(PetscPopErrorHandler());
250 "zero sample weight should be rejected"));
252 PetscCall(PetscPushErrorHandler(PetscIgnoreErrorHandler, NULL));
254 PetscCall(PetscPopErrorHandler());
256 "negative sample weight should be rejected"));
258 PetscCall(PetscPushErrorHandler(PetscIgnoreErrorHandler, NULL));
260 PetscCall(PetscPopErrorHandler());
262 "zero co-moment weight should be rejected"));
265 "a rejected sample must leave the accumulator untouched"));
266 PetscFunctionReturn(0);
286 ierr = PetscInitialize(&argc, &argv, NULL,
"PICurv centered-moment kernel tests");
291 ierr =
PicurvRunTests(
"unit-statistics", cases,
sizeof(cases) /
sizeof(cases[0]));
297 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.