小编Pas*_*age的帖子

Python:如何得到两个连续分布的卷积?

设 X, Y 为 2 个随机变量,概率密度函数为 pdf1 和 pdf2。

Z = X + Y

那么Z的概率密度函数由pdf1和pdf2的卷积给出。由于我们无法处理连续分布,我们描述了连续分布并处理它们。

为了找到均匀分布和正态分布的卷积,我想出了以下代码。

import matplotlib.pyplot as plt
import numpy as np
import scipy.stats as stats
from scipy import signal


uniform_dist = stats.uniform(loc=2, scale=3)
std = 0.25
normal_dist = stats.norm(loc=0, scale=std)

delta = 1e-4
big_grid = np.arange(-10,10,delta)

pdf1 = uniform_dist.pdf(big_grid)
print("Integral over uniform pdf: "+str(np.trapz(pdf1, big_grid)))

pdf2 = normal_dist.pdf(big_grid)
print("Integral over normal pdf: "+str(np.trapz(pdf2, big_grid)))


conv_pdf = signal.fftconvolve(pdf1,pdf2,'same')
print("Integral over convoluted pdf: "+str(np.trapz(conv_pdf, big_grid)))

plt.plot(big_grid,pdf1, label='Tophat')
plt.plot(big_grid,pdf2, label='Gaussian …
Run Code Online (Sandbox Code Playgroud)

python probability scipy

7
推荐指数
1
解决办法
6311
查看次数

Python卡方拟合优度测试以获得最佳分布

给定一组数据值,我试图获得能够很好地描述数据的最佳理论分布。经过几天的研究,我想出了以下 python 代码。

import numpy as np
import csv
import pandas as pd
import scipy.stats as st
import math
import sys
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt

def fit_to_all_distributions(data):
    dist_names = ['fatiguelife', 'invgauss', 'johnsonsu', 'johnsonsb', 'lognorm', 'norminvgauss', 'powerlognorm', 'exponweib','genextreme', 'pareto']

    params = {}
    for dist_name in dist_names:
        try:
            dist = getattr(st, dist_name)
            param = dist.fit(data)

            params[dist_name] = param
        except Exception:
            print("Error occurred in fitting")
            params[dist_name] = "Error"

    return params 


def get_best_distribution_using_chisquared_test(data, params):

    histo, bin_edges = np.histogram(data, bins='auto', normed=False)
    number_of_bins …
Run Code Online (Sandbox Code Playgroud)

python statistics scipy

5
推荐指数
1
解决办法
9005
查看次数

如何在JAVA非阻塞I / O(NIO)API中使用多个内核?

JAVA NIO提供了使用NIO架构编写TCP服务器的API,如下所示。

import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.text.ParseException;
import java.util.*;

public class NIOServer implements Runnable{
    private InetAddress addr;
    private int port;
    private Selector selector;

    public NIOServer(InetAddress addr, int port) throws IOException {
        this.addr = addr;
        this.port = port;
    }

    public void run(){
        try {
            startServer();
        }catch(IOException ex){
            System.out.println(ex.getMessage());
        }
    }

    private void startServer() throws IOException {

        this.selector = Selector.open();
        ServerSocketChannel serverChannel = …
Run Code Online (Sandbox Code Playgroud)

java nio

2
推荐指数
1
解决办法
121
查看次数

标签 统计

python ×2

scipy ×2

java ×1

nio ×1

probability ×1

statistics ×1