GeMSE.GS.Analysis.Stats.TwoSampleCovariancePanel.java Source code

Java tutorial

Introduction

Here is the source code for GeMSE.GS.Analysis.Stats.TwoSampleCovariancePanel.java

Source

/** GenoMetric Space Explorer (GeMSE) Copyright (C) 2017 Vahid Jalili
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software Foundation,
 *  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
 */
package GeMSE.GS.Analysis.Stats;

import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import org.apache.commons.math3.stat.correlation.Covariance;

/**
 *
 * @author Vahid Jalili
 */
public final class TwoSampleCovariancePanel extends javax.swing.JPanel {
    public TwoSampleCovariancePanel() {
        initComponents();

        _decFor = new DecimalFormat("#.#########");
        _decFor.setRoundingMode(RoundingMode.CEILING);
        DecimalFormatSymbols decFors = _decFor.getDecimalFormatSymbols();
        decFors.setNaN("NaN");
        decFors.setInfinity("");
        _decFor.setDecimalFormatSymbols(decFors);

        _biasCorrected = false;
        BiasCorrectedCB.setSelected(_biasCorrected);
    }

    private Boolean _biasCorrected;
    private double[] _xArray;
    private double[] _yArray;
    private final DecimalFormat _decFor;
    private Covariance _covariance;

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        BiasCorrectedCB = new javax.swing.JCheckBox();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        CovarianceL = new javax.swing.JLabel();

        BiasCorrectedCB.setText("bias corrected");
        BiasCorrectedCB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BiasCorrectedCBActionPerformed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Lucida Grande", 0, 14)); // NOI18N
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("Covariance between the two arrays");

        CovarianceL.setFont(new java.awt.Font("Courier New", 0, 24)); // NOI18N
        CovarianceL.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        CovarianceL.setText("NaN");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(jPanel1Layout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup().addContainerGap()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(CovarianceL, javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 336, Short.MAX_VALUE))
                        .addContainerGap()));
        jPanel1Layout.setVerticalGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup().addContainerGap().addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(CovarianceL, javax.swing.GroupLayout.DEFAULT_SIZE, 37, Short.MAX_VALUE)
                        .addContainerGap()));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup().addContainerGap().addComponent(BiasCorrectedCB)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 171, Short.MAX_VALUE)
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap()));
        layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup().addContainerGap().addGroup(layout
                        .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(BiasCorrectedCB).addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)));
    }// </editor-fold>//GEN-END:initComponents

    private void BiasCorrectedCBActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_BiasCorrectedCBActionPerformed
    {//GEN-HEADEREND:event_BiasCorrectedCBActionPerformed
        _biasCorrected = BiasCorrectedCB.isSelected();
        RunAnalysis();
    }//GEN-LAST:event_BiasCorrectedCBActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JCheckBox BiasCorrectedCB;
    private javax.swing.JLabel CovarianceL;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    // End of variables declaration//GEN-END:variables

    public void RunAnalysis(double[] xArray, double[] yArray) {
        if ((xArray == null || yArray == null) || (xArray.length != yArray.length)
                || (xArray.length < 3 || yArray.length < 3)) {
            CovarianceL.setText("NaN");
            return;
        }

        _xArray = xArray;
        _yArray = yArray;
        RunAnalysis();
    }

    private void RunAnalysis() {
        if (_xArray == null || _yArray == null) {
            CovarianceL.setText("NaN");
            return;
        }
        _covariance = new Covariance();
        CovarianceL
                .setText(String.valueOf(_decFor.format(_covariance.covariance(_xArray, _yArray, _biasCorrected))));
    }
}