SPB Git

Repository Public

HoltWintersTests.swift 5.2 KB 120 lines swift

1// =============================================================================2//                  SP BOUCHER | SOFTWARE & RESEARCH3//             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// PROJECT METADATA7// -----------------------------------------------------------------------------8// Project          : ORIX Sheet9// File             : Packages/SheetAnalytics/Tests/FinanceModelsTests/HoltWintersTests.swift10// Module           : FinanceModelsTests11// Repository       : https://git.spboucher.ai/orix-sheet12// Created          : 2026-11-2113// Last Modified    : 2026-11-2114//15// =============================================================================16// AUTHOR & PROFESSIONAL AFFILIATION17// =============================================================================18//19// Author           : Simon-Pierre Boucher20// Position         : Professor of Finance21// Institution      : Université du Québec en Outaouais (UQO)22// Department       : Département des sciences administratives23// Organization     : SP Boucher24//25// Website          : https://spboucher.ai26// Git              : https://git.spboucher.ai27//28// =============================================================================29// OFFICIAL CONTACT DIRECTORY30// =============================================================================31//32// General          : info@spboucher.ai33// Professional     : contact@spboucher.ai34// Direct           : simon@spboucher.ai35// Research         : research@spboucher.ai36// Development      : dev@spboucher.ai37// Support          : support@spboucher.ai38// Security/Admin   : admin@spboucher.ai39// Notifications    : notifications@spboucher.ai40//41// Preferred Contact: support@spboucher.ai42//43// =============================================================================44// COPYRIGHT & INTELLECTUAL PROPERTY45// =============================================================================46//47// Copyright (c) 202648// Simon-Pierre Boucher49//50// All rights reserved where applicable.51//52// This file is governed by the applicable project license,53// copyright notices, institutional agreements and relevant law.54//55// Unless expressly authorized by the applicable license or56// rights holder, copying, redistribution, sublicensing,57// publication and commercial exploitation are not permitted.58//59// Existing third-party rights and notices remain unaffected.60//61// =============================================================================62// CONFIDENTIALITY & SECURITY63// =============================================================================64//65// Classification   : Public66// Distribution     : Public repository; use is governed by the project license67// Security Contact : admin@spboucher.ai68//69// Access to this source does not independently grant permission70// to redistribute or disclose confidential materials.71//72// Do not embed credentials, private keys, access tokens,73// certificates or confidential personal information.74//75// =============================================================================76// LICENSE & MAINTENANCE77// =============================================================================78//79// License          : None specified (no license file in this repository)80// Maintainer       : Simon-Pierre Boucher81// Technical Contact: dev@spboucher.ai82//83// =============================================================================84// END OF SP BOUCHER SOURCE HEADER85// =============================================================================8687import Foundation88import Testing89import AnalyticsTestSupport90import Statistics91@testable import FinanceModels9293@Suite struct HoltWintersWrapperTests {94    /// Exactly linear trend + exactly periodic seasonal pattern: the forecast must continue it.95    @Test func recoversDeterministicSeasonalTrend() throws {96        let season = [5.0, -2, -6, 3]97        let y = (0..<32).map { 100 + 2.5 * Double($0) + season[$0 % 4] }98        let f = try Forecasting.holtWintersForecast(y, seasonLength: 4, horizon: 8)99        let want = (32..<40).map { 100 + 2.5 * Double($0) + season[$0 % 4] }100        #expect(f.count == 8)101        #expect(allClose(f, want, rel: 1e-4), "\(f) vs \(want)")102        let fit = try Forecasting.holtWinters(y, seasonLength: 4)103        #expect(fit.period == 4 && fit.rmse < 0.2 * Descriptive.standardDeviation(y), "rmse \(fit.rmse)")104        #expect(fit.forecast(8) == f)105        #expect(fit.forecastInterval(4) != nil)106    }107108    @Test func multiplicativeAndErrors() throws {109        let idx = [1.2, 0.8, 0.9, 1.1]110        let y = (0..<40).map { (50 + Double($0)) * idx[$0 % 4] }111        let fit = try Forecasting.holtWinters(y, seasonLength: 4, multiplicative: true)112        let f = fit.forecast(4)113        let want = (40..<44).map { (50 + Double($0)) * idx[$0 % 4] }114        #expect(allClose(f, want, rel: 2e-2), "\(f) vs \(want)")115        #expect(fit.forecastInterval(4) == nil)        // no closed-form interval with multiplicative seasonality116        #expect(throws: StatError.self) { try Forecasting.holtWinters([1, 2, 3, 4, 5], seasonLength: 4) }117        #expect(throws: StatError.self) { try Forecasting.holtWinters(y, seasonLength: 1) }118    }119}120
Files main
Menu

Theme