Switch to full style
For C/C++ coders discussions and solutions
Post a reply

Inverse Transformation for some Statistical Distributions (C

Tue Apr 10, 2007 1:28 pm

This a Statistical program to find the inverse transformation for some distributions depending on randomization that we are using them in most of our simulations programs. Some Distributions such that:
  • Uniform distribution.
  • Normal distribution.
  • Exponential distribution.

java code
#include <iostream>
#include <cmath>
#include <ctime>
#include <cstdlib>
#include <iomanip>
#include <unistd.h>
using namespace std;

double uniform(double randnum,double a, double b)
{
return (a+(randnum*(b-a)));
}

double exponential(double randnum,double lambda)
{
return (-lambda*(log(randnum)));
}

double normal(double mu, double sigma)
{
srand(time(NULL));
double random;
double tot=0.0;

for(int i=1;i<=12;i++)
{
random=(rand()%100)/100.0;
tot+=random;
}

return(mu+sigma*(tot-6));
}

int main()
{
double a;
double b;
double lambda;
double mu;
double sigma;
double val;
int choice;

cout << "Choose a distribution: " << endl;
cout << " - Press 1 for uniform\n"
<< " - Press 2 for exponential\n"
<< " - Press 3 for normal\n"
<< endl
<< endl
<< endl
<< endl
<< endl
<< "Choice:";
cin >> choice;

srand(time(NULL));

switch(choice)
{
case 1:
cout << "Enter lower limit for uniform distribution (a): ";
cin >> a;

cout << "Enter upper limit for uniform distribution (b): ";
cin >> b;

cout << "F(x)\t\t\t\tx" << endl;
cout << "------------------------------------" << endl;
for(int i=0;i<10;i++)
{
val=(rand()%100)/100.0;
cout << val << "\t\t\t\t" << uniform(val,a,b) << endl;
}
break;
case 2:
cout << "Enter the mean of exponential distrbution (lambda): ";
cin >> lambda;

cout << "F(x)\t\t\t\tx" << endl;
cout << "------------------------------------" << endl;
for(int i=0;i<10;i++)
{
val=(rand()%100)/100.0;
cout << val << "\t\t\t\t" << exponential(val,lambda) << endl;
}
break;
case 3:
cout << "Enter the mean of normal distribution (mu): ";
cin >> mu;

cout << "Enter the standard deviation of normal distribution (sigma): ";
cin >> sigma;
cout << "These are 10 normally distributed random variables" << endl;
for(int i=0;i<10;i++)
{
cout << normal(mu,sigma) << endl;
sleep(1);
}
break;
default:
cout << "Invalid choice" << endl;
}
return 0;
}




Re: Inverse Transformation for some Statistical Distributions (C

Sat Jan 26, 2013 6:08 pm

updated.

Post a reply
  Related Posts  to : Inverse Transformation for some Statistical Distributions (C
 cube 3d transformation     -  
 Generalized inverse filtering using threshold gamma     -  
 Feature selection: Statistical and Recursive examples     -