tiny.simd.Stats
Defined in stats.
API (35)
Actions
Public operations.
assimilatecountgeometricMeankurtosismaxmeanminmu1mu2mu3mu4notifyresetsampleKurtosissampleSkewnesssampleVarianceskewnessstandardDeviationvariancewrite
Types and contracts
Public types and contracts.
Fields and members
Public fields and members.
Source
Source: lib/simd/src/stats.zig:128
zig
pub const Stats = struct { count_value: i64 = 0, minimum: f32 = std.math.floatMax(f32), maximum: f32 = -std.math.floatMax(f32), sum_log: f64 = 0, moment_1: f64 = 0, moment_2: f64 = 0, moment_3: f64 = 0, moment_4: f64 = 0, pub const max_count: i64 = 3_037_000_499; pub const no_count: u8 = 1; pub const no_mean_sd: u8 = 2; pub const no_min_max: u8 = 4; pub const no_skew_kurt: u8 = 8; pub const no_geometric_mean: u8 = 16; pub const all_exclusions: u8 = no_count | no_mean_sd | no_min_max | no_skew_kurt | no_geometric_mean; pub fn notify(self: *@This(), value: f32) void { std.debug.assert(self.count_value >= 0); std.debug.assert(self.count_value < max_count); self.count_value += 1; self.minimum = @min(self.minimum, value); self.maximum = @max(self.maximum, value); self.sum_log += @log(@as(f64, value)); const count_f64: f64 = @floatFromInt(self.count_value); const delta = @as(f64, value) - self.moment_1; const delta_div_count = delta / count_f64; const delta2_count_minus_1_div_count = delta * (count_f64 - 1) * delta_div_count; const count_polynomial = count_f64 * count_f64 - 3 * count_f64 + 3; self.moment_1 += delta_div_count; self.moment_4 += delta_div_count * (delta_div_count * (delta2_count_minus_1_div_count * count_polynomial + 6 * self.moment_2) - 4 * self.moment_3); self.moment_3 += delta_div_count * (delta2_count_minus_1_div_count * (count_f64 - 2) - 3 * self.moment_2); self.moment_2 += delta2_count_minus_1_div_count; } pub fn assimilate(self: *@This(), other: *const @This()) void { std.debug.assert(self.count_value >= 0); std.debug.assert(other.count_value >= 0); std.debug.assert(self.count_value <= max_count - other.count_value); const total_count = self.count_value + other.count_value; if (total_count == 0) return; self.minimum = @min(self.minimum, other.minimum); self.maximum = @max(self.maximum, other.maximum); self.sum_log += other.sum_log; const own_count: f64 = @floatFromInt(self.count_value); const other_count: f64 = @floatFromInt(other.count_value); const total_count_f64: f64 = @floatFromInt(total_count); const product = own_count * other_count; const count_squared = own_count * own_count; const other_count_squared = other_count * other_count; const total_count_squared = total_count_f64 * total_count_f64; const total_count_cubed = total_count_squared * total_count_f64; const inverse_total_count = 1 / total_count_f64; const inverse_total_count_squared = 1 / total_count_squared; const delta = other.moment_1 - self.moment_1; const delta_squared = delta * delta; const delta_cubed = delta * delta_squared; const delta_fourth = delta_squared * delta_squared; self.moment_1 = (own_count * self.moment_1 + other_count * other.moment_1) * inverse_total_count; const new_moment_2 = self.moment_2 + other.moment_2 + delta_squared * product * inverse_total_count; const new_moment_3 = self.moment_3 + other.moment_3 + delta_cubed * product * (own_count - other_count) * inverse_total_count_squared + 3 * delta * (own_count * other.moment_2 - other_count * self.moment_2) * inverse_total_count; self.moment_4 += other.moment_4 + delta_fourth * product * (count_squared - product + other_count_squared) / total_count_cubed + 6 * delta_squared * (count_squared * other.moment_2 + other_count_squared * self.moment_2) * inverse_total_count_squared + 4 * delta * (own_count * other.moment_3 - other_count * self.moment_3) * inverse_total_count; self.moment_2 = new_moment_2; self.moment_3 = new_moment_3; self.count_value = total_count; } pub fn count(self: *const @This()) i64 { return self.count_value; } pub fn min(self: *const @This()) f32 { return self.minimum; } pub fn max(self: *const @This()) f32 { return self.maximum; } pub fn geometricMean(self: *const @This()) f64 { if (self.count_value == 0) return 0; return @exp(self.sum_log / @as(f64, @floatFromInt(self.count_value))); } pub fn mean(self: *const @This()) f64 { return self.moment_1; } pub fn sampleVariance(self: *const @This()) f64 { if (self.count_value == 0) return 0; return self.moment_2 / @as(f64, @floatFromInt(self.count_value)); } pub fn variance(self: *const @This()) f64 { if (self.count_value == 0) return 0; if (self.count_value == 1) return self.moment_2; return self.moment_2 / @as(f64, @floatFromInt(self.count_value - 1)); } pub fn standardDeviation(self: *const @This()) f64 { return @sqrt(self.variance()); } pub fn sampleSkewness(self: *const @This()) f64 { if (@abs(self.moment_2) < 1e-7) return 0; const count_f64: f64 = @floatFromInt(self.count_value); return self.moment_3 * @sqrt(count_f64) / std.math.pow(f64, self.moment_2, 1.5); } pub fn skewness(self: *const @This()) f64 { if (self.count_value == 0) return 0; const count_f64: f64 = @floatFromInt(self.count_value); const ratio = (count_f64 - 1) / count_f64; return self.sampleSkewness() * std.math.pow(f64, ratio, 1.5); } pub fn sampleKurtosis(self: *const @This()) f64 { if (@abs(self.moment_2) < 1e-7) return 0; const count_f64: f64 = @floatFromInt(self.count_value); return self.moment_4 * count_f64 / (self.moment_2 * self.moment_2); } pub fn kurtosis(self: *const @This()) f64 { if (self.count_value == 0) return 0; const count_f64: f64 = @floatFromInt(self.count_value); const ratio = (count_f64 - 1) / count_f64; return self.sampleKurtosis() * ratio * ratio; } pub fn mu1(self: *const @This()) f64 { std.debug.assert(self.count_value != 0); return self.moment_1; } pub fn mu2(self: *const @This()) f64 { return self.centralMoment(self.moment_2); } pub fn mu3(self: *const @This()) f64 { return self.centralMoment(self.moment_3); } pub fn mu4(self: *const @This()) f64 { return self.centralMoment(self.moment_4); } pub fn write( self: *const @This(), writer: *std.Io.Writer, exclude: u8, ) std.Io.Writer.Error!void { if (self.count_value == 0) return writer.writeAll("(none)"); if (exclude & no_count == 0) { try writer.print("Count={d:9} ", .{@as(u64, @intCast(self.count_value))}); } if (exclude & no_mean_sd == 0) { try writer.writeAll("Mean="); try writeCScientific(writer, self.mean(), 10, 3); try writer.writeAll(" SD="); try writeCScientific(writer, self.standardDeviation(), 8, 2); try writer.writeByte(' '); } if (exclude & no_min_max == 0) { try writer.writeAll("Min="); try writeCScientific(writer, self.minimum, 10, 3); try writer.writeAll(" Max="); try writeCScientific(writer, self.maximum, 10, 3); try writer.writeByte(' '); } if (exclude & no_skew_kurt == 0) { try writer.print("Skew={d:5.2} Kurt={d:7.2} ", .{ self.skewness(), self.kurtosis(), }); } if (exclude & no_geometric_mean == 0) { try writer.print("GeoMean={d:9.6} ", .{self.geometricMean()}); } } pub fn reset(self: *@This()) void { self.* = .{}; } fn centralMoment(self: *const @This(), moment: f64) f64 { std.debug.assert(self.count_value != 0); return moment / @as(f64, @floatFromInt(self.count_value)); }};Source: lib/simd/src/root.zig:560
zig
pub const Stats = stats.Stats;Audit
| Definitions | 28 |
|---|---|
| Public names | 56 |
| Members | 8 |
| Version | 26.7.0 |
| Revision | daab053ee433 |