Repository Public
1import Foundation2import SheetCore3import Statistics4import AnalysisKit56/// Shapiro–Wilk and Jarque–Bera normality tests with histogram and normal Q–Q plot.7enum NormalityTool {8 static let descriptor: ToolDescriptor = {9 var t = ToolDescriptor(10 id: "compare.normality", title: L("Normality Tests"), group: Common.groupCompare, systemImage: "waveform.path.ecg",11 summary: L("Shapiro–Wilk and Jarque–Bera tests of normality, with a histogram and a normal Q–Q plot."),12 fields: [13 .range("data", L("Data range")),14 .column("x", L("Variable")),15 .choice("test", L("Test"), [FieldOption("both", L("Both tests")), FieldOption("shapiroWilk", L("Shapiro–Wilk")), FieldOption("jarqueBera", L("Jarque–Bera"))]),16 .alpha(L("Significance level α")),17 ],18 info: [19 L("Shapiro–Wilk uses Royston's algorithm AS R94 and is available for 3 to 4999 observations."),20 L("Jarque–Bera is an asymptotic χ²(2) test on the skewness and excess kurtosis; it is unreliable in small samples."),21 L("Failing to reject normality does not prove that the data are normal."),22 ],23 run: run)24 t.demo = { _, v in v.set("data", range: "Regression!A1:G121"); v.set("x", column: 0) }25 return t26 }()2728 static func run(_ input: ToolInput) throws -> ToolResult {29 let alpha = try Common.alpha(input.values)30 let frame = try input.frame()31 let col = try input.column("x")32 let x = col.present33 let choice = input.values.choice("test")34 var tests = [HypothesisTest]()35 if choice != "jarqueBera" {36 guard x.count <= 4999 || choice == "both" else { throw ToolError(L("Shapiro–Wilk is available for 3 to 4999 observations; the column has %d.", x.count)) }37 if x.count <= 4999 { tests.append(try Inference.shapiroWilk(x)) }38 }39 if choice != "shapiroWilk" { tests.append(try Inference.jarqueBera(x)) }40 var blocks: [ResultBlock] = []41 var rows: [[Value]] = [[.text(L("Test")), .text(L("Statistic")), .text(L("Value")), .text(L("Degrees of freedom")), .text(L("p-value")), .text(L("Decision at α"))]]42 for t in tests {43 rows.append([.text(Common.testName(t.name)), .text(t.statisticName), .finite(t.statistic), .finite(t.df), .finite(t.pValue),44 .text(t.pValue < alpha ? L("reject normality") : L("fail to reject normality"))])45 }46 blocks.append(.metrics(tests.flatMap { [Metric(L("Statistic (%@)", $0.statisticName), $0.statistic, .fixed(4)), Metric(L("p-value (%@)", $0.statisticName), $0.pValue, .pValue)] }47 + [Metric(L("Observations"), Double(x.count), .integer)]))48 blocks.append(.table(ReportTable(title: L("Normality tests"), rows: rows, columnFormats: [2: .fixed(4), 3: .integer, 4: .pValue])))49 for t in tests {50 blocks.append(.interpretation(Common.testName(t.name) + ". " + Interpret.decision(p: t.pValue, alpha: alpha, null: L("the data come from a normal distribution"), locale: input.locale)))51 }52 let skew = Descriptive.skewness(x), kurt = Descriptive.kurtosis(x)53 blocks.append(.table(ReportTable.keyValue(title: L("Shape of the distribution"), header: (L("Quantity"), L("Value")), [54 (L("Mean"), .finite(Descriptive.mean(x)), .auto), (L("Standard deviation (n − 1)"), .finite(Descriptive.standardDeviation(x)), .auto),55 (L("Skewness"), .finite(skew), .fixed(4)), (L("Excess kurtosis"), .finite(kurt), .fixed(4)),56 ])))57 let qq = ChartKit.normalQQ(x)58 blocks.append(.charts([59 ChartBlock(title: L("Histogram of %@", col.name), spec: ChartKit.histogram(x, xTitle: col.name, yTitle: L("Frequency")),60 summary: L("Histogram of %d values of %@; skewness %@, excess kurtosis %@.", x.count, col.name, Fmt.fixed(skew, 2, locale: input.locale),61 Fmt.fixed(kurt, 2, locale: input.locale)), height: 220),62 ChartBlock(title: L("Normal Q–Q plot"),63 spec: ChartKit.scatter([(col.name, qq.theoretical, qq.ordered)], xTitle: L("Theoretical normal quantile"), yTitle: L("Ordered value"), trend: true),64 summary: L("Normal Q–Q plot of %d points; points close to the straight line indicate agreement with a normal distribution.", x.count), height: 220),65 ]))66 let assumptions = Array(Set(tests.flatMap(\.assumptions))).sorted()67 if !assumptions.isEmpty { blocks.append(.assumptions(assumptions, title: L("Assumptions"))) }68 var notes = tests.flatMap(\.notes)69 if choice == "both", x.count > 4999 { notes.append(L("Shapiro–Wilk was skipped: it is available for 3 to 4999 observations.")) }70 if let m = Common.missingNote([col]) { notes.append(m) }71 if !notes.isEmpty { blocks.append(.note(notes)) }72 return ToolResult(title: L("Normality Tests"), subtitle: frame.label + " — " + col.name, blocks: blocks, sheetName: L("Normality"))73 }74}75