Skip to content

Instantly share code, notes, and snippets.

@fwaris
Created October 11, 2019 12:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fwaris/63673859a6de131a6a5c99df85360b60 to your computer and use it in GitHub Desktop.
Save fwaris/63673859a6de131a6a5c99df85360b60 to your computer and use it in GitHub Desktop.
Kernel Density estimation and visualization to compare multiple distributions together
#r @"..\packages\MathNet.Numerics.FSharp.4.8.1\lib\net45\MathNet.Numerics.FSharp.dll"
#r @"..\packages\MathNet.Numerics.4.8.1\lib\net461\MathNet.Numerics.dll"
#r @"..\packages\FSharp.Plotly.1.1.21\lib\net47\FSharp.Plotly.dll"
#r @"..\packages\FSharp.Data.3.1.1\lib\net45\FSharp.Data.dll"
open FSharp.Data
open MathNet.Numerics
open FSharp.Plotly
(*
Sample based on NY Taxi data
https://www1.nyc.gov/site/tlc/about/tlc-trip-record-data.page
Shows how to calculate Kernel Densities of taxi fare (distributions)
by payment type (e.g. cash, credit card, etc.)
*)
type Trip = CsvProvider< @"C:\s\AutoMLDemo\taxi-fare-test.csv" >
let trips = Trip.GetSample()
let trows = trips.Rows |> Seq.toArray
let fareByPaymentType =
trows
|> Array.groupBy(fun x->x.Payment_type)
|> Array.map (fun (p,xs)->p, xs |> Array.map (fun x->float x.Fare_amount))
let histograms() =
fareByPaymentType
|> Array.map (fun (v,fs)->
Chart.Histogram fs
|> Chart.withTitle v
|> Chart.Show
)
open MathNet.Numerics.Statistics
let densityByPaymentType() =
let dsByV =
fareByPaymentType
|> Array.map(fun (v,fares) ->
let frs = fares //|> Seq.sample (0.5) |> Seq.toArray
let sfrs = Array.sort frs
let xs = [|for i in 0.0 .. 0.1 .. 100.0 -> i|]
let ds = xs |> Array.map (fun x -> KernelDensity.EstimateGaussian(x,1.0,sfrs))
v,xs,ds)
let area xs = Chart.Area(xs, Opacity=0.1)
let colors = [|"blue"; "red"|]
dsByV
|> Array.mapi (fun i (v,xs,ds) ->
Array.zip xs ds
|> area
|> Chart.withTraceName v)
|> Chart.Combine
|> Chart.withTitle "Fare Density by Payment Type"
|> Chart.Show
@fwaris
Copy link
Author

fwaris commented Oct 11, 2019

newplot (12)

@fwaris
Copy link
Author

fwaris commented Oct 11, 2019

Note that Ploty charts are interactive. You can hide/show plots by clicking on the legend

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment