Tag Info

Hot answers tagged

28

Here's an attempt in which I start with a set of "void points", which will be the centres of the gaps between filaments. The stars are then created as an initially random distribution, and are repeatedly nudged away from their nearest void point. Or, to look at it another way, they are attracted towards the edges of the Voronoi cells defined by the void ...


27

This sounds very similar to a traveling salesman problem for which we have the built-in (as of v8) FindShortestPath function. However, this function minimizes the overall path length (which is the sum of all sub-paths), whereas we need a functions that maximizes the path probability (which is the product of the transition probabilities along the path). ...


22

So your problem reduces to: SeedRandom[8396] RandomReal[NormalDistribution[0, 1], {4, 4, 4}, WorkingPrecision -> 6] It does appear to be caused by reducing WorkingPrecision to 6, because it goes away when you stop forcing Mathematica to behave like a bad pocket calculator. I can't see any reason for you to do this... Better to leave WorkingPrecision ...


21

Mathematica v8 does not provide support for automated random number generation from multivariate distributions, specified in terms of its probability density functions, as you have already discovered it. At the Wolfram Technology conference 2011, I gave a presentation "Create Your Own Distribution", where the issue of sampling from custom distribution is ...


18

Edit: this answer is now structured in two sections. The first deals about creating a candidate RNG from audio data. The second demonstrates some testing I performed on this RNG. Creating the RNG Okay, I'll got at it another way then. I recorded 10 seconds of ambient noise on my MacBook Pro internal speakers. I was possibly in the worst conditions for ...


17

You are trying to implement Euler-Maruyama simulation method for a 2-stage short-term interest rate model which is given by the following system of SDEs: $$\begin{eqnarray} \mathrm{d} \theta_t &=& -\lambda_\theta \left( \theta_t - \bar\theta\right) \mathrm{d}t + \sigma_\theta \mathrm{d}W_{\theta,t} \\ \mathrm{d} \pi_t &=& ...


14

This answer is going to be a bit of a sprawl. Please read on. I am going to present several methods of simulation, hopefully in increasing order of performance. Method 1 We can carry out the filling of seats, at least as I understand the puzzle, quite literally like this: fillseats[seats_List] := ReplacePart[seats, {{1}, {2}} + RandomChoice @ ...


14

A nice question. Sampling from tCopula is done in stages. First a sample is generated from the copula with uniform marginal distributions, and then quantiles of appropriate marginal distributions are applied to the respective slots. Most of the time goes into evaluation of these quantiles, and they are expensive to compute. Being interested in $\geqslant ...


13

I don't have much time right now to write a longer post (and anyway, this is not really an area in which I would trust myself as an "expert") but I think it is better not to use the words "high quality" and "low quality" too loosely. Essentially, when dealing with quasi-random number generators there are at least two senses in which these terms are used, and ...


13

This is not an efficient answer but it is fun to play with so I thought I'd post it. For efficiency the use of Nearest might provide a good starting point. g[n_, {low_, high_}, minDist_, step_: 1] := Block[{data = RandomReal[{low, high}, {n, 2}], temp, happy, sdata, hdata}, While[True, temp = ((Nearest[data][#, 2][[-1]] & /@ data)); happy ...


13

If you want a random vector just because you need some arbitrary vector and you don't really care what it is, then Mr.Wizard's method of picking three random coordinates in [-1,1] will work. But if you care about the statistical properties of your vector, and in particular if you want it drawn from a uniform distribution over the surface of the sphere, then ...


13

The integrable singularity of the PDF at the origin is not gracefully dealt with by the underlying solvers. Presently one can work around the issue by exploiting the symmetry of the PDF: Through[{Min, Max}[ sample = RandomVariate[ TransformedDistribution[(-1)^x y, {x \[Distributed] BernoulliDistribution[1/2], y \[Distributed] ...


13

It should converge to 1/2, I think you just need to try higher values for n. Which is probably slow with your current non-functional method. Here's a simpler (and faster, and more functional) way to do the same calculation: n = 1000000; means = N[Accumulate[RandomInteger[1, n]]]/Range[n]; Now you can see it converges to 1/2 as expected: ...


12

In general you should use RandomVariate for distributions and RandomReal for uniforms. Often RandomVariate calls RandomReal or RandomInteger under the hood but it varies on a distribution by distribution basis. After loading any necessary symbols, on evaluation, any timing differences should be negligible. RandomVariate is intended to give the flexibility ...


12

Here is my quick and dirty attempt based on: Cryptographic Key From Webcam Image. I've used an example image as I don't have a webcam on my desktop but you could simply use CurrentImage to grab the webcam image live if you have one. Update using a webcam image from my laptop image = CurrentImage[]; grayscale = ColorConvert[image, "Grayscale"]; imagedata = ...


12

For the example you give there is no reason you can't use NestList, you just need to make two simple changes: Don't use the side effect in deltaπt to get the value for θnow, give it as an explicit second argument Then you just do: NestList[{deltaπt[#[[1]], #[[2]]] + #[[1]], deltaθt[#[[2]]] + #[[2]]} &, {2, 2}, noYear] Similar changes would allow ...


12

You could use "PathComponents" property of TemporalData to split the vector-valued temporal data into the list of TemporalData objects and plot those: proc = ItoProcess[{{p[t], -p[t] - q[t]}, {{0}, {1}}}, {{q, p}, {0, 10}}, {t, 0}]; td = RandomFunction[proc, {0., 10., 0.02}, 10]; td["PathComponents"]


11

I finally found some time to investigate this. I think it warrants a detailed response. In places I will repeat what others have pointed out, but I wanted something that ties together the various threads as best I can discern them. I'm not certain what is meant by the Rule 30 RNG having "an extremely small effective size". Possibly it refers to taking bits ...


11

David's answer has given the methods for producing random points that are uniformly distributed over the surface of the sphere. Of course, there are other probability distributions on the sphere that are of interest, as well as a number of methods for generating them. For instance, here is how to generate a random unit vector which follows the von ...


11

Update: The idea below is not very good because it makes surfaces instead of filaments and does not create a fractal-like structure. Another idea would be to make use a process called diffusion limited aggregation. It is easy to simulate (though Mathematica will probably be slow for a 3D simulation), and it is often the process behind fractal like ...


10

If the power spectrum is always of the form $1/k^p$ there are some optimisations you can make. The first is to compile a function to generate the reciprocal of the spectrum: $k^p$. This can be very fast since you don't need to check for the $k=0$ point (the reciprocal spectrum is well behaved at $k=0\ $). We can then use ReplacePart to temporarily set the ...


10

You cannot, by principle, get true randomness through a deterministic algorithm, no matter how sophisticated. If you need true random numbers, you'll have to collect some physical randomness. I don't know about an OS-independent way to do that, but on Linux, the pseudo-file /dev/random collects entropy from various sources. Of course, unless you are ...


10

Why don't you pick a random vector on the sphere to be your first vector, instead of $\mathbf{n}$, and then pick a random uniform number between $0$ and $2 \pi$ to orient the second vector aronud the first? Something like this, assuming you have your function randomVectorOnUnitSphere[] already (haven't tested it) generateRandomPositioning[v1_, v2_] := ...


10

Here's a reorganization of GaussianRandomField[] that works for any valid dimension, without the use of casework: GaussianRandomField[size : (_Integer?Positive) : 256, dim : (_Integer?Positive) : 2, Pk_: Function[k, k^-3]] := Module[{Pkn, fftIndgen, noise, amplitude, s2}, Pkn = Compile[{{vec, _Real, 1}}, With[{nrm = Norm[vec]}, ...


10

This is not built into the system as far as I can tell. However, it's a really good example of how to construct a fairly sophisticated function using just a few lines of code. I've taken the definition directly from the Wikipedia page, and applied it to Sign[data] on the assumption that if you are testing residuals from a regression, you are interested in ...


10

As ssch aluded to in a comment this can be easily done with NestWhileList: NestWhileList[# + RandomChoice[{-1, 1}] &, 0, # < 10 &] This is hardly the only way to proceed however. You could for example Sow and Reap values from within a recursion: f[x_] /; x < 10 := f @ Sow[x + RandomChoice[{-1, 1}]]; Reap[f[0]][[2, 1]] This actually ...


10

If the current position is, say, 2, and the maximum value we allow is, say, 14, we can walk 12 steps with our eyes closed. This attempts to take advantage of that. newList gives the list of new values given the maximum and the starting value. newList = Compile[{{max, _Integer}, {start, _Integer}}, Module[{aux}, aux = RandomChoice[{-1, 1}, max - ...


10

I would proceed like the following. It will be natural to propose that the win-event occurs following BinomialDistribution with probability $p=0.8$ so that we can use the built-in BinomialProcess to simulate the win and losses in $20$ time steps for $50$ sample paths. timstep = 20; win = BinomialProcess[.8]; samplepaths=50; process = ...


9

Well, a very simple but very limited way could be to generate a random set of points, calculate all distances between them and scale the minimum distance to mindist: mindist = 1; npts = 200; pts = RandomReal[{0, 100}, {npts, 2}]; scaledpts = mindist/Min[Norm /@ Subtract @@@ Subsets[pts, {2}]]*pts; Graphics[{Green, Point[pts], Red, Point[scaledpts]}, Frame ...


9

If you don't need high precision, you can do something along these lines: canvas = Image@ConstantArray[0, {100, 100}]; distance = 6; {img, {pts}} = Reap[Nest[ ImageCompose[#, SetAlphaChannel[#, #] &@Image@DiskMatrix[distance], Sow@RandomChoice@ Position[Transpose@ImageData[#, DataReversed -> True], 0.]] &, ...



Only top voted, non community-wiki answers of a minimum length are eligible