# simple sampling method for a Kernel Density Estimator

I have developed a simple Kernel Density Estimator in Java, based on a few dozen points (maybe up to one hundred or so) and a Gaussian kernel function. The implementation gives me the PDF and CDF of my probability distribution at any point.

I would now like to implement a simple sampling method for this KDE. An obvious choice would of course be to draw from the very set of points making up the KDE, but I would like to be able to retrieve points that are slightly different from the ones in the KDE.

I haven't found so far a sampling technique that I could easily implement to solve this problem (without depending on external libraries for numerical integration or complex computations). Any advices? I don't have specially strong requirements when it comes to precision or efficiency, my main concern is to have a sampling function that works and can be easily implemented. Thanks!

• This is detailed in page 5 of this document. – user10525 Nov 15 '12 at 18:21
• thanks, that was useful! And simpler than I thought ;-) – Pierre Lison Nov 15 '12 at 19:49
• @user10525 the code provided is incorrect, it should be: rnorm(n, sample(dx$x, n, prob = dx$y, replace = TRUE), dx$bw) where dx is output from density function. Argument prob has to be provided because otherwise you sample uniformly. – Tim Dec 22 '15 at 20:29 ## 1 Answer As mentioned by Procrastinator, there's a simple way to sample from a Kernel density estimator: 1. Draw one point$x_i$from the set of points$x_1$,...$x_n$included in the KDE 2. Once you have the point$x_i$, draw a value from the kernel associated with the point. In this case, draw from the Gaussian$\mathcal{N}(x_i,h)$centered at$x_i$and of variance$h$(the bandwidth) • (+1) For sharing your solution. – user10525 Nov 19 '12 at 10:15 • Is$x_i$one of the original points? If so, looks like we don't really need to construct the actual KDE at all. Just sampling from one of the original points, and$N (x_i,h)\$ should suffice? – Ram Apr 8 '13 at 23:19
• Yes indeed, if you are only using the KDE distribution for sampling, you do not need to explicitly construct the PDF: the only information necessary for the sampling operation is the set of points and the bandwidth. – Pierre Lison Apr 9 '13 at 6:28
• just to add to Pierre Lison: In step 2.: For sampling from a Gaussian kernel, the bandwidth h should be taken as the standard deviation of the Gaussian distribution around the point x_i, not the variance. – user98904 Dec 22 '15 at 18:52
• Wouldn't you want to sample using standard deviation 1/h or something? As written, the less likely x_i is, the more likely you are to sample another unlikely point nearby because the standard deviation of N is low. – Chris Anderson Jul 3 '19 at 21:24
{"ft_lang_label":"__label__en","ft_lang_prob":0.92741716,"math_prob":0.95534396,"size":1099,"snap":"2020-24-2020-29","text_gpt3_token_len":240,"char_repetition_ratio":0.101369865,"word_repetition_ratio":0.0,"special_character_ratio":0.21292084,"punctuation_ratio":0.09047619,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.99193275,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-06-04T05:37:49Z\",\"WARC-Record-ID\":\"<urn:uuid:ab6ba488-42f6-448c-85f2-8bddec19bbff>\",\"Content-Length\":\"152216\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:3e31f7fa-31ce-4105-b966-b672d0f95866>\",\"WARC-Concurrent-To\":\"<urn:uuid:c0f0952d-79c5-44b8-9408-aee8283111b8>\",\"WARC-IP-Address\":\"151.101.65.69\",\"WARC-Target-URI\":\"https://stats.stackexchange.com/questions/43674/simple-sampling-method-for-a-kernel-density-estimator\",\"WARC-Payload-Digest\":\"sha1:3P2L56FZV4R2B5KPK7YMNA4XAPZJKSJZ\",\"WARC-Block-Digest\":\"sha1:MLVFPNBFEEME26WIIPOIOGTAW6IDGQZI\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-24/CC-MAIN-2020-24_segments_1590347439019.86_warc_CC-MAIN-20200604032435-20200604062435-00447.warc.gz\"}"}