Search in sources :

Example 91 with KahanObject

use of org.apache.sysml.runtime.instructions.cp.KahanObject in project systemml by apache.

the class CM method execute.

/**
 * Special case for weights w2==1
 */
@Override
public Data execute(Data in1, double in2) {
    CM_COV_Object cm1 = (CM_COV_Object) in1;
    if (cm1.isCMAllZeros()) {
        cm1.w = 1;
        cm1.mean.set(in2, 0);
        cm1.m2.set(0, 0);
        cm1.m3.set(0, 0);
        cm1.m4.set(0, 0);
        return cm1;
    }
    switch(_type) {
        case COUNT:
            {
                cm1.w = cm1.w + 1;
                break;
            }
        case MEAN:
            {
                double w = cm1.w + 1;
                double d = in2 - cm1.mean._sum;
                cm1.mean = (KahanObject) _plus.execute(cm1.mean, d / w);
                cm1.w = w;
                break;
            }
        case CM2:
            {
                double w = cm1.w + 1;
                double d = in2 - cm1.mean._sum;
                cm1.mean = (KahanObject) _plus.execute(cm1.mean, d / w);
                double t1 = cm1.w / w * d;
                double lt1 = t1 * d;
                _buff2.set(cm1.m2);
                _buff2 = (KahanObject) _plus.execute(_buff2, lt1);
                cm1.m2.set(_buff2);
                cm1.w = w;
                break;
            }
        case CM3:
            {
                double w = cm1.w + 1;
                double d = in2 - cm1.mean._sum;
                cm1.mean = (KahanObject) _plus.execute(cm1.mean, d / w);
                double t1 = cm1.w / w * d;
                double t2 = -1 / cm1.w;
                double lt1 = t1 * d;
                double lt2 = Math.pow(t1, 3) * (1.0 - Math.pow(t2, 2));
                double f2 = 1.0 / w;
                _buff2.set(cm1.m2);
                _buff2 = (KahanObject) _plus.execute(_buff2, lt1);
                _buff3.set(cm1.m3);
                _buff3 = (KahanObject) _plus.execute(_buff3, lt2 - 3 * cm1.m2._sum * f2 * d);
                cm1.m2.set(_buff2);
                cm1.m3.set(_buff3);
                cm1.w = w;
                break;
            }
        case CM4:
            {
                double w = cm1.w + 1;
                double d = in2 - cm1.mean._sum;
                cm1.mean = (KahanObject) _plus.execute(cm1.mean, d / w);
                double t1 = cm1.w / w * d;
                double t2 = -1 / cm1.w;
                double lt1 = t1 * d;
                double lt2 = Math.pow(t1, 3) * (1.0 - Math.pow(t2, 2));
                double lt3 = Math.pow(t1, 4) * (1.0 - Math.pow(t2, 3));
                double f2 = 1.0 / w;
                _buff2.set(cm1.m2);
                _buff2 = (KahanObject) _plus.execute(_buff2, lt1);
                _buff3.set(cm1.m3);
                _buff3 = (KahanObject) _plus.execute(_buff3, lt2 - 3 * cm1.m2._sum * f2 * d);
                cm1.m4 = (KahanObject) _plus.execute(cm1.m4, 6 * cm1.m2._sum * Math.pow(-f2 * d, 2) + lt3 - 4 * cm1.m3._sum * f2 * d);
                cm1.m2.set(_buff2);
                cm1.m3.set(_buff3);
                cm1.w = w;
                break;
            }
        case VARIANCE:
            {
                double w = cm1.w + 1;
                double d = in2 - cm1.mean._sum;
                cm1.mean = (KahanObject) _plus.execute(cm1.mean, d / w);
                double t1 = cm1.w / w * d;
                double lt1 = t1 * d;
                cm1.m2 = (KahanObject) _plus.execute(cm1.m2, lt1);
                cm1.w = w;
                break;
            }
        default:
            throw new DMLRuntimeException("Unsupported operation type: " + _type);
    }
    return cm1;
}
Also used : CM_COV_Object(org.apache.sysml.runtime.instructions.cp.CM_COV_Object) KahanObject(org.apache.sysml.runtime.instructions.cp.KahanObject) DMLRuntimeException(org.apache.sysml.runtime.DMLRuntimeException)

Example 92 with KahanObject

use of org.apache.sysml.runtime.instructions.cp.KahanObject in project systemml by apache.

the class CM method execute.

/**
 * Combining stats from two partitions of the data.
 */
@Override
public Data execute(Data in1, Data in2) {
    CM_COV_Object cm1 = (CM_COV_Object) in1;
    CM_COV_Object cm2 = (CM_COV_Object) in2;
    if (cm1.isCMAllZeros()) {
        cm1.w = cm2.w;
        cm1.mean.set(cm2.mean);
        cm1.m2.set(cm2.m2);
        cm1.m3.set(cm2.m3);
        cm1.m4.set(cm2.m4);
        return cm1;
    }
    if (cm2.isCMAllZeros())
        return cm1;
    switch(_type) {
        case COUNT:
            {
                cm1.w = Math.round(cm1.w + cm2.w);
                break;
            }
        case MEAN:
            {
                double w = cm1.w + cm2.w;
                double d = cm2.mean._sum - cm1.mean._sum;
                cm1.mean = (KahanObject) _plus.execute(cm1.mean, cm2.w * d / w);
                cm1.w = w;
                break;
            }
        case CM2:
            {
                double w = cm1.w + cm2.w;
                double d = cm2.mean._sum - cm1.mean._sum;
                cm1.mean = (KahanObject) _plus.execute(cm1.mean, cm2.w * d / w);
                double t1 = cm1.w * cm2.w / w * d;
                double lt1 = t1 * d;
                _buff2.set(cm1.m2);
                _buff2 = (KahanObject) _plus.execute(_buff2, cm2.m2._sum, cm2.m2._correction);
                _buff2 = (KahanObject) _plus.execute(_buff2, lt1);
                cm1.m2.set(_buff2);
                cm1.w = w;
                break;
            }
        case CM3:
            {
                double w = cm1.w + cm2.w;
                double d = cm2.mean._sum - cm1.mean._sum;
                cm1.mean = (KahanObject) _plus.execute(cm1.mean, cm2.w * d / w);
                double t1 = cm1.w * cm2.w / w * d;
                double t2 = -1 / cm1.w;
                double lt1 = t1 * d;
                double lt2 = Math.pow(t1, 3) * (1 / Math.pow(cm2.w, 2) - Math.pow(t2, 2));
                double f1 = cm1.w / w;
                double f2 = cm2.w / w;
                _buff2.set(cm1.m2);
                _buff2 = (KahanObject) _plus.execute(_buff2, cm2.m2._sum, cm2.m2._correction);
                _buff2 = (KahanObject) _plus.execute(_buff2, lt1);
                _buff3.set(cm1.m3);
                _buff3 = (KahanObject) _plus.execute(_buff3, cm2.m3._sum, cm2.m3._correction);
                _buff3 = (KahanObject) _plus.execute(_buff3, 3 * (-f2 * cm1.m2._sum + f1 * cm2.m2._sum) * d + lt2);
                cm1.m2.set(_buff2);
                cm1.m3.set(_buff3);
                cm1.w = w;
                break;
            }
        case CM4:
            {
                double w = cm1.w + cm2.w;
                double d = cm2.mean._sum - cm1.mean._sum;
                cm1.mean = (KahanObject) _plus.execute(cm1.mean, cm2.w * d / w);
                double t1 = cm1.w * cm2.w / w * d;
                double t2 = -1 / cm1.w;
                double lt1 = t1 * d;
                double lt2 = Math.pow(t1, 3) * (1 / Math.pow(cm2.w, 2) - Math.pow(t2, 2));
                double lt3 = Math.pow(t1, 4) * (1 / Math.pow(cm2.w, 3) - Math.pow(t2, 3));
                double f1 = cm1.w / w;
                double f2 = cm2.w / w;
                _buff2.set(cm1.m2);
                _buff2 = (KahanObject) _plus.execute(_buff2, cm2.m2._sum, cm2.m2._correction);
                _buff2 = (KahanObject) _plus.execute(_buff2, lt1);
                _buff3.set(cm1.m3);
                _buff3 = (KahanObject) _plus.execute(_buff3, cm2.m3._sum, cm2.m3._correction);
                _buff3 = (KahanObject) _plus.execute(_buff3, 3 * (-f2 * cm1.m2._sum + f1 * cm2.m2._sum) * d + lt2);
                cm1.m4 = (KahanObject) _plus.execute(cm1.m4, cm2.m4._sum, cm2.m4._correction);
                cm1.m4 = (KahanObject) _plus.execute(cm1.m4, 4 * (-f2 * cm1.m3._sum + f1 * cm2.m3._sum) * d + 6 * (Math.pow(-f2, 2) * cm1.m2._sum + Math.pow(f1, 2) * cm2.m2._sum) * Math.pow(d, 2) + lt3);
                cm1.m2.set(_buff2);
                cm1.m3.set(_buff3);
                cm1.w = w;
                break;
            }
        case VARIANCE:
            {
                double w = cm1.w + cm2.w;
                double d = cm2.mean._sum - cm1.mean._sum;
                cm1.mean = (KahanObject) _plus.execute(cm1.mean, cm2.w * d / w);
                double t1 = cm1.w * cm2.w / w * d;
                double lt1 = t1 * d;
                cm1.m2 = (KahanObject) _plus.execute(cm1.m2, cm2.m2._sum, cm2.m2._correction);
                cm1.m2 = (KahanObject) _plus.execute(cm1.m2, lt1);
                cm1.w = w;
                break;
            }
        default:
            throw new DMLRuntimeException("Unsupported operation type: " + _type);
    }
    return cm1;
}
Also used : CM_COV_Object(org.apache.sysml.runtime.instructions.cp.CM_COV_Object) KahanObject(org.apache.sysml.runtime.instructions.cp.KahanObject) DMLRuntimeException(org.apache.sysml.runtime.DMLRuntimeException)

Example 93 with KahanObject

use of org.apache.sysml.runtime.instructions.cp.KahanObject in project systemml by apache.

the class KahanPlus method execute.

// in1, in2 is the sum, in3 is the correction
@Override
public Data execute(Data in1, double in2, double in3) {
    KahanObject kahanObj = (KahanObject) in1;
    // (computing corrections otherwise incorrectly computes NaN)
    if (Double.isInfinite(kahanObj._sum) || Double.isInfinite(in2)) {
        kahanObj.set(Double.isInfinite(in2) ? in2 : kahanObj._sum, 0);
        return kahanObj;
    }
    // default path for any other value
    double correction = in2 + (kahanObj._correction + in3);
    double sum = kahanObj._sum + correction;
    // prevent eager JIT opt
    kahanObj.set(sum, correction - (sum - kahanObj._sum));
    return kahanObj;
}
Also used : KahanObject(org.apache.sysml.runtime.instructions.cp.KahanObject)

Example 94 with KahanObject

use of org.apache.sysml.runtime.instructions.cp.KahanObject in project systemml by apache.

the class Mean method execute.

@Override
public Data execute(Data in1, double in2, double count) {
    KahanObject kahanObj = (KahanObject) in1;
    double delta = (in2 - kahanObj._sum) / count;
    _plus.execute(in1, delta);
    return kahanObj;
}
Also used : KahanObject(org.apache.sysml.runtime.instructions.cp.KahanObject)

Example 95 with KahanObject

use of org.apache.sysml.runtime.instructions.cp.KahanObject in project systemml by apache.

the class EncoderMVImpute method parseMethodsAndReplacments.

private void parseMethodsAndReplacments(JSONObject parsedSpec) throws JSONException {
    JSONArray mvspec = (JSONArray) parsedSpec.get(TfUtils.TXMETHOD_IMPUTE);
    _mvMethodList = new MVMethod[mvspec.size()];
    _replacementList = new String[mvspec.size()];
    _meanList = new KahanObject[mvspec.size()];
    _countList = new long[mvspec.size()];
    for (int i = 0; i < mvspec.size(); i++) {
        JSONObject mvobj = (JSONObject) mvspec.get(i);
        _mvMethodList[i] = MVMethod.valueOf(mvobj.get("method").toString().toUpperCase());
        if (_mvMethodList[i] == MVMethod.CONSTANT) {
            _replacementList[i] = mvobj.getString("value").toString();
        }
        _meanList[i] = new KahanObject(0, 0);
    }
}
Also used : JSONObject(org.apache.wink.json4j.JSONObject) JSONArray(org.apache.wink.json4j.JSONArray) KahanObject(org.apache.sysml.runtime.instructions.cp.KahanObject)

Aggregations

KahanObject (org.apache.sysml.runtime.instructions.cp.KahanObject)115 KahanPlus (org.apache.sysml.runtime.functionobjects.KahanPlus)49 DMLRuntimeException (org.apache.sysml.runtime.DMLRuntimeException)28 KahanFunction (org.apache.sysml.runtime.functionobjects.KahanFunction)28 CM_COV_Object (org.apache.sysml.runtime.instructions.cp.CM_COV_Object)15 CM (org.apache.sysml.runtime.functionobjects.CM)14 Builtin (org.apache.sysml.runtime.functionobjects.Builtin)12 ReduceAll (org.apache.sysml.runtime.functionobjects.ReduceAll)10 DenseBlock (org.apache.sysml.runtime.matrix.data.DenseBlock)10 CMOperator (org.apache.sysml.runtime.matrix.operators.CMOperator)10 IOException (java.io.IOException)8 WeightedCell (org.apache.sysml.runtime.matrix.data.WeightedCell)8 AggregateOperator (org.apache.sysml.runtime.matrix.operators.AggregateOperator)8 KahanPlusSq (org.apache.sysml.runtime.functionobjects.KahanPlusSq)6 ReduceCol (org.apache.sysml.runtime.functionobjects.ReduceCol)6 ValueFunction (org.apache.sysml.runtime.functionobjects.ValueFunction)6 IJV (org.apache.sysml.runtime.matrix.data.IJV)6 ArrayList (java.util.ArrayList)4 ExecutorService (java.util.concurrent.ExecutorService)4 Future (java.util.concurrent.Future)4