Tag Info

Hot answers tagged

36

The only reason I am attempting to answer this is to perhaps get a Reversal badge. There you go... We will go slowly and this answer is the basis for what comes next. Let's start with two dimensions. You'll see why. We create a rectangular region: Needs["NDSolve`FEM`"] mesh = ToElementMesh[FullRegion[2], {{0, 5}, {0, 1}}, "MeshOrder" -> 1, ...


22

BernoulliDistribution is a perfect fit for this. RandomVariate[BernoulliDistribution[1 - 0.1], {50}] {1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1} Also, as kguler states, you can use RandomChoice, but the benefit of BernoulliDistribution is that ...


18

Usage Just use this function with any polyhedron in in form: GraphicsComplex[pts_, Polygon[vertices_, ___]]. When I find time and motivation maybe I will add more DownValues so it can be more general. At the moment you can play with solids given by PolyhedronData[... "Faces"]: polyhedronRandomWalk[ PolyhedronData["DuerersSolid", "Faces"] ] ...


13

The printed version of the 2002 edition was printed 3 times and sold out 3 times; Springer and Google recently started selling it (book only) as a PDF eBook (no software) on the Springer and Google sites for $79. I know other authors (e.g. here) have gone to some trouble to make their books available here on stack exchange ... We are delighted to be able ...


13

The separation-of-variables solution you quoted has two indices appearing in it: n and j (the subscripts of the coefficient $A_{nj}$). Here, n is azimuthal mode order, i.e. it counts the number of nodes along the direction in which the polar-angle $\theta$ varies (divided by 2). The index j is needed because the wave is supposed to satisfy the boundary ...


10

ClearAll[dist] dist [rho_] := CopulaDistribution[{"Binormal", rho}, {UniformDistribution[{0, 1}], UniformDistribution[{0, 1}]}]; data1 = RandomVariate[dist[-.9], 5000]; ListPlot[data1, Frame -> True, AspectRatio -> 1] data2 = RandomVariate[dist[.6], 5000]; ListPlot[data2, Frame -> True, AspectRatio -> 1]


8

A couple things: You don't update velocity (obx and oby appear to be unused?). You do your timestep wrong (you want x+=velocity*timestep, not x=velocity*time!). You also have your direction of your force wrong. You should take the negative gradient of potential. The gradient of 1/r is -1/r^2. The negative of that is 1/r^2. So you get the common sense result ...


7

With eqn[{k_, r_, H0_, P0_}] := {H'[t] == r (1 - H[t]/k) - d H[t] P[t], P'[t] == -s P[t] + e H[t] P[t], H[0] == H0, P[0] == P0} d = 0.01; s = 0.3; e = 0.02; I would define one simulation as sim := Module[ {k = RandomVariate[NormalDistribution[150, 20]], r = RandomVariate[NormalDistribution[0.4, 0.003]], H0 = RandomVariate[UniformDistribution[{50, ...


5

Take a uniform random distribution and check if it is above some threshold (0.9 in your case). For example: dist[] := If[RandomReal[] > 0.9, 0, 1]; Table[dist[], {i, 100}]


5

First, you're not using a fixed step method. (An Euler scheme may be applied to any step size and to one that varies.) To get a true fixed step method you have to turn off "DiscontinuityProcessing" when you have a discontinuous ODE; otherwise, NDSolve will try to adapt the steps to account for the discontinuity. The "DiscontinuityProcessing" stage resets ...


5

For loop is always slow. You may try this: f = Compile[{{x, _Real}, {y, _Real}}, If[y >= 12. Cos[x] && y >= 10 + x^3, 1, 0]]; vol[n_Integer /; n <= 10^6] := 3.* Total[f@@@Transpose@{RandomReal[{0, 1}, n], RandomReal[{10, 13}, n]}]/n; The calculation of 1000000 samples takes 1.1 s on my i5-3210M.


5

With f[a_, {x_, y_}] := Piecewise[{{a x, x < y}, {x, x == y}, {1 - a + a x, x > y}}, 0] one simulation can be defined by sim[length_] := Module[{rv = RandomVariate[BetaDistribution[3, 1], length], y, yBar}, y[1] = First@rv; yBar[t_Integer] := yBar[t] = 1/t * Sum[y[i], {i, 1, t}]; y[t_Integer] := y[t] = f[0.5, {rv[[t]], yBar[t - 1]}]; ...


4

I do not know why, but the following are much faster than Walk: ClearAll[walkF,walkF2]; walkF[x_List, a_List, b_List, sigma_] := With[{f = TruncatedDistribution[Transpose[{a - x, b - x}], ProductDistribution @@ ConstantArray[NormalDistribution[0, sigma], Length[x]]]}, x + RandomVariate[f]] walkF2[x_List, a_List, b_List, sigma_] := ...


4

I misunderstood and commented only about computing the Cesaro means as per the question In s I have all partial sums, but I do not know how to divide them by corresponding n. The desired scatter plot of 500 Monte Carlo attempts with samples of increasing length could be obtained with something like ticks = Range[-0.06, 0.06, 0.02]; s = Table[u = ...


4

In your example plot the Monte-Carlo integral is computed afresh for each new amount of sampling points: s[n_] := Total[Sqrt[1 - #^2] & /@ RandomReal[1, n]]/n Then take one random point, find the mean, take two new random points, find their mean, and so on until 500. The result is: ListPlot[Table[s[n], {n, 500}], PlotRange -> {0.7, 0.9}] The ...


4

RandomChoice[{.9, .1} -> {1, 0}, 10] (* {0, 1, 1, 1, 1, 1, 1, 0, 1, 1} *) Timing results Timing[RandomVariate[BernoulliDistribution[.9], {10^8}];] (* {3.38014, Null} *) Timing[RandomChoice[{.9, .1} -> {1, 0}, 10^8];](* {5.64937, Null} *) dist[] := If[RandomReal[] > 0.9, 0, 1]; Timing[Table[dist[], {i, 10^8}];] (* {13.9842, Null} *) One nice ...


4

The reason it's not finishing is that you set MaxSteps -> Infinity with a high AccuracyGoal. We can actually solve this system analytically, by replacing NDSolve with DSolve: qq = DSolve[{q'[t] == v[t]/r - q[t]/r*(1/c), q1'[t] == (((q[t] + q1[t])/(2*a*ϵ0*k2))*δ), q[0] == 10*10^-9, q1[0] == 10*10^-9}, {q, q1}, t]; We can inspect the solution: ...


3

You can also use RandomVariate with DiscreteUniformDistribution: rW[a_, b_, n_] := Accumulate[Prepend[ RandomVariate[DiscreteUniformDistribution[{{-a, a}, {-b, b}}], n]], {0,0}] dt = rW[10, 20, 100]; Graphics[{PointSize[Large], Red, Point@#, Thick, Blue, Line@#} &@dt, Frame -> True, Axes->True, AspectRatio -> 1/GoldenRatio] We get the ...


3

nextGen[n_] := Total@RandomChoice[{11/32, 3/8, 3/16, 3/32} -> {0, 1, 2, 3}, n] simulate[n0_, nrOfGenerations_] := Total@NestList[nextGen, n0, nrOfGenerations] Now we can simulate six generations a hundred times and compute the mean value. The initial number of organisms is 10 in this example. Table[simulate[10, 6], {100}] // Mean // N (* Out: 75.42 *) ...


3

data10 = RandomVariate[f[13, 0.5], {10, 25}]; (* 10 data sets from distribution f *) lls= LogLikelihood[EstimatedDistribution[#, f[a, b], {{a, 1}, {b, 1}}], #] & /@data10 (*{-32.4994, -25.2268, -21.9671, -26.8963, -25.9164, -22.8958, -26.5247, -24.9622, -33.9319, -28.6512}*) maxll=Block[{k=1}, MaximalBy[Last][ With[{dist = ...


2

I think using linked lists is usually the best way to proceed in cases like this. Start with coords = {}; Each time you generate a new point, r, add it with coords = {coords, r}; This is very fast, but will generate a deeply nested list. When all the points have been generated, flatten and re-partition the list, which is also very fast. coords = ...


2

Define a list large enough to hold your coordinates: coor = Table[Null, {100}]; Then, starting with i = 1; Let some program fill in the values coor[[i++]] = {0., 0., 0.}; coor[[i++]] = {0.1, 0.1, 0.003}; Finally, delete the remaining Null-values: DeleteCases[coor, Null] {{0., 0., 0.}, {0.1, 0.1, 0.003}} This should be significantly faster


2

Using sim[length_] := Module[{rv = RandomVariate[BetaDistribution[2, 1], length], y, yMed}, y[1] = First@rv; yMed[t_Integer] := yMed[t] = Median[y /@ Range[t]]; y[t_Integer] := y[t] = f[0.5, {rv[[t]], yMed[t - 1]}]; yMed /@ Range[length] ] results in plots like Here Median[y /@ Range[t]] calculates the median for y[1] to y[t], as stated ...


2

What you are missing, I believe, is sufficient experience of Mathematica's core language at the functional level that you experimenting with. I give you credit for making a good try at formulating your code in a functional way, but I'm afraid you gone somewhat wide of the mark. I have put your code into a form that works and which I think preserves your ...


2

Maybe you can try a matrix approach. 1/ The idea is to generate a matrix like this one : mat = {{a, 1, 0}, {b, 0, 1}, {c, 0, 0}}; then you can see that: {x1, x2, x3}.{{a, 1, 0}, {b, 0, 1}, {c, 0, 0}} {a x1 + b x2 + c x3, x1, x2} gives you the format you want. 2/ Then you can use that matrix directly in NestList (without the need to define a ...


2

f[r_?NumberQ,n_Integer]:={First[#],#.{r,Sqrt[1-r^2]}}&/@RandomReal[NormalDistribution[0,1],{n,2}]; Produces n pairs of numbers with the correlation r.


2

Using Event-Handling and Euler method Expanding the solution given by Michael E2 one might figure that using EventSeries is coming closest to the "real thing" which after all is a series of discrete events: eventTimes = Range[ 0, 10, 1 ]; (* example *) isEventTimeQ[ t_?NumericQ ] := Piecewise[ Table[ { 1 , t == eventTime }, {eventTime, eventTimes}], ...


1

a = 3; b = 5; randomWalk = NestList[# + {RandomInteger[{-a, a}], RandomInteger[{-b, b}]} &, {0, 0}, 100] (* {{0, 0}, {2, 2}, {1, -3}, {-2, 2}, {1, 4}, {1, 3}, {1, 2}, {4, 0}, {4, 0}, {3, -4}, {4, -1}, {2, -5}, {0, 0}, {0, 0}, {2, -3}, {3, -6}, {2, -11}, {4, -10}, {4, -5}, {7, -3}, {9, \ -3}, {6, -8}, {9, -12}, {7, -16}, {5, -11}, {6, -16}, {4, ...


1

Brute Force: While[! ( .9 < Correlation[ a = RandomReal[{0, 1}, 10], b = RandomReal[{0, 1}, 10 ]] < .91 ) ] Correlation[ a, b] 0.900731 {a, b} // MatrixForm I expect this breaks down in a hurry for larger sets.


1

Based on the comments appended to the question, I believe that what you are looking for is: mvBeta[myrating, #, myR] & /@ RandomReal[{0,1},100] where you can change 100 to be any length vector you like, and myrating and myR have already been defined.



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