diff --git a/kmath-commons/src/main/kotlin/space/kscience/kmath/commons/optimization/CMOptimization.kt b/kmath-commons/src/main/kotlin/space/kscience/kmath/commons/optimization/CMOptimization.kt index 549683d60..2faee1f5d 100644 --- a/kmath-commons/src/main/kotlin/space/kscience/kmath/commons/optimization/CMOptimization.kt +++ b/kmath-commons/src/main/kotlin/space/kscience/kmath/commons/optimization/CMOptimization.kt @@ -35,7 +35,7 @@ public class CMOptimizerData(public val data: List) : Optimiza @OptIn(UnstableKMathAPI::class) public class CMOptimization : Optimizer> { - override suspend fun process( + override suspend fun optimize( problem: FunctionOptimization, ): FunctionOptimization { val startPoint = problem.getFeature>()?.point diff --git a/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/optimization/FunctionOptimization.kt b/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/optimization/FunctionOptimization.kt index ee75bfbd6..62288242a 100644 --- a/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/optimization/FunctionOptimization.kt +++ b/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/optimization/FunctionOptimization.kt @@ -67,12 +67,12 @@ public fun FunctionOptimization.withFeatures( /** * Optimize differentiable expression using specific [optimizer] form given [startingPoint] */ -public suspend fun DifferentiableExpression>.optimizeWith( +public suspend fun DifferentiableExpression.optimizeWith( optimizer: Optimizer>, startingPoint: Map, vararg features: OptimizationFeature, ): FunctionOptimization { val problem = FunctionOptimization(FeatureSet.of(OptimizationStartPoint(startingPoint), *features), this) - return optimizer.process(problem) + return optimizer.optimize(problem) } diff --git a/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/optimization/OptimizationProblem.kt b/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/optimization/OptimizationProblem.kt index 2594fa182..739ce8ca5 100644 --- a/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/optimization/OptimizationProblem.kt +++ b/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/optimization/OptimizationProblem.kt @@ -40,7 +40,3 @@ public class OptimizationParameters(public val symbols: List): Optimizat } -public interface Optimizer

{ - public suspend fun process(problem: P): P -} - diff --git a/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/optimization/Optimizer.kt b/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/optimization/Optimizer.kt new file mode 100644 index 000000000..ad1a16007 --- /dev/null +++ b/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/optimization/Optimizer.kt @@ -0,0 +1,10 @@ +/* + * Copyright 2018-2021 KMath contributors. + * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. + */ + +package space.kscience.kmath.optimization + +public interface Optimizer

{ + public suspend fun optimize(problem: P): P +} \ No newline at end of file diff --git a/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/optimization/XYOptimization.kt b/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/optimization/XYOptimization.kt index dda95fcd7..7dc17f6d9 100644 --- a/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/optimization/XYOptimization.kt +++ b/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/optimization/XYOptimization.kt @@ -12,39 +12,63 @@ import space.kscience.kmath.expressions.Expression import space.kscience.kmath.expressions.Symbol import space.kscience.kmath.misc.FeatureSet import space.kscience.kmath.misc.UnstableKMathAPI -import space.kscience.kmath.operations.Field -import space.kscience.kmath.operations.invoke +import kotlin.math.pow -public fun interface PointToCurveDistance { - public fun distance(problem: XYOptimization, index: Int): DifferentiableExpression> +public interface PointToCurveDistance : OptimizationFeature { + public fun distance(problem: XYOptimization, index: Int): DifferentiableExpression public companion object { - public fun byY( - algebra: Field, - ): PointToCurveDistance = PointToCurveDistance { problem, index -> - algebra { + public val byY: PointToCurveDistance = object : PointToCurveDistance { + override fun distance(problem: XYOptimization, index: Int): DifferentiableExpression { + val x = problem.data.x[index] val y = problem.data.y[index] - - val model = problem.model(args + (Symbol.x to x)) - model - y + return object : DifferentiableExpression { + override fun derivativeOrNull(symbols: List): Expression? = + problem.model.derivativeOrNull(symbols) + + override fun invoke(arguments: Map): Double = + problem.model(arguments + (Symbol.x to x)) - y + } } + + override fun toString(): String = "PointToCurveDistanceByY" + } - -// val default = PointToCurveDistance{args, data, index -> -// -// } } } - -public class XYOptimization( +public class XYOptimization( override val features: FeatureSet, - public val data: XYColumnarData, - public val model: DifferentiableExpression>, + public val data: XYColumnarData, + public val model: DifferentiableExpression, ) : OptimizationProblem + +public suspend fun Optimizer>.maximumLogLikelihood(problem: XYOptimization): XYOptimization { + val distanceBuilder = problem.getFeature() ?: PointToCurveDistance.byY + val likelihood: DifferentiableExpression = object : DifferentiableExpression { + override fun derivativeOrNull(symbols: List): Expression? { + TODO("Not yet implemented") + } + + override fun invoke(arguments: Map): Double { + var res = 0.0 + for (index in 0 until problem.data.size) { + val d = distanceBuilder.distance(problem, index).invoke(arguments) + val sigma: Double = TODO() + res -= (d / sigma).pow(2) + } + return res + } + + } + val functionOptimization = FunctionOptimization(problem.features, likelihood) + val result = optimize(functionOptimization) + return XYOptimization(result.features, problem.data, problem.model) +} + // //@UnstableKMathAPI //public interface XYFit : OptimizationProblem {