num4normality 0.0.3-java → 0.0.6-java
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +8 -0
- data/ext/num4normality/Normality.java +249 -163
- data/lib/num4normality.rb +15 -0
- metadata +2 -2
    
        checksums.yaml
    CHANGED
    
    | @@ -1,7 +1,7 @@ | |
| 1 1 | 
             
            ---
         | 
| 2 2 | 
             
            SHA256:
         | 
| 3 | 
            -
              metadata.gz:  | 
| 4 | 
            -
              data.tar.gz:  | 
| 3 | 
            +
              metadata.gz: 9803b3bcb0408424c204ebda20e538aa178a2f35573e280dd222add412987cff
         | 
| 4 | 
            +
              data.tar.gz: 1258e8ad4f82177478d2f35d689f56b4d8d58d1177f7a6f45896f05ee6c50189
         | 
| 5 5 | 
             
            SHA512:
         | 
| 6 | 
            -
              metadata.gz:  | 
| 7 | 
            -
              data.tar.gz:  | 
| 6 | 
            +
              metadata.gz: 16ad0542c142e6b74871df83ae43fa4ac62a9bd977e3bd466b9a520f849f0852460a1b71051b072c5de14b93a4b7530e2ef1821300b7b02303cfd1b307f0a43c
         | 
| 7 | 
            +
              data.tar.gz: c49ca950c085da6760c1d0b171b7daad90354c7804e6521711e7469373a47aca58bec5d7fad47fb5d6c1a882c754ff389bdcae8e957fa93467cdc9db500ffa8a
         | 
    
        data/CHANGELOG.md
    CHANGED
    
    
| @@ -4,6 +4,7 @@ import org.jfree.chart.ChartFactory; | |
| 4 4 | 
             
            import org.jfree.data.xy.XYSeriesCollection;
         | 
| 5 5 | 
             
            import org.jfree.data.xy.XYSeries;
         | 
| 6 6 |  | 
| 7 | 
            +
            import org.jfree.chart.plot.CombinedDomainXYPlot;
         | 
| 7 8 | 
             
            import org.jfree.chart.plot.XYPlot;
         | 
| 8 9 | 
             
            import org.jfree.chart.axis.NumberAxis;
         | 
| 9 10 | 
             
            import org.jfree.chart.axis.ValueAxis;
         | 
| @@ -21,6 +22,7 @@ import java.io.IOException; | |
| 21 22 |  | 
| 22 23 | 
             
            import org.apache.commons.math3.stat.descriptive.DescriptiveStatistics;
         | 
| 23 24 | 
             
            import org.apache.commons.math3.distribution.NormalDistribution;
         | 
| 25 | 
            +
            import org.apache.commons.math3.distribution.ChiSquaredDistribution;
         | 
| 24 26 | 
             
            import org.apache.commons.math3.stat.regression.SimpleRegression;
         | 
| 25 27 | 
             
            import java.util.Arrays;
         | 
| 26 28 | 
             
            import java.text.DecimalFormat;
         | 
| @@ -28,18 +30,24 @@ import java.text.DecimalFormat; | |
| 28 30 | 
             
            import org.apache.commons.math3.stat.inference.TestUtils;
         | 
| 29 31 | 
             
            public class Normality {
         | 
| 30 32 | 
             
                public static void qqplot(String dname, double[] xi) {
         | 
| 31 | 
            -
                    ChartPlot plot = new  | 
| 32 | 
            -
                    JFreeChart chart = plot.createChart(dname, xi);
         | 
| 33 | 
            +
                    ChartPlot plot = new QQChartPlot();
         | 
| 34 | 
            +
                    JFreeChart chart = plot.createChart("正規Q-Qプロット", dname, xi);
         | 
| 33 35 |  | 
| 34 36 | 
             
                    plot.writeJPEG("qqplot.jpeg", chart, 800, 500);        
         | 
| 35 37 | 
             
                }
         | 
| 36 38 | 
             
                public static void ksplot(String dname, double[] xi) {
         | 
| 37 | 
            -
                    ChartPlot plot = new  | 
| 38 | 
            -
                    JFreeChart chart = plot.createChart(dname, xi);
         | 
| 39 | 
            +
                    ChartPlot plot = new KSChartPlot();
         | 
| 40 | 
            +
                    JFreeChart chart = plot.createChart("コルモゴルフ・スミルノフ検定", dname, xi);
         | 
| 39 41 |  | 
| 40 42 | 
             
                    plot.writeJPEG("ksplot.jpeg", chart, 800, 500);        
         | 
| 41 43 |  | 
| 42 44 | 
             
                }
         | 
| 45 | 
            +
                public static void qqksplot(String dname, double[] xi) {
         | 
| 46 | 
            +
                     ChartPlot plot = new QQKSChartPlot();
         | 
| 47 | 
            +
                    JFreeChart chart = plot.createChart("Q-Q and コルモゴルフ・スミルノフ検定", dname, xi);
         | 
| 48 | 
            +
             | 
| 49 | 
            +
                    plot.writeJPEG("qqksplot.jpeg", chart, 1000, 800);        
         | 
| 50 | 
            +
                }
         | 
| 43 51 | 
             
                public static boolean kstest(double[] xi) {
         | 
| 44 52 | 
             
                    KSTest ks = new KSTest();
         | 
| 45 53 |  | 
| @@ -57,14 +65,22 @@ public class Normality { | |
| 57 65 | 
             
                    double b2 = daigo.calcTestStatistic(xi);
         | 
| 58 66 | 
             
                    return daigo.test(b2, 0.05);
         | 
| 59 67 | 
             
                }
         | 
| 60 | 
            -
                
         | 
| 68 | 
            +
                public static boolean omnibustest(double[] xi) {
         | 
| 69 | 
            +
                    DAgostinosTest daigo = new OmnibusTest();
         | 
| 61 70 |  | 
| 71 | 
            +
                    double x = daigo.calcTestStatistic(xi);
         | 
| 72 | 
            +
                    return daigo.test(x, 0.05);
         | 
| 73 | 
            +
                }
         | 
| 74 | 
            +
                
         | 
| 75 | 
            +
                /*********************************/
         | 
| 76 | 
            +
                /* interface define              */
         | 
| 77 | 
            +
                /*********************************/
         | 
| 62 78 | 
             
                private interface ChartPlot {
         | 
| 63 79 | 
             
                    /* フィールド */
         | 
| 64 80 | 
             
                    static final double CLASS_MIN = -4.0;
         | 
| 65 81 | 
             
                    static final double CLASS_MAX = 4.0;
         | 
| 66 82 | 
             
                    /* メゾット */
         | 
| 67 | 
            -
                    JFreeChart createChart(String dname, double[] xi);
         | 
| 83 | 
            +
                    JFreeChart createChart(String title, String dname, double[] xi);
         | 
| 68 84 | 
             
                    default void writeJPEG(String fname, JFreeChart chart, int width, int height) {
         | 
| 69 85 | 
             
                        File file = new File(fname);
         | 
| 70 86 | 
             
                        try {
         | 
| @@ -74,43 +90,22 @@ public class Normality { | |
| 74 90 | 
             
                        }
         | 
| 75 91 | 
             
                    }
         | 
| 76 92 | 
             
                }
         | 
| 93 | 
            +
                private interface CreatePlot {
         | 
| 94 | 
            +
                    XYPlot createPlot(String dname, double[] xi);
         | 
| 95 | 
            +
                }
         | 
| 77 96 | 
             
                private interface DAgostinosTest {
         | 
| 78 97 | 
             
                    double calcTestStatistic(double[] xi);
         | 
| 79 98 | 
             
                    boolean test(double statistic, double a);
         | 
| 80 99 | 
             
                }
         | 
| 81 | 
            -
             | 
| 100 | 
            +
                /*********************************/
         | 
| 101 | 
            +
                /* Class define                  */
         | 
| 102 | 
            +
                /*********************************/
         | 
| 82 103 | 
             
                // Q-Qplot
         | 
| 83 | 
            -
                private static class  | 
| 84 | 
            -
                     | 
| 85 | 
            -
             | 
| 86 | 
            -
                    public QQPlot() {
         | 
| 87 | 
            -
                        stat = new DescriptiveStatistics();
         | 
| 88 | 
            -
                        ndist = new NormalDistribution(0, 1);
         | 
| 89 | 
            -
                    }
         | 
| 90 | 
            -
                    private double[][] createData(double[] xi) {
         | 
| 91 | 
            -
                        int n = xi.length;
         | 
| 92 | 
            -
                        Arrays.sort(xi);
         | 
| 93 | 
            -
                        Arrays.stream(xi).forEach(stat::addValue);
         | 
| 94 | 
            -
                        double sum = stat.getSum();
         | 
| 95 | 
            -
                        double[][] data = new double[n][2];
         | 
| 96 | 
            -
                        double p = 0.0;
         | 
| 97 | 
            -
             | 
| 98 | 
            -
                        for (int i = 0; i < n; i++) {
         | 
| 99 | 
            -
                            p += xi[i] / sum;
         | 
| 100 | 
            -
                            double x = 
         | 
| 101 | 
            -
                                ndist.inverseCumulativeProbability(p * (i + 1.0) / (n + 1.0));
         | 
| 102 | 
            -
             | 
| 103 | 
            -
                            data[i][0] = x;
         | 
| 104 | 
            -
                            data[i][1] = xi[i];
         | 
| 105 | 
            -
                        }
         | 
| 106 | 
            -
                        return data;
         | 
| 107 | 
            -
                    }
         | 
| 108 | 
            -
                    public JFreeChart createChart(String dname, double[] xi) {
         | 
| 109 | 
            -
                        double[][] data = createData(xi);
         | 
| 110 | 
            -
             | 
| 111 | 
            -
                        XYPlot plot = createPlot(dname, data);
         | 
| 104 | 
            +
                private static class QQChartPlot implements ChartPlot {
         | 
| 105 | 
            +
                    public JFreeChart createChart(String title, String dname, double[] xi) {
         | 
| 106 | 
            +
                        XYPlot plot = createPlot(dname, xi);
         | 
| 112 107 | 
             
                        /*--- 横軸 ---*/
         | 
| 113 | 
            -
                        NumberAxis domainAxis = new NumberAxis(" | 
| 108 | 
            +
                        NumberAxis domainAxis = new NumberAxis("期待値");
         | 
| 114 109 |  | 
| 115 110 | 
             
                        plot.setDomainAxis(domainAxis);
         | 
| 116 111 | 
             
                        domainAxis.setLowerMargin(0.03);
         | 
| @@ -119,150 +114,231 @@ public class Normality { | |
| 119 114 | 
             
                        domainAxis.setUpperBound(ChartPlot.CLASS_MAX);
         | 
| 120 115 |  | 
| 121 116 | 
             
                        ChartFactory.setChartTheme(StandardChartTheme.createLegacyTheme());
         | 
| 122 | 
            -
                        return new JFreeChart( | 
| 117 | 
            +
                        return new JFreeChart(title, plot);
         | 
| 123 118 | 
             
                    }
         | 
| 124 | 
            -
                    private XYPlot createPlot(String dname, double[] | 
| 125 | 
            -
                         | 
| 126 | 
            -
                        XYItemRenderer renderer1 = new XYLineAndShapeRenderer(true, false);
         | 
| 127 | 
            -
                        XYToolTipGenerator toolTipGenerator = new StandardXYToolTipGenerator();
         | 
| 128 | 
            -
             | 
| 129 | 
            -
                        renderer0.setDefaultToolTipGenerator(toolTipGenerator);
         | 
| 130 | 
            -
                        renderer0.setURLGenerator(null);
         | 
| 131 | 
            -
                        renderer1.setDefaultToolTipGenerator(toolTipGenerator);
         | 
| 132 | 
            -
                        renderer1.setURLGenerator(null);
         | 
| 133 | 
            -
             | 
| 134 | 
            -
                        XYPlot plot = new XYPlot();
         | 
| 135 | 
            -
                        plot.setOrientation(PlotOrientation.VERTICAL);
         | 
| 136 | 
            -
                        plot.mapDatasetToRangeAxis(0,0);
         | 
| 137 | 
            -
                        plot.mapDatasetToRangeAxis(1,0);
         | 
| 138 | 
            -
            	    plot.setDatasetRenderingOrder(DatasetRenderingOrder.FORWARD);
         | 
| 139 | 
            -
             | 
| 140 | 
            -
                        /*--- 縦軸 ---*/
         | 
| 141 | 
            -
                        NumberAxis valueAxis0 = new NumberAxis("観測値");
         | 
| 142 | 
            -
                        plot.setRangeAxis(valueAxis0);
         | 
| 143 | 
            -
             | 
| 144 | 
            -
                        plot.setRenderer(0, renderer0);
         | 
| 145 | 
            -
                        plot.setDataset(0, createDataset0(dname, data));
         | 
| 146 | 
            -
             | 
| 147 | 
            -
                        plot.setRenderer(1, renderer1);
         | 
| 148 | 
            -
                        plot.setDataset(1, createDataset1(data));
         | 
| 119 | 
            +
                    private XYPlot createPlot(String dname, double[] xi) {
         | 
| 120 | 
            +
                        CreatePlot plotImpl = new QQPlot();
         | 
| 149 121 |  | 
| 150 | 
            -
                        return  | 
| 122 | 
            +
                        return plotImpl.createPlot(dname, xi);
         | 
| 151 123 | 
             
                    }
         | 
| 152 | 
            -
                     | 
| 153 | 
            -
                         | 
| 154 | 
            -
             
         | 
| 155 | 
            -
                         | 
| 156 | 
            -
                             | 
| 124 | 
            +
                    public static class QQPlot implements CreatePlot {
         | 
| 125 | 
            +
                        private DescriptiveStatistics stat = null;
         | 
| 126 | 
            +
                        private NormalDistribution ndist = null;
         | 
| 127 | 
            +
                        public QQPlot() {
         | 
| 128 | 
            +
                            stat = new DescriptiveStatistics();
         | 
| 129 | 
            +
                            ndist = new NormalDistribution(0, 1);
         | 
| 130 | 
            +
                        }
         | 
| 131 | 
            +
                        private double[][] createData(double[] xi) {
         | 
| 132 | 
            +
                            int n = xi.length;
         | 
| 133 | 
            +
                            Arrays.sort(xi);
         | 
| 134 | 
            +
                            Arrays.stream(xi).forEach(stat::addValue);
         | 
| 135 | 
            +
                            double sum = stat.getSum();
         | 
| 136 | 
            +
                            double[][] data = new double[n][2];
         | 
| 137 | 
            +
                            double p = 0.0;
         | 
| 138 | 
            +
             | 
| 139 | 
            +
                            for (int i = 0; i < n; i++) {
         | 
| 140 | 
            +
                                p += xi[i] / sum;
         | 
| 141 | 
            +
                                double x = 
         | 
| 142 | 
            +
                                    ndist.inverseCumulativeProbability(p * (i + 1.0) / (n + 1.0));
         | 
| 143 | 
            +
             | 
| 144 | 
            +
                                data[i][0] = x;
         | 
| 145 | 
            +
                                data[i][1] = xi[i];
         | 
| 146 | 
            +
                            }
         | 
| 147 | 
            +
                            return data;
         | 
| 148 | 
            +
                        }
         | 
| 149 | 
            +
                        public XYPlot createPlot(String dname, double[] xi) {
         | 
| 150 | 
            +
                            double[][] data = createData(xi);
         | 
| 151 | 
            +
                            XYItemRenderer renderer0 = new XYLineAndShapeRenderer(false, true);
         | 
| 152 | 
            +
                            XYItemRenderer renderer1 = new XYLineAndShapeRenderer(true, false);
         | 
| 153 | 
            +
                            XYToolTipGenerator toolTipGenerator = new StandardXYToolTipGenerator();
         | 
| 154 | 
            +
             | 
| 155 | 
            +
                            renderer0.setDefaultToolTipGenerator(toolTipGenerator);
         | 
| 156 | 
            +
                            renderer0.setURLGenerator(null);
         | 
| 157 | 
            +
                            renderer1.setDefaultToolTipGenerator(toolTipGenerator);
         | 
| 158 | 
            +
                            renderer1.setURLGenerator(null);
         | 
| 159 | 
            +
             | 
| 160 | 
            +
                            XYPlot plot = new XYPlot();
         | 
| 161 | 
            +
                            plot.setOrientation(PlotOrientation.VERTICAL);
         | 
| 162 | 
            +
                            plot.mapDatasetToRangeAxis(0,0);
         | 
| 163 | 
            +
                            plot.mapDatasetToRangeAxis(1,0);
         | 
| 164 | 
            +
            	        plot.setDatasetRenderingOrder(DatasetRenderingOrder.FORWARD);
         | 
| 165 | 
            +
             | 
| 166 | 
            +
                            /*--- 縦軸 ---*/
         | 
| 167 | 
            +
                            NumberAxis valueAxis0 = new NumberAxis("観測値");
         | 
| 168 | 
            +
                            plot.setRangeAxis(valueAxis0);
         | 
| 169 | 
            +
             | 
| 170 | 
            +
                            plot.setRenderer(0, renderer0);
         | 
| 171 | 
            +
                            plot.setDataset(0, createDataset0(dname, data));
         | 
| 172 | 
            +
             | 
| 173 | 
            +
                            plot.setRenderer(1, renderer1);
         | 
| 174 | 
            +
                            plot.setDataset(1, createDataset1(data));
         | 
| 175 | 
            +
             | 
| 176 | 
            +
                            return plot;
         | 
| 157 177 | 
             
                        }
         | 
| 158 | 
            -
                        XYSeriesCollection  | 
| 178 | 
            +
                        private XYSeriesCollection createDataset0(String dname, double[][] data) {
         | 
| 179 | 
            +
                            XYSeries cu = new XYSeries(dname);
         | 
| 180 | 
            +
             
         | 
| 181 | 
            +
                            for (int i = 0; i < data.length; i++) {
         | 
| 182 | 
            +
                                cu.add(data[i][0], data[i][1]);
         | 
| 183 | 
            +
                            }
         | 
| 184 | 
            +
                            XYSeriesCollection series = new XYSeriesCollection();
         | 
| 159 185 |  | 
| 160 | 
            -
             | 
| 161 | 
            -
             | 
| 162 | 
            -
             | 
| 163 | 
            -
             | 
| 164 | 
            -
             | 
| 165 | 
            -
             | 
| 186 | 
            +
                            series.addSeries(cu);
         | 
| 187 | 
            +
                            return series;
         | 
| 188 | 
            +
                        }
         | 
| 189 | 
            +
                        private XYSeriesCollection createDataset1(double[][] data) {
         | 
| 190 | 
            +
                            SimpleRegression simpleReg = new SimpleRegression(true);
         | 
| 191 | 
            +
                            XYSeries cu     = new XYSeries("累積");
         | 
| 166 192 |  | 
| 167 | 
            -
             | 
| 168 | 
            -
             | 
| 169 | 
            -
             | 
| 193 | 
            +
                            simpleReg.addData(data);
         | 
| 194 | 
            +
                            double a = simpleReg.getSlope();
         | 
| 195 | 
            +
                            double b = simpleReg.getIntercept();
         | 
| 170 196 |  | 
| 171 | 
            -
             | 
| 172 | 
            -
             | 
| 173 | 
            -
             | 
| 174 | 
            -
             | 
| 197 | 
            +
                            for (double x = ChartPlot.CLASS_MIN; x < ChartPlot.CLASS_MAX; x += 0.01) {
         | 
| 198 | 
            +
                                double y = a * x + b;
         | 
| 199 | 
            +
             | 
| 200 | 
            +
                                cu.add(x, y);
         | 
| 201 | 
            +
                            }
         | 
| 202 | 
            +
                            XYSeriesCollection series = new XYSeriesCollection();
         | 
| 203 | 
            +
                            series.addSeries(cu);
         | 
| 204 | 
            +
                            return series;
         | 
| 175 205 | 
             
                        }
         | 
| 176 | 
            -
                        XYSeriesCollection series = new XYSeriesCollection();
         | 
| 177 | 
            -
                        series.addSeries(cu);
         | 
| 178 | 
            -
                        return series;
         | 
| 179 206 | 
             
                    }
         | 
| 180 207 | 
             
                }
         | 
| 181 208 | 
             
                // コルモゴルフ・スミルノフ検定
         | 
| 182 | 
            -
                private static class  | 
| 183 | 
            -
                    public JFreeChart createChart(String dname, double[] xi) {
         | 
| 184 | 
            -
                         | 
| 209 | 
            +
                private static class KSChartPlot implements ChartPlot {
         | 
| 210 | 
            +
                    public JFreeChart createChart(String title, String dname, double[] xi) {
         | 
| 211 | 
            +
                        /*--- 横軸 ---*/
         | 
| 212 | 
            +
                        NumberAxis domainAxis = new NumberAxis("期待値");
         | 
| 185 213 | 
             
                        XYPlot plot = createPlot(dname, xi);
         | 
| 186 214 |  | 
| 187 | 
            -
                        /*--- 横軸 ---*/
         | 
| 188 215 | 
             
                        plot.setDomainAxis(domainAxis);
         | 
| 189 216 | 
             
                        domainAxis.setLowerMargin(0.03);
         | 
| 190 217 | 
             
                        domainAxis.setUpperMargin(0.03);
         | 
| 191 | 
            -
                        domainAxis.setLowerBound( | 
| 192 | 
            -
                        domainAxis.setUpperBound( | 
| 218 | 
            +
                        domainAxis.setLowerBound(ChartPlot.CLASS_MIN);
         | 
| 219 | 
            +
                        domainAxis.setUpperBound(ChartPlot.CLASS_MAX);
         | 
| 193 220 |  | 
| 194 221 | 
             
                        ChartFactory.setChartTheme(StandardChartTheme.createLegacyTheme());
         | 
| 195 | 
            -
                        return new JFreeChart( | 
| 222 | 
            +
                        return new JFreeChart(title, plot);
         | 
| 196 223 | 
             
                    }
         | 
| 197 224 | 
             
                    private XYPlot createPlot(String dname, double[] xi) {
         | 
| 198 | 
            -
                         | 
| 199 | 
            -
                        XYItemRenderer renderer1 = new XYLineAndShapeRenderer(true, false);
         | 
| 200 | 
            -
                        XYToolTipGenerator toolTipGenerator = new StandardXYToolTipGenerator();
         | 
| 201 | 
            -
             | 
| 202 | 
            -
                        renderer0.setDefaultToolTipGenerator(toolTipGenerator);
         | 
| 203 | 
            -
                        renderer0.setURLGenerator(null);
         | 
| 204 | 
            -
                        renderer1.setDefaultToolTipGenerator(toolTipGenerator);
         | 
| 205 | 
            -
                        renderer1.setURLGenerator(null);
         | 
| 206 | 
            -
             | 
| 207 | 
            -
                        XYPlot plot = new XYPlot();
         | 
| 208 | 
            -
                        plot.setOrientation(PlotOrientation.VERTICAL);
         | 
| 209 | 
            -
                        plot.mapDatasetToRangeAxis(0,0);
         | 
| 210 | 
            -
                        plot.mapDatasetToRangeAxis(1,0);
         | 
| 211 | 
            -
            	    plot.setDatasetRenderingOrder(DatasetRenderingOrder.FORWARD);
         | 
| 212 | 
            -
             | 
| 213 | 
            -
                        /*--- 縦軸 ---*/
         | 
| 214 | 
            -
                        NumberAxis valueAxis0 = new NumberAxis("確率");
         | 
| 215 | 
            -
                        plot.setRangeAxis(valueAxis0);
         | 
| 216 | 
            -
                        valueAxis0.setLowerBound(0);
         | 
| 217 | 
            -
                        valueAxis0.setUpperBound(1);
         | 
| 218 | 
            -
                        valueAxis0.setTickUnit(new NumberTickUnit(0.1));
         | 
| 219 | 
            -
                        valueAxis0.setNumberFormatOverride(new DecimalFormat("0.0#"));
         | 
| 220 | 
            -
             | 
| 221 | 
            -
                        plot.setRenderer(0, renderer0);
         | 
| 222 | 
            -
                        plot.setDataset(0, createDataset0(dname, xi));
         | 
| 223 | 
            -
             | 
| 224 | 
            -
                        plot.setRenderer(1, renderer1);
         | 
| 225 | 
            -
                        plot.setDataset(1, createDataset1());
         | 
| 225 | 
            +
                        CreatePlot plotImpl = new KSPlot();
         | 
| 226 226 |  | 
| 227 | 
            -
                        return  | 
| 227 | 
            +
                        return plotImpl.createPlot(dname, xi);
         | 
| 228 228 | 
             
                    }
         | 
| 229 | 
            -
                     | 
| 230 | 
            -
                         | 
| 231 | 
            -
                         | 
| 232 | 
            -
             | 
| 233 | 
            -
                         | 
| 234 | 
            -
                        double  | 
| 235 | 
            -
             | 
| 236 | 
            -
             | 
| 237 | 
            -
             | 
| 229 | 
            +
                    public static class KSPlot implements CreatePlot {
         | 
| 230 | 
            +
                        private DescriptiveStatistics stat = null;
         | 
| 231 | 
            +
                        public KSPlot() {
         | 
| 232 | 
            +
                            stat = new DescriptiveStatistics();
         | 
| 233 | 
            +
                        }
         | 
| 234 | 
            +
                        private double[][] createData(double[] xi) {
         | 
| 235 | 
            +
                            int n = xi.length;
         | 
| 236 | 
            +
                            Arrays.sort(xi);
         | 
| 237 | 
            +
                            Arrays.stream(xi).forEach(stat::addValue);
         | 
| 238 | 
            +
                            double m = stat.getMean();     // 平均
         | 
| 239 | 
            +
                            double sd = stat.getStandardDeviation();// 標準偏差
         | 
| 240 | 
            +
                            double sum = stat.getSum();
         | 
| 241 | 
            +
                            double[][] data = new double[n][2];
         | 
| 242 | 
            +
                            double p = 0.0;
         | 
| 243 | 
            +
             | 
| 244 | 
            +
                            for (int i = 0; i < n; i++) {
         | 
| 245 | 
            +
                                p += xi[i] / sum;
         | 
| 246 | 
            +
                                data[i][0] = (xi[i] - m) / sd;
         | 
| 247 | 
            +
                                data[i][1] = p;
         | 
| 248 | 
            +
                            }
         | 
| 249 | 
            +
                            return data;
         | 
| 250 | 
            +
                        }
         | 
| 251 | 
            +
                        public XYPlot createPlot(String dname, double[] xi) {
         | 
| 252 | 
            +
                            XYItemRenderer renderer0 = new XYLineAndShapeRenderer(false, true);
         | 
| 253 | 
            +
                            XYItemRenderer renderer1 = new XYLineAndShapeRenderer(true, false);
         | 
| 254 | 
            +
                            XYToolTipGenerator toolTipGenerator = new StandardXYToolTipGenerator();
         | 
| 255 | 
            +
             | 
| 256 | 
            +
                            renderer0.setDefaultToolTipGenerator(toolTipGenerator);
         | 
| 257 | 
            +
                            renderer0.setURLGenerator(null);
         | 
| 258 | 
            +
                            renderer1.setDefaultToolTipGenerator(toolTipGenerator);
         | 
| 259 | 
            +
                            renderer1.setURLGenerator(null);
         | 
| 260 | 
            +
             | 
| 261 | 
            +
                            XYPlot plot = new XYPlot();
         | 
| 262 | 
            +
                            plot.setOrientation(PlotOrientation.VERTICAL);
         | 
| 263 | 
            +
                            plot.mapDatasetToRangeAxis(0,0);
         | 
| 264 | 
            +
                            plot.mapDatasetToRangeAxis(1,0);
         | 
| 265 | 
            +
            	        plot.setDatasetRenderingOrder(DatasetRenderingOrder.FORWARD);
         | 
| 266 | 
            +
             | 
| 267 | 
            +
                            /*--- 縦軸 ---*/
         | 
| 268 | 
            +
                            NumberAxis valueAxis0 = new NumberAxis("確率");
         | 
| 269 | 
            +
                            plot.setRangeAxis(valueAxis0);
         | 
| 270 | 
            +
                            valueAxis0.setLowerBound(0);
         | 
| 271 | 
            +
                            valueAxis0.setUpperBound(1);
         | 
| 272 | 
            +
                            valueAxis0.setTickUnit(new NumberTickUnit(0.1));
         | 
| 273 | 
            +
                            valueAxis0.setNumberFormatOverride(new DecimalFormat("0.0#"));
         | 
| 274 | 
            +
             | 
| 275 | 
            +
                            plot.setRenderer(0, renderer0);
         | 
| 276 | 
            +
                            plot.setDataset(0, createDataset0(dname, createData(xi)));
         | 
| 277 | 
            +
             | 
| 278 | 
            +
                            plot.setRenderer(1, renderer1);
         | 
| 279 | 
            +
                            plot.setDataset(1, createDataset1());
         | 
| 280 | 
            +
             | 
| 281 | 
            +
                            return plot;
         | 
| 282 | 
            +
                        }
         | 
| 283 | 
            +
                        private XYSeriesCollection createDataset0(String dname, double[][] data) {
         | 
| 284 | 
            +
                            XYSeries cu = new XYSeries(dname);
         | 
| 238 285 |  | 
| 239 | 
            -
             | 
| 240 | 
            -
             | 
| 241 | 
            -
                             | 
| 286 | 
            +
                            for (int i = 0; i < data.length; i++) {
         | 
| 287 | 
            +
                                cu.add(data[i][0], data[i][1]);
         | 
| 288 | 
            +
                            }
         | 
| 289 | 
            +
                            XYSeriesCollection series = new XYSeriesCollection();
         | 
| 242 290 |  | 
| 243 | 
            -
                             | 
| 244 | 
            -
                             | 
| 291 | 
            +
                            series.addSeries(cu);
         | 
| 292 | 
            +
                            return series;
         | 
| 245 293 | 
             
                        }
         | 
| 246 | 
            -
                        XYSeriesCollection  | 
| 294 | 
            +
                        private XYSeriesCollection createDataset1() {
         | 
| 295 | 
            +
                            NormalDistribution ndist = new NormalDistribution(0, 1);
         | 
| 296 | 
            +
                            XYSeries cu = new XYSeries("累積p");
         | 
| 247 297 |  | 
| 248 | 
            -
             | 
| 249 | 
            -
             | 
| 250 | 
            -
                    }
         | 
| 251 | 
            -
                    private XYSeriesCollection createDataset1() {
         | 
| 252 | 
            -
                        NormalDistribution ndist = new NormalDistribution(0, 1);
         | 
| 253 | 
            -
                        XYSeries cu = new XYSeries("累積p");
         | 
| 298 | 
            +
                            for (double x = ChartPlot.CLASS_MIN; x < ChartPlot.CLASS_MAX; x += 0.01) {
         | 
| 299 | 
            +
                                double y = ndist.cumulativeProbability(x);
         | 
| 254 300 |  | 
| 255 | 
            -
             | 
| 256 | 
            -
                             | 
| 301 | 
            +
                                cu.add(x, y);
         | 
| 302 | 
            +
                            }
         | 
| 303 | 
            +
                            XYSeriesCollection series = new XYSeriesCollection();
         | 
| 257 304 |  | 
| 258 | 
            -
                             | 
| 305 | 
            +
                            series.addSeries(cu);
         | 
| 306 | 
            +
                            return series;
         | 
| 259 307 | 
             
                        }
         | 
| 260 | 
            -
             | 
| 308 | 
            +
                    }
         | 
| 309 | 
            +
                }
         | 
| 310 | 
            +
                // Q-QandKSplot
         | 
| 311 | 
            +
                private static class QQKSChartPlot implements ChartPlot {
         | 
| 312 | 
            +
                    private CreatePlot plot0 = null;
         | 
| 313 | 
            +
                    private CreatePlot plot1 = null;
         | 
| 314 | 
            +
                    public QQKSChartPlot() {
         | 
| 315 | 
            +
                        plot0 = new QQChartPlot.QQPlot();
         | 
| 316 | 
            +
                        plot1 = new KSChartPlot.KSPlot();
         | 
| 317 | 
            +
                    }
         | 
| 318 | 
            +
                    public JFreeChart createChart(String title, String dname, double[] xi) {
         | 
| 319 | 
            +
                        XYPlot plot = createPlot(dname, xi);
         | 
| 320 | 
            +
                        
         | 
| 321 | 
            +
                        /*--- 横軸 ---*/
         | 
| 322 | 
            +
                        NumberAxis domainAxis = (NumberAxis)plot.getDomainAxis();
         | 
| 323 | 
            +
                        domainAxis.setLabel("期待値");
         | 
| 324 | 
            +
                        domainAxis.setLowerMargin(0.03);
         | 
| 325 | 
            +
                        domainAxis.setUpperMargin(0.03);
         | 
| 326 | 
            +
                        domainAxis.setLowerBound(ChartPlot.CLASS_MIN);
         | 
| 327 | 
            +
                        domainAxis.setUpperBound(ChartPlot.CLASS_MAX);
         | 
| 261 328 |  | 
| 262 | 
            -
                         | 
| 263 | 
            -
                        return  | 
| 329 | 
            +
                        ChartFactory.setChartTheme(StandardChartTheme.createLegacyTheme());
         | 
| 330 | 
            +
                        return new JFreeChart(title, plot);
         | 
| 264 331 | 
             
                    }
         | 
| 332 | 
            +
                    private XYPlot createPlot(String dname, double[] xi) {
         | 
| 333 | 
            +
                        CombinedDomainXYPlot plot = new CombinedDomainXYPlot();
         | 
| 334 | 
            +
             | 
| 335 | 
            +
                        plot.add(plot0.createPlot(dname, xi), 1);
         | 
| 336 | 
            +
                        plot.add(plot1.createPlot(dname, xi), 1);
         | 
| 337 | 
            +
                        return plot;
         | 
| 338 | 
            +
                    }
         | 
| 339 | 
            +
             | 
| 265 340 | 
             
                }
         | 
| 341 | 
            +
                // KS検定
         | 
| 266 342 | 
             
                private static class KSTest {
         | 
| 267 343 | 
             
                    public boolean test(double[] xi) {
         | 
| 268 344 | 
             
                        double[] data = new double[xi.length];
         | 
| @@ -298,14 +374,14 @@ public class Normality { | |
| 298 374 | 
             
                    public boolean test(double statistic, double a) {
         | 
| 299 375 | 
             
                        double ua_2 = ndist.inverseCumulativeProbability(1.0 - a / 2.0);
         | 
| 300 376 |  | 
| 301 | 
            -
                        return (Math.abs(statistic) >  | 
| 377 | 
            +
                        return (Math.abs(statistic) > calcNormStatic(ua_2))
         | 
| 302 378 | 
             
                            ? true : false;
         | 
| 303 379 | 
             
                    }
         | 
| 304 | 
            -
                    private double  | 
| 380 | 
            +
                    private double calcNormStatic(double ua_2) {
         | 
| 305 381 | 
             
                       double  el = (n + 1.0) * (n + 1.0) * (n + 3.0) * (n + 5.0); // 分子
         | 
| 306 382 | 
             
                       double  den = 24 * n * (n - 2.0) * (n - 3.0);               // 分母
         | 
| 307 383 | 
             
                       double b2p = Math.sqrt(el / den) * 
         | 
| 308 | 
            -
                              ( | 
| 384 | 
            +
                              (ua_2 + 3.0 / (2.0 * n) * (ua_2 * ua_2 * ua_2 - 3 * ua_2));
         | 
| 309 385 |  | 
| 310 386 | 
             
                        return b2p;
         | 
| 311 387 | 
             
                    }
         | 
| @@ -322,33 +398,43 @@ public class Normality { | |
| 322 398 |  | 
| 323 399 | 
             
                        Arrays.stream(xi).forEach(stat::addValue);
         | 
| 324 400 | 
             
                        n = stat.getN();
         | 
| 325 | 
            -
                  
         | 
| 326 401 | 
             
                        return stat.getKurtosis();
         | 
| 327 402 | 
             
                    }
         | 
| 328 403 | 
             
                    public boolean test(double statistic, double a) {
         | 
| 329 404 | 
             
                        boolean ret = false;
         | 
| 330 405 | 
             
                        double ua_2 = ndist.inverseCumulativeProbability(1.0 - a / 2.0);
         | 
| 331 | 
            -
                        double b2p = cnvb2tob2p(statistic);
         | 
| 332 406 |  | 
| 333 407 | 
             
                        double r_val = ua_2 + Math.sqrt(6.0 / n) * (ua_2 * ua_2 - 1.0);
         | 
| 334 408 | 
             
                        double l_val = -1.0 * ua_2 + Math.sqrt(6.0 / n) * (ua_2 * ua_2 - 1.0);
         | 
| 335 409 |  | 
| 336 | 
            -
                        if ( | 
| 410 | 
            +
                        if (statistic > r_val) {
         | 
| 337 411 | 
             
                            ret = true;
         | 
| 338 412 | 
             
                        }
         | 
| 339 | 
            -
                        if ( | 
| 413 | 
            +
                        if (statistic < l_val) {
         | 
| 340 414 | 
             
                            ret = true;
         | 
| 341 415 | 
             
                        }
         | 
| 342 416 | 
             
                        return ret;
         | 
| 343 417 | 
             
                    }
         | 
| 344 | 
            -
             | 
| 345 | 
            -
             | 
| 346 | 
            -
             | 
| 418 | 
            +
                }
         | 
| 419 | 
            +
                // オムニバス検定
         | 
| 420 | 
            +
                private static class OmnibusTest implements DAgostinosTest {
         | 
| 421 | 
            +
                    private DAgostinosTest skewness = null;
         | 
| 422 | 
            +
                    private DAgostinosTest kurtosis = null;
         | 
| 423 | 
            +
                    public OmnibusTest() {
         | 
| 424 | 
            +
                        skewness = new SkewnessTest();
         | 
| 425 | 
            +
                        kurtosis = new KurtosisTest();
         | 
| 426 | 
            +
                    }
         | 
| 427 | 
            +
                    public double calcTestStatistic(double[] xi) {
         | 
| 428 | 
            +
                        double x1 = skewness.calcTestStatistic(xi);
         | 
| 429 | 
            +
                        double x2 = kurtosis.calcTestStatistic(xi);
         | 
| 347 430 |  | 
| 348 | 
            -
             | 
| 349 | 
            -
             | 
| 350 | 
            -
             | 
| 351 | 
            -
             | 
| 431 | 
            +
                        return x1 * x1 + x2 * x2;
         | 
| 432 | 
            +
                    }
         | 
| 433 | 
            +
                    public boolean test(double statistic, double a) {
         | 
| 434 | 
            +
                        ChiSquaredDistribution chi2Dist = new ChiSquaredDistribution(2);
         | 
| 435 | 
            +
                        double p = chi2Dist.cumulativeProbability(statistic);
         | 
| 436 | 
            +
             | 
| 437 | 
            +
                        return (p < a) ? true  : false;
         | 
| 352 438 | 
             
                    }
         | 
| 353 439 | 
             
                }
         | 
| 354 440 | 
             
            }
         | 
    
        data/lib/num4normality.rb
    CHANGED
    
    | @@ -39,6 +39,9 @@ module Num4NormalityLib | |
| 39 39 | 
             
                    def ksplot(dname, xi)
         | 
| 40 40 | 
             
                        Normality.ksplot(dname, xi.to_java(Java::double))
         | 
| 41 41 | 
             
                    end
         | 
| 42 | 
            +
                    def qqksplot(dname, xi)
         | 
| 43 | 
            +
                        Normality.qqksplot(dname, xi.to_java(Java::double))
         | 
| 44 | 
            +
                    end
         | 
| 42 45 | 
             
                    # コルモゴルフ・スミルノフ検定(1標本)
         | 
| 43 46 | 
             
                    #
         | 
| 44 47 | 
             
                    # @overload kstest(xi)
         | 
| @@ -76,6 +79,18 @@ module Num4NormalityLib | |
| 76 79 | 
             
                    def kurtosistest(xi)
         | 
| 77 80 | 
             
                        Normality.kurtosistest(xi.to_java(Java::double))
         | 
| 78 81 | 
             
                    end
         | 
| 82 | 
            +
                    # オムニバス検定
         | 
| 83 | 
            +
                    #
         | 
| 84 | 
            +
                    # @overload omnibustest(xi)
         | 
| 85 | 
            +
                    #   @param  [Array]  xi データ(double[])
         | 
| 86 | 
            +
                    #   @return [boolean] 検定結果(true:棄却域内 false:棄却域外)
         | 
| 87 | 
            +
                    # @example
         | 
| 88 | 
            +
                    #   xi = [320, 240, 402, 325, 440, 286, 362, 281, 560, 212, 198, 209, 374]
         | 
| 89 | 
            +
                    #   Num4NormalityLib.omnibustest(xi)
         | 
| 90 | 
            +
                    #   => false
         | 
| 91 | 
            +
                    def omnibustest(xi)
         | 
| 92 | 
            +
                         Normality.omnibustest(xi.to_java(Java::double))
         | 
| 93 | 
            +
                    end
         | 
| 79 94 | 
             
                end
         | 
| 80 95 | 
             
            end
         | 
| 81 96 |  | 
    
        metadata
    CHANGED
    
    | @@ -1,14 +1,14 @@ | |
| 1 1 | 
             
            --- !ruby/object:Gem::Specification
         | 
| 2 2 | 
             
            name: num4normality
         | 
| 3 3 | 
             
            version: !ruby/object:Gem::Version
         | 
| 4 | 
            -
              version: 0.0. | 
| 4 | 
            +
              version: 0.0.6
         | 
| 5 5 | 
             
            platform: java
         | 
| 6 6 | 
             
            authors:
         | 
| 7 7 | 
             
            - siranovel
         | 
| 8 8 | 
             
            autorequire: 
         | 
| 9 9 | 
             
            bindir: bin
         | 
| 10 10 | 
             
            cert_chain: []
         | 
| 11 | 
            -
            date: 2023-12- | 
| 11 | 
            +
            date: 2023-12-23 00:00:00.000000000 Z
         | 
| 12 12 | 
             
            dependencies:
         | 
| 13 13 | 
             
            - !ruby/object:Gem::Dependency
         | 
| 14 14 | 
             
              name: rake
         |