sampling

Sample with a max

隐身守侯 提交于 2019-12-01 18:08:48
问题 If I want to sample numbers to create a vector I do: set.seed(123) x <- sample(1:100,200, replace = TRUE) sum(x) # [1] 10228 What if I want to sample 20 random numbers that sum to 100, and then 30 numbers but still sum to 100. This I imagine will be more of a challenge than it seems. ?sample and searching Google has not provided me with a clue. And a loop to sample then reject if not close enough( e.g. within 5) of the desired sum I guess may take some time. Is there a better way to achieve

Monte Carlo integration using importance sampling given a proposal function

痞子三分冷 提交于 2019-12-01 14:11:37
Given a Laplace Distribution proposal: g(x) = 1/2*e^(-|x|) and sample size n = 1000 , I want to Conduct the Monte Carlo (MC) integration for estimating θ: via importance sampling. Eventually I want to calculate the mean and standard deviation of this MC estimate in R once I get there. Edit (arrived late after the answer below) This is what I have for my R code so far: library(VGAM) n = 1000 x = rexp(n,0.5) hx = mean(2*exp(-sqrt(x))*(sin(x))^2) gx = rlaplace(n, location = 0, scale = 1) Now we can write a simple R function to sample from Laplace distribution: ## `n` is sample size rlaplace <-

Stratified sample when some strata are too small

99封情书 提交于 2019-12-01 11:58:28
I need to draw a stratified sample with n observation in each stratum, but some strata have fewer observations than n . If a stratum has too few observations (say, k<n observations), I want to sample all k observations from that stratum. require(sampling) n <- 10 geo_ID <- c(rep(1, times = 20), rep(2, times = 20), rep(c(1, 2, 3, 4), times = 5)) set.seed(42) V1 <- rnorm(60, 0, 1) V2 <- rnorm(60, 2, 1) DF <- data.frame(geo_ID = geo_ID, V1 = V1, V2 = V2) #Sort as explained in ?strata help file DF <- DF[order(DF[, "geo_ID"]), ] strata(DF, stratanames = "geo_ID", size = c(n, n, n, n), method =

MATLAB: Comparing 2 arrays with different lengths

陌路散爱 提交于 2019-12-01 10:58:35
I have two arrays with different lengths (due to different sampling rates) that I need to compare. I'd like to downsample the larger array to match the smaller one in length, however the factor is not an integer but a decimal. For an example: a = 1 1.375 1.75 2.125 2.5 2.875 3.25 b = 1 2 3 Is there any way to manipulate these arrays to match lengths? That's easy to do with clever use of interp1 . The trick is that the keypoints used for interpolation is an array going from 1 up to as many values as you have in a which we will call N , and the interpolated keypoints would be a linearly

Monte Carlo integration using importance sampling given a proposal function

十年热恋 提交于 2019-12-01 10:31:23
问题 Given a Laplace Distribution proposal: g(x) = 1/2*e^(-|x|) and sample size n = 1000 , I want to Conduct the Monte Carlo (MC) integration for estimating θ: via importance sampling. Eventually I want to calculate the mean and standard deviation of this MC estimate in R once I get there. Edit (arrived late after the answer below) This is what I have for my R code so far: library(VGAM) n = 1000 x = rexp(n,0.5) hx = mean(2*exp(-sqrt(x))*(sin(x))^2) gx = rlaplace(n, location = 0, scale = 1) 回答1:

Stratified sample when some strata are too small

你。 提交于 2019-12-01 10:11:34
问题 I need to draw a stratified sample with n observation in each stratum, but some strata have fewer observations than n . If a stratum has too few observations (say, k<n observations), I want to sample all k observations from that stratum. require(sampling) n <- 10 geo_ID <- c(rep(1, times = 20), rep(2, times = 20), rep(c(1, 2, 3, 4), times = 5)) set.seed(42) V1 <- rnorm(60, 0, 1) V2 <- rnorm(60, 2, 1) DF <- data.frame(geo_ID = geo_ID, V1 = V1, V2 = V2) #Sort as explained in ?strata help file

Stratified sampling - not enough observations

我的梦境 提交于 2019-12-01 09:18:14
问题 What I would like to achieve is get a 10% sample from each group (which is a combination of 2 factors - recency and frequency category). So far I have thought about package sampling and function strata() . Which looks promising but I am getting the following error and it is really hard to understand the error message and what is wrong or how to get around this. Here is my code: > d[1:10,] date id_email_op recency frequecy r_cat f_cat 1 29.8.2011 19393 294 1 A G 2 29.8.2011 19394 230 4 A D 3

Efficient algorithm for generating unique (non-repeating) random numbers

天涯浪子 提交于 2019-12-01 08:13:40
I want to solve the following problem. I have to sample among an extremely large set, of the order of 10^20 and extracting a sample without repetitions of size about 10%-20%. Given the size of the set, I believe that an algorithm like Fisher–Yates is not feasible. I'm thinking that something like random path tree might work for doing it in O(n log n) and can't be done faster, but I want to ask if something like this has already been implemented. Thank you for your time! I don't know how well the technique I describe below would do on formal tests of randomness, but it does give "random-looking

MATLAB: Comparing 2 arrays with different lengths

為{幸葍}努か 提交于 2019-12-01 08:07:34
问题 I have two arrays with different lengths (due to different sampling rates) that I need to compare. I'd like to downsample the larger array to match the smaller one in length, however the factor is not an integer but a decimal. For an example: a = 1 1.375 1.75 2.125 2.5 2.875 3.25 b = 1 2 3 Is there any way to manipulate these arrays to match lengths? 回答1: That's easy to do with clever use of interp1. The trick is that the keypoints used for interpolation is an array going from 1 up to as many

How do I call CMSampleBufferGetAudioBufferListWithRetainedBlockBuffer?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-01 06:02:07
I'm trying to figure out how to call this AVFoundation function in Swift. I've spent a ton of time fiddling with declarations and syntax, and got this far. The compiler is mostly happy, but I'm left with one last quandary. public func captureOutput( captureOutput: AVCaptureOutput!, didOutputSampleBuffer sampleBuffer: CMSampleBuffer!, fromConnection connection: AVCaptureConnection! ) { let samplesInBuffer = CMSampleBufferGetNumSamples(sampleBuffer) var audioBufferList: AudioBufferList var buffer: Unmanaged<CMBlockBuffer>? = nil CMSampleBufferGetAudioBufferListWithRetainedBlockBuffer(