123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661# 1 "src/owl/ppl/owl_distribution.ml"(*
* OWL - OCaml Scientific and Engineering Computing
* Copyright (c) 2016-2019 Liang Wang <liang.wang@cl.cam.ac.uk>
*)openOwl_types(* Functor of making Lazy module of different number types *)moduleMake(A:Stats_Dist)=structmoduleUtility=struct(* check the elements in [xs] and make sure their shapes are broadcastable,
by satisfying either of the following rules: 1) equal; 2) equal to one.
*)let_check_broadcast_shapexs=letxs=Array.mapA.shapexsinlets=Array.copyxs.(0)inArray.iter(funx->assert(Array.lengths=Array.lengthx);Array.iteri(funid->ifd<>s.(i)then(ifs.(i)=1thens.(i)<-delseifd=1then()elsefailwith"_check_broadcast_shape"))x)xsendmoduleUniform=structtypet={a:A.arr;b:A.arr;}letmake~a~b=Utility._check_broadcast_shape[|a;b|];{a;b}letsampletn=A.uniform_rvs~a:t.a~b:t.b~nletpdftx=A.uniform_pdf~a:t.a~b:t.bxletlogpdftx=A.uniform_logpdf~a:t.a~b:t.bxletcdftx=A.uniform_cdf~a:t.a~b:t.bxletlogcdftx=A.uniform_logcdf~a:t.a~b:t.bxletppftx=A.uniform_ppf~a:t.a~b:t.bxletsftx=A.uniform_sf~a:t.a~b:t.bxletlogsftx=A.uniform_logsf~a:t.a~b:t.bxletisftx=A.uniform_isf~a:t.a~b:t.bxendmoduleGaussian=structtypet={mu:A.arr;sigma:A.arr;}letmake~mu~sigma=Utility._check_broadcast_shape[|mu;sigma|];{mu;sigma}letsampletn=A.gaussian_rvs~mu:t.mu~sigma:t.sigma~nletpdftx=A.gaussian_pdf~mu:t.mu~sigma:t.sigmaxletlogpdftx=A.gaussian_logpdf~mu:t.mu~sigma:t.sigmaxletcdftx=A.gaussian_cdf~mu:t.mu~sigma:t.sigmaxletlogcdftx=A.gaussian_logcdf~mu:t.mu~sigma:t.sigmaxletppftx=A.gaussian_ppf~mu:t.mu~sigma:t.sigmaxletsftx=A.gaussian_sf~mu:t.mu~sigma:t.sigmaxletlogsftx=A.gaussian_logsf~mu:t.mu~sigma:t.sigmaxletisftx=A.gaussian_isf~mu:t.mu~sigma:t.sigmaxendmoduleExponential=structtypet={lambda:A.arr;}letmake~lambda=Utility._check_broadcast_shape[|lambda|];{lambda}letsampletn=A.exponential_rvs~lambda:t.lambda~nletpdftx=A.exponential_pdf~lambda:t.lambdaxletlogpdftx=A.exponential_logpdf~lambda:t.lambdaxletcdftx=A.exponential_cdf~lambda:t.lambdaxletlogcdftx=A.exponential_logcdf~lambda:t.lambdaxletppftx=A.exponential_ppf~lambda:t.lambdaxletsftx=A.exponential_sf~lambda:t.lambdaxletlogsftx=A.exponential_logsf~lambda:t.lambdaxletisftx=A.exponential_isf~lambda:t.lambdaxendmoduleGamma=structtypet={shape:A.arr;scale:A.arr;}letmake~shape~scale=Utility._check_broadcast_shape[|shape;scale|];{shape;scale}letsampletn=A.gamma_rvs~shape:t.shape~scale:t.scale~nletpdftx=A.gamma_pdf~shape:t.shape~scale:t.scalexletlogpdftx=A.gamma_logpdf~shape:t.shape~scale:t.scalexletcdftx=A.gamma_cdf~shape:t.shape~scale:t.scalexletlogcdftx=A.gamma_logcdf~shape:t.shape~scale:t.scalexletppftx=A.gamma_ppf~shape:t.shape~scale:t.scalexletsftx=A.gamma_sf~shape:t.shape~scale:t.scalexletlogsftx=A.gamma_logsf~shape:t.shape~scale:t.scalexletisftx=A.gamma_isf~shape:t.shape~scale:t.scalexendmoduleBeta=structtypet={a:A.arr;b:A.arr;}letmake~a~b=Utility._check_broadcast_shape[|a;b|];{a;b}letsampletn=A.beta_rvs~a:t.a~b:t.b~nletpdftx=A.beta_pdf~a:t.a~b:t.bxletlogpdftx=A.beta_logpdf~a:t.a~b:t.bxletcdftx=A.beta_cdf~a:t.a~b:t.bxletlogcdftx=A.beta_logcdf~a:t.a~b:t.bxletppftx=A.beta_ppf~a:t.a~b:t.bxletsftx=A.beta_sf~a:t.a~b:t.bxletlogsftx=A.beta_logsf~a:t.a~b:t.bxletisftx=A.beta_isf~a:t.a~b:t.bxendmoduleChi2=structtypet={df:A.arr;}letmake~df~_sigma=Utility._check_broadcast_shape[|df|];{df}letsampletn=A.chi2_rvs~df:t.df~nletpdftx=A.chi2_pdf~df:t.dfxletlogpdftx=A.chi2_logpdf~df:t.dfxletcdftx=A.chi2_cdf~df:t.dfxletlogcdftx=A.chi2_logcdf~df:t.dfxletppftx=A.chi2_ppf~df:t.dfxletsftx=A.chi2_sf~df:t.dfxletlogsftx=A.chi2_logsf~df:t.dfxletisftx=A.chi2_isf~df:t.dfxendmoduleF=structtypet={dfnum:A.arr;dfden:A.arr;}letmake~dfnum~dfden=Utility._check_broadcast_shape[|dfnum;dfden|];{dfnum;dfden}letsampletn=A.f_rvs~dfnum:t.dfnum~dfden:t.dfden~nletpdftx=A.f_pdf~dfnum:t.dfnum~dfden:t.dfdenxletlogpdftx=A.f_logpdf~dfnum:t.dfnum~dfden:t.dfdenxletcdftx=A.f_cdf~dfnum:t.dfnum~dfden:t.dfdenxletlogcdftx=A.f_logcdf~dfnum:t.dfnum~dfden:t.dfdenxletppftx=A.f_ppf~dfnum:t.dfnum~dfden:t.dfdenxletsftx=A.f_sf~dfnum:t.dfnum~dfden:t.dfdenxletlogsftx=A.f_logsf~dfnum:t.dfnum~dfden:t.dfdenxletisftx=A.f_isf~dfnum:t.dfnum~dfden:t.dfdenxendmoduleCauchy=structtypet={loc:A.arr;scale:A.arr;}letmake~loc~scale=Utility._check_broadcast_shape[|loc;scale|];{loc;scale}letsampletn=A.cauchy_rvs~loc:t.loc~scale:t.scale~nletpdftx=A.cauchy_pdf~loc:t.loc~scale:t.scalexletlogpdftx=A.cauchy_logpdf~loc:t.loc~scale:t.scalexletcdftx=A.cauchy_cdf~loc:t.loc~scale:t.scalexletlogcdftx=A.cauchy_logcdf~loc:t.loc~scale:t.scalexletppftx=A.cauchy_ppf~loc:t.loc~scale:t.scalexletsftx=A.cauchy_sf~loc:t.loc~scale:t.scalexletlogsftx=A.cauchy_logsf~loc:t.loc~scale:t.scalexletisftx=A.cauchy_isf~loc:t.loc~scale:t.scalexendmoduleLomax=structtypet={shape:A.arr;scale:A.arr;}letmake~shape~scale=Utility._check_broadcast_shape[|shape;scale|];{shape;scale}letsampletn=A.lomax_rvs~shape:t.shape~scale:t.scale~nletpdftx=A.lomax_pdf~shape:t.shape~scale:t.scalexletlogpdftx=A.lomax_logpdf~shape:t.shape~scale:t.scalexletcdftx=A.lomax_cdf~shape:t.shape~scale:t.scalexletlogcdftx=A.lomax_logcdf~shape:t.shape~scale:t.scalexletppftx=A.lomax_ppf~shape:t.shape~scale:t.scalexletsftx=A.lomax_sf~shape:t.shape~scale:t.scalexletlogsftx=A.lomax_logsf~shape:t.shape~scale:t.scalexletisftx=A.lomax_isf~shape:t.shape~scale:t.scalexendmoduleWeibull=structtypet={shape:A.arr;scale:A.arr;}letmake~shape~scale=Utility._check_broadcast_shape[|shape;scale|];{shape;scale}letsampletn=A.weibull_rvs~shape:t.shape~scale:t.scale~nletpdftx=A.weibull_pdf~shape:t.shape~scale:t.scalexletlogpdftx=A.weibull_logpdf~shape:t.shape~scale:t.scalexletcdftx=A.weibull_cdf~shape:t.shape~scale:t.scalexletlogcdftx=A.weibull_logcdf~shape:t.shape~scale:t.scalexletppftx=A.weibull_ppf~shape:t.shape~scale:t.scalexletsftx=A.weibull_sf~shape:t.shape~scale:t.scalexletlogsftx=A.weibull_logsf~shape:t.shape~scale:t.scalexletisftx=A.weibull_isf~shape:t.shape~scale:t.scalexendmoduleLaplace=structtypet={loc:A.arr;scale:A.arr;}letmake~loc~scale=Utility._check_broadcast_shape[|loc;scale|];{loc;scale}letsampletn=A.laplace_rvs~loc:t.loc~scale:t.scale~nletpdftx=A.laplace_pdf~loc:t.loc~scale:t.scalexletlogpdftx=A.laplace_logpdf~loc:t.loc~scale:t.scalexletcdftx=A.laplace_cdf~loc:t.loc~scale:t.scalexletlogcdftx=A.laplace_logcdf~loc:t.loc~scale:t.scalexletppftx=A.laplace_ppf~loc:t.loc~scale:t.scalexletsftx=A.laplace_sf~loc:t.loc~scale:t.scalexletlogsftx=A.laplace_logsf~loc:t.loc~scale:t.scalexletisftx=A.laplace_isf~loc:t.loc~scale:t.scalexendmoduleGumbel1=structtypet={a:A.arr;b:A.arr;}letmake~a~b=Utility._check_broadcast_shape[|a;b|];{a;b}letsampletn=A.gumbel1_rvs~a:t.a~b:t.b~nletpdftx=A.gumbel1_pdf~a:t.a~b:t.bxletlogpdftx=A.gumbel1_logpdf~a:t.a~b:t.bxletcdftx=A.gumbel1_cdf~a:t.a~b:t.bxletlogcdftx=A.gumbel1_logcdf~a:t.a~b:t.bxletppftx=A.gumbel1_ppf~a:t.a~b:t.bxletsftx=A.gumbel1_sf~a:t.a~b:t.bxletlogsftx=A.gumbel1_logsf~a:t.a~b:t.bxletisftx=A.gumbel1_isf~a:t.a~b:t.bxendmoduleGumbel2=structtypet={a:A.arr;b:A.arr;}letmake~a~b=Utility._check_broadcast_shape[|a;b|];{a;b}letsampletn=A.gumbel2_rvs~a:t.a~b:t.b~nletpdftx=A.gumbel2_pdf~a:t.a~b:t.bxletlogpdftx=A.gumbel2_logpdf~a:t.a~b:t.bxletcdftx=A.gumbel2_cdf~a:t.a~b:t.bxletlogcdftx=A.gumbel2_logcdf~a:t.a~b:t.bxletppftx=A.gumbel2_ppf~a:t.a~b:t.bxletsftx=A.gumbel2_sf~a:t.a~b:t.bxletlogsftx=A.gumbel2_logsf~a:t.a~b:t.bxletisftx=A.gumbel2_isf~a:t.a~b:t.bxendmoduleLogistic=structtypet={loc:A.arr;scale:A.arr;}letmake~loc~scale=Utility._check_broadcast_shape[|loc;scale|];{loc;scale}letsampletn=A.logistic_rvs~loc:t.loc~scale:t.scale~nletpdftx=A.logistic_pdf~loc:t.loc~scale:t.scalexletlogpdftx=A.logistic_logpdf~loc:t.loc~scale:t.scalexletcdftx=A.logistic_cdf~loc:t.loc~scale:t.scalexletlogcdftx=A.logistic_logcdf~loc:t.loc~scale:t.scalexletppftx=A.logistic_ppf~loc:t.loc~scale:t.scalexletsftx=A.logistic_sf~loc:t.loc~scale:t.scalexletlogsftx=A.logistic_logsf~loc:t.loc~scale:t.scalexletisftx=A.logistic_isf~loc:t.loc~scale:t.scalexendmoduleLognormal=structtypet={mu:A.arr;sigma:A.arr;}letmake~mu~sigma=Utility._check_broadcast_shape[|mu;sigma|];{mu;sigma}letsampletn=A.lognormal_rvs~mu:t.mu~sigma:t.sigma~nletpdftx=A.lognormal_pdf~mu:t.mu~sigma:t.sigmaxletlogpdftx=A.lognormal_logpdf~mu:t.mu~sigma:t.sigmaxletcdftx=A.lognormal_cdf~mu:t.mu~sigma:t.sigmaxletlogcdftx=A.lognormal_logcdf~mu:t.mu~sigma:t.sigmaxletppftx=A.lognormal_ppf~mu:t.mu~sigma:t.sigmaxletsftx=A.lognormal_sf~mu:t.mu~sigma:t.sigmaxletlogsftx=A.lognormal_logsf~mu:t.mu~sigma:t.sigmaxletisftx=A.lognormal_isf~mu:t.mu~sigma:t.sigmaxendmoduleRayleigh=structtypet={sigma:A.arr;}letmake~sigma=Utility._check_broadcast_shape[|sigma|];{sigma}letsampletn=A.rayleigh_rvs~sigma:t.sigma~nletpdftx=A.rayleigh_pdf~sigma:t.sigmaxletlogpdftx=A.rayleigh_logpdf~sigma:t.sigmaxletcdftx=A.rayleigh_cdf~sigma:t.sigmaxletlogcdftx=A.rayleigh_logcdf~sigma:t.sigmaxletppftx=A.rayleigh_ppf~sigma:t.sigmaxletsftx=A.rayleigh_sf~sigma:t.sigmaxletlogsftx=A.rayleigh_logsf~sigma:t.sigmaxletisftx=A.rayleigh_isf~sigma:t.sigmaxendtypedist=|UniformofUniform.t|GaussianofGaussian.t|ExponentialofExponential.t|GammaofGamma.t|BetaofBeta.t|Chi2ofChi2.t|FofF.t|CauchyofCauchy.t|LomaxofLomax.t|WeibullofWeibull.t|LaplaceofLaplace.t|Gumbel1ofGumbel1.t|Gumbel2ofGumbel2.t|LogisticofLogistic.t|LognormalofLognormal.t|RayleighofRayleigh.tletsampletn=matchtwith|Uniformt->Uniform.sampletn|Gaussiant->Gaussian.sampletn|Exponentialt->Exponential.sampletn|Gammat->Gamma.sampletn|Betat->Beta.sampletn|Chi2t->Chi2.sampletn|Ft->F.sampletn|Cauchyt->Cauchy.sampletn|Lomaxt->Lomax.sampletn|Weibullt->Weibull.sampletn|Laplacet->Laplace.sampletn|Gumbel1t->Gumbel1.sampletn|Gumbel2t->Gumbel2.sampletn|Logistict->Logistic.sampletn|Lognormalt->Lognormal.sampletn|Rayleight->Rayleigh.sampletnletprobtx=matchtwith|Uniformt->Uniform.pdftx|Gaussiant->Gaussian.pdftx|Exponentialt->Exponential.pdftx|Gammat->Gamma.pdftx|Betat->Beta.pdftx|Chi2t->Chi2.pdftx|Ft->F.pdftx|Cauchyt->Cauchy.pdftx|Lomaxt->Lomax.pdftx|Weibullt->Weibull.pdftx|Laplacet->Laplace.pdftx|Gumbel1t->Gumbel1.pdftx|Gumbel2t->Gumbel2.pdftx|Logistict->Logistic.pdftx|Lognormalt->Lognormal.pdftx|Rayleight->Rayleigh.pdftxletlog_probtx=matchtwith|Uniformt->Uniform.logpdftx|Gaussiant->Gaussian.logpdftx|Exponentialt->Exponential.logpdftx|Gammat->Gamma.logpdftx|Betat->Beta.logpdftx|Chi2t->Chi2.logpdftx|Ft->F.logpdftx|Cauchyt->Cauchy.logpdftx|Lomaxt->Lomax.logpdftx|Weibullt->Weibull.logpdftx|Laplacet->Laplace.logpdftx|Gumbel1t->Gumbel1.logpdftx|Gumbel2t->Gumbel2.logpdftx|Logistict->Logistic.logpdftx|Lognormalt->Lognormal.logpdftx|Rayleight->Rayleigh.logpdftxletcdftx=matchtwith|Uniformt->Uniform.cdftx|Gaussiant->Gaussian.cdftx|Exponentialt->Exponential.cdftx|Gammat->Gamma.cdftx|Betat->Beta.cdftx|Chi2t->Chi2.cdftx|Ft->F.cdftx|Cauchyt->Cauchy.cdftx|Lomaxt->Lomax.cdftx|Weibullt->Weibull.cdftx|Laplacet->Laplace.cdftx|Gumbel1t->Gumbel1.cdftx|Gumbel2t->Gumbel2.cdftx|Logistict->Logistic.cdftx|Lognormalt->Lognormal.cdftx|Rayleight->Rayleigh.cdftxletlogcdftx=matchtwith|Uniformt->Uniform.logcdftx|Gaussiant->Gaussian.logcdftx|Exponentialt->Exponential.logcdftx|Gammat->Gamma.logcdftx|Betat->Beta.logcdftx|Chi2t->Chi2.logcdftx|Ft->F.logcdftx|Cauchyt->Cauchy.logcdftx|Lomaxt->Lomax.logcdftx|Weibullt->Weibull.logcdftx|Laplacet->Laplace.logcdftx|Gumbel1t->Gumbel1.logcdftx|Gumbel2t->Gumbel2.logcdftx|Logistict->Logistic.logcdftx|Lognormalt->Lognormal.logcdftx|Rayleight->Rayleigh.logcdftx(*
let mean t = match t with
| Uniform t -> Uniform.mean t
| Gaussian t -> Gaussian.mean t
*)end