Language Safety Score using Logistic Regression
(* | |
This is a reaction to this blog post by Steve Shogren: | |
http://deliberate-software.com/safety-rank-part-2/ | |
*) | |
#I @"../packages" | |
#r @"Accord.3.0.1-alpha\lib\net45\Accord.dll" | |
#r @"Accord.MachineLearning.3.0.1-alpha\lib\net45\Accord.MachineLearning.dll" | |
#r @"Accord.Math.3.0.1-alpha\lib\net45\Accord.Math.dll" | |
#r @"Accord.Statistics.3.0.1-alpha\lib\net45\Accord.Statistics.dll" | |
let language, bugrate, criteria = | |
[| "F#", 0.023486288, [|1.;1.;1.;0.;1.;1.;1.;0.;0.;0.;1.;1.;1.;0.|] | |
"Haskell", 0.015551204, [|1.;1.;1.;0.;1.;1.;1.;1.;1.;0.;1.;1.;0.;1.|] | |
"Javascript", 0.039445132, [|0.;0.;0.;0.;0.;0.;0.;0.;0.;0.;1.;0.;1.;0.|] | |
"CoffeeScript", 0.047242288, [|0.;0.;0.;0.;0.;0.;0.;0.;0.;0.;1.;0.;1.;0.|] | |
"Clojure", 0.011503478, [|0.;1.;0.;0.;0.;0.;1.;0.;1.;1.;1.;0.;0.;0.|] | |
"C#", 0.03261284, [|0.;0.;1.;0.;0.;1.;1.;0.;0.;0.;1.;0.;1.;0.|] | |
"Python", 0.02531419, [|0.;0.;0.;0.;0.;0.;0.;0.;0.;0.;1.;0.;1.;0.|] | |
"Java", 0.032567736, [|0.;0.;0.;0.;0.;0.;0.;0.;0.;0.;1.;0.;1.;0.|] | |
"Ruby", 0.020303702, [|0.;0.;0.;0.;0.;0.;0.;0.;0.;0.;1.;0.;1.;0.|] | |
"Scala", 0.01904762, [|1.;1.;1.;0.;1.;1.;1.;0.;0.;0.;1.;0.;0.;0.|] | |
"Go", 0.024698375, [|0.;0.;1.;0.;0.;1.;1.;0.;0.;0.;1.;0.;1.;0.|] | |
"PHP", 0.031669293, [|0.;0.;0.;0.;0.;0.;0.;0.;0.;0.;1.;0.;1.;0.|] |] | |
|> Array.unzip3 | |
open Accord.Statistics.Models.Regression | |
open Accord.Statistics.Models.Regression.Fitting | |
let features = 14 | |
let model = LogisticRegression(features) | |
let learner = LogisticGradientDescent(model) | |
let rec learn () = | |
let delta = learner.Run(criteria, bugrate) | |
if delta > 0.0001 | |
then learn () | |
else ignore () | |
learn () |> ignore | |
for i in 0 .. (language.Length - 1) do | |
let lang = language.[i] | |
let predicted = model.Compute(criteria.[i]) | |
let real = bugrate.[i] | |
printfn "%16s Real: %.3f Pred: %.3f" lang real predicted | |
#load "FSharp.Charting.0.90.12\FSharp.Charting.fsx" | |
open FSharp.Charting | |
let last = language.Length - 1 | |
Chart.Combine [ | |
Chart.Line ([ for i in 0 .. last -> bugrate.[i]], "Real", Labels=language) | |
Chart.Line ([ for i in 0 .. last -> model.Compute(criteria.[i])], "Pred") ] | |
|> Chart.WithLegend() | |
let criteriaNames = [| | |
"Null Variable Usage" | |
"Null List Iteration" | |
"Prevent Variable Reuse" | |
"Ensure List Element Exists" | |
"Safe Type Casting" | |
"Passing Wrong Type" | |
"Misspelled Method" | |
"Missing Enum Value" | |
"Variable Mutation" | |
"Prevent Deadlocks" | |
"Memory Deallocation" | |
"Tail Call Optimization" | |
"Guaranteed Code Evaluation" | |
"Functional Purity" |] | |
for i in 0 .. (features - 1) do | |
let name = criteriaNames.[i] | |
let wald = model.GetWaldTest(i) | |
let odds = model.GetOddsRatio(i) | |
(printfn "%28s odds: %4.2f significant: %b" name odds wald.Significant) | |
[ for i in 0 .. (features - 1) -> | |
let name = criteriaNames.[i] | |
let odds = model.GetOddsRatio(i) | |
name,odds ] | |
|> List.sortBy snd | |
|> List.iter (printfn "%A") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This comment has been minimized.
Thorium commentedMar 22, 2017
With Accord.3.4.0 you can do things more functional style and don't have to write the learn-loop manually: