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
8 // http://www.apache.org/licenses/LICENSE-2.0
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");
14 goog.provide('i18n.input.chrome.inputview.elements.content.GaussianEstimator');
17 goog.scope(function() {
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))
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.
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.
39 i18n.input.chrome.inputview.elements.content.GaussianEstimator = function(
40 center, covariance, amplitude) {
44 * @private {!goog.math.Coordinate}
46 this.center_ = center;
53 this.cinvX_ = 1 / (amplitude * covariance);
60 this.cinvY_ = 1 / covariance;
63 * The Norm in log space.
67 this.logNorm_ = Math.log(1 / (2 * Math.PI * Math.sqrt(amplitude *
68 covariance * covariance)));
70 var GaussianEstimator = i18n.input.chrome.inputview.elements.content.
75 * Estimates the possibility in log space.
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;