Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source
Source file fin_dist.ml
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378moduleMake(Reals:Basic_intf.Reals)=structtypereals=Reals.tmoduletypeFin_fun=sigtypetmoduleV:Basic_intf.Free_modulewithtypeR.t=Reals.tandtypebasis=tvalweightmap:V.tendmoduletypeFin_kernel=sigtypettypeumoduleV:Basic_intf.Free_modulewithtypeR.t=Reals.tandtypebasis=uvalkernel:t->V.tendtype'afin_fun=(moduleFin_funwithtypet='a)type'afin_den='afin_fun(* A finitely supported probability is normalized. *)type'afin_prb='afin_funtype('a,'b)kernel=(moduleFin_kernelwithtypet='aandtypeu='b)letsample_prb:typea.afin_prb->Random.State.t->a=fun(typet)(moduleP:Fin_funwithtypet=t)rng_state->letexceptionSampledoftinletr=Reals.lebesguerng_stateintrylet_=P.V.fold(funeltweightcumu->letcumu=Reals.addcumuweightinifReals.(r<=cumu)thenraise(Sampledelt)elsecumu)P.weightmapReals.zeroinassertfalsewithSampledx->xletdensity(typet)(moduleV:Basic_intf.Free_modulewithtypeR.t=Reals.tandtypebasis=t)(elements:(t*Reals.t)list):tfin_den=(modulestructtypenonrect=tmoduleV=Vletweightmap=V.of_listelementsend)letprobability(typet)(moduleV:Basic_intf.Free_modulewithtypeR.t=Reals.tandtypebasis=t)(elements:(t*Reals.t)list):tfin_prb=let(_points,weights)=List.splitelementsinlettotal_weight=List.fold_leftReals.addReals.zeroweightsinifReals.comparetotal_weightReals.one<>0theninvalid_arg"Stats.probability: weights do not sum up to 1";density(moduleV)elementslettotal_mass(typet)((moduleD):tfin_den):Reals.t=D.V.fold(fun_wacc->Reals.addwacc)D.weightmapReals.zeroletnormalize(typet)((moduleD):tfin_den):tfin_prb=letmass=total_mass(moduleD)inletimass=Reals.divReals.onemassin(modulestructtypet=D.tmoduleV=D.Vletweightmap=V.smulimassD.weightmapend)letfin_prb_of_empirical(typet)(moduleV:Basic_intf.Free_modulewithtypeR.t=Reals.tandtypebasis=t)(p:tarray):tfin_den=letweightmap=Array.fold_left(funvecelt->V.addvec(V.of_list[(elt,Reals.one)]))V.zeropinletdensity:tfin_den=(modulestructtypenonrect=tmoduleV=Vletweightmap=weightmapend)innormalizedensityletuniform(typet)(moduleV:Basic_intf.Free_modulewithtypeR.t=Reals.tandtypebasis=t)(arr:tarray):tfin_prb=letlen=Array.lengtharrinifInt.equallen0thenfailwith"uniform: empty array"elseletprb=Reals.(divone(of_intlen))in(modulestructtypenonrect=tmoduleV=Vletweightmap=Array.fold_left(funmapx->V.add(V.of_list[(x,prb)])map)V.zeroarrend)leteval_prb(typet)((moduleP):tfin_prb)(x:t):Reals.t=P.V.evalP.weightmapxletintegrate(typet)((moduleP):tfin_prb)(f:t->Reals.t):Reals.t=P.V.fold(funxwacc->Reals.(acc+(w*fx)))P.weightmapReals.zeroletkernel(typeab)?(h:(moduleBasic_intf.Stdwithtypet=a)option)(moduleV:Basic_intf.Free_modulewithtypeR.t=Reals.tandtypebasis=b)(kernel:a->(b*Reals.t)list):(a,b)kernel=letkernel=matchhwith|None->funx->V.of_list(kernelx)|Some(moduleH)->(letmoduleElement=structtypet={key:H.t;data:V.t}lethash{key;_}=H.hashkeyletequalx1x2=H.equalx1.keyx2.keyendinletmoduleTable=Weak.Make(Element)inlettable=Table.create11infunx->matchTable.find_opttable{Element.key=x;data=V.zero}with|None->letres=V.of_list(kernelx)inTable.addtable{Element.key=x;data=res};res|Some{Element.data;_}->data)inletmoduleK=structtypet=atypeu=bmoduleV=Vletkernel=kernelendin(moduleK)letcompose:typeabc.?h:(moduleBasic_intf.Stdwithtypet=a)->(a,b)kernel->(b,c)kernel->(a,c)kernel=fun?h(moduleK1)(moduleK2)->letkernel=matchhwith|None->funx->letvec=K1.kernelxinK1.V.fold(funbpbacc->letvec=K2.kernelbinK2.V.add(K2.V.smulpbvec)acc)vecK2.V.zero|Some(moduleH)->(letmoduleElement=structtypet={key:H.t;data:K2.V.t}lethash{key;_}=H.hashkeyletequalx1x2=H.equalx1.keyx2.keyendinletmoduleTable=Weak.Make(Element)inlettable=Table.create11infunx->matchTable.find_opttable{Element.key=x;data=K2.V.zero}with|None->letvec=K1.kernelxinletres=K1.V.fold(funbpbacc->letvec=K2.kernelbinK2.V.(add(smulpbvec)acc))vecK2.V.zeroinTable.addtable{Element.key=x;data=res};res|Some{Element.data;_}->data)inletmoduleKernel=structtypet=K1.ttypeu=K2.umoduleV=K2.Vletkernel=kernelendin(moduleKernel)letconstant_kernel:typeab.bfin_prb->(a,b)kernel=fun(modulePrb)->letmoduleKernel=structtypet=atypeu=Prb.tmoduleV=Prb.Vletkernel_x=Prb.weightmapendin(moduleKernel)leteval_kernel:typeab.a->(a,b)kernel->(b*Reals.t)list=funx(moduleK)->K.V.fold(funkpacc->(k,p)::acc)(K.kernelx)[]letraw_data_density(typet)((moduleD):tfin_den)=letden=D.V.fold(funeltwacc->(elt,w)::acc)D.weightmap[]in`Densitydenletraw_data_probability(typet)((moduleD):tfin_prb)=letden=D.V.fold(funeltwacc->(elt,w)::acc)D.weightmap[]in`Probabilitydenletpp_fin_fun:(Format.formatter->'a->unit)->Format.formatter->'afin_den->unit=funkffden->let(`Densityl)=raw_data_densitydeninFormat.fprintff"@[<h>%a@]"(Format.pp_print_list(funelt_fmt(elt,pr)->Format.fprintfelt_fmt"(%a, %a);@,"kfeltReals.pppr))lletpushforward(typetu)~(prior:tfin_fun)~(likelihood:(t,u)kernel):ufin_prb=let(modulePrior)=priorinlet(moduleLikelihood)=likelihoodinletmap=Prior.V.fold(funxpxacc->letfx=Likelihood.kernelxinLikelihood.V.(add(smulpxfx)acc))Prior.weightmapLikelihood.V.zeroinletmoduleResult=structtypet=umoduleV=Likelihood.Vletweightmap=mapendin(moduleResult)letinverse(typetu)?(h:(moduleBasic_intf.Stdwithtypet=u)option)(prior:tfin_prb)(likelihood:(t,u)kernel):ufin_prb*(u,t)kernel=let(modulePrior)=priorinlet(moduleLikelihood)=likelihoodinlet(modulePushforward)=pushforward~prior~likelihoodinletkernel(y:u)=letnu_y=Pushforward.V.evalPushforward.weightmapyinPrior.V.fold(funxmu_xacc->letforward=Likelihood.kernelxinletf_x_y=Likelihood.V.evalforwardyinletprob=Reals.(mulmu_xf_x_y/nu_y)inPrior.V.(addacc(of_list[(x,prob)])))Prior.weightmapPrior.V.zeroinletmoduleKernel=structtypet=Likelihood.utypeu=Likelihood.tmoduleV=Prior.Vletkernel=matchhwith|None->kernel|Some(moduleH)->(letmoduleElement=structtypet={key:H.t;data:V.t}lethash{key;_}=H.hashkeyletequalx1x2=H.equalx1.keyx2.keyendinletmoduleTable=Weak.Make(Element)inlettable=Table.create11infuny->matchTable.find_opttable{Element.key=y;data=Prior.V.zero}with|None->letres=kernelyinTable.addtable{Element.key=y;data=res};res|Someres->res.data)endin((modulePushforward),(moduleKernel))moduleBool_vec=Basic_impl.Free_module.Make(Std.Bool)(Reals)(Basic_impl.Bool_map)moduleInt_vec=Basic_impl.Free_module.Make(Std.Int)(Reals)(Basic_impl.Int_map)letcoin~bias:boolfin_prb=ifReals.(bias<zero||bias>one)thenfailwith"Stats.coin: invalid bias"elsedensity(moduleBool_vec)[(true,bias);(false,Reals.(one-bias))]letbincoeffnk=letn=Reals.of_intninletrecloopiacc=ifInt.equali(k+1)thenaccelseletfi=Reals.of_intiinloop(i+1)Reals.(acc*((n+one-fi)/fi))inloop1Reals.oneletbinomial(coin:boolfin_prb)n=letp=eval_prbcointrueinletnot_p=eval_prbcoinfalseinletelements=List.initn(funk->letn_minus_k=n-kinReals.(k,bincoeffnk*npowpk*npownot_pn_minus_k))indensity(moduleInt_vec)elementsletmean_generic(typeelt)(moduleL:Basic_intf.Modulewithtypet=eltandtypeR.t=Reals.t)((moduleDist):eltfin_fun)=Dist.V.fold(funxwacc->L.add(L.smulwx)acc)Dist.weightmapL.zeroletmean((moduleDist):realsfin_fun)=integrate(moduleDist)(funx->x)letvariance((moduleDist):realsfin_fun)=letm=mean(moduleDist)inDist.V.fold(funxwacc->letopenRealsinletdelta=x-minletdelta_squared=delta*deltainacc+(delta_squared*w))Dist.weightmapReals.zeroend