/*
 * HyTest1PopVarDialog.java
 *
 * Created on May 30, 2008, 9:55 AM
 */

package org.statcato.dialogs.stat;

import org.statcato.*;
import org.statcato.spreadsheet.*;
import org.statcato.statistics.inferential.*;
import org.statcato.utils.HelperFunctions;
import java.util.*;
import javax.swing.*;
import org.statcato.statistics.BasicStatistics;

/**
 * A dialog for testing claims about a population variance or standard deviation.
 * Allows the user to specify data samples in a column of a Datasheet,
 * or provide summary data values.  The user must also provide
 * the confidence level, alternative hypothesis, and hypothesized variance or
 * standard deviation.
 * Computes and displays in log window the significance level, 
 * critical value, test statistic, and p-Value.
 * 
 * @author  Margaret Yau
 * @version %I%, %G%
 * @see org.statcato.statistics.inferential.HypothesisTest1Var
 * @since 1.0
 */
public class HyTest1PopVarDialog extends StatcatoDialog {
  
    /** Creates new form HyTest1PopVarDialog */
    public HyTest1PopVarDialog(java.awt.Frame parent, boolean modal,
            Statcato mTab) {
        super(parent, modal);
        initComponents();
        
        app = mTab;
        ParentSpreadsheet = app.getSpreadsheet(); 
        
        ButtonGroup group1 = new ButtonGroup();
        group1.add(SampleRadioButton);
        group1.add(SummaryRadioButton);
        
        ButtonGroup group2 = new ButtonGroup();
        group2.add(VarRadioButton);
        group2.add(StdevRadioButton);
        
        ButtonGroup group3 = new ButtonGroup();
        group3.add(HyVarRadioButton);
        group3.add(HyStdevRadioButton);
        
        ButtonGroup group4 = new ButtonGroup();
        group4.add(SigRadioButton);
        group4.add(ConfRadioButton);
        
        ParentSpreadsheet.populateComboBox(ColumnComboBox);
        
        getRootPane().setDefaultButton(OKButton);
        setHelpFile("stat-hytest-var");
        name = "Hypothesis Test: 1-Population Variance";
        description = "For performing calculations for testing claims " +
                "about a population variance (or standard deviation).";
        helpStrings.add("If individual samples are entered in a single " +
                "column of the datasheet, select the Samples in column: radio " +
                "button, and select the column name in the drop-down menu.");
        helpStrings.add("To use summary statistics of the sample data, " +
                "select the Summarized sample data: radio button, " +
                "and input the sample size and the sample variance " +
                "or standard deviation.");
        helpStrings.add("Enter the significance or confidence level (between 0 and 1) ");
        helpStrings.add("Select the form of the alternative " +
                "hypothesis and enter the hypothesized population variance " +
                "in the provided text box.");
    }
 
    /**
     * Updates elements on the dialog so that they have the most
     * current values.  Called by {@link #setVisible} to make
     * sure the dialog displays current values when made visible again.
     * 
     * @see #setVisible(boolean)
     */
    @Override
    public void updateElements() {
        updateComboBox(ColumnComboBox);
    }
    
    /** 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.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        OKButton = new javax.swing.JButton();
        CancelButton = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        VarRadioButton = new javax.swing.JRadioButton();
        SampleSizeTextField = new javax.swing.JTextField();
        SampleVarTextField = new javax.swing.JTextField();
        ColumnComboBox = new javax.swing.JComboBox();
        SampleStdevTextField = new javax.swing.JTextField();
        SampleSizeLabel = new javax.swing.JLabel();
        SampleRadioButton = new javax.swing.JRadioButton();
        SummaryRadioButton = new javax.swing.JRadioButton();
        StdevRadioButton = new javax.swing.JRadioButton();
        jPanel3 = new javax.swing.JPanel();
        HyStdevRadioButton = new javax.swing.JRadioButton();
        AltComboBox = new javax.swing.JComboBox();
        jLabel3 = new javax.swing.JLabel();
        HyStdevTextField = new javax.swing.JTextField();
        HyVarTextField = new javax.swing.JTextField();
        HyVarRadioButton = new javax.swing.JRadioButton();
        SigPanel = new javax.swing.JPanel();
        SigLabel = new javax.swing.JLabel();
        ConfidenceTextField = new javax.swing.JTextField();
        ConfRadioButton = new javax.swing.JRadioButton();
        SigRadioButton = new javax.swing.JRadioButton();
        SigTextField = new javax.swing.JTextField();
        ConfLabel = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Hypothesis Test: 1-Population Variance");

        OKButton.setText("OK");
        OKButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                OKButtonActionPerformed(evt);
            }
        });

        CancelButton.setText("Cancel");
        CancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CancelButtonActionPerformed(evt);
            }
        });

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Inputs"));

        VarRadioButton.setSelected(true);
        VarRadioButton.setText("Variance:");
        VarRadioButton.setEnabled(false);

        SampleSizeTextField.setEnabled(false);

        SampleVarTextField.setEnabled(false);

        SampleStdevTextField.setEnabled(false);

        SampleSizeLabel.setText("Sample Size:");
        SampleSizeLabel.setEnabled(false);

        SampleRadioButton.setSelected(true);
        SampleRadioButton.setText("Samples in column:");
        SampleRadioButton.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                SampleRadioButtonStateChanged(evt);
            }
        });

        SummaryRadioButton.setText("Summarized sample data:");
        SummaryRadioButton.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                SummaryRadioButtonStateChanged(evt);
            }
        });

        StdevRadioButton.setText("Standard deviation:");
        StdevRadioButton.setEnabled(false);

        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)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(65, 65, 65)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(SampleSizeLabel)
                            .addComponent(VarRadioButton)
                            .addComponent(StdevRadioButton))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(SampleSizeTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(SampleVarTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(SampleStdevTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(SummaryRadioButton)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                            .addGap(21, 21, 21)
                            .addComponent(ColumnComboBox, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addComponent(SampleRadioButton, javax.swing.GroupLayout.Alignment.LEADING)))
                .addContainerGap(31, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(SampleRadioButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(ColumnComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(SummaryRadioButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(SampleSizeLabel)
                    .addComponent(SampleSizeTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(14, 14, 14)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(SampleVarTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(VarRadioButton))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(SampleStdevTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(StdevRadioButton))
                .addContainerGap())
        );

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("Alternative Hypothesis"));

        HyStdevRadioButton.setText("Hypothesized standard deviation:");

        AltComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Less than", "Not Equal to", "Greater than" }));
        AltComboBox.setSelectedIndex(1);

        jLabel3.setText("Alternative Hypothesis:");

        HyVarRadioButton.setSelected(true);
        HyVarRadioButton.setText("Hypothesized variance:");

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(AltComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGap(19, 19, 19)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel3Layout.createSequentialGroup()
                                .addComponent(HyStdevRadioButton)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(HyStdevTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(jPanel3Layout.createSequentialGroup()
                                .addComponent(HyVarRadioButton)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(HyVarTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(AltComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(HyVarRadioButton)
                    .addComponent(HyVarTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(HyStdevRadioButton)
                    .addComponent(HyStdevTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        SigPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Significance"));

        SigLabel.setText("0 - 1.00 (e.g. 0.05)");

        ConfidenceTextField.setText("0.95");
        ConfidenceTextField.setEnabled(false);
        ConfidenceTextField.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                ConfidenceTextFieldFocusLost(evt);
            }
        });

        ConfRadioButton.setText("Confidence Level:");
        ConfRadioButton.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                ConfRadioButtonStateChanged(evt);
            }
        });

        SigRadioButton.setSelected(true);
        SigRadioButton.setText("Significance Level:");
        SigRadioButton.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                SigRadioButtonStateChanged(evt);
            }
        });

        SigTextField.setText("0.05");
        SigTextField.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                SigTextFieldFocusLost(evt);
            }
        });

        ConfLabel.setText("0 - 1.00 (e.g. 0.95)");
        ConfLabel.setEnabled(false);

        javax.swing.GroupLayout SigPanelLayout = new javax.swing.GroupLayout(SigPanel);
        SigPanel.setLayout(SigPanelLayout);
        SigPanelLayout.setHorizontalGroup(
            SigPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(SigPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(SigPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, SigPanelLayout.createSequentialGroup()
                        .addComponent(ConfRadioButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(ConfidenceTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(SigPanelLayout.createSequentialGroup()
                        .addComponent(SigRadioButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(SigTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(SigPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(SigLabel)
                    .addComponent(ConfLabel))
                .addContainerGap())
        );
        SigPanelLayout.setVerticalGroup(
            SigPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(SigPanelLayout.createSequentialGroup()
                .addGap(6, 6, 6)
                .addGroup(SigPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(SigRadioButton)
                    .addComponent(SigTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(SigLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(SigPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(ConfidenceTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(ConfRadioButton)
                    .addComponent(ConfLabel))
                .addContainerGap(17, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(15, 15, 15))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(SigPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(11, 11, 11))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(15, 15, 15))
            .addGroup(layout.createSequentialGroup()
                .addGap(98, 98, 98)
                .addComponent(OKButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(CancelButton)
                .addContainerGap(117, Short.MAX_VALUE))
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {CancelButton, OKButton});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(SigPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(OKButton)
                    .addComponent(CancelButton))
                .addGap(32, 32, 32))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void OKButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_OKButtonActionPerformed
        app.compoundEdit = new DialogEdit("hypothesis test 1 variance");
        
        int n = 0, typeTest;
        double s = 0, s2 = 0, confidenceLevel;
        double s_0 = 0, s2_0 = 0;
        Vector<Double> ColumnVector = new Vector<Double>();

        String heading = "Hypothesis Test - One population variance:";
        String text = "";
        
        if (ConfRadioButton.isSelected()) {
            // get confidence level from dialog
            try {
                confidenceLevel = Double.parseDouble(ConfidenceTextField.getText());
            } catch (NumberFormatException e) {
                app.showErrorDialog("Enter a number between 0 and 1 for the confidence level.");
                return;
            }
            if (confidenceLevel <= 0 || confidenceLevel >= 1) {
                app.showErrorDialog("Enter a number between 0 and 1 for the confidence level.");
                return;
            }
        }
        else {
            // get confidence level from dialog
            try {
                confidenceLevel = 1 - Double.parseDouble(SigTextField.getText());
            } catch (NumberFormatException e) {
                app.showErrorDialog("Enter a number between 0 and 1 for the significance level.");
                return;
            }
            if (confidenceLevel <= 0 || confidenceLevel >= 1) {
                app.showErrorDialog("Enter a number between 0 and 1 for the significance level.");
                return;
            }
        }
        
        heading += "  confidence level = " + 
                String.format("%."+
                HelperFunctions.getNumDecimalPlaces(ConfidenceTextField.getText())+"f", 
                confidenceLevel);
        
        // get input
        if (SampleRadioButton.isSelected()) {
            // get data for selected input column
            String columnLabel = ColumnComboBox.getSelectedItem().toString();
            if (columnLabel.equals("")) { // no input variable
                app.showErrorDialog("Select the input column.");
                return;
            }
            int column = ParentSpreadsheet.parseColumnNumber(columnLabel);
            Vector<Cell> StrColumnVector = ParentSpreadsheet.getColumn(column);
            ColumnVector = HelperFunctions.ConvertInputVectorToDoubles(StrColumnVector);
            if (ColumnVector == null) {
                app.showErrorDialog("Invalid input column " + columnLabel +
                        ": all data must be numbers.");
                return;
            }
            text += "Input: " + columnLabel + "<br>";
            n = BasicStatistics.Nnonmissing(ColumnVector);
            s2 = BasicStatistics.variance(ColumnVector).doubleValue();
            s = BasicStatistics.stdev(ColumnVector).doubleValue();
        } else {
            // get summary data
            try {
                n = Integer.parseInt(SampleSizeTextField.getText());
                if (VarRadioButton.isSelected()) {
                    s2 = Double.parseDouble(SampleVarTextField.getText());
                    s = Math.sqrt(s2);
                } else {
                    s = Double.parseDouble(SampleStdevTextField.getText());
                    s2 = s * s;
                }
            } catch (NumberFormatException e) {
                app.showErrorDialog("Invalid summary data.");
                return;
            }
            text += "Input: Summary data" + "<br>";
        }
        
        // get hypothesized variance
        if (HyVarRadioButton.isSelected()) {
            try {
                s2_0 = Double.parseDouble(HyVarTextField.getText());
                s_0 = Math.sqrt(s2_0);
            }
            catch (NumberFormatException e) {
                app.showErrorDialog("Enter a valid number for the hypothesized variance.");
                return;
            }
        }
        else { // get hypothesized standard deviation
            try {
                s_0 = Double.parseDouble(HyStdevTextField.getText());
                s2_0 = s_0 * s_0;
            }
            catch (NumberFormatException e) {
                app.showErrorDialog("Enter a valid number for the hypothesized " +
                        "standard deviation.");
                return;
            }
        }
        // get type of test
        typeTest = AltComboBox.getSelectedIndex();
        
        HypothesisTest1Var ht = new HypothesisTest1Var(n, s2_0, confidenceLevel, typeTest);
        
        // outputs
        text += "Null hypothesis: &sigma;<sup>2</sup> = " + s2_0 + "<br>";
        text += "Alternative hypothesis: &sigma;<sup>2</sup> ";
        if (typeTest == HypothesisTest.LEFT_TAIL)
            text += "&lt;";
        else if (typeTest == HypothesisTest.RIGHT_TAIL)
            text += "&gt;";
        else
            text += "&ne;";
        text += " " + s2_0 + "<br>";
        text += "<br><table border='1'>";
        text += "<tr><td>N</td><td>Sample Stdev s</td><td>Sample Var s<sup>2</sup></td>";
        text += "<td>Significance Level</td><td>Critical Value</td>" +
                "<td>Test Statistic</td>" + "<td>p-Value</td></tr>";
        text += "<tr><td>" + n + "</td><td>" +
                HelperFunctions.formatFloat(s, 3) + "</td><td>" +
                HelperFunctions.formatFloat(s2, 3) + "</td><td>" + 
                HelperFunctions.formatFloat(1-confidenceLevel, 2)  +
                "</td><td>" + ht.criticalValue()
                + "</td><td>" + HelperFunctions.formatFloat(ht.testStatistics(s2), 3)
                + "</td><td>" + HelperFunctions.formatFloat(ht.pValue(s2), 4)
                + "</td></tr></table>";        
        
        
        app.addLogParagraph(heading, text);

        app.compoundEdit.end();
        app.addCompoundEdit(app.compoundEdit);
        
        setVisible(false);
    }//GEN-LAST:event_OKButtonActionPerformed

    private void CancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CancelButtonActionPerformed
        setVisible(false);
    }//GEN-LAST:event_CancelButtonActionPerformed

    private void SampleRadioButtonStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_SampleRadioButtonStateChanged
        if (SampleRadioButton.isSelected()) {
            ColumnComboBox.setEnabled(true);
        } else {
            ColumnComboBox.setEnabled(false);
        }
    }//GEN-LAST:event_SampleRadioButtonStateChanged

    private void SummaryRadioButtonStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_SummaryRadioButtonStateChanged
        if (SummaryRadioButton.isSelected()) {
            SampleSizeLabel.setEnabled(true);
            SampleSizeTextField.setEnabled(true);
            SampleStdevTextField.setEnabled(true);
            SampleVarTextField.setEnabled(true);
            VarRadioButton.setEnabled(true);
            StdevRadioButton.setEnabled(true);
        } else  {
            SampleSizeLabel.setEnabled(false);
            SampleSizeTextField.setEnabled(false);
            SampleStdevTextField.setEnabled(false);
            SampleVarTextField.setEnabled(false);
            VarRadioButton.setEnabled(false);
            StdevRadioButton.setEnabled(false);
        }
    }//GEN-LAST:event_SummaryRadioButtonStateChanged

    private void ConfidenceTextFieldFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_ConfidenceTextFieldFocusLost
        try {
            String confString = ConfidenceTextField.getText();
            double conf = Double.parseDouble(confString);
            double sig = 1.0 - conf;
            SigTextField.setText(
                    String.format("%."+HelperFunctions.getNumDecimalPlaces(confString)+"f", sig));
        } catch (NumberFormatException e) {
        }
    }//GEN-LAST:event_ConfidenceTextFieldFocusLost

    private void ConfRadioButtonStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_ConfRadioButtonStateChanged
        if (ConfRadioButton.isSelected()) {
            ConfidenceTextField.setEnabled(true);
            ConfLabel.setEnabled(true);
        } else {
            ConfidenceTextField.setEnabled(false);
            ConfLabel.setEnabled(false);
        }
    }//GEN-LAST:event_ConfRadioButtonStateChanged

    private void SigRadioButtonStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_SigRadioButtonStateChanged
        if (SigRadioButton.isSelected()) {
            SigTextField.setEnabled(true);
            SigLabel.setEnabled(true);
        } else {
            SigTextField.setEnabled(false);
            SigLabel.setEnabled(false);
        }
    }//GEN-LAST:event_SigRadioButtonStateChanged

    private void SigTextFieldFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_SigTextFieldFocusLost
        try {
            String sigString = SigTextField.getText();
            double sig = Double.parseDouble(sigString);
            double conf = 1 - sig;
            ConfidenceTextField.setText(
                    String.format("%."+HelperFunctions.getNumDecimalPlaces(sigString)+"f", conf));
        } catch (NumberFormatException e) {
        }
    }//GEN-LAST:event_SigTextFieldFocusLost

    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox AltComboBox;
    private javax.swing.JButton CancelButton;
    private javax.swing.JComboBox ColumnComboBox;
    private javax.swing.JLabel ConfLabel;
    private javax.swing.JRadioButton ConfRadioButton;
    private javax.swing.JTextField ConfidenceTextField;
    private javax.swing.JRadioButton HyStdevRadioButton;
    private javax.swing.JTextField HyStdevTextField;
    private javax.swing.JRadioButton HyVarRadioButton;
    private javax.swing.JTextField HyVarTextField;
    private javax.swing.JButton OKButton;
    private javax.swing.JRadioButton SampleRadioButton;
    private javax.swing.JLabel SampleSizeLabel;
    private javax.swing.JTextField SampleSizeTextField;
    private javax.swing.JTextField SampleStdevTextField;
    private javax.swing.JTextField SampleVarTextField;
    private javax.swing.JLabel SigLabel;
    private javax.swing.JPanel SigPanel;
    private javax.swing.JRadioButton SigRadioButton;
    private javax.swing.JTextField SigTextField;
    private javax.swing.JRadioButton StdevRadioButton;
    private javax.swing.JRadioButton SummaryRadioButton;
    private javax.swing.JRadioButton VarRadioButton;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel3;
    // End of variables declaration//GEN-END:variables
    
}
