LoginSignup
0
0

More than 5 years have passed since last update.

F#でPRMLに出てくるガウス分布に従うランダムノイズを乗せたデータを作る

Last updated at Posted at 2015-10-12

PRMLの付録Aにでてくる人工データ集合をF#でコーディングしました。
sin(2πx)に対して、標準偏差が0.2のガウス分布に従うランダムノイズを加算してデータを作成します。PRLMでは標準偏差が0.3でしたが、sinカーブとは似ても似つかぬ何かになったので0.2に変えています。

ガウス分布に従うノイズ生成のアルゴリズムは、こちらをそのまま持ってきました。
http://www.sat.t.u-tokyo.ac.jp/~omi/random_variables_generation.html#Gauss

open System
let random = System.Random()

//Gaissian noise
//http://www.sat.t.u-tokyo.ac.jp/~omi/random_variables_generation.html#Gauss
let randomNormal (u:float) (s:float) : float =
    let x = random.NextDouble()
    let y = random.NextDouble()
    let z = sqrt(-2.0*log(x)) * sin(2.0*Math.PI*y)
    u + s * z

let makeSinData (num:int) = 
    let mutable l = []
    for x in 0.0..float(num) do
        let xrad = x*2.0*Math.PI/float(num)
        let noise = randomNormal 0.0 0.2
        let y = sin(xrad)+noise
        l <- l@[y]
    l

[<EntryPoint>]
let main argv = 
    let data = makeSinData 10
    printfn "%A" data
    0 // 整数の終了コードを返します

実行結果をExcelでプロットするとこんな感じです。

plot.png
それっぽいのできてますね。

0
0
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
0
0