All Downloads are FREE. Search and download functionalities are using the official Maven repository.

.12.dp.source-code.fit.scala Maven / Gradle / Ivy

The newest version!
/*
   Copyright 2010 Aaron J. Radke

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/
package cc.drx
//import cc.drx.Implicits._
import scala.collection.Iterable

object Math{

  implicit object FitterPolynomial extends Fitter[Polynomial]{
    import org.apache.commons.math3.fitting.{WeightedObservedPoint, PolynomialCurveFitter}
    def fit[A](proto:Polynomial, ds:Iterable[A], f:A=>(Double,Double)):Polynomial = {
       val wops = new java.util.LinkedList[WeightedObservedPoint]
       for(d <- ds; (x,y) = f(d)) wops add new WeightedObservedPoint(1.0, x,y)
       Polynomial(PolynomialCurveFitter.create(proto.order).fit(wops))
    }
  }

  implicit object FitterLogistic extends Fitter[Logistic]{
    import org.apache.commons.math3.fitting.{WeightedObservedPoint, SimpleCurveFitter}
    import org.apache.commons.math3.analysis.function.Logistic.Parametric
    def fit[A](proto:Logistic, ds:Iterable[A], f:A=>(Double,Double)):Logistic = {
       val wops = new java.util.LinkedList[WeightedObservedPoint]
       for(d <- ds; (x,y) = f(d)) wops add new WeightedObservedPoint(1.0, x,y)
       val init = Array(proto.k, proto.m, proto.b, proto.q, proto.a, proto.n)
       val res = SimpleCurveFitter.create(new Parametric, init).fit(wops)
       Logistic(res(0), res(1), res(2), res(3), res(4), res(5))
    }
  }
  implicit object FitterFirstOrderStep extends Fitter[FirstOrderStep]{
    import org.apache.commons.math3.fitting.{WeightedObservedPoint, SimpleCurveFitter}
    import org.apache.commons.math3.analysis.ParametricUnivariateFunction
    class Func extends ParametricUnivariateFunction{
      def value(x:Double, ps:Double*) = ps(0)*(1.0 - math.exp(-ps(1)*x))
      def gradient(x:Double, ps:Double*) = Array[Double](
        1.0 - math.exp(-ps(1)*x),
        ps(0)*x*math.exp(-ps(1)*x)
      )
    }

    def fit[A](proto:FirstOrderStep, ds:Iterable[A], f:A=>(Double,Double)):FirstOrderStep = {
       val wops = new java.util.LinkedList[WeightedObservedPoint]
       for(d <- ds; (x,y) = f(d)) wops add new WeightedObservedPoint(1.0, x,y)
       val init = Array(proto.a, proto.w)
       val res = SimpleCurveFitter.create(new Func, init).fit(wops)
       FirstOrderStep(res(0), res(1))
    }
  }
  implicit object FitterExponential extends Fitter[Exponential]{
    import org.apache.commons.math3.fitting.{WeightedObservedPoint, SimpleCurveFitter}
    import org.apache.commons.math3.analysis.ParametricUnivariateFunction
    class Func extends ParametricUnivariateFunction{
      //b,a,k,t =>  b + a e^k(x-t)
      def value(x:Double, ps:Double*) = ps(0) + ps(1)*math.exp(ps(2)*(x-ps(3)))
      def gradient(x:Double, ps:Double*) = {
         val (b,a,k,t) = (ps(0),ps(1),ps(2),ps(3))
         val qt = x-t
         val qe = math.exp(k*qt)
         Array[Double](1.0, qe,   a*qt*qe,   -a*k*qe)
      }
    }

    def fit[A](proto:Exponential, ds:Iterable[A], f:A=>(Double,Double)):Exponential = {
       val wops = new java.util.LinkedList[WeightedObservedPoint]
       for(d <- ds; (x,y) = f(d)) wops add new WeightedObservedPoint(1.0, x,y)
       val init = Array(proto.b, proto.a, proto.k, proto.t)
       val res = SimpleCurveFitter.create(new Func, init).fit(wops)
       Exponential(res(0), res(1), res(2), res(3))
    }
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy