Benutzer:MovGP0/F♯/Statistics

aus Wikipedia, der freien Enzyklopädie
Zur Navigation springen Zur Suche springen
   MovGP0        Über mich        Hilfen        Artikel        Weblinks        Literatur        Zitate        Notizen        Programmierung        MSCert        Physik      

open System

let random = new Random()
let randomNumber() = random.NextDouble() // define alias
let randomData = [for i in 1..100 -> randomNumber() * 10.0]

let sum = randomData |> Seq.sum
let average = randomData |> Seq.average
let minimum = randomData |> Seq.min
let maximum = randomData |> Seq.max

// calculate variance
let variance (values:seq<float>) = 
   let average = Seq.average values
   values
   |> Seq.map (fun x -> (1.0 / float(Seq.length values)) * (x - average) ** 2.0)
   |> Seq.sum
let var = variance randomData

// calculate standard derivation
let standardDerivation (values:seq<float>) = sqrt(variance(values))
let standardDerivation (values:seq<float>) = 
   let avg = Seq.average values
   values
   |> Seq.fold (fun acc x -> acc + (1.0 / float (Seq.length values)) * (x - avg) ** 2.0) 0.0 // apply function to every element and accumulate the result
   |> sqrt

Statistics with Math.NET

[Bearbeiten | Quelltext bearbeiten]
let dist = new Normal(0.0, 1.0)
let samples = dist.Samples() |> Seq.take 1000 |> Seq.toList

let statistics = new DescriptiveStatistics(samples)

let maximum = statistics.Maximum
let minimum = statistics.Minimum
let mean = statistics.Mean
let variance = statistics.Variance
let standardDerivation = statistics.StandardDerivation

Linear Regression

[Bearbeiten | Quelltext bearbeiten]
open System
open MathNet.Numerics
open MathNet.Numerics.LinearAlgebra
open MathNet.Numerics.LinearAlgebra.Double
open MathNet.Numerics.Distributions

let M = DenseMatrix.ofColumsList 5 2 [ List.init 5 (fun i -> 1.0); [10.0; 20.0; 30.0; 40.0; 50.0] ]
let v = DenseVector [| 8.0; 21.0; 32.0; 40.0; 49.0 |]
let p = M.QR().Solve(v)
let (a, b) = (p.[0], p.[1])

Polynomial Regression

[Bearbeiten | Quelltext bearbeiten]

Input data is

let noise = Normal.WithMeanVariance(0.0, 0.5)
let xData = [ -10.0 .. 0.2 .. 10.0 ]
let yData = [ for x in xData do yield (x ** 2) - (3.0 * x) + noise.Sample() ]

square estimation for coefficients

siehe auch: Vandermonde-Matrix

let N = xData.length
let order = 2

let vandermondeRow v = [for x in [0..order] do yield v ** (float x)]

let vandermonde = xdata |> Seq.map vandermondeRow |> Seq.toList

let A = (vandermonde |> DenseMatrix.ofRowsList N (order + 1))

let createYVector order l = [for x in [0..order] do yield l]
let Y = (createYVector order ydata |> DenseMatrix.ofRowsList (order + 1)).Transpose()

let coeffs = (A.Transpose() * A).LU().Solve(A.Transpose() * Y).Column(0)

let calculate x = (vandermondeRow(x) |> DenseVector.ofList) * coeffs

let fitxs = [(Seq.min xData) .. 0.2 .. (Seq.max xData)]
let fitys = fitxs |> List.Map calculate
let fits = [for x in [(Seq.min xData) .. 0.2 .. (Seq.max xData)] do yield (x, calculate x)]

Chart Result

open FSharp.Charting
open System.Windows.Forms.DataVisualization.Charting

fsi.AddPrinter(fun (ch:ChartTypes.GenericChart) -> ch.ShowChart(); "FSharpCharting")
let chart = Chart.Combine [Chart.Point(List.zip xData yData); Chart.Line(fits).WithTitle("Polynomial regression")]