use of org.apache.commons.math3.distribution.ChiSquaredDistribution in project tetrad by cmu-phil.
the class MimbuildTrek method getCov.
private TetradMatrix getCov(ICovarianceMatrix _measurescov, List<Node> latents, Node[][] indicators) {
if (latents.size() != indicators.length) {
throw new IllegalArgumentException();
}
TetradMatrix measurescov = _measurescov.getMatrix();
TetradMatrix latentscov = new TetradMatrix(latents.size(), latents.size());
for (int i = 0; i < latentscov.rows(); i++) {
for (int j = i; j < latentscov.columns(); j++) {
if (i == j)
latentscov.set(i, j, 1.0);
else {
double v = .5;
latentscov.set(i, j, v);
latentscov.set(j, i, v);
}
}
}
double[][] loadings = new double[indicators.length][];
for (int i = 0; i < indicators.length; i++) {
loadings[i] = new double[indicators[i].length];
}
for (int i = 0; i < indicators.length; i++) {
loadings[i] = new double[indicators[i].length];
for (int j = 0; j < indicators[i].length; j++) {
loadings[i][j] = .5;
}
}
int[][] indicatorIndices = new int[indicators.length][];
List<Node> measures = _measurescov.getVariables();
for (int i = 0; i < indicators.length; i++) {
indicatorIndices[i] = new int[indicators[i].length];
for (int j = 0; j < indicators[i].length; j++) {
indicatorIndices[i][j] = measures.indexOf(indicators[i][j]);
}
}
// Variances of the measures.
double[] delta = new double[measurescov.rows()];
for (int i = 0; i < delta.length; i++) {
delta[i] = 1;
}
int numNonMeasureVarianceParams = 0;
for (int i = 0; i < latentscov.rows(); i++) {
for (int j = i; j < latentscov.columns(); j++) {
numNonMeasureVarianceParams++;
}
}
for (int i = 0; i < indicators.length; i++) {
numNonMeasureVarianceParams += indicators[i].length;
}
double[] allParams1 = getAllParams(indicators, latentscov, loadings, delta);
optimizeNonMeasureVariancesQuick(indicators, measurescov, latentscov, loadings, indicatorIndices);
// for (int i = 0; i < 10; i++) {
// optimizeNonMeasureVariancesConditionally(indicators, measurescov, latentscov, loadings, indicatorIndices, delta);
// optimizeMeasureVariancesConditionally(measurescov, latentscov, loadings, indicatorIndices, delta);
//
// double[] allParams2 = getAllParams(indicators, latentscov, loadings, delta);
// if (distance(allParams1, allParams2) < epsilon) break;
// allParams1 = allParams2;
// }
this.numParams = allParams1.length;
// // Very slow but could be done alone.
optimizeAllParamsSimultaneously(indicators, measurescov, latentscov, loadings, indicatorIndices, delta);
double N = _measurescov.getSampleSize();
int p = _measurescov.getDimension();
int df = (p) * (p + 1) / 2 - (numParams);
double x = (N - 1) * minimum;
this.pValue = 1.0 - new ChiSquaredDistribution(df).cumulativeProbability(x);
return latentscov;
}
use of org.apache.commons.math3.distribution.ChiSquaredDistribution in project tetrad by cmu-phil.
the class SemIm method getPValue.
/**
* @return the p-value for the model.
*/
public double getPValue() {
double chiSquare = getChiSquare();
int dof = semPm.getDof();
if (dof <= 0) {
return Double.NaN;
} else if (chiSquare < 0) {
return Double.NaN;
} else {
return 1.0 - new ChiSquaredDistribution(dof).cumulativeProbability(chiSquare);
}
}
use of org.apache.commons.math3.distribution.ChiSquaredDistribution in project tetrad by cmu-phil.
the class TestStatUtils method testChiSqCdf.
@Test
public void testChiSqCdf() {
ChiSquaredDistribution dist = new ChiSquaredDistribution(1);
double log = Math.log(1000);
double p = 1.0 - dist.cumulativeProbability(log);
assertEquals(0.008, p, 0.001);
}
use of org.apache.commons.math3.distribution.ChiSquaredDistribution in project tetrad by cmu-phil.
the class IndTestMNLRLRT method isIndependent.
/**
* @return true if the given independence question is judged true, false if not. The independence question is of the
* form x _||_ y | z, z = <z1,...,zn>, where x, y, z1,...,zn are searchVariables in the list returned by
* getVariableNames().
*/
public boolean isIndependent(Node x, Node y, List<Node> z) {
int _x = nodesHash.get(x);
int _y = nodesHash.get(y);
int[] list0 = new int[z.size() + 1];
int[] list1 = new int[z.size() + 1];
int[] list2 = new int[z.size()];
list0[0] = _x;
list1[0] = _y;
for (int i = 0; i < z.size(); i++) {
int _z = nodesHash.get(z.get(i));
list0[i + 1] = _z;
list1[i + 1] = _z;
list2[i] = _z;
}
double lik_0;
double dof_0;
double lik_1;
double dof_1;
lik_0 = likelihood.getLik(_y, list0) - likelihood.getLik(_y, list2);
dof_0 = likelihood.getLik(_y, list0) - likelihood.getLik(_y, list2);
lik_1 = likelihood.getLik(_x, list1) - likelihood.getLik(_x, list2);
dof_1 = likelihood.getLik(_x, list1) - likelihood.getLik(_x, list2);
if (dof_0 <= 0) {
dof_0 = 1;
}
if (dof_1 <= 0) {
dof_1 = 1;
}
double p_0 = 0;
double p_1 = 0;
try {
p_0 = 1.0 - new ChiSquaredDistribution(dof_0).cumulativeProbability(2.0 * lik_0);
} catch (Exception e) {
e.printStackTrace();
}
try {
p_1 = 1.0 - new ChiSquaredDistribution(dof_1).cumulativeProbability(2.0 * lik_1);
} catch (Exception e) {
e.printStackTrace();
}
this.pValue = Math.min(p_0, p_1);
return this.pValue > alpha;
}
use of org.apache.commons.math3.distribution.ChiSquaredDistribution in project tetrad by cmu-phil.
the class IndTestMixedMultipleTTest method dependencePvalsLogit.
private double[] dependencePvalsLogit(Node x, Node y, List<Node> z) {
if (!variablesPerNode.containsKey(x)) {
throw new IllegalArgumentException("Unrecogized node: " + x);
}
if (!variablesPerNode.containsKey(y)) {
throw new IllegalArgumentException("Unrecogized node: " + y);
}
for (Node node : z) {
if (!variablesPerNode.containsKey(node)) {
throw new IllegalArgumentException("Unrecogized node: " + node);
}
}
List<Double> pValues = new ArrayList<>();
int[] _rows = getNonMissingRows(x, y, z);
logisticRegression.setRows(_rows);
List<Node> yzDumList = new ArrayList<>();
List<Node> yzList = new ArrayList<>();
yzList.add(y);
yzList.addAll(z);
// List<Node> zList = new ArrayList<>();
yzDumList.addAll(variablesPerNode.get(y));
for (Node _z : z) {
yzDumList.addAll(variablesPerNode.get(_z));
// zList.addAll(variablesPerNode.get(_z));
}
// double[][] coeffsDep = new double[variablesPerNode.get(x).size()][];
// DoubleMatrix2D coeffsNull = DoubleFactory2D.dense.make(zList.size(), variablesPerNode.get(x).size());
// DoubleMatrix2D coeffsDep = DoubleFactory2D.dense.make(yzDumList.size()+1, variablesPerNode.get(x).size());
double[] sumLnP = new double[yzList.size()];
for (int i = 0; i < sumLnP.length; i++) sumLnP[i] = 0.0;
for (int i = 0; i < variablesPerNode.get(x).size(); i++) {
Node _x = variablesPerNode.get(x).get(i);
LogisticRegression.Result result1 = logisticRegression.regress((DiscreteVariable) _x, yzDumList);
int n = originalData.getNumRows();
int k = yzDumList.size();
// skip intercept at index 0
int coefIndex = 1;
for (int j = 0; j < yzList.size(); j++) {
for (int dum = 0; dum < variablesPerNode.get(yzList.get(j)).size(); dum++) {
double wald = Math.abs(result1.getCoefs()[coefIndex] / result1.getStdErrs()[coefIndex]);
// double val = (1.0 - new NormalDistribution(0,1).cumulativeProbability(wald))*2;//two-tailed test
// double val = 1-result1.getProbs()[i+1];
// this is exactly the same test as the linear case
double val = (1.0 - ProbUtils.tCdf(wald, n - k)) * 2;
// System.out.println(_x.getName() + "\t" + yzDumList.get(coefIndex-1).getName() + "\t" + val + "\t" + (n-k));
// if(val <= 0) System.out.println("Zero p-val t-test: p " + val + " stat " + wald + " k " + k + " n " + n);
sumLnP[j] += Math.log(val);
coefIndex++;
}
}
}
double[] pVec = new double[sumLnP.length];
for (int i = 0; i < pVec.length; i++) {
if (sumLnP[i] == Double.NEGATIVE_INFINITY)
pVec[i] = 0.0;
else {
int df = 2 * variablesPerNode.get(x).size() * variablesPerNode.get(yzList.get(i)).size();
pVec[i] = 1.0 - new ChiSquaredDistribution(df).cumulativeProbability(-2 * sumLnP[i]);
}
}
return pVec;
}
Aggregations