montecarlo

Create 10,000 date data.frames with fake years based on 365 days window

邮差的信 提交于 2019-12-13 07:27:50
问题 Here my time period range: start_day = as.Date('1974-01-01', format = '%Y-%m-%d') end_day = as.Date('2014-12-21', format = '%Y-%m-%d') df = as.data.frame(seq(from = start_day, to = end_day, by = 'day')) colnames(df) = 'date' I need to created 10,000 data.frames with different fake years of 365days each one. This means that each of the 10,000 data.frames needs to have different start and end of year. In total df has got 14,965 days which, divided by 365 days = 41 years. In other words, df

Perform a RandomWalk step with Tensorflow Probability's RandomWalkMetropolis function

醉酒当歌 提交于 2019-12-13 04:39:11
问题 I am new to Tensorflow Probability and would like to do a RandomWalk Montecarlo simulation. Let's say I have tensor r that represents a state. I want the tfp.mcmc.RandomWalkMetropolis function to return a proposal for a new state r'. tfp.mcmc.RandomWalkMetropolis(r) >>> <tensorflow_probability.python.mcmc.random_walk_metropolis.RandomWalkMetropolis object at 0x14abed2185c0> Instead of the same state, or a slightly perturbed state only this RandomWalkMetropolis object is returned. The

Using OpenMP to calculate the value of PI

我的梦境 提交于 2019-12-12 12:04:25
问题 I'm trying to learn how to use OpenMP by parallelizing a monte carlo code that calculates the value of PI with a given number of iterations. The meat of the code is this: int chunk = CHUNKSIZE; count=0; #pragma omp parallel shared(chunk,count) private(i) { #pragma omp for schedule(dynamic,chunk) for ( i=0; i<niter; i++) { x = (double)rand()/RAND_MAX; y = (double)rand()/RAND_MAX; z = x*x+y*y; if (z<=1) count++; } } pi=(double)count/niter*4; printf("# of trials= %d , estimate of pi is %g \n"

Monte Carlo Simulation in Python

点点圈 提交于 2019-12-12 10:17:56
问题 I just wrote a simple code for a monte carlo simulation: def loss(r, loc, arg, scale, lam): X = [] for x in range(27000): if(r < poisson.cdf(x, lam)): out = 0 else: out = lognorm.rvs(s=arg,loc=loc, scale=scale) X.append(out) return np.sum(X) losses = [] for _ in range(2000): r = np.random.random() losses.append(loss(r, loc, arg, scale, lam)) E = np.sum(losses)/len(losses) print(E) plt.hist(losses, bins='auto') But now, the sum is only consisting of lognormal distributed random variables - Is

Simulating the Geometric Brownian Motion

爱⌒轻易说出口 提交于 2019-12-12 06:56:55
问题 Background Information: Consider the Pseudocode: Question: What I seem to be having a problem with is the S superscript j part of the Pseudocode of algorithm 1, my professor said that since N = 10,000 and n = 10, I am computing 10 S's. Here is the part of my code that I having trouble with: double Z[n][N]; for(int j = 0; j < N; j++){ for(int i = 0; i < n/2; i++){ Z[2*i][j] = Box_MullerX(n,N,shifted_hs[i],shifted_hs[i+1]); Z[2*i+1][j] = Box_MullerY(n,N,shifted_hs[i],shifted_hs[i+1]); } } /*for

Why is my python 3 implementation much faster than the one I wrote in C++?

那年仲夏 提交于 2019-12-12 03:28:03
问题 I know that C++ should be much faster than Python 3 because it is a compiled language as opposed to an interpreted language. I wrote 2 two programs that use the Monte Carlo Simulation to calculate Pi , one in Python 3 and the other in C++. Python turned out to be approximately 16x faster than C++. As seen in the photos bellow, with a repetition value of ( 10,000,000 ), Python takes 8.5 seconds whilst C++ takes 137.4 seconds. I'm new to C++ but I can't find posts online that explains this

Monte-Carlo Simulation of expected tosses for two consecutive heads in python

我的未来我决定 提交于 2019-12-12 03:03:56
问题 The expected number of tosses to get two heads in a row is 6. However on executing the below simulation of multiple runs of this experiment, I get a different plot from that expected. Could you help me identify the error in the logic? Thanks Code to simulate tosses and check at when two consecutive heads are encountered repeated 10000 times: import random def toss(): return random.randint(0, 1) expected_tosses=list() for test in range(10000): a=toss() for i in range(2,100): b=toss() if a and

Efficient way to randomly select set bit

耗尽温柔 提交于 2019-12-12 02:30:00
问题 My current hobby project provides Monte-Carlo-Simulations for card games with French decks (52 cards, from 2 to Ace). To simulate as fast as possible, I use to represent multiple cards as bitmasks in some spots. Here is some (simplified) code: public struct Card { public enum CardColor : byte { Diamonds = 0, Hearts = 1, Spades = 2, Clubs = 3 } public enum CardValue : byte { Two = 0, Three = 1, Four = 2, Five = 3, Six = 4, Seven = 5, Eight = 6, Nine = 7, Ten = 8, Jack = 9, Queen = 10, King =

Volume with Monte Carlo method

笑着哭i 提交于 2019-12-12 01:53:04
问题 I have a question. I must build a program to calculate a volume of a cube with Monte Carlo method. Cube has a beginning in (0,0,0) on XYZ axis. There can also be some spheres. The spheres can be in cube then the volume of cube is := cube vol. - sphere vol. This sphere can also be outside the cube. I know how this method works but i have problem to define interval of random points ( i only know to calculate an integral with this method). The parameters are: a - lenght of cube side, point - (X

MATLAB: Pricing a digital option, Monte Carlo vs. explicit integral formula?

笑着哭i 提交于 2019-12-11 21:14:53
问题 I am stuck with the following problem using MATLAB: Let Z be lognormally distributed such that ln Z has mean m and variance w. Let eta be a negative number and c a positive constant. I am trying to compute the expected value (let I(Z<=c) denote the indicator function of the set (Z<=c)) E[Z^(eta+1) I(Z<=c)] = (1/sqrt(w)) integral_0^c x^(eta) phi((ln x - m)/sqrt(w)) dx, where phi() denotes the probability distribution function of a standard normal random variable. First thing I did was to