Problem with the Pick function
Clash Royale CLAN TAG#URR8PPP
up vote
2
down vote
favorite
My problem is, Pick evaluates all the elements of a list at once. In my code i need a similar function that evaluates each element of a list.
I need to generate 1000000 numbers for a PDF.
My code is:
n = 1000000;
yteste = RandomReal[0, 2/3, 2 n];
xteste = RandomReal[-1, 1, 2 n];
f[x_] := 2/3 (1 - Abs[x]^3)
If[Length[xaceitos] <= n,
AppendTo[xaceitos,
Pick[xteste, UnitStep[f[xteste] - yteste], 1]], Nothing]
Is there any other function that can do it?
probability-or-statistics
add a comment |Â
up vote
2
down vote
favorite
My problem is, Pick evaluates all the elements of a list at once. In my code i need a similar function that evaluates each element of a list.
I need to generate 1000000 numbers for a PDF.
My code is:
n = 1000000;
yteste = RandomReal[0, 2/3, 2 n];
xteste = RandomReal[-1, 1, 2 n];
f[x_] := 2/3 (1 - Abs[x]^3)
If[Length[xaceitos] <= n,
AppendTo[xaceitos,
Pick[xteste, UnitStep[f[xteste] - yteste], 1]], Nothing]
Is there any other function that can do it?
probability-or-statistics
Can you explain what you want your code to produce? What is the expected form ofxaceitos
, for instance?
â J. M. is somewhat okay.â¦
2 hours ago
I need to generate n numbers that follow the PDF f[x], this is the monte carlo Method. The problem is, when i use the rejection method i only receive less than n numbers. I need to receive exactly n numbers.
â Hugo David Costa Pereira
2 hours ago
1
That is slightly clearer, but only a little. So, you want to sample under the curve off[x]
forAbs[x] < 1
? If so:dist = ProbabilityDistribution[f[x], x, -1, 1]; xaceitos = RandomVariate[dist, n]; Show[Histogram[xaceitos, Automatic, "PDF"], Plot[f[x], x, -1, 1]]
â J. M. is somewhat okay.â¦
2 hours ago
Yes, exactly. The curve of the histogram[xaceitos] must have the same shape o f[x].
â Hugo David Costa Pereira
2 hours ago
add a comment |Â
up vote
2
down vote
favorite
up vote
2
down vote
favorite
My problem is, Pick evaluates all the elements of a list at once. In my code i need a similar function that evaluates each element of a list.
I need to generate 1000000 numbers for a PDF.
My code is:
n = 1000000;
yteste = RandomReal[0, 2/3, 2 n];
xteste = RandomReal[-1, 1, 2 n];
f[x_] := 2/3 (1 - Abs[x]^3)
If[Length[xaceitos] <= n,
AppendTo[xaceitos,
Pick[xteste, UnitStep[f[xteste] - yteste], 1]], Nothing]
Is there any other function that can do it?
probability-or-statistics
My problem is, Pick evaluates all the elements of a list at once. In my code i need a similar function that evaluates each element of a list.
I need to generate 1000000 numbers for a PDF.
My code is:
n = 1000000;
yteste = RandomReal[0, 2/3, 2 n];
xteste = RandomReal[-1, 1, 2 n];
f[x_] := 2/3 (1 - Abs[x]^3)
If[Length[xaceitos] <= n,
AppendTo[xaceitos,
Pick[xteste, UnitStep[f[xteste] - yteste], 1]], Nothing]
Is there any other function that can do it?
probability-or-statistics
probability-or-statistics
edited 5 mins ago
m_goldberg
82.3k869190
82.3k869190
asked 2 hours ago
Hugo David Costa Pereira
212
212
Can you explain what you want your code to produce? What is the expected form ofxaceitos
, for instance?
â J. M. is somewhat okay.â¦
2 hours ago
I need to generate n numbers that follow the PDF f[x], this is the monte carlo Method. The problem is, when i use the rejection method i only receive less than n numbers. I need to receive exactly n numbers.
â Hugo David Costa Pereira
2 hours ago
1
That is slightly clearer, but only a little. So, you want to sample under the curve off[x]
forAbs[x] < 1
? If so:dist = ProbabilityDistribution[f[x], x, -1, 1]; xaceitos = RandomVariate[dist, n]; Show[Histogram[xaceitos, Automatic, "PDF"], Plot[f[x], x, -1, 1]]
â J. M. is somewhat okay.â¦
2 hours ago
Yes, exactly. The curve of the histogram[xaceitos] must have the same shape o f[x].
â Hugo David Costa Pereira
2 hours ago
add a comment |Â
Can you explain what you want your code to produce? What is the expected form ofxaceitos
, for instance?
â J. M. is somewhat okay.â¦
2 hours ago
I need to generate n numbers that follow the PDF f[x], this is the monte carlo Method. The problem is, when i use the rejection method i only receive less than n numbers. I need to receive exactly n numbers.
â Hugo David Costa Pereira
2 hours ago
1
That is slightly clearer, but only a little. So, you want to sample under the curve off[x]
forAbs[x] < 1
? If so:dist = ProbabilityDistribution[f[x], x, -1, 1]; xaceitos = RandomVariate[dist, n]; Show[Histogram[xaceitos, Automatic, "PDF"], Plot[f[x], x, -1, 1]]
â J. M. is somewhat okay.â¦
2 hours ago
Yes, exactly. The curve of the histogram[xaceitos] must have the same shape o f[x].
â Hugo David Costa Pereira
2 hours ago
Can you explain what you want your code to produce? What is the expected form of
xaceitos
, for instance?â J. M. is somewhat okay.â¦
2 hours ago
Can you explain what you want your code to produce? What is the expected form of
xaceitos
, for instance?â J. M. is somewhat okay.â¦
2 hours ago
I need to generate n numbers that follow the PDF f[x], this is the monte carlo Method. The problem is, when i use the rejection method i only receive less than n numbers. I need to receive exactly n numbers.
â Hugo David Costa Pereira
2 hours ago
I need to generate n numbers that follow the PDF f[x], this is the monte carlo Method. The problem is, when i use the rejection method i only receive less than n numbers. I need to receive exactly n numbers.
â Hugo David Costa Pereira
2 hours ago
1
1
That is slightly clearer, but only a little. So, you want to sample under the curve of
f[x]
for Abs[x] < 1
? If so: dist = ProbabilityDistribution[f[x], x, -1, 1]; xaceitos = RandomVariate[dist, n]; Show[Histogram[xaceitos, Automatic, "PDF"], Plot[f[x], x, -1, 1]]
â J. M. is somewhat okay.â¦
2 hours ago
That is slightly clearer, but only a little. So, you want to sample under the curve of
f[x]
for Abs[x] < 1
? If so: dist = ProbabilityDistribution[f[x], x, -1, 1]; xaceitos = RandomVariate[dist, n]; Show[Histogram[xaceitos, Automatic, "PDF"], Plot[f[x], x, -1, 1]]
â J. M. is somewhat okay.â¦
2 hours ago
Yes, exactly. The curve of the histogram[xaceitos] must have the same shape o f[x].
â Hugo David Costa Pereira
2 hours ago
Yes, exactly. The curve of the histogram[xaceitos] must have the same shape o f[x].
â Hugo David Costa Pereira
2 hours ago
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
3
down vote
As @JM says, just use ProbabilityDistribution
:
dist = ProbabilityDistribution[f[x], x, -1, 1];
random = RandomVariate[dist, 10^6];
Length[random]
1000000
Visualization:
Histogram[random, Automatic, "PDF"]
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
As @JM says, just use ProbabilityDistribution
:
dist = ProbabilityDistribution[f[x], x, -1, 1];
random = RandomVariate[dist, 10^6];
Length[random]
1000000
Visualization:
Histogram[random, Automatic, "PDF"]
add a comment |Â
up vote
3
down vote
As @JM says, just use ProbabilityDistribution
:
dist = ProbabilityDistribution[f[x], x, -1, 1];
random = RandomVariate[dist, 10^6];
Length[random]
1000000
Visualization:
Histogram[random, Automatic, "PDF"]
add a comment |Â
up vote
3
down vote
up vote
3
down vote
As @JM says, just use ProbabilityDistribution
:
dist = ProbabilityDistribution[f[x], x, -1, 1];
random = RandomVariate[dist, 10^6];
Length[random]
1000000
Visualization:
Histogram[random, Automatic, "PDF"]
As @JM says, just use ProbabilityDistribution
:
dist = ProbabilityDistribution[f[x], x, -1, 1];
random = RandomVariate[dist, 10^6];
Length[random]
1000000
Visualization:
Histogram[random, Automatic, "PDF"]
answered 2 hours ago
Carl Woll
58.4k275150
58.4k275150
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f182581%2fproblem-with-the-pick-function%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Can you explain what you want your code to produce? What is the expected form of
xaceitos
, for instance?â J. M. is somewhat okay.â¦
2 hours ago
I need to generate n numbers that follow the PDF f[x], this is the monte carlo Method. The problem is, when i use the rejection method i only receive less than n numbers. I need to receive exactly n numbers.
â Hugo David Costa Pereira
2 hours ago
1
That is slightly clearer, but only a little. So, you want to sample under the curve of
f[x]
forAbs[x] < 1
? If so:dist = ProbabilityDistribution[f[x], x, -1, 1]; xaceitos = RandomVariate[dist, n]; Show[Histogram[xaceitos, Automatic, "PDF"], Plot[f[x], x, -1, 1]]
â J. M. is somewhat okay.â¦
2 hours ago
Yes, exactly. The curve of the histogram[xaceitos] must have the same shape o f[x].
â Hugo David Costa Pereira
2 hours ago