use of structures._HDPThetaStar._Connection in project IR_Base by Linda-sunshine.
the class CLRWithMMB method updateEdgeMembership.
public void updateEdgeMembership(int i, int j, int e) {
_MMBAdaptStruct ui = (_MMBAdaptStruct) m_userList.get(i);
_MMBAdaptStruct uj = (_MMBAdaptStruct) m_userList.get(j);
int index = -1;
_HDPThetaStar thetai = ui.getThetaStar(uj);
// remove the neighbor from user
ui.rmNeighbor(uj);
// update the edge information inside the user
ui.incHDPThetaStarEdgeSize(thetai, -1);
// update the edge count for the thetastar
thetai.updateEdgeCount(e, -1);
m_MNL[e]--;
// No data associated with the cluster
if (thetai.getMemSize() == 0 && thetai.getTotalEdgeSize() == 0) {
System.out.println("[Info]Zero cluster detected in updating doc!");
// recycle the gamma
m_gamma_e += thetai.getGamma();
// swap the disabled theta to the last for later use
index = findHDPThetaStar(thetai);
if (index == -1)
System.out.println("Bug");
// move it back to \theta*
swapTheta(m_kBar - 1, index);
thetai.reset();
m_kBar--;
}
}
use of structures._HDPThetaStar._Connection in project IR_Base by Linda-sunshine.
the class CLRWithMMB method printBMatrix.
public void printBMatrix(String filename) {
// Get the B matrix
int idx = filename.indexOf("txt");
String zerofile = filename.substring(0, idx - 1) + "_0.txt";
String onefile = filename.substring(0, idx - 1) + "_1.txt";
int[] eij;
int[][][] B = new int[m_kBar][m_kBar][2];
_HDPThetaStar theta1;
int index1 = 0, index2 = 0;
for (int i = 0; i < m_kBar; i++) {
theta1 = m_hdpThetaStars[i];
index1 = theta1.getIndex();
HashMap<_HDPThetaStar, _Connection> connectionMap = theta1.getConnectionMap();
for (_HDPThetaStar theta2 : connectionMap.keySet()) {
index2 = theta2.getIndex();
eij = connectionMap.get(theta2).getEdge();
B[index1][index2][0] = eij[0];
B[index1][index2][1] = eij[1];
}
}
try {
// print out the zero edges in B matrix
PrintWriter writer = new PrintWriter(new File(zerofile), "UTF-8");
for (int i = 0; i < B.length; i++) {
int[][] row = B[i];
for (int j = 0; j < row.length; j++) {
writer.write(String.format("%d", B[i][j][0]));
if (j != row.length - 1) {
writer.write("\t");
}
}
writer.write("\n");
}
writer.close();
// print out the one edges in B matrix
writer = new PrintWriter(new File(onefile), "UTF-8");
for (int i = 0; i < B.length; i++) {
int[][] row = B[i];
for (int j = 0; j < row.length; j++) {
writer.write(String.format("%d", B[i][j][1]));
if (j != row.length - 1) {
writer.write("\t");
}
}
writer.write("\n");
}
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
use of structures._HDPThetaStar._Connection in project IR_Base by Linda-sunshine.
the class CLRWithMMB method printClusterInfo.
public void printClusterInfo(String filename) {
try {
_HDPThetaStar theta;
PrintWriter writer = new PrintWriter(new File(filename));
for (int k = 0; k < m_kBar; k++) {
theta = m_hdpThetaStars[k];
writer.write(String.format("%d,%d,%d\n", theta.getMemSize(), theta.getEdgeSize(0), theta.getEdgeSize(1)));
}
writer.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
use of structures._HDPThetaStar._Connection in project IR_Base by Linda-sunshine.
the class CLRWithMMB method accumulateLikelihoodEMMB.
// traverse all the clusters to get the likelihood given by mmb edges
protected double accumulateLikelihoodEMMB() {
double likelihoodE = 0;
_Connection connection;
int e_0, e_1;
_HDPThetaStar theta_g, theta_h;
for (int g = 0; g < m_kBar; g++) {
theta_g = m_hdpThetaStars[g];
for (int h = g; h < m_kBar; h++) {
theta_h = m_hdpThetaStars[h];
if (!theta_g.hasConnection(theta_h))
continue;
connection = theta_g.getConnection(theta_h);
e_1 = connection.getEdge()[1];
e_0 = connection.getEdge()[0];
// likelihoodE: m*log(rho*(a+e_1))/(a+b+e_0+e_1))+n*log(rho*(b+e_0))/(a+b+e_0+e_1))
likelihoodE += (e_0 + e_1) * Math.log(m_rho) + e_1 * Math.log(m_abcd[0] + e_1) + e_0 * Math.log(m_abcd[1] + e_0) - (e_0 + e_1) * Math.log(m_abcd[0] + m_abcd[1] + e_0 + e_1);
// likelihoodE += e_1*Math.log(m_abcd[0]+e_1)+e_0*Math.log(m_abcd[1]+e_0)
// -(e_0+e_1)*Math.log(m_abcd[0]+m_abcd[1]+e_0+e_1);
}
}
return likelihoodE;
}
use of structures._HDPThetaStar._Connection in project IR_Base by Linda-sunshine.
the class CLRWithMMB method calculateMixture4TestUser.
// calculate the mixture for test user based on review assignment
public void calculateMixture4TestUser(_MMBAdaptStruct user) {
int cIndex = 0;
double prob, logSum, sum = 0;
double[] probs = new double[m_kBar];
_HDPThetaStar curTheta;
// calculate the cluster assignment for each review first
for (_Review r : user.getReviews()) {
// suppose all reviews are test review in this setting
if (r.getType() != rType.TEST)
continue;
for (int k = 0; k < probs.length; k++) {
curTheta = m_hdpThetaStars[k];
r.setHDPThetaStar(curTheta);
prob = calcLogLikelihoodX(r) + Math.log(calcGroupPopularity(user, k, curTheta.getGamma()));
probs[k] = prob;
}
// normalize the prob
logSum = Utils.logSumOfExponentials(probs);
for (int k = 0; k < probs.length; k++) probs[k] -= logSum;
// take the cluster that has maximum prob as the review's cluster assignment
curTheta = m_hdpThetaStars[Utils.argmax(probs)];
r.setHDPThetaStar(curTheta);
// update the cluster assignment for the user
user.incHDPThetaStarMemSize(r.getHDPThetaStar(), 1);
}
// calculate the mixture: get the review assignment and normalize it
Arrays.fill(probs, 0);
// calculate the sum first
for (_HDPThetaStar theta : user.getHDPTheta4Rvw()) {
sum += user.getHDPThetaMemSize(theta);
}
// calculate the prob for each dim
for (_HDPThetaStar theta : user.getHDPTheta4Rvw()) {
cIndex = theta.getIndex();
probs[cIndex] = user.getHDPThetaMemSize(theta) / sum;
}
user.setMixture(probs);
}
Aggregations