Skip to content

Commit

Permalink
Merged 'Mean' and 'Variance' into a single signature 'Features'
Browse files Browse the repository at this point in the history
  See #26 for motivation
  • Loading branch information
superbobry committed Jun 26, 2013
1 parent 6b17455 commit c7cf57b
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 66 deletions.
4 changes: 2 additions & 2 deletions examples/distributions_ex.ml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ open Common


let distribution_mean (type t)
(module D : Mean with type t = t)
(module F : Features with type t = t and type elt = float)
(d : t) () =
Printf.printf "E[X] = %.4f\n" (D.mean d)
Printf.printf "E[X] = %.4f\n" (F.mean d)

let distribution_quantile (type t)
(module D : ContinuousDistribution with type t = t and type elt = float)
Expand Down
25 changes: 8 additions & 17 deletions lib/distributions.ml
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,20 @@ open Internal
module Randist = Gsl.Randist
module Cdf = Gsl.Cdf


module type Mean = sig
type t

val mean : t -> float
end

module type MeanOpt = sig
type t

val mean_opt : t -> float option
end

module type Variance = sig
module type Features = sig
type t
type elt

val variance : t -> float
val mean : t -> elt
val variance : t -> elt
end

module type VarianceOpt = sig
module type FeaturesOpt = sig
type t
type elt

val variance_opt : t -> float option
val mean_opt : t -> elt option
val variance_opt : t -> elt option
end

module type MLE = sig
Expand Down
72 changes: 25 additions & 47 deletions lib/distributions.mli
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,20 @@

open Internal


module type Mean = sig
type t

val mean : t -> float
end

module type MeanOpt = sig
type t

val mean_opt : t -> float option
end

module type Variance = sig
module type Features = sig
type t
type elt

val variance : t -> float
val mean : t -> elt
val variance : t -> elt
end

module type VarianceOpt = sig
module type FeaturesOpt = sig
type t
type elt

val variance_opt : t -> float option
val mean_opt : t -> elt option
val variance_opt : t -> elt option
end

module type MLE = sig
Expand Down Expand Up @@ -61,9 +52,8 @@ end
(** The normal distribution. *)
module Normal : sig
include ContinuousDistribution with type elt = float
include Mean with type t := t
include Variance with type t := t
include MLE with type t := t and type elt := elt
include Features with type t := t and type elt := float
include MLE with type t := t and type elt := float

(** Creates normal distribution from parameters. *)
val create : mean:float -> sd:float -> t
Expand All @@ -75,9 +65,8 @@ end
(** Random variate distributed uniformly in the interval. *)
module Uniform : sig
include ContinuousDistribution with type elt = float
include Mean with type t := t
include Variance with type t := t
include MLE with type t := t and type elt := elt
include Features with type t := t and type elt := float
include MLE with type t := t and type elt := float

(** Creates uniform distribution over a given interval. *)
val create : lower:float -> upper:float -> t
Expand All @@ -90,9 +79,8 @@ end
constant average [rate]. *)
module Exponential : sig
include ContinuousDistribution with type elt = float
include Mean with type t := t
include Variance with type t := t
include MLE with type t := t and type elt := elt
include Features with type t := t and type elt := float
include MLE with type t := t and type elt := float

(** Creates exponential distribution. [rate] must be positive. *)
val create : rate:float -> t
Expand All @@ -105,9 +93,8 @@ end
and occur independently from each other within that interval. *)
module Poisson : sig
include DiscreteDistribution with type elt = int
include Mean with type t := t
include Variance with type t := t
include MLE with type t := t and type elt := elt
include Features with type t := t and type elt := float
include MLE with type t := t and type elt := int

(** Creates a Poisson distribution. [rate] must be positive. *)
val create : rate:float -> t
Expand All @@ -119,8 +106,7 @@ end
of independent Bernoulli [trials]. *)
module Binomial : sig
include DiscreteDistribution with type elt = int
include Mean with type t := t
include Variance with type t := t
include Features with type t := t and type elt := float

(** Creates binomial distribution. Number of [trials] must be
non-negative. *)
Expand All @@ -133,8 +119,7 @@ end
standard normal distributions. *)
module ChiSquared : sig
include ContinuousDistribution with type elt = float
include Mean with type t := t
include Variance with type t := t
include Features with type t := t and type elt := float

(** Construct chi-squared distribution. Number of degrees of freedom
must be positive. *)
Expand All @@ -144,8 +129,7 @@ end
(** Fisher-Snedecor distribution. *)
module F : sig
include ContinuousDistribution with type elt = float
include MeanOpt with type t := t
include VarianceOpt with type t := t
include FeaturesOpt with type t := t and type elt := float

(** Creates Fisher-Snedecor distribution with a given number of degrees
of freedom. *)
Expand All @@ -155,8 +139,7 @@ end
(* Student's t-distribution. *)
module T : sig
include ContinuousDistribution with type elt = float
include MeanOpt with type t := t
include VarianceOpt with type t := t
include FeaturesOpt with type t := t and type elt := float

(** Creates Student's t-distribution with a given number of degrees
of freedom. *)
Expand All @@ -166,8 +149,7 @@ end
(** The gamma distribution. *)
module Gamma : sig
include ContinuousDistribution with type elt = float
include Mean with type t := t
include Variance with type t := t
include Features with type t := t and type elt := float

(** Creates gamma distribution. Both shape and scale must be positive. *)
val create : shape:float -> scale:float -> t
Expand All @@ -189,8 +171,7 @@ end
(** The beta distribution. *)
module Beta : sig
include ContinuousDistribution with type elt = float
include Mean with type t := t
include Variance with type t := t
include Features with type t := t and type elt := float

(** Creates beta distribution. Both shape parameters must be positive. *)
val create : alpha:float -> beta:float -> t
Expand All @@ -202,8 +183,7 @@ end
first success, supported on the set [[0, 1, ...]]. *)
module Geometric : sig
include DiscreteDistribution with type elt = int
include Mean with type t := t
include Variance with type t := t
include Features with type t := t and type elt := float

(** Creates Geometric distribution with a given probability of success. *)
val create : p:float -> t
Expand All @@ -217,8 +197,7 @@ end
of "type 2". *)
module Hypergeometric : sig
include DiscreteDistribution with type elt = int
include Mean with type t := t
include Variance with type t := t
include Features with type t := t and type elt := float

(** Creates Hypergeometric distribution. *)
val create : m:int -> t:int -> k:int -> t
Expand All @@ -230,8 +209,7 @@ end
of Bernoulli trials before a specified number of [failures] occur. *)
module NegativeBinomial : sig
include DiscreteDistribution with type elt = int
include Mean with type t := t
include Variance with type t := t
include Features with type t := t and type elt := float

(** Creates negaive Binomial distribution with a given number of
failures and success probability. *)
Expand Down

0 comments on commit c7cf57b

Please sign in to comment.