SPB Git

Repository Public MIT

Domains.swift 6.5 KB 142 lines swift

1// =============================================================================2//                  SP BOUCHER | SOFTWARE & RESEARCH3//             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// PROJECT METADATA7// -----------------------------------------------------------------------------8// Project          : ORIX Calc9// File             : Benchmarks/Sources/orix-bench/Domains.swift10// Description      : Benchmarks of the financial, statistics, graph and unit11//                    engines.12// Module           : orix-bench13// Repository       : https://git.spboucher.ai/orix-calc14// Created          : 2026-11-2015// Last Modified    : 2026-11-2016//17// =============================================================================18// AUTHOR & PROFESSIONAL AFFILIATION19// =============================================================================20//21// Author           : Simon-Pierre Boucher22// Position         : Professor of Finance23// Institution      : Université du Québec en Outaouais (UQO)24// Department       : Département des sciences administratives25// Organization     : SP Boucher26//27// Website          : https://spboucher.ai28// Git              : https://git.spboucher.ai29//30// =============================================================================31// OFFICIAL CONTACT DIRECTORY32// =============================================================================33//34// General          : info@spboucher.ai35// Professional     : contact@spboucher.ai36// Direct           : simon@spboucher.ai37// Research         : research@spboucher.ai38// Development      : dev@spboucher.ai39// Support          : support@spboucher.ai40// Security/Admin   : admin@spboucher.ai41// Notifications    : notifications@spboucher.ai42//43// Preferred Contact: support@spboucher.ai44//45// =============================================================================46// COPYRIGHT & INTELLECTUAL PROPERTY47// =============================================================================48//49// Copyright (c) 202650// Simon-Pierre Boucher51//52// All rights reserved where applicable.53//54// This file is governed by the applicable project license,55// copyright notices, institutional agreements and relevant law.56//57// Unless expressly authorized by the applicable license or58// rights holder, copying, redistribution, sublicensing,59// publication and commercial exploitation are not permitted.60//61// Existing third-party rights and notices remain unaffected.62//63// =============================================================================64// CONFIDENTIALITY & SECURITY65// =============================================================================66//67// Classification   : Public68// Distribution     : Public repository; use is governed by the project license69// Security Contact : admin@spboucher.ai70//71// Access to this source does not independently grant permission72// to redistribute or disclose confidential materials.73//74// Do not embed credentials, private keys, access tokens,75// certificates or confidential personal information.76//77// =============================================================================78// LICENSE & MAINTENANCE79// =============================================================================80//81// License          : Proprietary, all rights reserved (see license file)82// License File     : LICENSE83// Maintainer       : Simon-Pierre Boucher84// Technical Contact: dev@spboucher.ai85//86// =============================================================================87// END OF SP BOUCHER SOURCE HEADER88// =============================================================================8990import Foundation91import ORIXExpression92import ORIXFinancial93import ORIXStatistics94import ORIXGraph95import ORIXUnits9697func benchDomains() {98    // Financial99    let terms = LoanTerms(principal: 300_000, annualRate: 0.05, amortizationYears: 25, compounding: .canadianFixed)100    measure("Mortgage payment (Canadian convention)") { _ = try Loan.payment(terms) }101    measure("Amortization schedule, 300 payments, cent-exact") { _ = try Loan.schedule(terms) }102    let flows = CashFlowSeries(flows: [-100_000] + (1...240).map { Double(400 + $0 * 7) })103    measure("IRR of 241 cash flows (all real roots searched)") { _ = try flows.irr() }104    let option = OptionInputs(spot: 42, strike: 40, rate: 0.10, volatility: 0.20, time: 0.5)105    measure("Black–Scholes–Merton price") { _ = try BlackScholes.price(.call, option) }106    measure("Implied volatility") { _ = try BlackScholes.impliedVolatility(.call, price: 4.76, option) }107    measure("American put, binomial tree, 1 000 steps") { _ = try BinomialTree.price(.put, style: .american, option, steps: 1000) }108109    // Statistics110    measure("Monte Carlo, 1 000 000 normal trials, seeded") {111        _ = try MonteCarlo.run(trials: 1_000_000, seed: 42) { g in Sampling.standardNormal(using: &g) }112    }113    var generator = SeededGenerator(seed: 7)114    let n = 10_000115    let x1 = (0..<n).map { _ in Sampling.standardNormal(using: &generator) }116    let x2 = (0..<n).map { _ in Sampling.standardNormal(using: &generator) }117    let x3 = (0..<n).map { _ in Sampling.standardNormal(using: &generator) }118    let y = (0..<n).map { 1 + 2 * x1[$0] - x2[$0] + 0.5 * x3[$0] + 0.1 * Sampling.standardNormal(using: &generator) }119    measure("Multiple regression, 10 000 rows × 3 predictors, with diagnostics") { _ = try LinearRegression.multiple(y: y, predictors: [x1, x2, x3]) }120    measure("Quantile of 10 000 values") { _ = try Descriptive.quantile(x1, 0.975) }121    let t = try! StudentTDistribution(degreesOfFreedom: 12)122    measure("Student t quantile (inverse incomplete beta)") { _ = try t.quantile(0.975) }123124    // Graph125    let viewport = Viewport(xMin: -10, xMax: 10, yMin: -7.5, yMax: 7.5, width: 1200, height: 800)126    for text in ["sin(x)", "tan(x)", "1/x", "sin(100x)"] {127        if let item = try? PlotParser.parse(text) {128            measure("Graph: sample `\(text)` at 1200 × 800 with discontinuity detection") { _ = try GraphEngine.geometry(for: item, viewport: viewport) }129        }130    }131    if let item = try? PlotParser.parse("x^2 + y^2 = 25") {132        measure("Graph: implicit curve x² + y² = 25 at 1200 × 800") { _ = try GraphEngine.geometry(for: item, viewport: viewport) }133    }134    measure("Graph: 60 × 60 surface mesh of sin(x)·cos(y) with normals") {135        _ = SurfacePlotter.mesh({ x, y in sin(x) * cos(y) }, x: -5...5, y: -5...5)136    }137138    // Units139    let catalog = UnitCatalog.standard140    measure("Unit conversion, exact decimal (mi → km)") { _ = try catalog.convert(Decimal(26.2), from: "mi", to: "km") }141}142
Files main
Menu

Theme