Repository Public
1import Foundation2import SheetCore3import Statistics4import AnalysisKit56/// Tests of equal variances: F (two samples), Levene, Brown–Forsythe, Bartlett.7enum VarianceTestTool {8 static let descriptor: ToolDescriptor = {9 var t = ToolDescriptor(10 id: "compare.variance", title: L("Variance Tests"), group: Common.groupCompare, systemImage: "arrow.up.and.down",11 summary: L("Tests whether two or more groups have equal variances."),12 fields: [13 .choice("test", L("Test"), [FieldOption("brownForsythe", L("Brown–Forsythe (median)")), FieldOption("levene", L("Levene (mean)")),14 FieldOption("bartlett", L("Bartlett")), FieldOption("f", L("F-test (two samples)"))]),15 .range("data", L("Data range")),16 ] + GroupFields.fields() + [17 Common.alternativeField(visible: { $0.choice("test") == "f" }),18 Common.levelField(visible: { $0.choice("test") == "f" }),19 .alpha(L("Significance level α")),20 ],21 info: [22 L("F-test: ratio of the two sample variances with (n₁ − 1, n₂ − 1) degrees of freedom; it is sensitive to departures from normality."),23 L("Levene's test is a one-way ANOVA on absolute deviations from the group means; Brown–Forsythe uses the group medians and is more robust."),24 L("Bartlett's test assumes normal populations."),25 ],26 run: run)27 t.demo = { _, v in v.set("data", range: "Groups!A1:C31"); v.set("groupCols", columns: [0, 1, 2]) }28 return t29 }()3031 static func run(_ input: ToolInput) throws -> ToolResult {32 let v = input.values33 let alpha = try Common.alpha(v)34 let frame = try input.frame()35 let (groups, missing) = try GroupFields.read(input)36 let data = groups.map(\.values)37 let test: HypothesisTest38 var null = L("all groups have the same variance")39 switch v.choice("test") {40 case "f":41 guard groups.count == 2 else { throw ToolError(L("The F-test compares exactly two groups; %d were given.", groups.count)) }42 test = try Inference.varianceFTest(data[0], data[1], alternative: Common.alternative(v), level: try Common.level(v))43 null = L("the two population variances are equal (ratio = 1)")44 case "levene": test = try Inference.leveneTest(data, center: .mean)45 case "bartlett": test = try Inference.bartlettTest(data)46 default: test = try Inference.leveneTest(data, center: .median)47 }48 var rows: [[Value]] = [[.text(L("Group")), .text(L("n")), .text(L("Mean")), .text(L("Variance")), .text(L("Standard deviation"))]]49 for g in groups {50 rows.append([.text(g.label), .number(Double(g.values.count)), .finite(Descriptive.mean(g.values)), .finite(Descriptive.variance(g.values)),51 .finite(Descriptive.standardDeviation(g.values))])52 }53 let extra: [ResultBlock] = [54 .table(ReportTable(title: L("Groups"), rows: rows, columnFormats: [1: .integer])),55 .chart(Common.boxPlot(groups, title: L("Distribution by group"), yTitle: L("Value"), locale: input.locale)),56 ]57 return Common.testResult(test, title: Common.testName(test.name), subtitle: L("%@ — %d groups", frame.label, groups.count), alpha: alpha, null: null, locale: input.locale,58 notes: [GroupFields.missingNote(missing)], extra: extra, sheetName: L("Variance test"))59 }60}61