📄 localscoresearchalgorithm.java
字号:
+ iSymbol] * Math.log(nCounts[iParent * numValues + iSymbol] / nSumOfCounts);
}
}
}
break;
default :
{
}
}
}
switch (m_nScoreType) {
case (Scoreable.MDL) :
{
fLogScore -= 0.5 * nCardinality * (numValues - 1) * Math.log(instances.numInstances());
// it seems safe to assume that numInstances>0 here
}
break;
case (Scoreable.AIC) :
{
fLogScore -= nCardinality * (numValues - 1);
}
break;
}
return fLogScore;
} // CalcNodeScore
protected double calcScoreOfCounts2(int[][] nCounts, int nCardinality, int numValues, Instances instances) {
// calculate scores using the distributions
double fLogScore = 0.0;
for (int iParent = 0; iParent < nCardinality; iParent++) {
switch (m_nScoreType) {
case (Scoreable.BAYES) :
{
double nSumOfCounts = 0;
for (int iSymbol = 0; iSymbol < numValues; iSymbol++) {
if (m_fAlpha + nCounts[iParent][iSymbol] != 0) {
fLogScore += Statistics.lnGamma(m_fAlpha + nCounts[iParent][iSymbol]);
nSumOfCounts += m_fAlpha + nCounts[iParent][iSymbol];
}
}
if (nSumOfCounts != 0) {
fLogScore -= Statistics.lnGamma(nSumOfCounts);
}
if (m_fAlpha != 0) {
fLogScore -= numValues * Statistics.lnGamma(m_fAlpha);
fLogScore += Statistics.lnGamma(numValues * m_fAlpha);
}
}
break;
case (Scoreable.BDeu) :
{
double nSumOfCounts = 0;
for (int iSymbol = 0; iSymbol < numValues; iSymbol++) {
if (m_fAlpha + nCounts[iParent * numValues][iSymbol] != 0) {
fLogScore += Statistics.lnGamma(1.0/(numValues * nCardinality) + nCounts[iParent * numValues][iSymbol]);
nSumOfCounts += 1.0/(numValues * nCardinality) + nCounts[iParent * numValues][iSymbol];
}
}
fLogScore -= Statistics.lnGamma(nSumOfCounts);
fLogScore -= numValues * Statistics.lnGamma(1.0);
fLogScore += Statistics.lnGamma(numValues * 1.0);
}
break;
case (Scoreable.MDL) :
case (Scoreable.AIC) :
case (Scoreable.ENTROPY) :
{
double nSumOfCounts = 0;
for (int iSymbol = 0; iSymbol < numValues; iSymbol++) {
nSumOfCounts += nCounts[iParent][iSymbol];
}
for (int iSymbol = 0; iSymbol < numValues; iSymbol++) {
if (nCounts[iParent][iSymbol] > 0) {
fLogScore += nCounts[iParent][iSymbol]
* Math.log(nCounts[iParent][iSymbol] / nSumOfCounts);
}
}
}
break;
default :
{
}
}
}
switch (m_nScoreType) {
case (Scoreable.MDL) :
{
fLogScore -= 0.5 * nCardinality * (numValues - 1) * Math.log(instances.numInstances());
// it seems safe to assume that numInstances>0 here
}
break;
case (Scoreable.AIC) :
{
fLogScore -= nCardinality * (numValues - 1);
}
break;
}
return fLogScore;
} // CalcNodeScore
/**
* Calc Node Score With AddedParent
*
* @param nNode node for which the score is calculate
* @param nCandidateParent candidate parent to add to the existing parent set
* @return log score
*/
public double calcScoreWithExtraParent(int nNode, int nCandidateParent) {
ParentSet oParentSet = m_BayesNet.getParentSet(nNode);
// sanity check: nCandidateParent should not be in parent set already
if (oParentSet.contains(nCandidateParent)) {
return -1e100;
}
// set up candidate parent
oParentSet.addParent(nCandidateParent, m_BayesNet.m_Instances);
// determine cardinality of parent set & reserve space for frequency counts
int nCardinality = oParentSet.getCardinalityOfParents();
int numValues = m_BayesNet.m_Instances.attribute(nNode).numValues();
int [][] nCounts = new int[nCardinality][numValues];
// calculate the score
double logScore = calcNodeScore(nNode);
// delete temporarily added parent
oParentSet.deleteLastParent(m_BayesNet.m_Instances);
return logScore;
} // CalcScoreWithExtraParent
/**
* Calc Node Score With Parent Deleted
*
* @param nNode node for which the score is calculate
* @param nCandidateParent candidate parent to delete from the existing parent set
* @return log score
*/
public double calcScoreWithMissingParent(int nNode, int nCandidateParent) {
ParentSet oParentSet = m_BayesNet.getParentSet(nNode);
// sanity check: nCandidateParent should be in parent set already
if (!oParentSet.contains( nCandidateParent)) {
return -1e100;
}
// set up candidate parent
int iParent = oParentSet.deleteParent(nCandidateParent, m_BayesNet.m_Instances);
// determine cardinality of parent set & reserve space for frequency counts
int nCardinality = oParentSet.getCardinalityOfParents();
int numValues = m_BayesNet.m_Instances.attribute(nNode).numValues();
int [][] nCounts = new int[nCardinality][numValues];
// calculate the score
double logScore = calcNodeScore(nNode);
// restore temporarily deleted parent
oParentSet.addParent(nCandidateParent, iParent, m_BayesNet.m_Instances);
return logScore;
} // CalcScoreWithMissingParent
/**
* set quality measure to be used in searching for networks.
* @param scoreType
*/
public void setScoreType(SelectedTag newScoreType) {
if (newScoreType.getTags() == TAGS_SCORE_TYPE) {
m_nScoreType = newScoreType.getSelectedTag().getID();
}
}
/**
* get quality measure to be used in searching for networks.
* @return quality measure
*/
public SelectedTag getScoreType() {
return new SelectedTag(m_nScoreType, TAGS_SCORE_TYPE);
}
/**
* Returns an enumeration describing the available options
*
* @return an enumeration of all the available options
*/
public Enumeration listOptions() {
Vector newVector = new Vector(1);
newVector.addElement(
new Option(
"\tScore type (BAYES, BDeu, MDL, ENTROPY and AIC)\n",
"S",
1,
"-S [BAYES|MDL|ENTROPY|AIC|CROSS_CLASSIC|CROSS_BAYES]"));
return newVector.elements();
} // listOptions
public void setOptions(String[] options) throws Exception {
String sScore = Utils.getOption('S', options);
if (sScore.compareTo("BAYES") == 0) {
setScoreType(new SelectedTag(Scoreable.BAYES, TAGS_SCORE_TYPE));
}
if (sScore.compareTo("BDeu") == 0) {
setScoreType(new SelectedTag(Scoreable.BDeu, TAGS_SCORE_TYPE));
}
if (sScore.compareTo("MDL") == 0) {
setScoreType(new SelectedTag(Scoreable.MDL, TAGS_SCORE_TYPE));
}
if (sScore.compareTo("ENTROPY") == 0) {
setScoreType(new SelectedTag(Scoreable.ENTROPY, TAGS_SCORE_TYPE));
}
if (sScore.compareTo("AIC") == 0) {
setScoreType(new SelectedTag(Scoreable.AIC, TAGS_SCORE_TYPE));
}
} // setOptions
public String[] getOptions() {
String[] options = new String[2];
int current = 0;
options[current++] = "-S";
switch (m_nScoreType) {
case (Scoreable.BAYES) :
options[current++] = "BAYES";
break;
case (Scoreable.BDeu) :
options[current++] = "BDeu";
break;
case (Scoreable.MDL) :
options[current++] = "MDL";
break;
case (Scoreable.ENTROPY) :
options[current++] = "ENTROPY";
break;
case (Scoreable.AIC) :
options[current++] = "AIC";
break;
}
// Fill up rest with empty strings, not nulls!
while (current < options.length) {
options[current++] = "";
}
return options;
} // getOptions
/**
* @return a string to describe the ScoreType option.
*/
public String scoreTypeTipText() {
return "The score type determines the measure used to judge the quality of a"
+ " network structure. It can be one of Bayes, BDeu, Minimum Description Length (MDL),"
+ " Akaike Information Criterion (AIC), and Entropy.";
}
} // class ScoreBasedSearchAlgorithm
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -