breeze.linalg.diag Scala Examples

The following examples show how to use breeze.linalg.diag. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.
Example 1
Source File: WaldTest.scala    From seqspark   with Apache License 2.0 5 votes vote down vote up
package org.dizhang.seqspark.stat

import breeze.linalg.{DenseMatrix, DenseVector, diag, inv}
import breeze.numerics.sqrt
import breeze.stats.distributions.StudentsT
import org.dizhang.seqspark.stat.HypoTest.NullModel
import org.dizhang.seqspark.stat.HypoTest.NullModel._


trait WaldTest {
  def nm: NullModel
  def x: DenseVector[Double]
  def reg: Regression = {
    nm match {
      case Simple(y, b) =>
        if (b)
          LogisticRegression(y, x.toDenseMatrix.t)
        else
          LinearRegression(y, x.toDenseMatrix.t)
      case Mutiple(y, c, b) =>
        if (b)
          LogisticRegression(y, DenseMatrix.horzcat(x.toDenseMatrix.t, c))
        else
          LinearRegression(y, DenseMatrix.horzcat(x.toDenseMatrix.t, c))
      case Fitted(y, _, xs, _, _, b) =>
        if (b)
          LogisticRegression(y, DenseMatrix.horzcat(x.toDenseMatrix.t, xs(::, 1 until xs.cols)))
        else
          LinearRegression(y, DenseMatrix.horzcat(x.toDenseMatrix.t, xs(::, 1 until xs.cols)))
    }
  }
  def beta: DenseVector[Double] = reg.coefficients
  def std: DenseVector[Double] = {
    sqrt(diag(inv(reg.information)))
  }
  def dof: Int = nm.dof - 1
  def t: DenseVector[Double] = beta /:/ std
  def pValue(oneSided: Boolean = true): DenseVector[Double] = {
    val dis = new StudentsT(dof)
    if (oneSided) {
      t.map(c => 1.0 - dis.cdf(c))
    } else {
      t.map(c => (1.0 - dis.cdf(math.abs(c))) * 2.0)
    }
  }
}

object WaldTest {

  def apply(nm: NullModel, x: DenseVector[Double]): WaldTest = {
    Default(nm, x)
  }

  case class Default(nm: NullModel, x: DenseVector[Double]) extends WaldTest

} 
Example 2
Source File: VectorIIDProbit.scala    From DynaML   with Apache License 2.0 5 votes vote down vote up
package io.github.mandar2812.dynaml.probability

import breeze.linalg.{DenseMatrix, DenseVector, diag}
import breeze.stats.distributions.Gaussian


  override def hessian(y: DenseVector[Double],
                       f: DenseVector[Double]): DenseMatrix[Double] = {
    diag(DenseVector((y.toArray zip f.toArray).map((couple) => {
      val n = standardGaussian.pdf(couple._2)
      val product = couple._1*couple._2
      val l = h(product)
      -1.0*(n*n)/(l*l) - product*n/l
    })))
  }

  override def gaussianExpectation(normalDistParams: (DenseVector[Double],
    DenseVector[Double])): DenseVector[Double] = {
    DenseVector((normalDistParams._1.toArray zip normalDistParams._2.toArray).map((couple) => {
      val gamma = math.sqrt(1.0 + couple._2)
      standardGaussian.pdf(couple._1/gamma)
    }))
  }
} 
Example 3
Source File: VectorIIDSigmoid.scala    From DynaML   with Apache License 2.0 5 votes vote down vote up
package io.github.mandar2812.dynaml.probability

import breeze.linalg.{DenseMatrix, DenseVector, diag}
import breeze.numerics.sigmoid


  override def hessian(y: DenseVector[Double],
                       f: DenseVector[Double]): DenseMatrix[Double] = {
    diag(DenseVector((y.toArray zip f.toArray).map((couple) => {
      val pi = sigmoid(couple._1*couple._2)
      -1.0*pi*(1.0 - pi)
    })))
  }

  override def gaussianExpectation(normalDistParams: (DenseVector[Double],
    DenseVector[Double])): DenseVector[Double] = {
    DenseVector((normalDistParams._1.toArray zip normalDistParams._2.toArray).map((couple) => {
      val gamma = math.sqrt(1.0 + (math.Pi*couple._2/8.0))
      sigmoid(couple._1/gamma)
    }))
  }
}