1 |
efrain |
1 |
<?php
|
|
|
2 |
// This file is part of Moodle - http://moodle.org/
|
|
|
3 |
//
|
|
|
4 |
// Moodle is free software: you can redistribute it and/or modify
|
|
|
5 |
// it under the terms of the GNU General Public License as published by
|
|
|
6 |
// the Free Software Foundation, either version 3 of the License, or
|
|
|
7 |
// (at your option) any later version.
|
|
|
8 |
//
|
|
|
9 |
// Moodle is distributed in the hope that it will be useful,
|
|
|
10 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
11 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
12 |
// GNU General Public License for more details.
|
|
|
13 |
//
|
|
|
14 |
// You should have received a copy of the GNU General Public License
|
|
|
15 |
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
|
|
16 |
|
|
|
17 |
/**
|
|
|
18 |
* Linear values target.
|
|
|
19 |
*
|
|
|
20 |
* @package core_analytics
|
|
|
21 |
* @copyright 2017 David Monllao {@link http://www.davidmonllao.com}
|
|
|
22 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
23 |
*/
|
|
|
24 |
|
|
|
25 |
namespace core_analytics\local\target;
|
|
|
26 |
|
|
|
27 |
defined('MOODLE_INTERNAL') || die();
|
|
|
28 |
|
|
|
29 |
/**
|
|
|
30 |
* Linear values target.
|
|
|
31 |
*
|
|
|
32 |
* @package core_analytics
|
|
|
33 |
* @copyright 2017 David Monllao {@link http://www.davidmonllao.com}
|
|
|
34 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
35 |
*/
|
|
|
36 |
abstract class linear extends base {
|
|
|
37 |
|
|
|
38 |
/**
|
|
|
39 |
* Are the calculated values this target returns linear values?
|
|
|
40 |
*
|
|
|
41 |
* @return bool
|
|
|
42 |
*/
|
|
|
43 |
public function is_linear() {
|
|
|
44 |
// Not supported yet.
|
|
|
45 |
throw new \coding_exception('Sorry, this version\'s prediction processors only support targets with binary values.' .
|
|
|
46 |
' You can write your own and overwrite this method though.');
|
|
|
47 |
}
|
|
|
48 |
|
|
|
49 |
/**
|
|
|
50 |
* How positive is this calculated value?
|
|
|
51 |
*
|
|
|
52 |
* @param float $value
|
|
|
53 |
* @param string $ignoredsubtype
|
|
|
54 |
* @return int
|
|
|
55 |
*/
|
|
|
56 |
public function get_calculation_outcome($value, $ignoredsubtype = false) {
|
|
|
57 |
|
|
|
58 |
// This is very generic, targets will probably be interested in overwriting this.
|
|
|
59 |
$diff = static::get_max_value() - static::get_min_value();
|
|
|
60 |
if (($value - static::get_min_value()) / $diff >= 0.5) {
|
|
|
61 |
return self::OUTCOME_VERY_POSITIVE;
|
|
|
62 |
}
|
|
|
63 |
return self::OUTCOME_VERY_NEGATIVE;
|
|
|
64 |
}
|
|
|
65 |
|
|
|
66 |
/**
|
|
|
67 |
* Gets the maximum value for this target
|
|
|
68 |
*
|
|
|
69 |
* @return float
|
|
|
70 |
*/
|
|
|
71 |
public static function get_max_value() {
|
|
|
72 |
// Coding exception as this will only be called if this target have linear values.
|
|
|
73 |
throw new \coding_exception('Overwrite get_max_value() and return the target max value');
|
|
|
74 |
}
|
|
|
75 |
|
|
|
76 |
/**
|
|
|
77 |
* Gets the minimum value for this target
|
|
|
78 |
*
|
|
|
79 |
* @return float
|
|
|
80 |
*/
|
|
|
81 |
public static function get_min_value() {
|
|
|
82 |
// Coding exception as this will only be called if this target have linear values.
|
|
|
83 |
throw new \coding_exception('Overwrite get_min_value() and return the target min value');
|
|
|
84 |
}
|
|
|
85 |
|
|
|
86 |
/**
|
|
|
87 |
* This method determines if a prediction is interesing for the model or not.
|
|
|
88 |
*
|
|
|
89 |
* @param mixed $predictedvalue
|
|
|
90 |
* @param float $predictionscore
|
|
|
91 |
* @return bool
|
|
|
92 |
*/
|
|
|
93 |
public function triggers_callback($predictedvalue, $predictionscore) {
|
|
|
94 |
|
|
|
95 |
if (!parent::triggers_callback($predictedvalue, $predictionscore)) {
|
|
|
96 |
return false;
|
|
|
97 |
}
|
|
|
98 |
|
|
|
99 |
// People may not want to set a boundary.
|
|
|
100 |
$boundary = $this->get_callback_boundary();
|
|
|
101 |
if (!empty($boundary) && floatval($predictedvalue) < $boundary) {
|
|
|
102 |
return false;
|
|
|
103 |
}
|
|
|
104 |
|
|
|
105 |
return true;
|
|
|
106 |
}
|
|
|
107 |
|
|
|
108 |
/**
|
|
|
109 |
* Returns the minimum value that triggers the callback.
|
|
|
110 |
*
|
|
|
111 |
* @return float
|
|
|
112 |
*/
|
|
|
113 |
protected function get_callback_boundary() {
|
|
|
114 |
// Coding exception as this will only be called if this target have linear values.
|
|
|
115 |
throw new \coding_exception('Overwrite get_callback_boundary() and return the min value that ' .
|
|
|
116 |
'should trigger the callback');
|
|
|
117 |
}
|
|
|
118 |
}
|