def __init__(self, array, alpha=0.05, reductor=CPUReductor): self.__k__ = array.shape[0] self.__b__ = array.shape[1] self.__n__ = self.b * self.k self.__cm__ = reductor.correction_for_mean_block(array) self.__ssb__ = reductor.sum_of_squares_block( array, self.cm) # Blocks/Subjects self.__sst__ = reductor.sum_of_squares_total_block( array, self.cm) # Treatments self.__sse__ = reductor.standard_squared_error_block(array, self.cm) self.__df__ = self.n - self.k - self.b + 1 self.__mst__ = self.sst / (self.k - 1) self.__msb__ = self.ssb / (self.b - 1) self.__mse__ = self.sse / self.df self.__Ft__ = self.mst / self.mse # F-Ratio used to test H0 for treatments self.__Fb__ = self.msb / self.mse # F-Ratio used to test H0 for subjects self.__Pt__ = stats.f.sf(self.Ft, self.k - 1, self.df) self.__Pb__ = stats.f.sf(self.Fb, self.b - 1, self.df) row_mean = Arrays.mean(array, axis=1) row_var = Arrays.var(array, axis=1) row_pairs = sorted(combinations(range(0, len(row_mean)), 2), key=lambda p: p[0]) self.__turkey_intervals__ = create_turkey_intervals( row_mean, row_var, self.mse, alpha, self.k, self.b, self.df, row_pairs) self.__bonferroni_intervals__ = create_bonferroni_intervals( row_mean, row_var, self.mse, alpha, self.k, self.b, self.df, row_pairs)
def correction_for_mean_block(clazz, samples): # CM for randomized blocks """ Args: samples (2d array like): .- Return: (double): CM for randomized blocks """ array = Arrays.to_array(samples) k = array.shape[0] b = array.shape[1] n = b * k s = numpy.square(numpy.atleast_1d(skcuda.misc.sum(array).get())[0]) return (s / n)
def sum_of_squares_block(clazz, samples, cm=None): # SSB """ Args: samples (2d array like): .- cm (double): CM for randomized blocks Return: (double): Sum of squares for randomized block """ array = Arrays.to_array(samples) k = array.shape[0] column_sum = numpy.sum( numpy.square(skcuda.misc.sum(array, axis=0).get())) / k if cm is None: cm = GPUReductor.correction_for_mean_block(array) ssb = column_sum - cm return ssb
def sum_of_squares_total_block(clazz, samples, cm=None): # SST for randomized blocks """ Args: samples (2d array like): .- cm (double): CM for randomized blocks Return: (double): SST for randomized blocks """ array = Arrays.to_array(samples) b = array.shape[1] row_sum = numpy.sum(numpy.square(skcuda.misc.sum(array, axis=1).get())) / b if cm is None: cm = GPUReductor.correction_for_mean_block(array) sst = row_sum - cm return sst
def standard_squared_error_block(clazz, samples, cm=None): # SSE for randomized blocks """ Args: samples (square array like): .- cm (double): CM for randomized blocks Return: (double): SSE for randomized blocks """ array = Arrays.to_array(samples) s = numpy.square(skcuda.linalg.norm(array)) if cm is None: cm = GPUReductor.correction_for_mean_block(array) ssb = GPUReductor.sum_of_squares_block(array) sst = GPUReductor.sum_of_squares_total_block(array) total_ss = s - cm sse = total_ss - ssb - sst return sse
import pandas import sys if __name__ == "__main__": input_file = sys.argv[1] a = pandas.read_csv(input_file, infer_datetime_format=True).values.astype( numpy.float64) # a = numpy.array( # ( # (1, 3.0, 5), # (7.0, 11, 9), # (13, 17.0, 19), # ) # ) b = Arrays.to_array(a) print(type(a), type(b)) print(Arrays.sum(a)) print(Arrays.sum(b)) c = Arrays.sum(a, 0) d = Arrays.sum(b, 0) f = Arrays.sum(a, 1) g = Arrays.sum(b, 1) print(c) print(d) print(f) print(g)