summaryrefslogtreecommitdiff
path: root/gi/posterior-regularisation/prjava/src/optimization/util/StaticTools.java
blob: bcabee06115c8e06cd0d9da7f6ad48bda03300f1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package optimization.util;


import java.io.File;
import java.io.PrintStream;

public class StaticTools {

	static java.text.DecimalFormat fmt = new java.text.DecimalFormat();

	public static void createDir(String directory) {

		File dir = new File(directory);
		if (!dir.isDirectory()) {
			boolean success = dir.mkdirs();
			if (!success) {
				System.out.println("Unable to create directory " + directory);
				System.exit(0);
			}
			System.out.println("Created directory " + directory);
		} else {
			System.out.println("Reusing directory " + directory);
		}
	}

	/*
	 * q and p are indexed by source/foreign Sum_S(q) = 1 the same for p KL(q,p) =
	 * Eq*q/p
	 */
	public static double KLDistance(double[][] p, double[][] q, int sourceSize,
			int foreignSize) {
		double totalKL = 0;
		// common.StaticTools.printMatrix(q, sourceSize, foreignSize, "q",
		// System.out);
		// common.StaticTools.printMatrix(p, sourceSize, foreignSize, "p",
		// System.out);
		for (int i = 0; i < sourceSize; i++) {
			double kl = 0;
			for (int j = 0; j < foreignSize; j++) {
				assert !Double.isNaN(q[i][j]) : "KLDistance q:  prob is NaN";
				assert !Double.isNaN(p[i][j]) : "KLDistance p:  prob is NaN";
				if (p[i][j] == 0 || q[i][j] == 0) {
					continue;
				} else {
					kl += q[i][j] * Math.log(q[i][j] / p[i][j]);
				}

			}
			totalKL += kl;
		}
		assert !Double.isNaN(totalKL) : "KLDistance: prob is NaN";
		if (totalKL < -1.0E-10) {
			System.out.println("KL Smaller than zero " + totalKL);
			System.out.println("Source Size" + sourceSize);
			System.out.println("Foreign Size" + foreignSize);
			StaticTools.printMatrix(q, sourceSize, foreignSize, "q",
					System.out);
			StaticTools.printMatrix(p, sourceSize, foreignSize, "p",
					System.out);
			System.exit(-1);
		}
		return totalKL / sourceSize;
	}

	/*
	 * indexed the by [fi][si]
	 */
	public static double KLDistancePrime(double[][] p, double[][] q,
			int sourceSize, int foreignSize) {
		double totalKL = 0;
		for (int i = 0; i < sourceSize; i++) {
			double kl = 0;
			for (int j = 0; j < foreignSize; j++) {
				assert !Double.isNaN(q[j][i]) : "KLDistance q:  prob is NaN";
				assert !Double.isNaN(p[j][i]) : "KLDistance p:  prob is NaN";
				if (p[j][i] == 0 || q[j][i] == 0) {
					continue;
				} else {
					kl += q[j][i] * Math.log(q[j][i] / p[j][i]);
				}

			}
			totalKL += kl;
		}
		assert !Double.isNaN(totalKL) : "KLDistance: prob is NaN";
		return totalKL / sourceSize;
	}

	public static double Entropy(double[][] p, int sourceSize, int foreignSize) {
		double totalE = 0;
		for (int i = 0; i < foreignSize; i++) {
			double e = 0;
			for (int j = 0; j < sourceSize; j++) {
				e += p[i][j] * Math.log(p[i][j]);
			}
			totalE += e;
		}
		return totalE / sourceSize;
	}

	public static double[][] copyMatrix(double[][] original, int sourceSize,
			int foreignSize) {
		double[][] result = new double[sourceSize][foreignSize];
		for (int i = 0; i < sourceSize; i++) {
			for (int j = 0; j < foreignSize; j++) {
				result[i][j] = original[i][j];
			}
		}
		return result;
	}

	public static void printMatrix(double[][] matrix, int sourceSize,
			int foreignSize, String info, PrintStream out) {

		java.text.DecimalFormat fmt = new java.text.DecimalFormat();
		fmt.setMaximumFractionDigits(3);
		fmt.setMaximumIntegerDigits(3);
		fmt.setMinimumFractionDigits(3);
		fmt.setMinimumIntegerDigits(3);

		out.println(info);

		for (int i = 0; i < foreignSize; i++) {
			for (int j = 0; j < sourceSize; j++) {
				out.print(prettyPrint(matrix[j][i], ".00E00", 6) + " ");
			}
			out.println();
		}
		out.println();
		out.println();
	}

	public static void printMatrix(int[][] matrix, int sourceSize,
			int foreignSize, String info, PrintStream out) {

		out.println(info);
		for (int i = 0; i < foreignSize; i++) {
			for (int j = 0; j < sourceSize; j++) {
				out.print(matrix[j][i] + " ");
			}
			out.println();
		}
		out.println();
		out.println();
	}

	public static String formatTime(long duration) {
		StringBuilder sb = new StringBuilder();
		double d = duration / 1000;
		fmt.applyPattern("00");
		sb.append(fmt.format((int) (d / (60 * 60))) + ":");
		d -= ((int) d / (60 * 60)) * 60 * 60;
		sb.append(fmt.format((int) (d / 60)) + ":");
		d -= ((int) d / 60) * 60;
		fmt.applyPattern("00.0");
		sb.append(fmt.format(d));
		return sb.toString();
	}

	public static String prettyPrint(double d, String patt, int len) {
		fmt.applyPattern(patt);
		String s = fmt.format(d);
		while (s.length() < len) {
			s = " " + s;
		}
		return s;
	}
	
	
	public static long getUsedMemory(){
		System.gc();
		return (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory())/ (1024 * 1024);
	}
	
	public final static boolean compareDoubles(double d1, double d2){
		return Math.abs(d1-d2) <= 1.E-10;
	}
	
	
}