@@ -1574,6 +1574,16 @@ package void testPercentogramDuplicates(alias factory)()
15741574}
15751575
15761576// Keep statistical rules shared without coupling custom allocation to GC/RC ownership.
1577+ package void validatePercentogramLevels(Levels)(scope Levels levels)
1578+ {
1579+ static assert (Levels.N == 1 , " percentogram: probabilities must be one-dimensional" );
1580+ assert (levels.length >= 2 , " percentogram: at least two probabilities are required" );
1581+ assert (levels[0 ] >= 0 && levels[$ - 1 ] <= 1 ,
1582+ " percentogram: probabilities must lie between zero and one" );
1583+ foreach (i; 1 .. levels.length)
1584+ assert (levels[i] > levels[i - 1 ], " percentogram: probabilities must strictly increase" );
1585+ }
1586+
15771587package void validatePercentogramInputs(Observations, Levels)(scope Observations observations, scope Levels levels)
15781588{
15791589 import std.traits : isIntegral;
@@ -1586,12 +1596,133 @@ package void validatePercentogramInputs(Observations, Levels)(scope Observations
15861596 static if (! isIntegral! (typeof (x)))
15871597 assert (isFinite(x), " percentogram: observations must be finite" );
15881598 }
1589- assert (levels.length >= 2 , " percentogram: at least two probabilities are required" );
1590- assert (levels[0 ] >= 0 && levels[$ - 1 ] <= 1 ,
1591- " percentogram: probabilities must lie between zero and one" );
1592- foreach (i; 1 .. levels.length)
1593- assert (levels[i] > levels[i - 1 ], " percentogram: probabilities must strictly increase" );
1599+ validatePercentogramLevels(levels);
1600+ }
1601+
1602+ // Preserve floating observation precision; integral quantiles need floating
1603+ // boundaries for widths and the final nextUp endpoint, even for inverseCDF.
1604+ package template WeightedPercentogramBoundary(Data)
1605+ {
1606+ import mir.primitives: DeepElementType;
1607+ import std.traits : Unqual, isFloatingPoint;
1608+ alias Element = Unqual! (DeepElementType! Data);
1609+ static if (isFloatingPoint! Element) alias WeightedPercentogramBoundary = Element;
1610+ else alias WeightedPercentogramBoundary = double ;
1611+ }
1612+
1613+ // The automatic-bin heuristic counts positive-weight rows, not their total
1614+ // weight. It is therefore unchanged by rescaling probability weights.
1615+ package size_t weightedPercentogramBinCount(Weights)(scope auto ref Weights weights)
1616+ {
1617+ import mir.ndslice.slice: isSlice, sliced;
1618+ import std.math : isFinite;
1619+ static if (isSlice! Weights) scope auto masses = weights;
1620+ else scope auto masses = weights[].sliced;
1621+ static assert (typeof (masses).N == 1 , " percentogram: weights must be one-dimensional" );
1622+ size_t n;
1623+ foreach (w; masses)
1624+ {
1625+ assert (isFinite(cast (real ) w) && w >= 0 , " percentogram: invalid weight" );
1626+ if (w > 0 ) ++ n;
1627+ }
1628+ return defaultPercentogramBinCount (n);
1629+ }
1630+
1631+ // Quantile validation has already checked shapes, weights, and positive-weight
1632+ // values. Skip zero weights before reading their possibly nonfinite values.
1633+ package void fillWeightedPercentogram(H, Weights, Data)(ref H histogram,
1634+ scope auto ref Weights weights, scope auto ref Data data)
1635+ {
1636+ import mir.ndslice.slice: isSlice, sliced;
1637+ static if (isSlice! Weights) scope auto masses = weights;
1638+ else scope auto masses = weights[].sliced;
1639+ static if (isSlice! Data) scope auto values = data;
1640+ else scope auto values = data[].sliced;
1641+ alias Boundary = WeightedPercentogramBoundary! Data;
1642+ foreach (i; 0 .. masses.length)
1643+ if (masses[i] > 0 )
1644+ histogram.putWeighted(masses[i], cast (Boundary) values [i]);
1645+ }
15941646
1647+ package auto buildWeightedPercentogram(alias allocate, alias quantiles, alias histogram,
1648+ Counts, alias algorithm, Weights, Data, P)(scope auto ref Weights weights,
1649+ scope auto ref Data data, scope auto ref P probabilities)
1650+ {
1651+ import mir.ndslice.slice: isSlice, sliced;
1652+ import mir.stat.descriptive.histogram.axis: variableAxis, AxisOptions;
1653+ import std.traits : isIntegral;
1654+ static if (isIntegral! P)
1655+ {
1656+ assert (probabilities > 0 && probabilities < size_t .max,
1657+ " percentogram: bin count must be positive and leave room for an extra boundary" );
1658+ NoAllocationContext context;
1659+ auto levels = allocate! double (context, cast (size_t ) probabilities + 1 );
1660+ foreach (i; 0 .. levels.length) levels[i] = cast (double ) i / probabilities;
1661+ return buildWeightedPercentogram! (allocate, quantiles, histogram, Counts, algorithm)(weights, data, levels);
1662+ }
1663+ else
1664+ {
1665+ static if (isSlice! P) scope auto levels = probabilities;
1666+ else scope auto levels = probabilities[].sliced;
1667+ validatePercentogramLevels(levels);
1668+ auto edges = quantiles! (WeightedPercentogramBoundary! Data, algorithm)(weights, data, levels);
1669+ const distinct = preparePercentogramEdges(edges);
1670+ auto axis = variableAxis! (AxisOptions(false , true , true ))(edges[0 .. distinct]);
1671+ auto result = histogram! Counts(axis);
1672+ fillWeightedPercentogram(result, weights, data);
1673+ return result;
1674+ }
1675+ }
1676+
1677+ // Exercise the same weighted distribution across all allocation policies.
1678+ version (mir_stat_test)
1679+ package void testWeightedPercentograms(alias factory, alias release = null )()
1680+ {
1681+ import mir.ndslice.slice: sliced;
1682+ import mir.ndslice.topology: stride;
1683+ import mir.stat.descriptive.histogram.relative_frequency: Normalization;
1684+ import std.math : nextUp;
1685+ static ref auto histogramOf (T)(return ref T value)
1686+ {
1687+ static if (__traits(hasMember, T, " histogram" )) return value.histogram;
1688+ else return value;
1689+ }
1690+ // A zero-weight nonfinite value neither affects boundaries nor gets indexed.
1691+ const double [5 ] data = [double .nan, 0 , 10 , 20 , 30 ];
1692+ const double [5 ] weights = [0 , 1 , 1 , 6 , 2 ], scaled = [0 , 2 , 2 , 12 , 4 ];
1693+ const double [3 ] levels = [0 , 0.5 , 1 ];
1694+ auto result = factory(weights, data, levels);
1695+ scope (exit) { static if (! is (typeof (release) == typeof (null ))) release(result); }
1696+ auto p = histogramOf(result);
1697+ assert (p.counts == [0.0 , 2 , 8 , 0 ] && p.total == 10 );
1698+ assert (p.bins()[0 ].bin.high == 20 && p.bins()[1 ].bin.high == nextUp(30.0 ));
1699+ assert (p.density(0 ) * 20 == 0.2 );
1700+ auto rescaled = factory(scaled, data, 2 );
1701+ scope (exit) { static if (! is (typeof (release) == typeof (null ))) release(rescaled); }
1702+ assert (histogramOf(rescaled).bins()[0 ].bin.high == 20 );
1703+ assert (histogramOf(rescaled).relativeFrequency(1 ) == p.relativeFrequency(1 ));
1704+ auto automatic = factory(weights, data);
1705+ scope (exit) { static if (! is (typeof (release) == typeof (null ))) release(automatic); }
1706+ assert (histogramOf(automatic).counts == p.counts);
1707+ // Restrict the quantile interval: both tails retain their weights.
1708+ const int [5 ] x = [0 , 1 , 2 , 3 , 4 ], w = [1 , 1 , 1 , 1 , 1 ];
1709+ const double [3 ] middle = [0.25 , 0.5 , 0.75 ];
1710+ auto trimmed = factory(w, x, middle);
1711+ scope (exit) { static if (! is (typeof (release) == typeof (null ))) release(trimmed); }
1712+ auto t = histogramOf(trimmed);
1713+ assert (t.counts == [1.0 , 1 , 2 , 1 ]);
1714+ assert (t.relativeFrequency! (double , Normalization.ordinary)(1 ) == 2.0 / 3 );
1715+ // Ties compact boundaries while preserving all weighted observations.
1716+ const int [5 ] tied = [0 , 0 , 0 , 10 , 20 ];
1717+ auto compact = factory(w, tied, 4 );
1718+ scope (exit) { static if (! is (typeof (release) == typeof (null ))) release(compact); }
1719+ assert (histogramOf(compact).axis.N_bin == 2 );
1720+ assert (histogramOf(compact).counts == [0.0 , 3 , 2 , 0 ]);
1721+ const double [8 ] backing = [0 , - 1 , 10 , - 1 , 20 , - 1 , 30 , - 1 ];
1722+ const double [8 ] massBacking = [1 , - 1 , 1 , - 1 , 6 , - 1 , 2 , - 1 ];
1723+ auto strided = factory(massBacking[].sliced.stride(2 ), backing[].sliced.stride(2 ), levels[].sliced);
1724+ scope (exit) { static if (! is (typeof (release) == typeof (null ))) release(strided); }
1725+ assert (histogramOf(strided).counts == p.counts);
15951726}
15961727
15971728// Compact in place but retain the complete allocation handle for manual cleanup.
0 commit comments