probability-density

Integrate 2D kernel density estimate

梦想与她 提交于 2019-12-17 18:58:54
问题 I have a x,y distribution of points for which I obtain the KDE through scipy.stats.gaussian_kde. This is my code and how the output looks (the x,y data can be obtained from here): import numpy as np from scipy import stats # Obtain data from file. data = np.loadtxt('data.dat', unpack=True) m1, m2 = data[0], data[1] xmin, xmax = min(m1), max(m1) ymin, ymax = min(m2), max(m2) # Perform a kernel density estimate (KDE) on the data x, y = np.mgrid[xmin:xmax:100j, ymin:ymax:100j] positions = np

2d density plot from curves

烂漫一生 提交于 2019-12-14 03:58:17
问题 I have a multi-parameter function on which I infer the parameters using MCMC. This means that I have many samples of the parameters, and I can plot the functions: # Simulate some parameters. Really, I get these from MCMC sampling. first = rnorm(1000) # a second = rnorm(1000) # b # The function (geometric) geometric = function(x, a, b) b*(1 - a^(x + 1)/a) # Plot curves. Perhaps not the most efficient way, but it works. curve(geometric(x, first[1], second[1]), ylim=c(-3, 3)) # first curve for(i

Add shaded standard error curves to geom_density in ggplot2

天涯浪子 提交于 2019-12-13 12:35:28
问题 I would like to add shaded standard error curves to geom_density using ggplot2 . My code looks like this: data.plot <- data.frame(x = c(rnorm(100, mean = 0, sd = 5), rnorm(100, mean = 1, sd =2 )), g = factor(c(rep(1, 100), rep(2,100)))) ggplot(data.plot, aes(x, linetype = g)) + geom_density() I couldn't find a tutorial or examples to do so. Thank you. 回答1: The best solution is with bootstrapping, as mentioned in the comments. I'll use the classic iris data, focusing on the density of Sepal

R - Bootstrapped Confidence Interval - Obtain Parameters of Upper and Lower Bounds

。_饼干妹妹 提交于 2019-12-13 08:59:12
问题 I used bootstrapping to obtain confidence intervals of a Weibull distribution. Then I plotted the Confidence Bands in a plot. Code is below: set.seed(123) rw.small<-rweibull(100,shape=1.781096,scale=33.669511) xs <- seq(0,100, len=500) boot.pdf <- sapply(1:100, function(i) { xi <- sample(rw.small, size=length(rw.small), replace=TRUE) MLE.est <- suppressWarnings(fitdist(xi, distr="weibull",lower=0)) dweibull(xs, shape=MLE.est$estimate["shape"], scale = MLE.est$estimate["scale"]) }) par(bg=

The dimensions in hist for numpy.histogram with density = True

时光总嘲笑我的痴心妄想 提交于 2019-12-12 19:19:38
问题 let's say I have this array A: array([ 0.0019879 , -0.00172861, -0.00527226, 0.00639585, -0.00242005, -0.00717373, 0.00371651, 0.00164218, 0.00034572, -0.00864304, -0.00639585, 0.006828 , 0.00354365, 0.00043215, -0.00440795, 0.00544512, 0.00319793, 0.00164218, 0.00025929, -0.00155575, 0.00129646, 0.00259291, -0.0039758 , 0.00328436, 0.00207433, 0.0011236 , 0.00440795, 0.00164218, -0.00319793, 0.00233362, 0.00025929, 0.00017286, 0.0008643 , 0.00363008]) If I run: np.histogram(A, bins=9,

Random number with given PDF in Python

喜欢而已 提交于 2019-12-12 13:01:18
问题 I want to generate an integer random number with a probability distribution function given as a list. For example if pdf=[3,2,1] then I like rndWDist(pdf) to return 0,1, and 2, with probabilities of 3/6, 2/6, and 1/6. I wrote my own function for that since I couldn't find it in the random module. def randintWDist(pdf): cdf=[] for x in pdf: if cdf: cdf.append(cdf[-1]+x) else: cdf.append(x) a=random.randint(1,cdf[-1]) i=0 while cdf[i]<a: i=i+1 return i Is there any shorter method to achieve the

Generate an array of random integers with non-uniform distribution

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-12 10:16:36
问题 I want to write Java code to produce an array of random integers in the range [1,4]. The array's length is N, which is provided at run time. The problem is that the range [1,4] is not uniformly distributed: It means that if I create arrays with N=100, the number '1' will appear averagely 40 times in an array, number '2' 10 times, and so on. For now I am using this code to generate uniform-distributed random numbers in range [1,4]: public static void main(String[] args) { int N; System.out

How do I perform a convolution in python with a variable-width Gaussian?

萝らか妹 提交于 2019-12-12 08:34:10
问题 I need to perform a convolution using a Gaussian, however the width of the Gaussian needs to change. I'm not doing traditional signal processing but instead I need to take my perfect Probability Density Function (PDF) and ``smear" it, based on the resolution of my equipment. For instance, suppose my PDF starts out as a spike/delta-function. I'll model this as a very narrow Gaussian. After being run through my equipment, it will be smeared out according to some Gaussian resolution. I can

Calculating the derivative of cumulative density function in Python

拜拜、爱过 提交于 2019-12-12 08:29:49
问题 Is it the case that the exact derivative of a cumulative density function is the probability density function (PDF)? I am calculating the derivative using the numpy.diff() , is this correct? See below code below: import scipy.stats as s import matplotlib.pyplot as plt import numpy as np wei = s.weibull_min(2, 0, 2) # shape, loc, scale - creates weibull object sample = wei.rvs(1000) shape, loc, scale = s.weibull_min.fit(sample, floc=0) x = np.linspace(np.min(sample), np.max(sample)) plt.hist

How to extract fitted data from normal probability density function

耗尽温柔 提交于 2019-12-11 21:58:56
问题 If I fit a uni-variate data with normal distribution, how can i get back the fitted values in MATLAB. I am using this simple example load hospital % data x = hospital.Weight; [mu sigma]=normfit(x) %normal fitting %To visualize the pdf xval=min(x):0.1:max(x) yval=normpdf(xval,mu,sigma) plot(xval,yval) yval is giving the probabilities of xval values. Now, If I would like to extract the fitted values of 'x' after approximating it with the above normal distribution, how do I do that?. As can be