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.inverse_gamma;
9 
10 import core.stdc.tgmath;
11 
12 import std.traits;
13 import std.typecons;
14 
15 import atmosphere.statistic: InverseGammaStatistic;
16 
17 /++
18 Normalized log-likelihood function of the inverse-gamma distribution.
19 +/
20 T inverseGammaLikelihood(T)(T shape, T scale, in T[] sample)
21 	if(isFloatingPoint!T)
22 {
23 	return inverseGammaLikelihood!T(shape, scale, InverseGammaStatistic!T(sample));
24 }
25 
26 ///
27 unittest {
28 	import atmosphere.likelihood.generalized_gamma;
29 	immutable l = inverseGammaLikelihood!double(2,3,[1,2]);
30 	immutable m = generalizedGammaLikelihood!double(2,-1,3,[1,2]);
31 	assert(l == m);
32 }
33 
34 ///ditto
35 T inverseGammaLikelihood(T)(T shape, T scale, in T[] sample, in T[] weights)
36 	if(isFloatingPoint!T)
37 {
38 	return inverseGammaLikelihood!T(shape, scale, InverseGammaStatistic!T(sample, weights));
39 }
40 
41 ///
42 unittest {
43 	import atmosphere.likelihood.generalized_gamma;
44 	immutable l = inverseGammaLikelihood!double(2,3,[1,2],[3,4]);
45 	immutable m = generalizedGammaLikelihood!double(2,-1,3,[1,2],[3,4]);
46 	assert(l == m);
47 }
48 
49 ///
50 unittest {
51 	immutable l = inverseGammaLikelihood!double(1,3,[1,2,2]);
52 	immutable m = inverseGammaLikelihood!double(1,3,[1,2],[2,4]);
53 	assert(l == m);
54 }
55 
56 
57 ///ditto
58 T inverseGammaLikelihood(T)(T shape, T scale, InverseGammaStatistic!T stat)
59 	if(isFloatingPoint!T)
60 {
61 	with(stat) return 
62 		- log(scale * tgamma(shape))
63 		- (1 + shape) * (meanl - log(scale)) 
64 		- meani * scale;
65 }