Question

In: Computer Science

Program a code for the random variable (Exponential distribution), where the user informs the data and...

Program a code for the random variable (Exponential distribution), where the user informs the data and parameters necessary to generate the Probability Density function (PDF) and the Cumulative Distribution Function (CDF).

Solutions

Expert Solution

The formula for exponential distribution is given below

Here   is the probability density function.

The program to generate exponential distribution is given below

#include <stdio.h>            // Needed for printf()
#include <stdlib.h>           // Needed for exit() and ato*()
#include <math.h>             // Needed for log()

//----- Function prototypes -------------------------------------------------
double expon(double x);       // Returns an exponential random variable
double rand_val(int seed);   

//===== Main program ========================================================
void main(void)
{
  char   in_string[256];      // Input string
  FILE   *fp;                 // File pointer to output file
  double lambda;              // Mean rate
  double exp_rv;              // Exponential random variable
  int    num_values;          // Number of values
  int    i;                   // Loop counter

  // Output banner
  printf("----------------------------------------- genexp.c ----- \n");
  printf("-  Program to generate exponential random variables    - \n");
  printf("-------------------------------------------------------- \n");

  // Prompt for output filename and then create/open the file
  printf("Output file name ===================================> ");
  scanf("%s", in_string);
  fp = fopen(in_string, "w");
  if (fp == NULL)
  {
    printf("ERROR in creating output file (%s) \n", in_string);
    exit(1);
  }

  // Prompt for random number seed and then use it
  printf("Random number seed (greater than 0) ================> ");
  scanf("%s", in_string);
  rand_val((int) atoi(in_string));

  // Prompt for rate (lambda)
  printf("Rate parameter (lambda) ============================> ");
  scanf("%s", in_string);
  lambda = atof(in_string);

  // Prompt for number of values to generate
  printf("Number of values to generate =======================> ");
  scanf("%s", in_string);
  num_values = atoi(in_string);

  // Output message and generate interarrival times
  printf("-------------------------------------------------------- \n");
  printf("-  Generating samples to file                          - \n");
  printf("-------------------------------------------------------- \n");

  // Generate and output exponential random variables
  for (i=0; i<num_values; i++)
  {
    exp_rv = expon(1.0 / lambda);
    fprintf(fp, "%f \n", exp_rv);
  }

  // Output message and close the output file
  printf("-------------------------------------------------------- \n");
  printf("-  Done! \n");
  printf("-------------------------------------------------------- \n");
  fclose(fp);
}

//===========================================================================
//=  Function to generate exponentially distributed random variables        =
//=    - Input:  Mean value of distribution                                 =
//=    - Output: Returns with exponentially distributed random variable     =
//===========================================================================
double expon(double x)
{
  double z;                     // Uniform random number (0 < z < 1)
  double exp_value;             // Computed exponential value to be returned

  // Pull a uniform random number (0 < z < 1)
  do
  {
    z = rand_val(0);
  }
  while ((z == 0) || (z == 1));

  // Compute exponential random variable using inversion method
  exp_value = -x * log(z);

  return(exp_value);
}

double rand_val(int seed)
{
  const long  a =      16807;  // Multiplier
  const long  m = 2147483647;  // Modulus
  const long  q =     127773;  // m div a
  const long  r =       2836;  // m mod a
  static long x;               // Random int value
  long        x_div_q;         // x divided by q
  long        x_mod_q;         // x modulo q
  long        x_new;           // New x value

  // Set the seed if argument is non-zero and then return zero
  if (seed > 0)
  {
    x = seed;
    return(0.0);
  }

  // RNG using integer arithmetic
  x_div_q = x / q;
  x_mod_q = x % q;
  x_new = (a * x_mod_q) - (r * x_div_q);
  if (x_new > 0)
    x = x_new;
  else
    x = x_new + m;

  // Return a random value between 0.0 and 1.0
  return((double) x / m);
}

Here you will enter the lambda parameter and get upto 5 values in the output file provided by you.


Related Solutions

This is the code for the LLN question iid random variable from exponential distribution with mean...
This is the code for the LLN question iid random variable from exponential distribution with mean beta. Beta is fixed 1 for illustration. # Testing LLN plot.ybar <- function(n, m = 1e5, beta = 1) { Ybars <- rep(0,m) for(i in 1:m) { Y.sample <- rexp(n,1/beta) Ybars[i] <- mean(Y.sample) }    p <- hist(Ybars, xlim=c(0,4), freq=F,main = paste("Distribution of Ybar when n=",n, sep="")) } par(mfrow = c(2,2)) for (n in c(2, 10, 50, 200)) { plot.ybar(n) } Using similar code...
Create a problem where the given is about a random variable that is exponential. Ask a...
Create a problem where the given is about a random variable that is exponential. Ask a question that requires the exponential distribution &amp; solve. Ask a question that requires the use of the Poisson &amp; solve. (Note – problem 4 gives information about a Poisson random variable and then asks Poisson and exponential questions.)
Suppose X is an exponential random variable with mean 5 and Y is an exponential random...
Suppose X is an exponential random variable with mean 5 and Y is an exponential random variable with mean 10. X and Y are independent. Determine the coefficient of variation of X + Y
If Y is a random variable with exponential distribution with mean 1/λ, show that E (Y...
If Y is a random variable with exponential distribution with mean 1/λ, show that E (Y ^ k) = k! / (λ^k), k = 1,2, ...
Given the cumulative distribution of an exponential random variable find: The probability density function Show that...
Given the cumulative distribution of an exponential random variable find: The probability density function Show that it is a valid probability function The moment generating function The Expected mean The variance
Q1. (A) Explain the exponential random variable and normal random variable with at-least ten examples in...
Q1. (A) Explain the exponential random variable and normal random variable with at-least ten examples in real life? In which situation we prefer normal random variable instead of exponential variable? (B) Find the Mean, variance and cumulative density function for following probability density function as: (state your results in detail computation) f(w)=10 e-10w ; 0≤w≤∞
The random variable X has an Exponential distribution with parameter beta= 5. The P(X > 18|X...
The random variable X has an Exponential distribution with parameter beta= 5. The P(X > 18|X > 12) is equal to
The random variable Y has an exponential distribution with probability density function (pdf) as follows: f(y)...
The random variable Y has an exponential distribution with probability density function (pdf) as follows: f(y) = λe−λy, y >0 = 0, otherwise (i) Showing your workings, find P (Y > s|Y > t), for s ≥ t. [3] (ii) Derive an expression for the conditional pdf of Y , conditional on that Y ≤ 200. [3] N(t) is a Poisson process with rate λ (iii) Find an expression for the Cumulative Distribution Function (CDF) of the waiting time until...
Probability And Statistics Question: Explain the exponential random variable and normal random variable with at-least ten...
Probability And Statistics Question: Explain the exponential random variable and normal random variable with at-least ten examples in real life? In which situation we prefer normal random variable instead of exponential variable?
Using the provided code (found here), write a program using the main method where the user...
Using the provided code (found here), write a program using the main method where the user enters Strings and the program echoes these strings to the console until the user enters “quit”. When user quits the program should print out, “Goodbye”. You may assume that the case is ignored when the user enters, “quit”, so “quit”, “QUIT”, “Quit”,“qUiT”, etc. are all acceptable ways to end the program. The results should be printed out to the console in the following format:...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT