Upstream version 11.40.271.0
[platform/framework/web/crosswalk.git] / src / third_party / google_input_tools / src / chrome / os / inputview / elements / content / gaussianestimator.js
1 // Copyright 2014 The ChromeOS IME Authors. All Rights Reserved.
2 // limitations under the License.
3 // See the License for the specific language governing permissions and
4 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
5 // distributed under the License is distributed on an "AS-IS" BASIS,
6 // Unless required by applicable law or agreed to in writing, software
7 //
8 //      http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // You may obtain a copy of the License at
11 // you may not use this file except in compliance with the License.
12 // Licensed under the Apache License, Version 2.0 (the "License");
13 //
14 goog.provide('i18n.input.chrome.inputview.elements.content.GaussianEstimator');
15
16
17 goog.scope(function() {
18
19 /**
20  * A tool class to calculate probability with Gaussian distribution.
21  * Gaussian(x, y) = Norm * exp (-(1/2) * ((x - centerX) ^ 2 * CinvX + (y -
22  * centerY) ^ 2 * CinvY))
23  * where
24  * CinvX = 1 / (AmplitudeX * Covariance)
25  * CinvY = 1 / (AmplitudeY * Covariance)
26  * Norm = 1 / (2 * PI) * Sqrt(CinX * CinY)
27  * LogGaussian(x, y) = LogNorm + (-1/2) * ((x - centerX) ^ 2 * CinvX
28  * + (y - centerY) ^ 2 * CinvY))
29  * In this class we assumes amplitude Y is normalized to 1, so
30  * amplitude X is real amplitude X relative to amplitude Y.
31  *
32  * @param {!goog.math.Coordinate} center .
33  * @param {number} covariance .
34  * @param {!number} amplitude Amplitude on dimension X of the distribution. The
35  * estimator assumes amplitude on dimension Y is 1, so this value is real
36  * amplitude X relative to amplitude Y.
37  * @constructor
38  */
39 i18n.input.chrome.inputview.elements.content.GaussianEstimator = function(
40     center, covariance, amplitude) {
41   /**
42    * The center point.
43    *
44    * @private {!goog.math.Coordinate}
45    */
46   this.center_ = center;
47
48   /**
49    * The CinvX.
50    *
51    * @private {number}
52    */
53   this.cinvX_ = 1 / (amplitude * covariance);
54
55   /**
56    * The CinvY.
57    *
58    * @private {number}
59    */
60   this.cinvY_ = 1 / covariance;
61
62   /**
63    * The Norm in log space.
64    *
65    * @private {number}
66    */
67   this.logNorm_ = Math.log(1 / (2 * Math.PI * Math.sqrt(amplitude *
68       covariance * covariance)));
69 };
70 var GaussianEstimator = i18n.input.chrome.inputview.elements.content.
71     GaussianEstimator;
72
73
74 /**
75  * Estimates the possibility in log space.
76  *
77  * @param {number} x .
78  * @param {number} y .
79  */
80 GaussianEstimator.prototype.estimateInLogSpace = function(x, y) {
81   var dx = x - this.center_.x;
82   var dy = y - this.center_.y;
83   var exponent = this.cinvX_ * dx * dx + this.cinvY_ * dy * dy;
84   return this.logNorm_ + (-0.5) * exponent;
85 };
86
87 }); // goog.scope