Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1""" 

2This script contains empirical likelihood ANOVA. 

3 

4Currently the script only contains one feature that allows the user to compare 

5means of multiple groups. 

6 

7General References 

8------------------ 

9 

10Owen, A. B. (2001). Empirical Likelihood. Chapman and Hall. 

11""" 

12import numpy as np 

13from .descriptive import _OptFuncts 

14from scipy import optimize 

15from scipy.stats import chi2 

16 

17 

18class _ANOVAOpt(_OptFuncts): 

19 """ 

20 

21 Class containing functions that are optimized over when 

22 conducting ANOVA. 

23 """ 

24 def _opt_common_mu(self, mu): 

25 """ 

26 Optimizes the likelihood under the null hypothesis that all groups have 

27 mean mu. 

28 

29 Parameters 

30 ---------- 

31 mu : float 

32 The common mean. 

33 

34 Returns 

35 ------- 

36 llr : float 

37 -2 times the llr ratio, which is the test statistic. 

38 """ 

39 nobs = self.nobs 

40 endog = self.endog 

41 num_groups = self.num_groups 

42 endog_asarray = np.zeros((nobs, num_groups)) 

43 obs_num = 0 

44 for arr_num in range(len(endog)): 

45 new_obs_num = obs_num + len(endog[arr_num]) 

46 endog_asarray[obs_num: new_obs_num, arr_num] = endog[arr_num] - \ 

47 mu 

48 obs_num = new_obs_num 

49 est_vect = endog_asarray 

50 wts = np.ones(est_vect.shape[0]) * (1. / (est_vect.shape[0])) 

51 eta_star = self._modif_newton(np.zeros(num_groups), est_vect, wts) 

52 denom = 1. + np.dot(eta_star, est_vect.T) 

53 self.new_weights = 1. / nobs * 1. / denom 

54 llr = np.sum(np.log(nobs * self.new_weights)) 

55 return -2 * llr 

56 

57 

58class ANOVA(_ANOVAOpt): 

59 """ 

60 A class for ANOVA and comparing means. 

61 

62 Parameters 

63 ---------- 

64 

65 endog : list of arrays 

66 endog should be a list containing 1 dimensional arrays. Each array 

67 is the data collected from a certain group. 

68 """ 

69 

70 def __init__(self, endog): 

71 self.endog = endog 

72 self.num_groups = len(self.endog) 

73 self.nobs = 0 

74 for i in self.endog: 

75 self.nobs = self.nobs + len(i) 

76 

77 def compute_ANOVA(self, mu=None, mu_start=0, return_weights=0): 

78 """ 

79 Returns -2 log likelihood, the pvalue and the maximum likelihood 

80 estimate for a common mean. 

81 

82 Parameters 

83 ---------- 

84 

85 mu : float 

86 If a mu is specified, ANOVA is conducted with mu as the 

87 common mean. Otherwise, the common mean is the maximum 

88 empirical likelihood estimate of the common mean. 

89 Default is None. 

90 

91 mu_start : float 

92 Starting value for commean mean if specific mu is not specified. 

93 Default = 0. 

94 

95 return_weights : bool 

96 if TRUE, returns the weights on observations that maximize the 

97 likelihood. Default is FALSE. 

98 

99 Returns 

100 ------- 

101 

102 res: tuple 

103 The log-likelihood, p-value and estimate for the common mean. 

104 """ 

105 if mu is not None: 

106 llr = self._opt_common_mu(mu) 

107 pval = 1 - chi2.cdf(llr, self.num_groups - 1) 

108 if return_weights: 

109 return llr, pval, mu, self.new_weights 

110 else: 

111 return llr, pval, mu 

112 else: 

113 res = optimize.fmin_powell(self._opt_common_mu, mu_start, 

114 full_output=1, disp=False) 

115 llr = res[1] 

116 mu_common = float(res[0]) 

117 pval = 1 - chi2.cdf(llr, self.num_groups - 1) 

118 if return_weights: 

119 return llr, pval, mu_common, self.new_weights 

120 else: 

121 return llr, pval, mu_common