Java tutorial
/** 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.inference.TTest; /** * * @author Vahid Jalili */ public final class OneSampleTTestPanel extends javax.swing.JPanel { public OneSampleTTestPanel() { initComponents(); _sample = new double[0]; _studentTest = new TTest(); _decFor = new DecimalFormat("#.#########"); _decFor.setRoundingMode(RoundingMode.CEILING); DecimalFormatSymbols decFors = _decFor.getDecimalFormatSymbols(); decFors.setNaN("NaN"); decFors.setInfinity(""); _decFor.setDecimalFormatSymbols(decFors); } private double[] _sample; private final TTest _studentTest; private final DecimalFormat _decFor; /** * 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() { jPanel14 = new javax.swing.JPanel(); DFL13 = new javax.swing.JLabel(); TStatisticL3 = new javax.swing.JLabel(); jLabel5 = new javax.swing.JLabel(); AlphaTF = new javax.swing.JTextField(); jScrollPane7 = new javax.swing.JScrollPane(); jTextPane7 = new javax.swing.JTextPane(); jPanel15 = new javax.swing.JPanel(); DFL14 = new javax.swing.JLabel(); TStatisticL4 = new javax.swing.JLabel(); AlphaTF1 = new javax.swing.JTextField(); jLabel7 = new javax.swing.JLabel(); jScrollPane8 = new javax.swing.JScrollPane(); jTextPane8 = new javax.swing.JTextPane(); jPanel16 = new javax.swing.JPanel(); DFL15 = new javax.swing.JLabel(); TStatisticL5 = new javax.swing.JLabel(); AlphaTF2 = new javax.swing.JTextField(); jLabel8 = new javax.swing.JLabel(); jScrollPane9 = new javax.swing.JScrollPane(); jTextPane9 = new javax.swing.JTextPane(); jLabel9 = new javax.swing.JLabel(); AlphaTF3 = new javax.swing.JTextField(); jPanel14.setBorder(javax.swing.BorderFactory.createEtchedBorder()); jPanel14.setPreferredSize(new java.awt.Dimension(450, 80)); jPanel14.setSize(new java.awt.Dimension(450, 80)); DFL13.setFont(new java.awt.Font("Lucida Grande", 0, 18)); // NOI18N DFL13.setHorizontalAlignment(javax.swing.SwingConstants.CENTER); DFL13.setText("t-Statistic"); TStatisticL3.setFont(new java.awt.Font("Courier New", 0, 24)); // NOI18N TStatisticL3.setHorizontalAlignment(javax.swing.SwingConstants.CENTER); TStatisticL3.setText("NaN"); jLabel5.setFont(new java.awt.Font("Courier New", 0, 30)); // NOI18N jLabel5.setHorizontalAlignment(javax.swing.SwingConstants.CENTER); jLabel5.setText(""); AlphaTF.setFont(new java.awt.Font("Courier New", 0, 18)); // NOI18N AlphaTF.setHorizontalAlignment(javax.swing.JTextField.CENTER); AlphaTF.setText("0.05"); AlphaTF.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { AlphaTFActionPerformed(evt); } }); jTextPane7.setText( "Computes a t statistic given observed values and a comparison constant. This statistic can be used to perform a one sample t-test for the mean."); jScrollPane7.setViewportView(jTextPane7); javax.swing.GroupLayout jPanel14Layout = new javax.swing.GroupLayout(jPanel14); jPanel14.setLayout(jPanel14Layout); jPanel14Layout.setHorizontalGroup(jPanel14Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel14Layout.createSequentialGroup().addContainerGap() .addGroup(jPanel14Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(DFL13, javax.swing.GroupLayout.PREFERRED_SIZE, 250, javax.swing.GroupLayout.PREFERRED_SIZE) .addGroup(jPanel14Layout.createSequentialGroup() .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(AlphaTF, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)) .addComponent(TStatisticL3, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 250, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jScrollPane7, javax.swing.GroupLayout.DEFAULT_SIZE, 368, Short.MAX_VALUE) .addContainerGap())); jPanel14Layout.setVerticalGroup(jPanel14Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel14Layout.createSequentialGroup().addContainerGap().addGroup(jPanel14Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addGroup(jPanel14Layout.createSequentialGroup().addComponent(DFL13) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(jPanel14Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel14Layout.createSequentialGroup() .addComponent(jLabel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addGap(9, 9, 9)) .addComponent(AlphaTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addComponent(TStatisticL3, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(5, 5, 5)) .addComponent(jScrollPane7)).addContainerGap())); jPanel15.setBorder(javax.swing.BorderFactory.createEtchedBorder()); jPanel15.setPreferredSize(new java.awt.Dimension(450, 80)); jPanel15.setSize(new java.awt.Dimension(450, 80)); DFL14.setFont(new java.awt.Font("Lucida Grande", 0, 18)); // NOI18N DFL14.setHorizontalAlignment(javax.swing.SwingConstants.CENTER); DFL14.setText("t-Test"); TStatisticL4.setFont(new java.awt.Font("Courier New", 0, 24)); // NOI18N TStatisticL4.setHorizontalAlignment(javax.swing.SwingConstants.CENTER); TStatisticL4.setText("NaN"); AlphaTF1.setFont(new java.awt.Font("Courier New", 0, 18)); // NOI18N AlphaTF1.setHorizontalAlignment(javax.swing.JTextField.CENTER); AlphaTF1.setText("0.05"); AlphaTF1.setSize(new java.awt.Dimension(80, 26)); AlphaTF1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { AlphaTF1ActionPerformed(evt); } }); jLabel7.setFont(new java.awt.Font("Courier New", 0, 30)); // NOI18N jLabel7.setHorizontalAlignment(javax.swing.SwingConstants.CENTER); jLabel7.setText(""); jTextPane8.setText( "Returns the observed significance level, or p-value, associated with a one-sample, two-tailed t-test comparing the mean of the input array with the constant mu."); jScrollPane8.setViewportView(jTextPane8); javax.swing.GroupLayout jPanel15Layout = new javax.swing.GroupLayout(jPanel15); jPanel15.setLayout(jPanel15Layout); jPanel15Layout.setHorizontalGroup(jPanel15Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel15Layout.createSequentialGroup().addContainerGap() .addGroup(jPanel15Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(DFL14, javax.swing.GroupLayout.PREFERRED_SIZE, 250, javax.swing.GroupLayout.PREFERRED_SIZE) .addGroup(jPanel15Layout.createSequentialGroup() .addComponent(jLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(AlphaTF1, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)) .addComponent(TStatisticL4, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 250, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jScrollPane8, javax.swing.GroupLayout.DEFAULT_SIZE, 368, Short.MAX_VALUE) .addContainerGap())); jPanel15Layout.setVerticalGroup(jPanel15Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel15Layout.createSequentialGroup().addContainerGap() .addGroup(jPanel15Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jScrollPane8) .addGroup(jPanel15Layout.createSequentialGroup().addComponent(DFL14) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(jPanel15Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel7).addComponent(AlphaTF1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(TStatisticL4).addGap(0, 4, Short.MAX_VALUE))) .addContainerGap())); jPanel16.setBorder(javax.swing.BorderFactory.createEtchedBorder()); jPanel16.setPreferredSize(new java.awt.Dimension(450, 80)); jPanel16.setSize(new java.awt.Dimension(450, 80)); DFL15.setFont(new java.awt.Font("Lucida Grande", 0, 18)); // NOI18N DFL15.setHorizontalAlignment(javax.swing.SwingConstants.CENTER); DFL15.setText("t-Test"); TStatisticL5.setFont(new java.awt.Font("Courier New", 0, 24)); // NOI18N TStatisticL5.setHorizontalAlignment(javax.swing.SwingConstants.CENTER); TStatisticL5.setText("NaN"); AlphaTF2.setFont(new java.awt.Font("Courier New", 0, 18)); // NOI18N AlphaTF2.setHorizontalAlignment(javax.swing.JTextField.CENTER); AlphaTF2.setText("0.05"); AlphaTF2.setSize(new java.awt.Dimension(80, 26)); AlphaTF2.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { AlphaTF2ActionPerformed(evt); } }); jLabel8.setFont(new java.awt.Font("Courier New", 0, 30)); // NOI18N jLabel8.setHorizontalAlignment(javax.swing.SwingConstants.CENTER); jLabel8.setText(""); jTextPane9.setText( "Performs a two-sided t-test evaluating the null hypothesis that the mean of the population from which sample is drawn equals mu.\nReturns true iff the null hypothesis can be rejected with confidence 1 - alpha. To perform a 1-sided test, use alpha * 2"); jScrollPane9.setViewportView(jTextPane9); jLabel9.setFont(new java.awt.Font("Courier New", 0, 30)); // NOI18N jLabel9.setHorizontalAlignment(javax.swing.SwingConstants.CENTER); jLabel9.setText(""); AlphaTF3.setFont(new java.awt.Font("Courier New", 0, 18)); // NOI18N AlphaTF3.setHorizontalAlignment(javax.swing.JTextField.CENTER); AlphaTF3.setText("0.05"); AlphaTF3.setSize(new java.awt.Dimension(80, 26)); AlphaTF3.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { AlphaTF3ActionPerformed(evt); } }); javax.swing.GroupLayout jPanel16Layout = new javax.swing.GroupLayout(jPanel16); jPanel16.setLayout(jPanel16Layout); jPanel16Layout .setHorizontalGroup(jPanel16Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel16Layout.createSequentialGroup().addContainerGap() .addGroup(jPanel16Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(DFL15, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addGroup(jPanel16Layout.createSequentialGroup() .addComponent(jLabel9, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(AlphaTF3, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(32, 32, 32) .addComponent(jLabel8, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(AlphaTF2, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE)) .addComponent(TStatisticL5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jScrollPane9, javax.swing.GroupLayout.DEFAULT_SIZE, 373, Short.MAX_VALUE) .addContainerGap())); jPanel16Layout.setVerticalGroup(jPanel16Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel16Layout.createSequentialGroup().addContainerGap().addGroup(jPanel16Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup( jPanel16Layout.createSequentialGroup().addComponent(jScrollPane9).addContainerGap()) .addGroup(jPanel16Layout.createSequentialGroup().addComponent(DFL15) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(jPanel16Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel9, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(AlphaTF3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(AlphaTF2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel8)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(TStatisticL5).addGap(11, 11, 11))))); 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().addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jPanel16, javax.swing.GroupLayout.DEFAULT_SIZE, 645, Short.MAX_VALUE) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jPanel14, javax.swing.GroupLayout.PREFERRED_SIZE, 640, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jPanel15, javax.swing.GroupLayout.PREFERRED_SIZE, 640, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(0, 0, Short.MAX_VALUE))) .addContainerGap())); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addContainerGap() .addComponent(jPanel14, javax.swing.GroupLayout.PREFERRED_SIZE, 128, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jPanel15, javax.swing.GroupLayout.PREFERRED_SIZE, 122, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(jPanel16, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap())); }// </editor-fold>//GEN-END:initComponents private void AlphaTFActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_AlphaTFActionPerformed {//GEN-HEADEREND:event_AlphaTFActionPerformed RunTStatistic(); }//GEN-LAST:event_AlphaTFActionPerformed private void AlphaTF1ActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_AlphaTF1ActionPerformed {//GEN-HEADEREND:event_AlphaTF1ActionPerformed RunTTest(); }//GEN-LAST:event_AlphaTF1ActionPerformed private void AlphaTF3ActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_AlphaTF3ActionPerformed {//GEN-HEADEREND:event_AlphaTF3ActionPerformed RunTTest2(); }//GEN-LAST:event_AlphaTF3ActionPerformed private void AlphaTF2ActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_AlphaTF2ActionPerformed {//GEN-HEADEREND:event_AlphaTF2ActionPerformed RunTTest2(); }//GEN-LAST:event_AlphaTF2ActionPerformed // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JTextField AlphaTF; private javax.swing.JTextField AlphaTF1; private javax.swing.JTextField AlphaTF2; private javax.swing.JTextField AlphaTF3; private javax.swing.JLabel DFL13; private javax.swing.JLabel DFL14; private javax.swing.JLabel DFL15; private javax.swing.JLabel TStatisticL3; private javax.swing.JLabel TStatisticL4; private javax.swing.JLabel TStatisticL5; private javax.swing.JLabel jLabel5; private javax.swing.JLabel jLabel7; private javax.swing.JLabel jLabel8; private javax.swing.JLabel jLabel9; private javax.swing.JPanel jPanel14; private javax.swing.JPanel jPanel15; private javax.swing.JPanel jPanel16; private javax.swing.JScrollPane jScrollPane7; private javax.swing.JScrollPane jScrollPane8; private javax.swing.JScrollPane jScrollPane9; private javax.swing.JTextPane jTextPane7; private javax.swing.JTextPane jTextPane8; private javax.swing.JTextPane jTextPane9; // End of variables declaration//GEN-END:variables public void RunAnalysis(double[] sample) { if (sample == null) return; if (sample.length < 3) return; _sample = sample; RunTStatistic(); RunTTest(); RunTTest2(); } private void RunTStatistic() { double mu; try { mu = Double.parseDouble(AlphaTF.getText()); } catch (Exception e) { mu = 1; } AlphaTF.setText(String.valueOf(mu)); TStatisticL3.setText(String.valueOf(_decFor.format(_studentTest.t(mu, _sample)))); } private void RunTTest() { double mu; try { mu = Double.parseDouble(AlphaTF1.getText()); } catch (Exception e) { mu = 1; } AlphaTF1.setText(String.valueOf(mu)); TStatisticL4.setText(String.valueOf(_decFor.format(_studentTest.tTest(mu, _sample)))); } private void RunTTest2() { double mu; try { mu = Double.parseDouble(AlphaTF2.getText()); } catch (Exception e) { mu = 1; } AlphaTF2.setText(String.valueOf(mu)); double alpha; try { alpha = Double.parseDouble(AlphaTF3.getText()); } catch (Exception e) { alpha = 0.05; } if (alpha <= 0 || alpha >= 0.5) alpha = 0.05; AlphaTF3.setText(String.valueOf(alpha)); TStatisticL5.setText(String.valueOf(_studentTest.tTest(mu, _sample, alpha))); } }