InferenceQL / inferenceql.gpm.glm

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

inferenceql.gpm.glm

tests linter

Linear models.

Prerequisites

Model training requires OpenBLAS.

brew install openblas

Usage

This library is intended for use with inferenceql.query. First, launch a REPL with inferenceql.query on the class path:

clj -Sdeps '{:deps {probcomp/inferenceql.query {:git/url "git@github.com:probcomp/inferenceql.query.git" :sha "…"}}}'

Next, define the model and the data set to be queried. model->gpm accepts instances of smile.regression.LinearModel or instances of smile.glm.GLM:

(require '[clojure.walk :as walk]
         '[inferenceql.gpm.glm.smile :as smile])

(import '[smile.data.formula Formula]
        '[smile.io Read]
        '[smile.regression OLS])

(def data-frame (Read/arff "https://raw.githubusercontent.com/meteoinfo/MIML/master/miml/datasets/data/weka/regression/2dplanes.arff"))

(def data
  (-> data-frame
      (smile/data-frame->maps)
      (walk/keywordize-keys)))

(def smile-model (OLS/fit (Formula/lhs "y") data-frame))

(def gpm (smile/model->gpm smile-model "y" (.names data-frame)))
(import '[smile.data.formula Formula]
        '[smile.glm GLM]
        '[smile.glm.model Bernoulli])

(def data
   (let [names [:x :y :z]
         gen-cell #(rand-int 2)
         gen-row #(repeatedly (count names) gen-cell)
         gen-dataset #(repeatedly 20 gen-row)]
     (smile/vectors->data-frame Integer/TYPE names (gen-dataset))))

(def model (GLM/fit (Formula/lhs "x") data (Bernoulli/logit)))

Finally, launch the InferenceQL REPL:

(require '[inferenceql.query.main :as main])

(main/repl data {:model gpm})
select * from generate y under model conditioned by x1=1 and x2=1 and x3=1 and x4=1 and x5=1 and x6=1 and x7=1 and x8=1 and x9=1 and x10=1 limit 5;

About

License:Apache License 2.0


Languages

Language:Clojure 95.2%Language:Nix 4.3%Language:Shell 0.5%