def linearfunction(x,y,name='linear rating'): datadf = pd.DataFrame.from_dict({'x':x,'y':y}).dropna() ## put x and y in a dataframe so you can drop ones that don't match up datadf = datadf[datadf>=0].dropna() ##verify data is valid (not inf) regression = pd.ols(y=datadf['y'],x=datadf['x']) pearson = pearson_r(datadf['x'],datadf['y'])[0] spearman = spearman_r(datadf['x'],datadf['y'])[0] coeffdf = pd.DataFrame({'a':[regression.beta[1]],'b':[regression.beta[0]],'r2':[regression.r2],'rmse':[regression.rmse],'pearson':[pearson],'spearman':[spearman]},index=[name]) return coeffdf
def powerfunction(x,y,name='power rating',pvalue=0.01): ## put x and y in a dataframe so you can drop ones that don't match up datadf = pd.DataFrame.from_dict({'x':x,'y':y}).dropna().apply(np.log10) datadf = datadf[datadf>=-10] ##verify data is valid (not inf) regression = pd.ols(y=datadf['y'],x=datadf['x']) if pearson_r(datadf['x'],datadf['y'])[1] < pvalue: pearson = pearson_r(datadf['x'],datadf['y'])[0] else: pearson = np.nan if spearman_r(datadf['x'],datadf['y'])[1] < pvalue: spearman = spearman_r(datadf['x'],datadf['y'])[0] else: spearman = np.nan coeffdf = pd.DataFrame({'a':[10**regression.beta[1]],'b':[regression.beta[0]], 'r2':[regression.r2],'rmse':[regression.rmse],'pearson':[pearson],'spearman':[spearman]}, index=[name]) return coeffdf
def calculate_correlation(x: List[float], y: List[float]) -> Tuple[float, float, float]: r, p = pearson_r(x, y) return r, pow(r, 2), p