probability

Ruby - Picking an element in an array with 50% chance for a[0], 25% chance for a[1]

好久不见. 提交于 2019-12-07 13:10:43
问题 Nothing too complicated, basically I just want to pick an element from the array as if I were making coin tosses for each index and and choosing the index when I first get a head. Also no heads means I choose the last bin. I came up with the following and was wondering if there was a better/more efficient way of doing this. def coin_toss(size) random_number = rand(2**size) if random_number == 0 return size-1 else return (0..size-1).detect { |n| random_number[n] == 1 } end end 回答1: First guess

Using a Naive Bayes Classifier to classify tweets: some problems

孤街醉人 提交于 2019-12-07 10:21:18
问题 Using, amongst other sources, various posts here on Stackoverflow, I'm trying to implement my own PHP classier to classify tweets into a positive, neutral and negative class. Before coding, I need to get the process straigt. My train-of-thought and an example are as follows: p(class) * p(words|class) Bayes theorem: p(class|words) = ------------------------- with p(words) assumption that p(words) is the same for every class leads to calculating arg max p(class) * p(words|class) with p(words

Why am I getting dups with random.shuffle in Python?

我是研究僧i 提交于 2019-12-07 09:44:24
问题 For a list of 10 ints, there are 10! possible orders or permutations. Why does random.shuffle give duplicates after only 5000 tries? >>> L = range(10) >>> rL = list() >>> for i in range(5000): ... random.shuffle(L) ... rL.append(L[:]) ... >>> rL = [tuple(e) for e in rL] >>> len(set(rL)) 4997 >>> for i,t in enumerate(rL): ... if rL.count(t) > 1: ... print i,t ... 102 (7, 5, 2, 4, 0, 6, 9, 3, 1, 8) 258 (1, 4, 0, 2, 7, 3, 5, 9, 6, 8) 892 (1, 4, 0, 2, 7, 3, 5, 9, 6, 8) 2878 (7, 5, 2, 4, 0, 6, 9,

Randomize matrix elements between two values while keeping row and column sums fixed (MATLAB)

血红的双手。 提交于 2019-12-07 09:30:17
问题 I have a bit of a technical issue, but I feel like it should be possible with MATLAB's powerful toolset. What I have is a random n by n matrix of 0's and w's, say generated with A=w*(rand(n,n)<p); A typical value of w would be 3000, but that should not matter too much. Now, this matrix has two important quantities, the vectors c = sum(A,1); r = sum(A,2)'; These are two row vectors, the first denotes the sum of each column and the second the sum of each row. What I want to do next is randomize

possible distributions and their probabilities after putting identical items into anonymous buckets

只愿长相守 提交于 2019-12-07 08:47:35
问题 Apologies if the answer to this is readily found elsewhere. My math and stats are weak and thus I don't even know the search terms for what I'm trying to do . . . I have b anonymous indistinguishable buckets into which I'm putting i identical items. I want to know all possible distributions and their probabilities. For example, if I have 3 buckets and 3 items, the answer I want is: [3,0,0] -> 1/9 [2,1,0] -> 6/9 [1,1,1] -> 2/9 Notice that the buckets are anonymous and thus I want identical

Iteration performance

℡╲_俬逩灬. 提交于 2019-12-07 08:35:31
I made a function to evaluate the following problem experimentally, taken from a A Primer for the Mathematics of Financial Engineering . Problem : Let X be the number of times you must flip a fair coin until it lands heads. What are E[X] (expected value) and var(X) (variance)? Following the textbook solution, the following code yields the correct answer: from sympy import * k = symbols('k') Expected_Value = summation(k/2**k, (k, 1, oo)) # Both solutions work Variance = summation(k**2/2**k, (k, 1, oo)) - Expected_Value**2 To validate this answer, I decided to have a go at making a function to

R: monte carlo integration using Importance Sampling

徘徊边缘 提交于 2019-12-07 06:19:55
问题 I have an integral to evaluate "x^(-0.5)" ; x in [0.01,1] for which I am using Importance Sampling MC : The theory says that an approximate PDF has to be used to compute the expected value (which will almost surely converge to the mean - value of the integral) After plotting the given integral, and exponential PDF, based only on the plots, I chose the rexp and dexp to generate the PDF - and my code looks like this - #Without Importance Sampling set.seed(1909) X <- runif(1000,0.01,1) Y <- X^(

How to create a probability by a given percentage?

﹥>﹥吖頭↗ 提交于 2019-12-07 03:05:36
问题 I'm trying to create a percentage-based probability for a game. E.g. if an item has a 45% chance of a critical hit, that must mean it is 45 of 100 hits would be critical. First, I tried to use a simple solution: R = new Random(); int C = R.Next(1, 101); if (C <= ProbabilityPercent) DoSomething() But in 100 iterations with a chance of e.g. 48%, it gives 40-52 out of 100. Same goes for 49, 50, 51. So, there is no difference between these "percents". The question is how to set a percentage of e

Poisson point process in matlab

柔情痞子 提交于 2019-12-06 14:58:50
问题 I am new with poisson point process. I did one simluation (matlab) as below. My intensity lambda = 50 ; clear all; lambda=50; npoints = poissrnd(lambda); pproc = rand(npoints, 2); plot(pproc(:, 1), pproc(:, 2), '.'); Then I have plot, However, the link http://connor-johnson.com/2014/02/25/spatial-point-processes/ showed me that when intensity lamuda = 0.2, smaller than 1 , he got The link also showed the code in Python.Please check it. Here is my question, why intensity is smaller than 1 , he

Estimate a probability from elements of a list in R

南楼画角 提交于 2019-12-06 13:37:16
问题 I have a list of 100,000 simulated numbers of T in R (min: 1.5, max 88.8) and I want to calculate the probability of T being between 10 and 50. I sumulated 100,000 numbers of T, where T is t(y) %*% M %*% y where M is a 8x8 matrix of constant values and y is a 8x1 matrix. The element in the i -th row if y, is equal to: a_i + b_i where a is a vector of constants and b is a vector whose elements follow a normal (0,sd=2) distribution (each element is a different simulated number of N(0,2) ) 回答1: