设 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 代码。
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) 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)