Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move interface definitions into packing modules. #186

Merged
merged 9 commits into from
Feb 20, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ DOC_BUILD_DIR="_doc_build"
PACKAGES_INSTALL=ocephes lacaml lbfgs
PACKAGES_INSTALL_TEST=kaputt bisect_ppx dsfo

.PHONY: all clean test build full setup doc
.PHONY: all clean test build full setup doc examples examples_lite

default: FORCE
@echo "available targets:"
Expand All @@ -19,6 +19,7 @@ default: FORCE
@echo " doc generates ocamldoc documentations"
@echo " clean deletes all produced files"
@echo " report generate Bisect_ppx coverage report"
@echo " showed generate showed.mli that contains the current interfaces"

# This should be called something else.
setup:
Expand All @@ -36,7 +37,6 @@ build:
build_lite:
ocaml pkg/pkg.ml build --with-lacaml false --with-lbfgs false --with-ocephes false


clean:
ocaml pkg/pkg.ml clean
ocaml pkg/pkg.ml clean --build-dir $(TEST_BUILD_DIR)
Expand All @@ -61,6 +61,7 @@ covered_test_lite:
ocaml pkg/pkg.ml build --build-dir $(COVERED_TEST_BUILD_DIR) --with-lacaml false --with-lbfgs false --with-ocephes false --with-coverage true -n omltest && \
time ocaml pkg/pkg.ml test --build-dir $(COVERED_TEST_BUILD_DIR)


#### Test Coverage

report_dir:
Expand All @@ -82,5 +83,19 @@ clean_reports:
doc:
ocamlbuild -classic-display -use-ocamlfind -plugin-tag 'package(str)' -no-links -build-dir $(DOC_BUILD_DIR) -docflags '-colorize-code,-charset,utf-8' doc/api.docdir/index.html

# topkg doc --build-dir $(DOC_BUILD_DIR)
showed: build
utop -I _build/src oml.cma tools/show.ml > showed.mli
utop -require lacaml -require lbfgs -require ocephes -I _build/src oml.cma -I _build/src-full oml_full.cma tools/show_full.ml > showed_full.mli

#### Examples

examples_lite:
ocaml pkg/pkg.ml build --build-dir $(TEST_BUILD_DIR) --with-lacaml false --with-lbfgs false --with-ocephes false -n examples && \
time ocaml pkg/pkg.ml test --build-dir $(TEST_BUILD_DIR)

examples:
ocaml pkg/pkg.ml build --build-dir $(TEST_BUILD_DIR) -n examples && \
time ocaml pkg/pkg.ml test --build-dir $(TEST_BUILD_DIR)

FORCE:

2 changes: 2 additions & 0 deletions _tags
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ true: safe_string, annot, bin_annot, principal, warn(A-42-44-48-50)
<src-full/**/*.mlj> : package(kaputt), package(dsfo)
<test/**> : package(dsfo), package(kaputt)
<test/omlf_test.native> : package(lacaml), package(ocephes), package(lbfgs)
<examples/*> : include
<examples/oml_full/*.native> : package(lacaml), package(ocephes), package(lbfgs)

"_build": -traverse
"_test_build": -traverse
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
open Oml
open Util
open Classification

let feature_map = function | `shortbread -> 0 | `lager -> 1 | `whiskey -> 2 | `porridge -> 3 | `football-> 4 ;;
let data =
[
Expand All @@ -20,10 +23,15 @@ let to_feature l = l |> List.map ~f:feature_map |> Array.of_list ;;
module NB = Naive_bayes.Binomial(
struct
type feature = [ `shortbread | `lager | `whiskey | `porridge | `football ] list
type clas = [ `English | `Scottish]
type class_ = [ `English | `Scottish]
let encoding = to_feature
let size = 5
end);;
let naiveb = NB.estimate ~opt:( NB.opt ~bernoulli:true ()) data ;;
let sample = [ `shortbread ; `whiskey; `porridge ] ;;
let result = NB.eval naiveb sample;;

let () =
if !Sys.interactive then () else
Printf.printf "Probability of English: %f\t(expecting 19%%)\n" (List.assoc `English result);
Printf.printf "Probability of Scottish: %f\t(expecting 81%%)\n" (List.assoc `Scottish result)
18 changes: 18 additions & 0 deletions examples/oml/generate_fat_tailed_data.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
open Oml.Util
open Oml.Statistics

let n1 = Sampling.normal ~mean:2. ~std:1. ()
let n2 = Sampling.normal ~mean:2. ~std:10. ()
let samples = 10000
let data = Array.init samples (fun i -> if i mod 10 = 0 then n2 () else n1 ())
let kurtosis = Descriptive.classify_kurtosis data

let () =
if !Sys.interactive then () else
Printf.printf "Our data has %s kurtosis\t (expecting fat).\n"
(match kurtosis with
| `Fat -> "fat"
| `Normal -> "normal"
| `Skinny -> "skinny"
| `Slightly_fat -> "slightly fat"
| `Slightly_skinny -> "slightly skinny")
28 changes: 28 additions & 0 deletions examples/oml/skewness.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
open Oml
open Statistics

(* TODO: Redo this example with a distribution that better models skew. *)
let samples = 10000
let n1 = Sampling.normal ~mean:2. ~std:1. ()
let n2 = Sampling.normal ~mean:3. ~std:1. ()

let data_skew ftf =
Array.init samples (fun i -> if i mod ftf = 0 then n2 () else n1())
|> Descriptive.summary
|> fun s -> s.Descriptive.skew
|> snd ;;

let count_big_skew =
Array.fold_left (fun c s ->
if s = `Positive || s = `Negative then c + 1 else c) 0 ;;

let test_size = 100 ;;

let t10 = Array.init test_size (fun _ -> data_skew 10) |> count_big_skew ;;
let t2 = Array.init test_size (fun _ -> data_skew 2) |> count_big_skew ;;

let () =
if !Sys.interactive then () else
Printf.printf "We observed skewed data %d out of %d when the frequency of drawing from a larger mean was 1/%d.\n\
But only %d out of %d when the frequency was 1/%d.\n"
t10 test_size 10 t2 test_size 2
18 changes: 18 additions & 0 deletions examples/oml_full/mean_test.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
open Oml_full
open Statistics

let mean = 2.
let std = 1.
let gen = Sampling.normal ~mean ~std () ;;
let samples = 100

let data = Array.init samples (fun _ -> gen ()) ;;
let test_value = 1.9
let tr = Hypothesis_test.mean_t_test test_value Hypothesis_test.Two_sided data ;;

let () =
if !Sys.interactive then () else
Printf.printf "Our data is %d samples with a mean of %f and a std of %f, \
if we test for a mean of %f \n\
the prob of seeing this result by chance is: %f\n"
samples mean std test_value tr.Hypothesis_test.prob_by_chance
30 changes: 30 additions & 0 deletions examples/oml_full/regression_ex.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
open Oml_full
open Statistics
open Regression

let rows = 100
let cols = 3

let gen = Sampling.normal_std () ;;
let pred = Array.init rows (fun _ -> Array.init cols (fun _ -> gen ())) ;;
let constant_term = 2.
let a1 = 3.
let a2 = 4.
let a3 = 5.
let resp =
Array.map (fun row ->
constant_term +. a1 *. row.(0) +. a2 *. row.(1) +. a3 *. row.(2)) pred ;;

let opt = (* Redundant, just for examples *)
Multivariate.opt ~add_constant_column:true
() ;;

let t = Multivariate.regress ~opt pred ~resp ;;
let c = Multivariate.coefficients t ;;

let () =
if !Sys.interactive then () else
Printf.printf "Regression a linear model of y = %f + %f * x1 + %f * x2 + %f * x3 \n\
over randomly generated input data, our regression coefficients are: \n\
%f %f %f %f."
constant_term a1 a2 a3 c.(0) c.(1) c.(2) c.(3)
5 changes: 2 additions & 3 deletions myocamlbuild.ml
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ let () =
| After_rules ->
begin
let regular_source_dirs =
[ "src" (* For the Online stuff that hasn't been packaged. *)
[ "src/onl"
; "src/util"
; "src/unc"
; "src/stats"
Expand All @@ -229,8 +229,7 @@ let () =
]
in
let full_source_dirs =
[ "src-full"
; "src-full/unc"
[ "src-full/unc"
; "src-full/stats"
; "src-full/cls"
; "src-full/rgr"
Expand Down
7 changes: 7 additions & 0 deletions pkg/pkg.ml
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,12 @@ let () =
Ok [ Pkg.test ~cond:(not full) "test/oml_test"
; Pkg.test ~cond:full "test/omlf_test"
]
| "examples" ->
Ok [ Pkg.test "examples/oml/classify"
; Pkg.test "examples/oml/generate_fat_tailed_data"
; Pkg.test "examples/oml/skewness"
; Pkg.test ~cond:full "examples/oml_full/mean_test"
; Pkg.test ~cond:full "examples/oml_full/regression_ex"
]
| other ->
R.error_msgf "Unrecognized package name: %s" other
1 change: 0 additions & 1 deletion scripts/examples/README.md

This file was deleted.

3 changes: 0 additions & 3 deletions scripts/examples/fat_tail_data.ml

This file was deleted.

1 change: 0 additions & 1 deletion scripts/examples/inference_example.ml

This file was deleted.

5 changes: 0 additions & 5 deletions scripts/examples/regression_example.ml

This file was deleted.

8 changes: 0 additions & 8 deletions scripts/examples/script.ml

This file was deleted.

10 changes: 0 additions & 10 deletions scripts/examples/skewness_example.ml

This file was deleted.

23 changes: 0 additions & 23 deletions scripts/examples/start_oml.ml

This file was deleted.

1 change: 0 additions & 1 deletion scripts/examples/summary_data.ml

This file was deleted.

2 changes: 2 additions & 0 deletions scripts/examples/float_format.ml → scripts/float_format.ml
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
(* When you want print shorter floats *)

let shorter_float_printer fr = Format.fprintf fr "%0.4f" ;;
#install_printer shorter_float_printer ;;
108 changes: 108 additions & 0 deletions scripts/how_good.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
(** Algorithm 241
http://lib.stat.cmu.edu/apstat/241
*)

module Float = struct
let ( + ) x y = x +. y
let ( - ) x y = x -. y
let ( * ) x y = x *. y
let ( / ) x y = x /. y
end

(**
DOUBLE PRECISION FUNCTION PPND16 (P, IFAULT)

ALGORITHM AS241 APPL. STATIST. (1988) VOL. 37, NO. 3

Produces the normal deviate Z corresponding to a given lower
tail area of P; Z is accurate to about 1 part in 10**16.
*)
let ppnd16 p =
(* Coefficients for P close to 0.5 *)
let a =
[| 3.3871328727963666080e+0
; 1.3314166789178437745e+2
; 1.9715909503065514427e+3
; 1.3731693765509461125e+4
; 4.5921953931549871457e+4
; 6.7265770927008700853e+4
; 3.3430575583588128105e+4
; 2.5090809287301226727e+3
|] in
let b =
[| 0.
; 4.2313330701600911252e+1
; 6.8718700749205790830e+2
; 5.3941960214247511077e+3
; 2.1213794301586595867e+4
; 3.9307895800092710610e+4
; 2.8729085735721942674e+4
; 5.2264952788528545610e+3
|] in
let c =
[| 1.42343711074968357734e0
; 4.63033784615654529590e0
; 5.76949722146069140550e0
; 3.64784832476320460504e0
; 1.27045825245236838258e0
; 2.41780725177450611770e-1
; 2.27238449892691845833e-2
; 7.74545014278341407640e-4
|] in
let d =
[| 0.
; 2.05319162663775882187e0
; 1.67638483018380384940e0
; 6.89767334985100004550e-1
; 1.48103976427480074590e-1
; 1.51986665636164571966e-2
; 5.47593808499534494600e-4
; 1.05075007164441684324e-9
|] in
let e =
[| 6.65790464350110377720e0
; 5.46378491116411436990e0
; 1.78482653991729133580e0
; 2.96560571828504891230e-1
; 2.65321895265761230930e-2
; 1.24266094738807843860e-3
; 2.71155556874348757815e-5
; 2.01033439929228813265e-7
|] in
let f =
[| 0.
; 5.99832206555887937690e-1
; 1.36929880922735805310e-1
; 1.48753612908506148525e-2
; 7.86869131145613259100e-4
; 1.84631831751005468180e-5
; 1.42151175831644588870e-7
; 2.04426310338993978564e-15
|] in
let open Float in
let q = p - 0.5 in
if abs_float q < 0.425 then
let r = 0.180625 - q * q in
q * (((((((a.(7) * r + a.(6)) * r + a.(5)) * r + a.(4)) * r + a.(3))
* r + a.(2)) * r + a.(1)) * r + a.(0)) /
(((((((b.(7) * r + b.(6)) * r + b.(5)) * r + b.(4)) * r + b.(3))
* r + b.(2)) * r + b.(1)) * r + 1.)
else
let inv p = if q < 0. then -1. * p else p in
let r = if q < 0.0 then p else 1. - p in
if r < 0. then 0. else
let r = sqrt (-1. * log r) in
if r < 5.0 then
let r = r - 1.6 in
(((((((c.(7) * r + c.(6)) * r + c.(5)) * r + c.(4)) * r + c.(3))
* r + c.(2)) * r + c.(1)) * r + c.(0)) /
(((((((d.(7) * r + d.(6)) * r + d.(5)) * r + d.(4)) * r + d.(3))
* r + d.(2)) * r + d.(1)) * r + 1.)
|> inv
else
let r = r - 5.0 in
(((((((e.(7) * r + e.(6)) * r + e.(5)) * r + e.(3)) * r + e.(3))
* r + e.(2)) * r + e.(1)) * r + e.(0)) /
(((((((f.(7) * r + f.(6)) * r + f.(5)) * r + f.(4)) * r + f.(3))
* r + f.(2)) * r + f.(1)) * r + 1.)
|> inv
Loading