LoginSignup
7
7

More than 5 years have passed since last update.

ド素人の統計勉強メモ with Scala ~ ガウス分布編

Last updated at Posted at 2015-06-01

使用するライブラリ

Breeze

sbt
name := "Stats"

version := "1.0"

scalaVersion := "2.11.6"

libraryDependencies  ++= Seq(
  // other dependencies here
  "org.scalanlp" %% "breeze" % "0.11.2",
  // native libraries are not included by default. add this if you want them (as of 0.7)
  // native libraries greatly improve performance, but increase jar sizes.
  "org.scalanlp" %% "breeze-natives" % "0.11.2",
  "org.scalanlp" %% "breeze-viz" % "0.8"
)

resolvers ++= Seq(
  // other resolvers here
  // if you want to use snapshot builds (currently 0.12-SNAPSHOT), use this.
  "Sonatype Snapshots" at "https://oss.sonatype.org/content/repositories/snapshots/",
  "Sonatype Releases" at "https://oss.sonatype.org/content/repositories/releases/"
)

ガウス分布

Gaussianを使う。

引数はμ(ミュー)とσ(シグマ)

  • μ: 分布の平均

  • σ: 標準偏差

やってみる

Main.scala

/**
 * Created by FScoward on 15/06/01.
 */

import breeze.linalg.{sum, DenseVector}
import breeze.numerics.{sqrt, pow}
import breeze.plot.Figure
import breeze.stats.distributions.Gaussian
import breeze.plot._

object Main {

  def main(args: Array[String]): Unit = {

    // 例えば10人の生徒がテストでそれぞれ以下の点数を取ったとする。
    val dv = DenseVector(61, 74, 55, 85, 68, 72, 64, 80, 82, 59)

    // μ: 分布の平均
    val mu = sum(dv) / dv.length
    // σ: 標準偏差
    val sigma = sqrt(sum(dv.map(x => pow(mu - x, 2))) / dv.length)

    val figure = Figure()
    val gaussian = Gaussian(mu, sigma)
    // subplot(m,n,p) は、現在の Figure を m 行 n 列のグリッドに分割し、p で指定された位置のサブプロットに座標軸を作成します
    // つまりグラフをどこに配置するかという話。
    val p = figure.subplot(0)
    // 100点満点
    val maxScore = 100
    // 条件を付けてやらないと100点を突き抜ける
    p += hist(gaussian.condition(_ <= maxScore).sample(5000), maxScore, "Hist")
    p.xlabel = "点数"
    p.ylabel = "人数"
    figure.saveas("gaussian.png")
  }
}

出力(ヒストグラム)

histogram.png

参考資料

Think Stats ―プログラマのための統計入門

7
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
7