@@ -0,0 +1,2494 @@
+package edu.neu.ccs.pyramid.classification.logistic_regression;%0A%0Aimport edu.neu.ccs.pyramid.dataset.ClfDataSet;%0Aimport org.apache.mahout.math.DenseVector;%0Aimport org.apache.mahout.math.Vector;%0A%0A/**%0A * Created by chengli on 11/27/14.%0A */%0Apublic class L2RFunction %7B%0A%0A    private final Vector C;%0A    private final Vector z;%0A    private final Vector D;%0A    /**%0A     * the first column should be all 1%0A     * the labels should be 1/-1%0A     */%0A    private final ClfDataSet dataSet;%0A%0A    public L2RFunction( ClfDataSet dataSet, Vector C ) %7B%0A        int l = dataSet.getNumDataPoints();%0A        this.dataSet = dataSet;%0A        z = new DenseVector(l);%0A        D = new DenseVector(l);%0A        this.C = C;%0A    %7D%0A%0A%0A    private void Xv(Vector v, Vector Xv) %7B%0A        for (int i = 0; i %3C dataSet.getNumDataPoints(); i++) %7B%0A            Xv.set(i, dataSet.getRow(i).dot(v));%0A        %7D%0A    %7D%0A%0A    private void XTv(Vector v, Vector XTv) %7B%0A        for (int i=0;i%3CdataSet.getNumFeatures();i++)%7B%0A            XTv.set(i,dataSet.getColumn(i).dot(v));%0A        %7D%0A    %7D%0A%0A%0A    public double fun(Vector w) %7B%0A        int i;%0A        double f = 0;%0A        int%5B%5D y = dataSet.getLabels();%0A        int l = dataSet.getNumDataPoints();%0A        Xv(w, z);%0A        f += w.dot(w);%0A        f /= 2.0;%0A        for (i = 0; i %3C l; i++) %7B%0A            double yz = y%5Bi%5D * z.get(i);%0A            if (yz %3E= 0)%0A                f += C.get(i) * Math.log(1 + Math.exp(-yz));%0A            else%0A                f += C.get(i) * (-yz + Math.log(1 + Math.exp(yz)));%0A        %7D%0A%0A        return (f);%0A    %7D%0A%0A    public void grad(Vector w, Vector g) %7B%0A%0A        int%5B%5D y = dataSet.getLabels();%0A        int l = dataSet.getNumDataPoints();%0A        for (int i = 0; i %3C l; i++) %7B%0A            z.set(i, 1 / (1 + Math.exp(-y%5Bi%5D * z.get(i))));%0A            D.set(i,z.get(i) * (1 - z.get(i)));%0A            z.set(i,C.get(i) * (z.get(i) - 1) * y%5Bi%5D);%0A        %7D%0A        XTv(z, g);%0A%0A        for (int i=0;i%3Cg.size();i++)%7B%0A            g.set(i,w.get(i)+g.get(i));%0A        %7D%0A    %7D%0A%0A    public void Hv(Vector s, Vector Hs) %7B%0A%0A        int l = dataSet.getNumDataPoints();%0A        int w_size = get_nr_variable();%0A        Vector wa = new DenseVector(l);%0A%0A        Xv(s, wa);%0A        for (int i = 0; i %3C l; i++)%0A            wa.set(i, C.get(i) * D.get(i) * wa.get(i));%0A%0A        XTv(wa, Hs);%0A        for (int i = 0; i %3C w_size; i++)%0A            Hs.set(i,s.get(i) + Hs.get(i));%0A        // delete%5B%5D wa;%0A    %7D%0A%0A    public int get_nr_variable() %7B%0A        return dataSet.getNumFeatures();%0A    %7D%0A%7D%0A

add class L2RFunction