blob: c332be5602d8bc45f73ca48f0caf251c54809ce0 [file] [log] [blame]
saberian984e52f2012-06-05 18:23:53 -07001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17
18package android.bordeaux.learning;
19
20import java.util.HashMap;
21import java.util.Map;
22import android.util.Log;
23
24/**
25 * A simple impelentation of histograms with sparse enteries using HashMap.
26 * User can push examples or extract probabilites from this histogram.
27 */
Ruei-sung Linf0f78442012-08-13 19:04:29 -070028public class PredictorHist {
saberian984e52f2012-06-05 18:23:53 -070029 private HashMap<String, Integer> mCountHist;
30 private int mSampleCount;
31 String TAG = "PredicrtHist";
32
Ruei-sung Linf0f78442012-08-13 19:04:29 -070033 public PredictorHist() {
saberian984e52f2012-06-05 18:23:53 -070034 mCountHist = new HashMap<String, Integer>();
35 mSampleCount = 0;
36 }
37
38 // reset histogram
Ruei-sung Linf0f78442012-08-13 19:04:29 -070039 public void resetPredictorHist() {
saberian984e52f2012-06-05 18:23:53 -070040 mCountHist.clear();
41 mSampleCount = 0;
42 }
43
44 // getters
45 public final HashMap<String, Integer> getHist() {
46 return mCountHist;
47 }
48
49 public int getHistCounts() {
50 return mSampleCount;
51 }
52
53 //setter
54 public void set(HashMap<String, Integer> hist) {
Ruei-sung Linf0f78442012-08-13 19:04:29 -070055 resetPredictorHist();
saberian984e52f2012-06-05 18:23:53 -070056 for (Map.Entry<String, Integer> x : hist.entrySet()) {
57 mCountHist.put(x.getKey(), x.getValue());
58 mSampleCount = mSampleCount + x.getValue();
59 }
60 }
61
62 /**
63 * pushes a new example to the histogram
64 */
65 public void pushSample( String fs) {
66 int histValue = 1;
Ruei-sung Linf0f78442012-08-13 19:04:29 -070067 if (mCountHist.containsKey(fs)) {
saberian984e52f2012-06-05 18:23:53 -070068 histValue = histValue + mCountHist.get(fs);
Ruei-sung Linf0f78442012-08-13 19:04:29 -070069 }
saberian984e52f2012-06-05 18:23:53 -070070 mCountHist.put(fs,histValue);
71 mSampleCount++;
72 }
73
74 /**
75 * return probabilty of an exmple using the histogram
76 */
77 public float getProbability(String fs) {
78 float res = 0;
Ruei-sung Linf0f78442012-08-13 19:04:29 -070079 if (mCountHist.containsKey(fs)) {
saberian984e52f2012-06-05 18:23:53 -070080 res = ((float) mCountHist.get(fs)) / ((float)mSampleCount);
Ruei-sung Linf0f78442012-08-13 19:04:29 -070081 }
saberian984e52f2012-06-05 18:23:53 -070082 return res;
83 }
84}