1 /**
2 Authors: [Ilya Yaroshenko](http://9il.github.io)
3 
4 Copyright: © 2014-2015 [Ilya Yaroshenko](http://9il.github.io)
5 
6 License: MIT
7 */
8 module atmosphere.likelihood.gamma;
9 
10 import core.stdc.tgmath;
11 
12 import std.traits;
13 import std.typecons;
14 
15 import atmosphere.statistic: GammaStatistic;
16 
17 
18 /++
19 Normalized log-likelihood function of the gamma distribution.
20 +/
21 T gammaLikelihood(T)(in T shape, in T scale, in T[] sample)
22 	if(isFloatingPoint!T)
23 {
24 	return gammaLikelihood!T(shape, scale, GammaStatistic!T(sample));
25 }
26 
27 ///
28 unittest {
29 	import atmosphere.likelihood.generalized_gamma;
30 	immutable l = gammaLikelihood!double(2,3,[1,2]);
31 	immutable m = generalizedGammaLikelihood!double(2,1,3,[1,2]);
32 	assert(l == m);
33 }
34 
35 ///ditto
36 T gammaLikelihood(T)(in T shape, in T scale, in T[] sample, in T[] weights)
37 	if(isFloatingPoint!T)
38 {
39 	return gammaLikelihood!T(shape, scale, GammaStatistic!T(sample, weights));
40 }
41 
42 ///
43 unittest {
44 	import atmosphere.likelihood.generalized_gamma;
45 	immutable l = gammaLikelihood!double(2,3,[1,2],[3,4]);
46 	immutable m = generalizedGammaLikelihood!double(2,1,3,[1,2],[3,4]);
47 	assert(l == m);
48 }
49 
50 ///
51 unittest {
52 	immutable l = gammaLikelihood!double(1,3,[1,2,2]);
53 	immutable m = gammaLikelihood!double(1,3,[1,2],[2,4]);
54 	assert(l == m);
55 }
56 
57 ///ditto
58 T gammaLikelihood(T)(in T shape, in T scale, in GammaStatistic!T stat)
59 	if(isFloatingPoint!T)
60 {
61 	with(stat) return 
62 		- log(scale * tgamma(shape)) 
63 		- (1 - shape) * (meanl - log(scale)) 
64 		- mean / scale;
65 }