Android Open Source - voc Linear Regression






From Project

Back to project page voc.

License

The source code is released under:

GNU General Public License

If you think the Android project voc listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package processing;
/**/*from   w ww .  ja  v a2 s.  co  m*/
 * Created with IntelliJ IDEA.
 * User: linyaoli
 * Date: 11/19/13
 * Time: 3:31 PM
 * Linear regression, find the line from sampling points.
 */

import java.util.Queue;

public class LinearRegression{

    private static int MAXN = 1000;
    private double[] x = new double[30];
    private double[] y = new double[30];
    private LinearParams returnedLinearParams = new LinearParams();

    /**
     * Constructor.
     */
    public class LinearParams {
        public double beta0 = 0;
        public double beta1 = 0;
    }
    /*
    /**
     *
     * @param qfCount30
     * @return

    public LinearParams getLinearParams(Queue<Float> qfCount30) {

//        return lineFit(qfCount30.toArray());
        return null;
    }
    */

    /**
     * Return the line parameters, coefficient (y=ax+b) for linear function.
     * @param arr
     * @return
     */
    public LinearParams lineFit (char[] arr) {

        // first pass: read in data, compute xbar and ybar
        int n = arr.length;
        for (int i=0;i<n;i++){
            x[i] = i;
            y[i] = (double)arr[i];
        }
        estimate(x, y, n );
        return returnedLinearParams;

    }


    /**
     * linear regression
     * y = a x + b
     * b = sum( y ) / n - a * sum( x ) / n
     * a = ( n * sum( xy ) - sum( x ) * sum( y ) ) / ( n * sum( x^2 ) - sum(x) ^ 2 )
     *
     */

    /**
     * prediction.
     * @param x
     * @param y
     * @param i
     * @return
     */
    private void estimate( double[] x , double[] y , int i ) {
        double a = getXc( x , y ) ;
        double b = getC( x , y , a ) ;
        returnedLinearParams.beta0 = a;
        returnedLinearParams.beta1 = b;
    }

    /**
     * get coeffecient of x
     * @param x
     * @param y
     * @return
     */
    public static double getXc( double[] x , double[] y ){
        int n = x.length ;
        return ( n * pSum( x , y ) - sum( x ) * sum( y ) )
                / ( n * sqSum( x ) - Math.pow(sum(x), 2) ) ;
    }
    /**
     * @param x
     * @param y
     * @param a
     * @return
     */
    public static double getC( double[] x , double[] y , double a ){
        int n = x.length ;
        return sum( y ) / n - a * sum( x ) / n ;
    }

    /**
     * constant
     * @param x
     * @param y
     * @return
     */
    public static double getC( double[] x , double[] y ){
        int n = x.length ;
        double a = getXc( x , y ) ;
        return sum( y ) / n - a * sum( x ) / n ;
    }

    /**
     *
     * @param ds
     * @return
     */
    private static double sum(double[] ds) {
        double s = 0 ;
        for( double d : ds ) s = s + d ;
        return s ;
    }

    /**
     *
     * @param ds
     * @return
     */
    private static double sqSum(double[] ds) {
        double s = 0 ;
        for( double d : ds ) s = s + Math.pow(d, 2) ;
        return s ;
    }

    /**
     *
     * @param x
     * @param y
     * @return
     */
    private static double pSum( double[] x , double[] y ) {
        double s = 0 ;
        for( int i = 0 ; i < x.length ; i++ ) s = s + x[i] * y[i] ;
        return s ;
    }
}




Java Source Code List

cleanup.DrawGraph.java
cleanup.Instructions.java
cleanup.PositionService.java
cleanup.QRcodeFromFile.java
cleanup.QRcodeParse.java
cleanup.SaveQRFile.java
display.DynamicPlotDataSource.java
display.DynamicPlotSeries.java
processing.LinearRegression.java
processing.SensorCurve.java
transmission.BluetoothChatService.java
transmission.LocalProfile.java
transmission.thirdlib.capture.CaptureActivityHandler.java
transmission.thirdlib.capture.CaptureActivity.java
transmission.thirdlib.capture.DecodeFormatManager.java
transmission.thirdlib.capture.DecodeHandler.java
transmission.thirdlib.capture.DecodeThread.java
transmission.thirdlib.capture.FinishListener.java
transmission.thirdlib.capture.InactivityTimer.java
transmission.thirdlib.capture.IntentSource.java
transmission.thirdlib.capture.Intents.java
transmission.thirdlib.capture.PlanarYUVLuminanceSource.java
transmission.thirdlib.capture.PreferencesActivity.java
transmission.thirdlib.capture.ViewfinderResultPointCallback.java
transmission.thirdlib.capture.ViewfinderView.java
transmission.thirdlib.manager.AutoFocusCallback.java
transmission.thirdlib.manager.CameraConfigurationManager.java
transmission.thirdlib.manager.CameraManager.java
transmission.thirdlib.manager.PreviewCallback.java
userinterface.BluetoothViewer.java
userinterface.ConfigActivity.java
userinterface.DeviceListActivity.java
userinterface.GuideActivity.java
userinterface.HistoryActivity.java
userinterface.SlidingDrawerActivity.java
userinterface.Welcome.java