From c9e5a50f05d065f4945d8c12423fa8ce1b80d6cb Mon Sep 17 00:00:00 2001 From: zunzun Date: Sat, 17 Dec 2016 05:04:50 -0600 Subject: [PATCH] Minor work regularizing fit statististics for Simple examples --- Examples/Simple/FitOneNamedEquation_3D.py | 101 ++++++++++++++++++ Examples/Simple/FitUserData_2D.py | 104 ++++++++++++++++++- Examples/Simple/FitUserDefinedFunction_2D.py | 100 +++++++++++++++++- 3 files changed, 300 insertions(+), 5 deletions(-) diff --git a/Examples/Simple/FitOneNamedEquation_3D.py b/Examples/Simple/FitOneNamedEquation_3D.py index 63db5bc..c51806d 100644 --- a/Examples/Simple/FitOneNamedEquation_3D.py +++ b/Examples/Simple/FitOneNamedEquation_3D.py @@ -30,3 +30,104 @@ print("Fitted Parameters:") for i in range(len(equation.solvedCoefficients)): print(" %s = %-.16E" % (equation.GetCoefficientDesignators()[i], equation.solvedCoefficients[i])) + + +equation.CalculateModelErrors(equation.solvedCoefficients, equation.dataCache.allDataCacheDictionary) +print() +for i in range(len(equation.dataCache.allDataCacheDictionary['DependentData'])): + print('X:', equation.dataCache.allDataCacheDictionary['IndependentData'][0][i],) + print('Y:', equation.dataCache.allDataCacheDictionary['IndependentData'][1][i],) + print('Z:', equation.dataCache.allDataCacheDictionary['DependentData'][i],) + print('Model:', equation.modelPredictions[i],) + print('Abs. Error:', equation.modelAbsoluteError[i],) + if not equation.dataCache.DependentDataContainsZeroFlag: + print('Rel. Error:', equation.modelRelativeError[i],) + print('Percent Error:', equation.modelPercentError[i]) + else: + print() +print() + + +########################################################## + + +equation.CalculateCoefficientAndFitStatistics() + +if equation.upperCoefficientBounds or equation.lowerCoefficientBounds: + print('You entered coefficient bounds. Parameter statistics may') + print('not be valid for parameter values at or near the bounds.') + print() + +print('Degress of freedom error', equation.df_e) +print('Degress of freedom regression', equation.df_r) + +if equation.rmse == None: + print('Root Mean Squared Error (RMSE): n/a') +else: + print('Root Mean Squared Error (RMSE):', equation.rmse) + +if equation.r2 == None: + print('R-squared: n/a') +else: + print('R-squared:', equation.r2) + +if equation.r2adj == None: + print('R-squared adjusted: n/a') +else: + print('R-squared adjusted:', equation.r2adj) + +if equation.Fstat == None: + print('Model F-statistic: n/a') +else: + print('Model F-statistic:', equation.Fstat) + +if equation.Fpv == None: + print('Model F-statistic p-value: n/a') +else: + print('Model F-statistic p-value:', equation.Fpv) + +if equation.ll == None: + print('Model log-likelihood: n/a') +else: + print('Model log-likelihood:', equation.ll) + +if equation.aic == None: + print('Model AIC: n/a') +else: + print('Model AIC:', equation.aic) + +if equation.bic == None: + print('Model BIC: n/a') +else: + print('Model BIC:', equation.bic) + + +print() +print("Individual Parameter Statistics:") +for i in range(len(equation.solvedCoefficients)): + if type(equation.tstat_beta) == type(None): + tstat = 'n/a' + else: + tstat = '%-.5E' % ( equation.tstat_beta[i]) + + if type(equation.pstat_beta) == type(None): + pstat = 'n/a' + else: + pstat = '%-.5E' % ( equation.pstat_beta[i]) + + if type(equation.sd_beta) != type(None): + print("Coefficient %s = %-.16E, std error: %-.5E" % (equation.GetCoefficientDesignators()[i], equation.solvedCoefficients[i], equation.sd_beta[i])) + else: + print("Coefficient %s = %-.16E, std error: n/a" % (equation.GetCoefficientDesignators()[i], equation.solvedCoefficients[i])) + print(" t-stat: %s, p-stat: %s, 95 percent confidence intervals: [%-.5E, %-.5E]" % (tstat, pstat, equation.ci[i][0], equation.ci[i][1])) + + +print() +print("Coefficient Covariance Matrix:") +for i in equation.cov_beta: + print(i) + + +print() +print('Java Source Code:') +print(pyeq3.outputSourceCodeService().GetOutputSourceCodeJAVA(equation)) diff --git a/Examples/Simple/FitUserData_2D.py b/Examples/Simple/FitUserData_2D.py index bc29f8d..f0cd342 100644 --- a/Examples/Simple/FitUserData_2D.py +++ b/Examples/Simple/FitUserData_2D.py @@ -38,8 +38,108 @@ ########################################################## -print(equation.GetDisplayName(), str(equation.GetDimensionality()) + "D") -print(equation.fittingTargetDictionary[equation.fittingTarget], '=', equation.CalculateAllDataFittingTarget(equation.solvedCoefficients)) +print("Equation:", equation.GetDisplayName(), str(equation.GetDimensionality()) + "D") +print("Fitting target of", equation.fittingTargetDictionary[equation.fittingTarget], '=', equation.CalculateAllDataFittingTarget(equation.solvedCoefficients)) print("Fitted Parameters:") for i in range(len(equation.solvedCoefficients)): print(" %s = %-.16E" % (equation.GetCoefficientDesignators()[i], equation.solvedCoefficients[i])) + + +equation.CalculateModelErrors(equation.solvedCoefficients, equation.dataCache.allDataCacheDictionary) +print() +for i in range(len(equation.dataCache.allDataCacheDictionary['DependentData'])): + print('X:', equation.dataCache.allDataCacheDictionary['IndependentData'][0][i],) + print('Y:', equation.dataCache.allDataCacheDictionary['DependentData'][i],) + print('Model:', equation.modelPredictions[i],) + print('Abs. Error:', equation.modelAbsoluteError[i],) + if not equation.dataCache.DependentDataContainsZeroFlag: + print('Rel. Error:', equation.modelRelativeError[i],) + print('Percent Error:', equation.modelPercentError[i]) + else: + print() +print() + + +########################################################## + + +equation.CalculateCoefficientAndFitStatistics() + +if equation.upperCoefficientBounds or equation.lowerCoefficientBounds: + print('You entered coefficient bounds. Parameter statistics may') + print('not be valid for parameter values at or near the bounds.') + print() + +print('Degress of freedom error', equation.df_e) +print('Degress of freedom regression', equation.df_r) + +if equation.rmse == None: + print('Root Mean Squared Error (RMSE): n/a') +else: + print('Root Mean Squared Error (RMSE):', equation.rmse) + +if equation.r2 == None: + print('R-squared: n/a') +else: + print('R-squared:', equation.r2) + +if equation.r2adj == None: + print('R-squared adjusted: n/a') +else: + print('R-squared adjusted:', equation.r2adj) + +if equation.Fstat == None: + print('Model F-statistic: n/a') +else: + print('Model F-statistic:', equation.Fstat) + +if equation.Fpv == None: + print('Model F-statistic p-value: n/a') +else: + print('Model F-statistic p-value:', equation.Fpv) + +if equation.ll == None: + print('Model log-likelihood: n/a') +else: + print('Model log-likelihood:', equation.ll) + +if equation.aic == None: + print('Model AIC: n/a') +else: + print('Model AIC:', equation.aic) + +if equation.bic == None: + print('Model BIC: n/a') +else: + print('Model BIC:', equation.bic) + + +print() +print("Individual Parameter Statistics:") +for i in range(len(equation.solvedCoefficients)): + if type(equation.tstat_beta) == type(None): + tstat = 'n/a' + else: + tstat = '%-.5E' % ( equation.tstat_beta[i]) + + if type(equation.pstat_beta) == type(None): + pstat = 'n/a' + else: + pstat = '%-.5E' % ( equation.pstat_beta[i]) + + if type(equation.sd_beta) != type(None): + print("Coefficient %s = %-.16E, std error: %-.5E" % (equation.GetCoefficientDesignators()[i], equation.solvedCoefficients[i], equation.sd_beta[i])) + else: + print("Coefficient %s = %-.16E, std error: n/a" % (equation.GetCoefficientDesignators()[i], equation.solvedCoefficients[i])) + print(" t-stat: %s, p-stat: %s, 95 percent confidence intervals: [%-.5E, %-.5E]" % (tstat, pstat, equation.ci[i][0], equation.ci[i][1])) + + +print() +print("Coefficient Covariance Matrix:") +for i in equation.cov_beta: + print(i) + + +print() +print('Java Source Code:') +print(pyeq3.outputSourceCodeService().GetOutputSourceCodeJAVA(equation)) diff --git a/Examples/Simple/FitUserDefinedFunction_2D.py b/Examples/Simple/FitUserDefinedFunction_2D.py index 7a45e0e..92bc12d 100644 --- a/Examples/Simple/FitUserDefinedFunction_2D.py +++ b/Examples/Simple/FitUserDefinedFunction_2D.py @@ -27,9 +27,103 @@ ########################################################## -print(equation.GetDisplayName(), str(equation.GetDimensionality()) + "D") -print('User Defined Function:', functionString) -print(equation.fittingTargetDictionary[equation.fittingTarget], '=', equation.CalculateAllDataFittingTarget(equation.solvedCoefficients)) +print("Equation:", equation.GetDisplayName(), str(equation.GetDimensionality()) + "D") +print("Fitting target of", equation.fittingTargetDictionary[equation.fittingTarget], '=', equation.CalculateAllDataFittingTarget(equation.solvedCoefficients)) print("Fitted Parameters:") for i in range(len(equation.solvedCoefficients)): print(" %s = %-.16E" % (equation.GetCoefficientDesignators()[i], equation.solvedCoefficients[i])) + + +equation.CalculateModelErrors(equation.solvedCoefficients, equation.dataCache.allDataCacheDictionary) +print() +for i in range(len(equation.dataCache.allDataCacheDictionary['DependentData'])): + print('X:', equation.dataCache.allDataCacheDictionary['IndependentData'][0][i],) + print('Y:', equation.dataCache.allDataCacheDictionary['DependentData'][i],) + print('Model:', equation.modelPredictions[i],) + print('Abs. Error:', equation.modelAbsoluteError[i],) + if not equation.dataCache.DependentDataContainsZeroFlag: + print('Rel. Error:', equation.modelRelativeError[i],) + print('Percent Error:', equation.modelPercentError[i]) + else: + print() +print() + + +########################################################## + + +equation.CalculateCoefficientAndFitStatistics() + +if equation.upperCoefficientBounds or equation.lowerCoefficientBounds: + print('You entered coefficient bounds. Parameter statistics may') + print('not be valid for parameter values at or near the bounds.') + print() + +print('Degress of freedom error', equation.df_e) +print('Degress of freedom regression', equation.df_r) + +if equation.rmse == None: + print('Root Mean Squared Error (RMSE): n/a') +else: + print('Root Mean Squared Error (RMSE):', equation.rmse) + +if equation.r2 == None: + print('R-squared: n/a') +else: + print('R-squared:', equation.r2) + +if equation.r2adj == None: + print('R-squared adjusted: n/a') +else: + print('R-squared adjusted:', equation.r2adj) + +if equation.Fstat == None: + print('Model F-statistic: n/a') +else: + print('Model F-statistic:', equation.Fstat) + +if equation.Fpv == None: + print('Model F-statistic p-value: n/a') +else: + print('Model F-statistic p-value:', equation.Fpv) + +if equation.ll == None: + print('Model log-likelihood: n/a') +else: + print('Model log-likelihood:', equation.ll) + +if equation.aic == None: + print('Model AIC: n/a') +else: + print('Model AIC:', equation.aic) + +if equation.bic == None: + print('Model BIC: n/a') +else: + print('Model BIC:', equation.bic) + + +print() +print("Individual Parameter Statistics:") +for i in range(len(equation.solvedCoefficients)): + if type(equation.tstat_beta) == type(None): + tstat = 'n/a' + else: + tstat = '%-.5E' % ( equation.tstat_beta[i]) + + if type(equation.pstat_beta) == type(None): + pstat = 'n/a' + else: + pstat = '%-.5E' % ( equation.pstat_beta[i]) + + if type(equation.sd_beta) != type(None): + print("Coefficient %s = %-.16E, std error: %-.5E" % (equation.GetCoefficientDesignators()[i], equation.solvedCoefficients[i], equation.sd_beta[i])) + else: + print("Coefficient %s = %-.16E, std error: n/a" % (equation.GetCoefficientDesignators()[i], equation.solvedCoefficients[i])) + print(" t-stat: %s, p-stat: %s, 95 percent confidence intervals: [%-.5E, %-.5E]" % (tstat, pstat, equation.ci[i][0], equation.ci[i][1])) + + +print() +print("Coefficient Covariance Matrix:") +for i in equation.cov_beta: + print(i)