PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, October 7, 2022

[FIXED] How do I randomly generate data that is distributed according to my own density function in R?

 October 07, 2022     probability, r, statistics     No comments   

Issue

I'm looking for a method that lets me randomly generate data according to my own pre-defined Probability density function

Is there a method that lets me do that? Or at least one that generates data according to this specific function?


Solution

Assuming that your own pre-defined pdf has $y \in {0, 1}$, the pdf is the pdf of a Bernoulli distribution with parameter $\pi$. Using that a Bernoulli random variable corresponds to a Binomial with number of trials equal to 1 ($n=1$), you can draw from the pdf using the following code:

pi <- 0.5 
n <- 10 # Number of draws from specified pdf 
draws <- rbinom(n, 1, pi) # Bernoulli corresponds to Binom with `size = 1`
print(draws) 
# Outputs: [1] 1 0 0 1 0 0 1 1 0 1


Answered By - wanderingashenvalewisp
Answer Checked By - Katrina (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing