| /* ----------------------------------------------------------------------- *//** |
| * |
| * Licensed to the Apache Software Foundation (ASF) under one |
| * or more contributor license agreements. See the NOTICE file |
| * distributed with this work for additional information |
| * regarding copyright ownership. The ASF licenses this file |
| * to you under the Apache License, Version 2.0 (the |
| * "License"); you may not use this file except in compliance |
| * with the License. You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, |
| * software distributed under the License is distributed on an |
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| * KIND, either express or implied. See the License for the |
| * specific language governing permissions and limitations |
| * under the License. |
| * |
| * @file pred_metrics.sql_in |
| * |
| * @brief A collection of summary statistics to gauge model |
| * accuracy based on predicted values vs. ground-truth values. |
| * @date April 2016 |
| * |
| * |
| *//* ----------------------------------------------------------------------- */ |
| |
| CREATE TABLE test_set( |
| pred FLOAT8, |
| obs FLOAT8 |
| ); |
| |
| INSERT INTO test_set VALUES |
| (37.5,53.1), (12.3,34.2), (74.2,65.4), (91.1,82.1); |
| |
| DROP TABLE IF EXISTS table_out; |
| SELECT mean_abs_error( 'test_set', 'table_out', 'pred', 'obs'); |
| SELECT * FROM table_out; |
| DROP TABLE IF EXISTS table_out; |
| SELECT mean_abs_perc_error( 'test_set', 'table_out', 'pred', 'obs'); |
| SELECT * FROM table_out; |
| DROP TABLE IF EXISTS table_out; |
| SELECT mean_perc_error( 'test_set', 'table_out', 'pred', 'obs'); |
| SELECT * FROM table_out; |
| DROP TABLE IF EXISTS table_out; |
| SELECT mean_squared_error( 'test_set', 'table_out', 'pred', 'obs'); |
| SELECT * FROM table_out; |
| DROP TABLE IF EXISTS table_out; |
| SELECT r2_score( 'test_set', 'table_out', 'pred', 'obs'); |
| SELECT * FROM table_out; |
| DROP TABLE IF EXISTS table_out; |
| SELECT adjusted_r2_score( 'test_set', 'table_out', 'pred', 'obs', 3, 100); |
| SELECT * FROM table_out; |
| DROP TABLE IF EXISTS table_out; |
| SELECT confusion_matrix( 'test_set', 'table_out', 'pred', 'obs'); |
| SELECT * FROM table_out; |
| DROP TABLE IF EXISTS table_out; |
| |
| |
| CREATE TABLE test_binary( |
| pred FLOAT8, |
| obs FLOAT8 |
| ); |
| |
| INSERT INTO test_binary VALUES |
| (0.5,0), (0.3,0), (0.2,1.0), (0.1,0.0); |
| SELECT binary_classifier( 'test_binary', 'table_out', 'pred', 'obs'); |
| SELECT * FROM table_out; |
| DROP TABLE IF EXISTS table_out; |
| |
| SELECT area_under_roc( 'test_binary', 'table_out', 'pred', 'obs'); |
| SELECT * FROM table_out; |
| DROP TABLE IF EXISTS table_out; |
| |
| |
| CREATE TABLE test_set_gr( |
| pred FLOAT8, |
| obs FLOAT8, |
| gr1 INTEGER, |
| gr2 INTEGER |
| ); |
| |
| INSERT INTO test_set_gr VALUES |
| (37.5,53.1,1,1), (12.3,34.2,1,1), (74.2,65.4,1,1), (91.1,82.1,1,1), |
| (11.1,22.2,2,3), (12.3,64.2,2,3), (10.1,10.2,2,3); |
| |
| DROP TABLE IF EXISTS table_out; |
| SELECT mean_abs_error( 'test_set_gr', 'table_out', 'pred', 'obs', 'gr1, gr2'); |
| SELECT assert( relative_error(mean_abs_error, 13.825) < 1e-2, 'Pred_metrics: |
| Assertion error mean_abs_error') |
| FROM table_out WHERE gr1 = 1; |
| |
| DROP TABLE IF EXISTS table_out; |
| SELECT mean_abs_perc_error( 'test_set_gr', 'table_out', 'pred', 'obs', 'gr1, gr2'); |
| SELECT assert( relative_error(mean_abs_perc_error, 0.2945) < 1e-2, 'Pred_metrics |
| : Assertion error mean_abs_perc_error') |
| FROM table_out WHERE gr1 = 1; |
| |
| DROP TABLE IF EXISTS table_out; |
| SELECT mean_perc_error( 'test_set_gr', 'table_out', 'pred', 'obs', 'gr1, gr2'); |
| SELECT assert( relative_error(mean_perc_error, -0.1724) < 1e-2, 'Pred_metrics: |
| Assertion error mean_perc_error') |
| FROM table_out WHERE gr1 = 1; |
| |
| DROP TABLE IF EXISTS table_out; |
| SELECT mean_squared_error( 'test_set_gr', 'table_out', 'pred', 'obs', 'gr1, gr2'); |
| SELECT assert( relative_error(mean_squared_error, 220.3525) < 1e-2, |
| 'Pred_metrics: Assertion error mean_squared_error') |
| FROM table_out WHERE gr1 = 1; |
| |
| DROP TABLE IF EXISTS table_out; |
| SELECT r2_score( 'test_set_gr', 'table_out', 'pred', 'obs', 'gr1, gr2'); |
| SELECT assert( relative_error(r2_score, 0.2799) < 1e-2, 'Pred_metrics: Assertion |
| error r2_score') |
| FROM table_out WHERE gr1 = 1; |
| |
| DROP TABLE IF EXISTS table_out; |
| SELECT adjusted_r2_score( 'test_set_gr', 'table_out', 'pred', 'obs', 3, 100, 'gr1, gr2'); |
| SELECT assert( relative_error(adjusted_r2_score, 0.2574) < 1e-2, 'Pred_metrics: |
| Assertion error adjusted_r2_score') |
| FROM table_out WHERE gr1 = 1; |
| DROP TABLE IF EXISTS table_out; |
| |
| CREATE VIEW test_set_view AS |
| SELECT (pred > 10) as pred, (obs > 40) as obs, gr1, gr2 |
| FROM test_set_gr; |
| |
| SELECT binary_classifier( 'test_set_view', 'table_out', 'pred', 'obs', 'gr1, |
| gr2'); |
| DROP TABLE IF EXISTS table_out; |
| |
| SELECT area_under_roc( 'test_set_view', 'table_out', 'pred', 'obs', 'gr1, gr2'); |
| DROP TABLE IF EXISTS table_out; |