Poisson distribution and Poisson cumulative distribution plot via sqlite in Python and Java

Poisson distribution and Poisson cumulative distribution plot via sqlite in Python and Java

Last time Graph the Poisson distribution and the Poisson cumulative distribution in Python and Java, respectively. I plotted the data created in Java with Matplotlib, but the one that was bridging the data with CSV I rewrote it with SQlite.

The Sqlite API uses 3.6 standard for Python and here for Java.

The code below creates tables Poisson and Poisson CDF in math_data.db and puts the probability mass and accumulation for each λ.

Java code

poisson.java




    public static void main(String[] args) {
        Connection connection = null;
        Statement statement = null;
        Calc c = new Calc();
        //Changed the method to store the λ value in the list
        int[] lamList = {1, 4, 8, 10};
        try {
            double p = 0;
            Class.forName("org.sqlite.JDBC");
            connection = DriverManager.getConnection("jdbc:sqlite:math_data.db");
            statement = connection.createStatement();
            connection.setAutoCommit(false);

            //Create table
            statement.executeUpdate("DROP TABLE IF EXISTS Poisson");
            statement.executeUpdate("CREATE TABLE IF NOT EXISTS Poisson( lam integer, count integer , p real )");
            //Enter the value
            PreparedStatement preparedStatement = connection.prepareStatement("INSERT INTO Poisson values (?, ?, ?);");
//Probability that an event that occurs λ times in a certain period occurs i times in a certain period p

            for (int i = 0; i < lamList.length; i++) {

                for (int j = 0; j <= 12; j++) {
                    p = c.poisson(lamList[i], j);

                    preparedStatement.setInt(1, lamList[i]);
                    preparedStatement.setInt(2, j);
                    preparedStatement.setDouble(3, p);
                    preparedStatement.addBatch();
                }

            }
            System.out.println(preparedStatement.executeBatch().length + "Register the batch.");
            connection.commit();


//Cumulative distribution
            statement.executeUpdate("DROP TABLE IF EXISTS PoissonCDF");
            statement.executeUpdate("CREATE TABLE IF NOT EXISTS PoissonCDF( lam integer, count integer , p real )");
            //Enter the value
            preparedStatement = connection.prepareStatement("INSERT INTO PoissonCDF values (?, ?, ?);");
            //Cumulative probability that an event that occurs λ times in a certain period occurs i times or less in a certain period p

            for (int i = 0; i < lamList.length; i++) {
                double pTotal = 0;
                for (int j = 0; j <= 12; j++) {
                    p = c.poisson(lamList[i], j);
                    pTotal += p;
                    preparedStatement.setInt(1, lamList[i]);
                    preparedStatement.setInt(2, j);
                    preparedStatement.setDouble(3, pTotal);
                    preparedStatement.addBatch();
                }

            }
//Batch write
            System.out.println(preparedStatement.executeBatch().length + "Register the batch.");
            connection.commit();


        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (statement != null) {
                    statement.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                if (connection != null) {
                    connection.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}


class Calc {

    int factorial(int n) {
        if (n == 0) {
            return 1;
        }
        return n * factorial(n - 1);
    }

    //Probability mass density that occurs only k times when the event that occurs in a certain period is average lam
    double poisson(double lam, int k) {
        double total = 0;

        total = Math.pow(lam, k) * Math.pow(Math.E, -lam) / factorial(k);

        return total;
    }

    //Probability mass density (including 0) that occurs k times or less when the event that occurs in a certain period is average lam
    double poisson_cdf(double lam, int k) {
        double p = 0;
        double total = 0;
        for (int i = 0; i <= k; i++) {
            p = poisson(lam, i);
            total += p;
        }
        return total;
    }
}
//Execution result
//Register 52 batches.
//Register 52 batches.

Execution is completed. Make sure it's in properly.


ResultSet resultSet;
            resultSet = statement.executeQuery("select * from Poisson");
            while (resultSet.next()) {
                System.out.print(resultSet.getString("lam"));
                System.out.print(",");
                System.out.print(resultSet.getString("count"));
                System.out.print(",");
                System.out.println(resultSet.getString("p"));
            }
            resultSet = statement.executeQuery("select * from PoissonCDF");
            while (resultSet.next()) {
                System.out.print(resultSet.getString("lam"));
                System.out.print(",");
                System.out.print(resultSet.getString("count"));
                System.out.print(",");
                System.out.println(resultSet.getString("p"));
            }

            /*
Execution result

            1,0,0.367879441171442
            1,1,0.367879441171442
            1,2,0.183939720585721
            1,3,0.0613132401952404
            1,4,0.0153283100488101
             */

It's firmly in it. Next I would like to read it in Python and plot it in Matplotlib.

Python code

plotting.py


import sqlite3
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns

sns.set(style="darkgrid")

conn = sqlite3.connect("math_data.db")
c = conn.cursor()
c.execute('SELECT * FROM Poisson')

#List
lam_db_list = np.array(c.fetchall())
#Since the number at the beginning of the line is λ, I will summarize it for the time being and plot for each λ
lam_db_f = sorted(list(set(map(lambda x: x[0], lam_db_list))))

fig, axe = plt.subplots(1, 2, constrained_layout=True, figsize=(12, 5))

#Make a list for each type of λ number at the beginning of the line, and plot based on it.
for i in range(len(lam_db_f)):
    y_axis = []
    for j in lam_db_list:
        if j[0:1] == lam_db_f[i]:
            y_axis.append(j[2:])

    y_axis = np.array(y_axis).reshape(-1)
    x_axis = range(len(y_axis))
    axe[0].plot(x_axis, y_axis, marker='o', label='$\lambda=%.2f$' % lam_db_f[i])

c.execute('SELECT * FROM PoissonCDF')
lam_db_list = np.array(c.fetchall())
lam_db_f = sorted(list(set(map(lambda x: x[0], lam_db_list))))

for i in range(len(lam_db_f)):
    y_axis = [j[2:] for j in lam_db_list if j[0:1] == lam_db_f[i]]
    y_axis = np.array(y_axis).reshape(-1)
    x_axis = range(len(y_axis))
    axe[1].plot(x_axis, y_axis, marker='o', label='$\lambda=%.2f$' % lam_db_f[i])

conn.close()
axe[0].set_xlabel('k')
axe[0].set_ylabel('probability')
axe[0].set_title('Poisson')
axe[0].legend()
axe[0].grid(True)
axe[0].set_xticks(range(len(x_axis)))
axe[1].set_xlabel('k')
axe[1].set_ylabel('probability')
axe[1].set_title('PoissonCDF')
axe[1].legend()
axe[1].grid(True)
axe[1].set_xticks(range(len(x_axis)))

plt.savefig("poisson_n_cdf.png ")
plt.show()

Figure_1.png

I was able to plot it neatly. You can also plot various Poisson distributions by changing the λ array passed on the Java side.

poisson_n_cdf.png

poisson_n_cdf.png

Difficulties

I created the DB in reverse matrix and tried to calculate all the arrays by brute force from the first line number, but the code became complicated. In order to make it easier to use later, we have to think about the output at the stage of creating the DB. Especially if the data deals with a small number of fixed points, it will go crazy, so I want to avoid having to play with the structure too much later.

Recommended Posts

Poisson distribution and Poisson cumulative distribution plot via sqlite in Python and Java
Graph the Poisson distribution and the Poisson cumulative distribution in Python and Java, respectively.
Plot and understand the multivariate normal distribution in Python
Carefully understand the Poisson distribution and draw in Python
Overlapping regular expressions in Python and Java
Differences in syntax between Python and Java
Sqlite in python
How to plot autocorrelation and partial autocorrelation in python
Implemented List and Bool in Python and SQLite3 (personal note)
Carefully understand the exponential distribution and draw in Python
Plot Bitcoin candle charts and technical indicators in Python
Write beta distribution in Python
Learn cumulative sum in Python
I compared Java and Python!
Generate U distribution in Python
I tried programming the chi-square test in Python and Java.
Stack and Queue in Python
Remove leading and trailing whitespace in Python, JavaScript, or Java
Unittest and CI in Python
Plot geographic information in Python
Solving in Ruby, Python and Java AtCoder ABC141 D Priority Queuing
Solve with Ruby, Python and Java AtCoder ARC104 B Cumulative sum
How to display bytes in the same way in Java and Python
Detect and process signals in Java.
Difference between java and python (memo)
MIDI packages in Python midi and pretty_midi
Difference between list () and [] in Python
Difference between == and is in python
View photos in Python and html
Sorting algorithm and implementation in Python
Mixed normal distribution implementation in python
How to use SQLite in Python
Manipulate files and folders in Python
About dtypes in Python and Cython
Assignments and changes in Python objects
Check and move directories in Python
Ciphertext in Python: IND-CCA2 and RSA-OAEP
Hashing data in R and Python
Foreign Key in Python SQLite [Note]
Function synthesis and application in Python
Export and output files in Python
Reverse Hiragana and Katakana in Python2.7
Reading and writing text in Python
[GUI in Python] PyQt5-Menu and Toolbar-
Create and read messagepacks in Python
Reading from text files and SQLite in Python (+ Pandas), R, Julia (+ DataFrames)
Solving with Ruby, Perl, Java, and Python AtCoder ARC 098 C Cumulative sum
Differences in authenticity between Python and JavaScript
Notes using cChardet and python3-chardet in Python 3.3.1.
Differences between Ruby and Python in scope
AM modulation and demodulation in Python Part 2
difference between statements (statements) and expressions (expressions) in Python
Eigenvalues and eigenvectors: Linear algebra in Python <7>
Express Python yield in JavaScript or Java
Line graphs and scale lines in python
Implement FIR filters in Python and C
Check and receive Serial port in Python (Port check)
Search and play YouTube videos in Python
Difference between @classmethod and @staticmethod in Python
Difference between append and + = in Python list
Difference between nonlocal and global in Python