#!/usr/bin/python ''' !----------------------------------------------------------------------------! ! This script calculated weighted RMSE ! ! Pavlo O. Dral, Xiamen University, 2019-2020 ! !----------------------------------------------------------------------------! Usage: python compare_weighted.txt ''' import math def weight(E): # Weight factors as in J. Chem. Phys. 142, 244306 (2015) N = 0.0001 Ew = max(E,10000) w = (math.tanh(-0.0006 * (E - 15000.0) ) + 1.002002002 ) / (2.002002002 * N * Ew) return w def RMSE_weighted(): # Root-mean-square error y_est = [float(yy) for yy in open('ylayer4.dat', 'r').readlines()] y_ref = [float(yy) for yy in open('y.dat', 'r').readlines()] value = 0.0 N = len(y_est) for i in range(N): value += ( (y_est[i] - y_ref[i]) ** 2 ) * weight(y_ref[i]) value /= N value = math.sqrt(value) return value print 'RMSE = ', RMSE_weighted(), ' (weighted)'