I try to define a type class for
reproducing kernel Hilbert spaces (RKHS), but I am stuck and need some help.
A kernel k is symmetric positive definite function (T, T) => Double. Moreover with curring we get T => H, where H is a RKHS of functions T => Double.
It is important that the inner product of two elements of H is given by k(x,_) dot k(y,_) = k(x,y)
A trivial example for a kernel is the linear kernel
k(x,y) => x*y.
import spire.algebra._
import spire.implicits._
/**
* A reproducing kernel Hilbert space (RKHS)
* May need to store the kernel and the input value it was mapped from
*/
trait RKHS[T] extends (T => Double)
/**
* A kernel is a symmetric positive definite function TxT -> Double
* It can map a T into a reproducing kernel Hilbert space (RKHS)
* with x -> k(x,_)
*/
trait Kernel[K, T] extends ((T, T) => Double) with (T => RKHS[T]) {
def eval(x: T, y: T): Double // kernel evaluation
override def apply(x: T): RKHS[T] = new RKHS[T] { // Hilbert space map
override def apply(y: T) = eval(x, y)
}
override final def apply(x: T, y: T): Double = apply(x)(y)
}
/**
* The space of functions of type T => V, where V is also a VectorSpace is a Vector space
*/
trait KernelIsVectorSpace[K, T] extends FunctionSpaceIsVectorSpace[T, T => Double] {
override implicit def rng: VectorSpace[T => Double, Double] = implicitly[VectorSpace[T => Double, Double]]
}
/**
* Needs to be implemented
*/
class RKHSisInnerProuctSpace[T] extends InnerProductSpace[RKHS[T], Double] {
override def dot(f: RKHS[T], g: RKHS[T]): Double = ???
override def scalar: Field[Double] = implicitly[Field[Double]]
override def zero: RKHS[T] = ???
override def negate(f: RKHS[T]): RKHS[T] = ???
override def plus(f: RKHS[T], g: RKHS[T]): RKHS[T] = ???
override def timesl(r: Double, f: RKHS[T]): RKHS[T] = ???
}
/**
* The space of functions of type T => V, where V is also a VectorSpace is a Vector space
*/
trait FunctionSpaceIsVectorSpace[U, V] extends VectorSpace[U => V, Double] {
type O = (U => V)
implicit def rng: VectorSpace[V, Double]
override def scalar: Field[Double] = implicitly[Field[Double]]
override def zero: U => V = _ => rng.zero
override def negate(f: O): O = x => -1.0 *: f(x)
override def plus(f: O, g: O): O = x => f(x) + g(x)
override def timesl(r: Double, f: O): O = x => r *: f(x)
}
It would be great if someone could help me here.