#! /usr/bin/env python # def r8col_variance ( m, n, a ): #*****************************************************************************80 # ## R8COL_VARIANCE returns the variances of an R8COL. # # Discussion: # # An R8COL is an M by N array of R8's, regarded as an array of N columns, # each of length M. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 26 February 2016 # # Author: # # John Burkardt # # Parameters: # # Input, integer M, N, the number of rows and columns in the array. # # Input, real A(M,N), the array whose variances are desired. # # Output, real VARIANCE(N), the variances of the rows. # import numpy as np variance = np.zeros ( n ) for j in range ( 0, n ): t = 0.0 for i in range ( 0, m ): t = t + a[i,j] mean = t / float ( m ) t = 0.0 for i in range ( 0, m ): t = t + ( a[i,j] - mean ) ** 2 if ( 1 < m ): t = t / float ( m - 1 ) else: t = 0.0 variance[j] = t return variance def r8col_variance_test ( ): #*****************************************************************************80 # ## R8COL_VARIANCE_TEST tests R8COL_VARIANCE. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 26 February 2016 # # Author: # # John Burkardt # import numpy as np from r8col_print import r8col_print from r8vec_print import r8vec_print m = 3 n = 4 print '' print 'R8COL_VARIANCE_TEST' print ' For an R8COL, an array of column vectors' print ' R8COL_VARIANCE computes variances' a = np.zeros ( [ m, n ] ) k = 0 for i in range ( 0, m ): for j in range ( 0, n ): k = k + 1 a[i,j] = float ( k ) r8col_print ( m, n, a, ' The array:' ) variance = r8col_variance ( m, n, a ) r8vec_print ( n, variance, ' Column variances:' ) # # Terminate. # print '' print 'R8COL_VARIANCE_TEST' print ' Normal end of execution.' return if ( __name__ == '__main__' ): from timestamp import timestamp timestamp ( ) r8col_variance_test ( ) timestamp ( )