KMP library for tensors #300

Merged
grinisrit merged 215 commits from feature/tensor-algebra into dev 2021-05-08 09:48:04 +03:00
Showing only changes of commit c2db3a23e1 - Show all commits

View File

@ -9,6 +9,8 @@ import space.kscience.kmath.tensors.core.DoubleTensor
import space.kscience.kmath.tensors.core.algebras.DoubleAnalyticTensorAlgebra
import space.kscience.kmath.tensors.core.algebras.DoubleLinearOpsTensorAlgebra
import kotlin.math.abs
// OLS estimator using SVD
fun main() {
@ -26,8 +28,7 @@ fun main() {
doubleArrayOf(1.0, 2.5, 3.4, 5.0, 10.1)
)
println("Real alpha:\n" +
"$alpha")
println("Real alpha:\n$alpha")
// also take sample of size 20 from normal distribution for x TODO rename
val x = randNormal(
@ -35,20 +36,22 @@ fun main() {
randSeed
)
// calculate y and add gaussian noise (N(0, 0.05)) TODO rename
// calculate y and add gaussian noise (N(0, 0.05))
// TODO: please add an intercept: Y = beta * X + alpha + N(0,0.5)
val y = x dot alpha
y += y.randNormalLike(randSeed) * 0.05
// now restore the coefficient vector with OSL estimator with SVD
// TODO: you need to change accordingly [X 1] [alpha beta] = Y
// TODO: inverting [X 1] via SVD
val (u, singValues, v) = x.svd()
// we have to make sure the singular values of the matrix are not close to zero
println("Singular values:\n" +
"$singValues")
// TODO something with Boolean tensors
println("Singular values:\n$singValues")
// inverse Sigma matrix can be restored from singular values with diagonalEmbedding function
val sigma = diagonalEmbedding(1.0/singValues)
val sigma = diagonalEmbedding(singValues.map{ x -> if (abs(x) < 1e-3) 0.0 else 1.0/x })
val alphaOLS = v dot sigma dot u.transpose() dot y
println("Estimated alpha:\n" +