SPB Git

Repository Public

Statistics.swift 3.6 KB 90 lines swift

1// =============================================================================2//                  SP BOUCHER | SOFTWARE & RESEARCH3//             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project          : QYVORA7// File             : Sources/QyvoraBenchmarks/Statistics.swift8// Module           : Qyvora Benchmarks9// Repository       : https://git.spboucher.ai/qyvora10// Project Website  : Not specified11// Created          : 2026-12-0412// Last Modified    : 2026-12-0413//14// Author           : Simon-Pierre Boucher15// Position         : Professor of Finance16// Institution      : Université du Québec en Outaouais (UQO)17// Organization     : SP Boucher18// Website          : https://spboucher.ai19// Git              : https://git.spboucher.ai20//21// Preferred Contact: contact@spboucher.ai22// Development      : dev@spboucher.ai23// Support          : support@spboucher.ai24// Security/Admin   : admin@spboucher.ai25//26// Copyright (c) 202627// Simon-Pierre Boucher28// All rights reserved where applicable. This file is governed by the29// applicable project license, copyright notices and relevant law.30// Existing third-party rights and notices remain unaffected.31//32// License          : None specified (no license file in this repository)33//34// Do not embed credentials, private keys, access tokens,35// certificates or confidential personal information.36//37// =============================================================================38// END OF SP BOUCHER SOURCE HEADER39// =============================================================================4041import Foundation4243/// Summary of the samples of one benchmark. Every value is in seconds per operation.44public struct BenchmarkStatistics: Codable, Sendable, Equatable {45    public let sampleCount: Int46    public let min: Double47    public let max: Double48    public let median: Double49    public let mean: Double50    /// 95th percentile, linear interpolation between closest ranks.51    public let p95: Double52    /// Sample standard deviation (n - 1 denominator); 0 with fewer than two samples.53    public let standardDeviation: Double5455    /// Summarises `samples` (seconds per operation). An empty array gives all zeros.56    public init(samples: [Double]) {57        let sorted = samples.sorted()58        sampleCount = sorted.count59        guard let first = sorted.first, let last = sorted.last else {60            min = 0; max = 0; median = 0; mean = 0; p95 = 0; standardDeviation = 061            return62        }63        min = first64        max = last65        median = Self.quantile(sorted, 0.5)66        p95 = Self.quantile(sorted, 0.95)67        let m = sorted.reduce(0, +) / Double(sorted.count)68        mean = m69        if sorted.count > 1 {70            let sumSquares = sorted.reduce(0) { $0 + ($1 - m) * ($1 - m) }71            standardDeviation = (sumSquares / Double(sorted.count - 1)).squareRoot()72        } else {73            standardDeviation = 074        }75    }7677    /// Quantile `q` in [0, 1] of an ascending array, linear interpolation (rank = q (n - 1)).78    public static func quantile(_ sorted: [Double], _ q: Double) -> Double {79        guard !sorted.isEmpty else { return 0 }80        let position = Swift.min(Swift.max(q, 0), 1) * Double(sorted.count - 1)81        let lower = Int(position.rounded(.down))82        let upper = Swift.min(lower + 1, sorted.count - 1)83        let fraction = position - Double(lower)84        return sorted[lower] + (sorted[upper] - sorted[lower]) * fraction85    }8687    /// Standard deviation divided by the mean; 0 when the mean is 0.88    public var coefficientOfVariation: Double { mean > 0 ? standardDeviation / mean : 0 }89}90
Files main
Menu

Theme