Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Practical No.1
- Mont carlo- GSL_RNG_TYPE="taus" GSL_RNG_SEED=124 ./pn.out
- .schema
- .databases
- cd ..
- .header on
- .mode column
- Select * from tn;
- Create database
- sqlite3 test.db
- Create table
- Create table emp(
- Id Int pk nn,
- Name text nn,
- Salary real);
- Insert records:
- Insert into tn(is,name,salary) values (6,'hii',2346);
- Alter column
- Alter table tn add cn datatype;
- Update column
- Update tn set salary=234 where cconditio cn= 466;
- 1. List all the records where Age>=25 and salary>=65000
- :select * from company where age>=25 and salary>=6500;
- 2. List all the records where Age>=25 or salary>=65000
- :select * from company where age>=25 or salary>=65000;
- 3. List all the records where Age is not null.
- :select * from company where age is not null;
- 4. List all the records where name starts with ‘R’
- :select * from from company where name like 'r%';
- 5. List all the records where age is either 25 or 27
- :select * from company where age in (25,27);
- 6. List all the records where age is neither 25 or 27
- :select * from company where age not in (25,27);
- 7. List all the records where age is in between 25 and 27
- :select * from company where age between 25 and 27;
- 8. Find the total amount of salary for each customer
- :select name,sum(salary) from company group by name;
- 9. Find total amount of salary on each customer order by name.
- :select id,name,sum(salary) from company group by name order by id;
- 10.Find total amount of salary on each customer order by id(desc).
- :select id,name,sum(salary) from company group by name order by id desc;
- 11.List down all the records where names occur greater than 2 times.
- :select * from company group by name having count(name)>2;
- 12.List down the salaries in ascending order.
- : select * from company order by salary asc;
- 13. List down first 5 records from the table
- :select * from company limit 5;
- 14.List down 4 records except the first 2 records.
- :select * from company limit 4 offset 2;
- 15. Cross join company and department table.
- :select emp_id,name,dept from company cross join department;
- 16. Inner join company and department table where company id=department id
- :select emp_id,name,dept from company inner join department on company.id=department.id;
- 17. Left outer join company and department table where company id=department id
- :select emp_id,name,dept from company left outer join department on company.id=department.id;
- 18.
- Select * from tn where purchasing date >'30-oct,-2018';
- 19.select name ,product,purchasing date from tn where product='32Gbusb' order by des
- HOW TO RUN :- gcc -std=gnu99 *program_name.c* -o *program_name.out* -lapophenia -lgsl -lsqlite3;
- Practical No.2
- a. Illustration of gsl Matrix multiplication
- #include <apop.h>
- int main()
- { gsl_matrix *m = gsl_matrix_alloc(20,15);
- gsl_matrix_set_all(m, 1);
- for (int i=0; i< m->size1; i++){ Apop_matrix_row(m, i, one_row);
- gsl_vector_scale(one_row, i+1);
- }
- for (int i=0; i< m->size2; i++)
- {
- Apop_matrix_col(m, i, one_col);
- gsl_vector_scale(one_col, i+1);
- }
- apop_matrix_show(m); gsl_matrix_free(m);
- }
- Practical No.3
- a. Gnu plot for plotting vectors 1: Plotting a vector
- #include <apop.h>
- void plot_matrix_now(gsl_matrix *data)
- { static FILE *gp = NULL;
- if (!gp)
- gp = popen("gnuplot -persist", "w");
- if (!gp)
- {
- printf("Couldn't open Gnuplot.\n");
- return;
- }
- fprintf(gp,"reset; plot '-' \n");
- apop_matrix_print(data, .output_pipe=gp);
- fflush(gp);
- }
- int main()
- {
- apop_db_open("data-climate.db");
- plot_matrix_now(apop_query_to_matrix("select (year*12+month)/12., temp from temp"));
- }
- c. Gnu plot for plotting vectors 3: Error bars
- #include <apop.h>
- int main()
- {
- apop_db_open("data-climate.db");
- apop_data *d = apop_query_to_data("select \ (yearmonth/100. - round(yearmonth/100.))*100 as month, \ avg(tmp), stddev(tmp) \ from precip group by month");
- printf("set xrange*0:13+; plot ’-’ with errorbars\n");
- apop_matrix_show(d->matrix);
- }
- Practical No.4
- Discrete Distributions:
- a . bernoulli.c
- #include<stdio.h>
- #include<gsl/gsl_randist.h>
- int main(void)
- {
- int i;
- double p = 0.6;
- float sum = 0;
- /* print probablity distribution */ printf("Random variable|||Probability|||cumulative
- probability\n");
- printf("---------------------------------------------------------\n");
- for(i=0;i<=1;i++)
- {
- float k = gsl_ran_bernoulli_pdf(i,p);
- sum = sum+k;
- printf("%d \t\t %f \t\t %f\n",i,k,sum);
- }
- printf("\n"); return 0;
- }
- b. binomial.c
- #include <stdio.h>
- #include <gsl/gsl_randist.h>
- int main(void)
- {
- int i, n=5;
- double p=0.6;
- float sum= 0.0;
- /*prints probability distribution table*/
- printf("random variable ||| probability ||| cumulative probability \n");
- printf("---------------------------------------------------------------------------------- \n");
- for (i=0 ; i<=n ; i++)
- {
- float k = gsl_ran_binomial_pdf(i,p,n);
- sum= sum +k;
- printf("%d\t\t%f\t\t%f\n",i,k,sum);
- }
- printf("\n");
- return 0;
- }
- c. poisson.c
- #include <stdio.h>
- #include <gsl/gsl_randist.h>
- int main(void)
- {
- int i, n=10;
- double mu=3.0;
- float sum= 0.0;
- /*prints probability distribution table*/
- printf("random variable ||| probability ||| cumulative probability \n");
- printf("---------------------------------------------------------------------------------- \n");
- for (i=0 ; i<=n ; i++)
- {
- float k = gsl_ran_poisson_pdf(i,mu);
- sum= sum +k;
- printf("%d\t\t%f\t\t%f\n",i,k,sum);
- }
- printf("\n");
- return 0;
- }
- d. hypergeometric.c
- #include <stdio.h>
- #include <gsl/gsl_randist.h>
- int main(void)
- {
- int x , s, f ,n;
- n=6;
- x=2;
- //random variable
- s= 13;
- //success
- f= 39;
- //failure
- /*prints probability */
- printf("random variable ||| probability \n"); printf("--------------------------------------------\n");
- double pmf = gsl_ran_hypergeometric_pdf(x,s,f,n);
- printf("%d\t%3.6f\n",x,pmf);
- return 0;
- }
- e. multinomial.c
- #include <stdio.h>
- #include
- <gsl/gsl_randist.h>
- int main(void)
- {
- int k=3;
- const double p[]={0.2,0.4,0.4};
- const unsigned int n[]={2,3,4};
- /*prints probability distribution table*/
- printf("random variable ||| probability \n");
- double pmf = gsl_ran_multinomial_pdf(k,p,n);
- printf("%3.9f\n",pmf);
- return 0;
- }
- f. uniform.c
- #include <stdio.h>
- #include <gsl/gsl_randist.h>
- Int main (void)
- {
- double x;
- int a,b ;
- printf("enter vaue for x ,a,b \n");
- scanf("%f",&x);
- scanf("%d",&a);
- scanf("%d",&b);
- float sum=0;
- /* prints probability distibution table*/
- printf("random variable|||probability \n");
- printf("------------------------------------------------------- \n");
- float k = (float)gsl_ran_flat_pdf (x,a,b);
- printf("%f\t\t%f\n",x,k);
- return 0;
- }
- Continuous Distributions:
- #include <stdio.h>
- #include <math.h>
- #include <gsl/gsl_rng.h>
- #include <gsl/gsl_randist.h>
- #include <gsl/gsl_cdf.h>
- void normal();
- void beta();
- void gamma1();
- void exponential();
- void lognormal();
- int main()
- {
- int choice;
- printf (" 1.Normal Distribution \n 2.Beta Distribution \n 3.Gamma Distribution \n 4.Exponential Distribution \n 5.Lognormal Distribution\n");
- printf ("Enter Your Choice\n");
- scanf("%d", &choice);
- switch (choice)
- {
- case 1:normal();
- break;
- case 2:beta();
- break;
- case 3:gamma1();
- break;
- case 4:exponential();
- break;
- case 5:lognormal();
- break;
- default : printf("Wrong Choice \n");
- }
- return 0;
- }
- void normal()
- { double p , q;
- double x= 10;
- double sigma=5;
- double pdf;
- printf("Normal Distribution: x= %f sigma=%f\n", x , sigma);
- pdf = gsl_ran_gaussian_pdf(x,sigma);
- printf("prob(x=%f)=%f\n",x,pdf);
- p=gsl_cdf_gaussian_P(x, sigma);
- printf("prob(x<%f)= %f\n",x,p);
- q=gsl_cdf_gaussian_Q(x,sigma);
- printf("prob(x>%f)= %f\n",x,q);
- x=gsl_cdf_gaussian_Pinv(p, sigma);
- printf("pinv(%f)= %f\n",p,x);
- x=gsl_cdf_gaussian_Qinv(q, sigma);
- printf("qinv(%f)= %f\n",q,x);
- }
- void gamma1()
- {
- double p , q;
- double x= 1.5;
- double a=1, b=2;
- double pdf;
- printf("Gamma Distribution: x= %f a=%f b=%f \n", x , a,b);
- pdf = gsl_ran_gamma_pdf(x,a,b);
- printf("prob(x=%f)=%f\n",x,pdf);
- p=gsl_cdf_gamma_P(x, a,b);
- printf("prob(x<%f)= %f\n",x,p);
- q=gsl_cdf_gamma_Q(x,a,b);
- printf("prob(x>%f)= %f\n",x,q);
- x=gsl_cdf_gamma_Pinv(p,a,b);
- printf("pinv(%f)= %f\n",p,x);
- x=gsl_cdf_gamma_Qinv(q,a,b);
- printf("qinv(%f)= %f\n",q,x);
- }
- void exponential()
- {
- double p , q;
- double x= 0.05;
- double lamda=2;
- double pdf;
- printf("Exponential Distribution: x= %f lamda=%f \n", x ,lamda);
- pdf = gsl_ran_exponential_pdf(x,lamda);
- printf("prob(x=%f)=%f\n",x,pdf);
- p=gsl_cdf_exponential_P(x,lamda);
- printf("prob(x<%f)= %f\n",x,p);
- q=gsl_cdf_exponential_Q(x,lamda);
- printf("prob(x>%f)= %f\n",x,q);
- x=gsl_cdf_exponential_Pinv(p,lamda);
- printf("pinv(%f)= %f\n",p,x);
- x=gsl_cdf_exponential_Qinv(q,lamda);
- printf("qinv(%f)= %f\n",q,x);
- }
- void beta()
- {
- double p , q;
- double x= 0.8;
- double a=0.5,
- b=0.5;
- double pdf;
- printf("Beta Distribution: x= %f a=%f b=%f \n", x , a,b);
- pdf = gsl_ran_beta_pdf(x,a,b);
- printf("prob(x=%f)=%f\n",x,pdf);
- p=gsl_cdf_beta_P(x, a,b);
- printf("prob(x<%f)= %f\n",x,p);
- q=gsl_cdf_beta_Q(x,a,b);
- printf("prob(x>%f)= %f\n",x,q);
- x=gsl_cdf_beta_Pinv(p,a,b);
- printf("pinv(%f)= %f\n",p,x);
- x=gsl_cdf_beta_Qinv(q,a,b);
- printf("qinv(%f)= %f\n",q,x);
- }
- void lognormal()
- {
- double p , q;
- double x=4;
- double zeta=2 ,sigma=5;
- double pdf;
- printf("Lognormal Distribution: x= %f zeta = %f sigma=%f\n", x , sigma);
- pdf = gsl_ran_lognormal_pdf(x,zeta,sigma);
- printf("prob(x=%f)=%f\n",x,pdf);
- p=gsl_cdf_lognormal_P(x,zeta, sigma);
- printf("prob(x<%f)= %f\n",x,p);
- q=gsl_cdf_lognormal_Q(x,zeta,sigma);
- printf("prob(x>%f)= %f\n",x,q);
- x=gsl_cdf_lognormal_Pinv(p,zeta,sigma);
- printf("pinv(%f)= %f\n",p,x);
- x=gsl_cdf_lognormal_Qinv(q,zeta, sigma);
- printf("qinv(%f)= %f\n",q,x);
- }
- Practical No.5
- a. Implementing OLS regression
- #include <stdio.h>
- #include <gsl/gsl_fit.h>
- int main(void)
- {
- int i,n=4;
- double x[4]={1970, 1980, 1990,2000};
- double y[4]={12,11,14,13};
- double w[4]={0.1,0.2,0.3,0.4};
- double c0,c1,cov00,cov01,cov11,chisq;
- gsl_fit_wlinear(x,1,w,1,y,1,n,&c0,&c1,&cov00,&cov01,&cov11,&chisq);
- printf("#bestfit:Y=%g+%gX\n",c0,c1);
- printf("#covariance matrix : \n");
- printf("#[%g,%g\n#%g%g]\n",cov00,cov01,cov01,cov11);
- printf("#chisq=%g\n",chisq);
- for (i=0;i<n;i++)
- printf("data:%g%g%g\n",x[i],y[i],1/sqrt(w[i]));
- printf("\n");
- for (i=-30;i<130;i++)
- {
- double xf = x[0] + (i/100.0)*(x[n-1]-x[0]);
- double yf , yf_err;
- gsl_fit_linear_est(xf,c0 , c1, cov00, cov01,cov11,&yf,&yf_err);
- printf("fit : %g%g\n",xf,yf);
- printf("hi : %g%g\n",xf,yf+yf_err);
- printf("lo : %g%g\n",xf,yf-yf_err);
- }
- return 0;
- }
- b. Implementing goodness of fit –chi square
- #include <apop.h>
- int main()
- {
- apop_db_open("data-climate.db");
- apop_data *precip = apop_query_to_data("select PCP from precip");
- apop_model *est = apop_estimate(precip,apop_normal);
- apop_data *precip_binned = apop_data_to_bins(precip/*,.bin_count=180*/);
- apop_model *datahist=apop_estimate(precip_binned,apop_pmf);
- apop_model*modelhist=apop_model_to_pmf(.model=est,.binspec=apop_data_get_page(precip_binned,"<binspec>"),.draws=1e5);
- double scaling=apop_sum(datahist->data->weights)/apop_sum(modelhist->data->weights);
- gsl_vector_scale(modelhist->data->weights,scaling);
- apop_data_show(apop_histograms_test_goodness_of_fit(datahist,modelhist));
- }
- Practical No.6
- Generating random numbers with Monte Carlo method
- #include <gsl/gsl_rng.h>
- gsl_rng *r;
- int main (void)
- {
- const gsl_rng_type *T;
- gsl_rng_env_setup();
- T =gsl_rng_default;
- r= gsl_rng_alloc(T);
- printf("Generator type: %s\n", gsl_rng_name(r));
- printf("seed=%lu \n",gsl_rng_default_seed);
- printf("First value = %lu\n",gsl_rng_get(r));
- gsl_rng_free(r);
- return 0;
- }
- a. Exponential distribution
- #include <stdio.h>
- #include <math.h>
- #include <stdlib.h>
- #include <gsl/gsl_rng.h>
- #include <gsl/gsl_randist.h>
- int main(int argc , char *argv[])
- {
- int i ,n; float x , alpha;
- gsl_rng *r=gsl_rng_alloc (gsl_rng_mt19937);
- /*gsl initialises GSL Rng*/ n= atoi(argv[1]);
- alpha=atof(argv[2]);
- x=0;
- for (i=0; i<n;i++)
- {
- x= alpha *x + gsl_ran_exponential(r,1);
- printf("%2.4f\n",x);
- }
- return 0;
- }
- b. Uniform distribution
- #include <stdio.h>
- #include <gsl/gsl_rng.h>
- int main (void)
- {
- const gsl_rng_type *T;
- gsl_rng *r;
- int i , n=10;
- gsl_rng_env_setup();
- T =gsl_rng_default;
- r= gsl_rng_alloc(T);
- for (i=0;i<n;i++)
- {
- double u=gsl_rng_uniform(r);
- printf("%.5f\n",u);
- }
- gsl_rng_free(r);
- return 0;
- }
- c. Binomial distribution
- #include <stdio.h>
- #include <gsl/gsl_rng.h>
- #include<gsl/gsl_randist.h>
- int main (void)
- {
- const gsl_rng_type *T;
- gsl_rng *r;
- int i , n=10;
- /*create a generator chosen by the environment variable gsl_rng_type*/
- gsl_rng_env_setup();
- T =gsl_rng_default;
- r= gsl_rng_alloc(T);
- float p= 0.3;
- /*print n random variates chosen from the binomial distribution with mean parameter mu*/
- for (i=0;i<n;i++)
- {
- unsigned int k = gsl_ran_binomial(r,p,n);
- printf("%u",k);
- }
- printf("\n");
- gsl_rng_free(r);
- return 0;
- }
- Practical No.8
- Implementing non-parametric testing – ANOVA
- #include <apop.h>
- int main()
- {
- apop_db_open("data-metro.db");
- char joinedtab[ ]="(select year , riders , line \ from riders , lines \
- where riders.station=lines.station)";
- apop_data_show(apop_anova(joinedtab,"riders","line","year"));
- }
- NEW sTART HERE
- Discrete Distribution
- 1.Binomial Distribution
- #include<stdio.h>
- #include<gsl/gsl_randist.h>
- int main (void)
- {
- int i,n=5;
- double p=0.6;
- float sum=0;
- printf("random variable ||| probability ||| cumulative prob\n");
- printf("---------------------------------------------------\n");
- for (i=0;i<=n;i++)
- {
- float k= gsl_ran_binomial_pdf(i,p,n);
- sum = sum+k;
- printf("%d\t\t%f\t\t\t%f\n",i,k,sum);
- }
- printf("\n");
- return 0;
- }
- 2.Hypergeometric Distributuon
- #include<stdio.h>
- #include<gsl/gsl_randist.h>
- int main (void)
- {
- int x,s,f,n;
- n=6;
- x=2; //random vaiable
- s=13; //sucess
- f=39; //failure
- printf("random variable ||| probability\n");
- printf("---------------------------------------------------\n");
- double pmf=gsl_ran_hypergeometric_pdf(x,s,f,n);
- printf("%d\t%3.6f\n",x,pmf);
- return 0;
- }
- 3.Multinomial Distribution
- #include<stdio.h>
- #include<gsl/gsl_randist.h>
- int main (void)
- {
- int k=3;
- const double p[] ={0.2,0.4,0.4};
- const unsigned int n[]= {2,3,4};
- printf("random variable ||| probability\n");
- printf("---------------------------------------------------\n");
- double pmf=gsl_ran_multinomial_pdf(k,p,n);
- printf("%3.9f\n",pmf);
- return 0;
- }
- 4.Poisson Distribution
- #include<stdio.h>
- #include<gsl/gsl_randist.h>
- int main (void)
- {
- int i,n=10;
- double mu=3.0;
- float sum=0;
- printf("random variable ||| probability ||| cumulative prob\n");
- printf("---------------------------------------------------\n");
- for (i=0;i<=n;i++)
- {
- float k= gsl_ran_poisson_pdf(i,mu);
- sum = sum+k;
- printf("%d\t\t%f\t\t\t%f\n",i,k,sum);
- }
- printf("\n");
- return 0;
- }
- 5.Bernoulli Distribution
- #include<stdio.h>
- #include<gsl/gsl_randist.h>
- int main (void)
- {
- int i;
- double p=0.6;
- float sum=0;
- printf("random variable ||| probability ||| cumulative prob\n");
- printf("---------------------------------------------------\n");
- for (i=0;i<=1;i++)
- {
- float k= gsl_ran_bernoulli_pdf(i,p);
- sum = sum+k;
- printf("%d\t\t%f\t\t\t%f\n",i,k,sum);
- }
- printf("\n");
- return 0;
- }
- Continuous Distribution
- normal ,Gamma,Exponential,Lognormal,Beta all using in case & also separate we do each
- #include<stdio.h>
- #include<gsl/gsl_randist.h>
- #include<math.h>
- #include<gsl/gsl_rng.h> //rng sstand range
- #include<gsl/gsl_cdf.h> //cdf cumulative distribution function
- void normal();
- void beta();
- void gamma1();
- void exponential();
- void lognormal();
- int main(void)
- {
- int choice;
- printf("continuous distribution\n");
- printf("--------------------------\n");
- printf("1.Normal Distribution\n");
- printf("2.Beta Distribution\n");
- printf("3.Gamma Distribution\n");
- printf("4.Exponential Distribution\n");
- printf("5.LogNormal Distribution\n");
- printf("Enter Your Choice\n");
- scanf("%d",&choice);
- switch(choice){
- case 1: normal();
- break;
- case 2: beta();
- break;
- case 3: gamma1();
- break;
- case 4: exponential();
- break;
- case 5: lognormal();
- break;
- default: printf("wrong choice\n");
- }
- return 0;
- }
- void normal() {
- double P,Q;
- double x=10;
- double sigma=5;
- double pdf;
- printf("normal distribution:x=%f sigma=%f\n",x,sigma);
- pdf=gsl_ran_gaussian_pdf(x,sigma);
- printf("probe(x=%f)=%f\n",x,pdf);
- P=gsl_cdf_gaussian_P(x,sigma);
- printf("prob(x<%f)=%f\n",x,P);
- Q=gsl_cdf_gaussian_Q(x,sigma);
- printf("prob(x>%f)=%f\n",x,Q);
- x=gsl_cdf_gaussian_Pinv(P,sigma);
- printf("Pinv(%f)=%f\n",P,x);
- x=gsl_cdf_gaussian_Qinv(Q,sigma);
- printf("Qinv(%f)=%f\n",Q,x);
- }
- void gamma1() {
- double P,Q;
- double x=1.5;
- double a=1;
- double b=2;
- double pdf;
- printf("gamma distribution:x=%f a=%f,b=%f\n",x,a,b);
- pdf=gsl_ran_gamma_pdf(x,a,b);
- printf("probe(x=%f)=%f\n",x,pdf);
- P=gsl_cdf_gamma_P(x,a,b);
- printf("prob(x<%f)=%f\n",x,P);
- Q=gsl_cdf_gamma_Q(x,a,b);
- printf("prob(x>%f)=%f\n",x,Q);
- x=gsl_cdf_gamma_Pinv(P,a,b);
- printf("Pinv(%f)=%f\n",P,x);
- x=gsl_cdf_gamma_Qinv(Q,a,b);
- printf("Qinv(%f)=%f\n",Q,x);
- }
- void exponential() {
- double P,Q;
- double x=0.05;
- double lambda=2;
- double pdf;
- printf("exponential distribution:x=%f lambda=%f,b=%f\n",x,lambda);
- pdf=gsl_ran_exponential_pdf(x,lambda);
- printf("probe(x=%f)=%f\n",x,pdf);
- P=gsl_cdf_exponential_P(x,lambda);
- printf("prob(x<%f)=%f\n",x,P);
- Q=gsl_cdf_exponential_Q(x,lambda);
- printf("prob(x>%f)=%f\n",x,Q);
- x=gsl_cdf_exponential_Pinv(P,lambda);
- printf("Pinv(%f)=%f\n",P,x);
- x=gsl_cdf_exponential_Qinv(Q,lambda);
- printf("Qinv(%f)=%f\n",Q,x);
- }
- void beta() {
- double P,Q;
- double x=0.8;
- double a=0.5;
- double b=0.5;
- double pdf;
- printf("beta distribution:x=%f a=%f,b=%f\n",x,a,b);
- pdf=gsl_ran_beta_pdf(x,a,b);
- printf("probe(x=%f)=%f\n",x,pdf);
- P=gsl_cdf_beta_P(x,a,b);
- printf("prob(x<%f)=%f\n",x,P);
- Q=gsl_cdf_beta_Q(x,a,b);
- printf("prob(x>%f)=%f\n",x,Q);
- x=gsl_cdf_beta_Pinv(P,a,b);
- printf("Pinv(%f)=%f\n",P,x);
- x=gsl_cdf_beta_Qinv(Q,a,b);
- printf("Qinv(%f)=%f\n",Q,x);
- }
- void lognormal() {
- double P,Q;
- double x=4;
- double zeta=2;
- double sigma=1.5;
- double pdf;
- printf("lognormal distribution:x=%f zeta=%f,sigma=%f\n",x,zeta,sigma);
- pdf=gsl_ran_lognormal_pdf(x,zeta,sigma);
- printf("probe(x=%f)=%f\n",x,pdf);
- P=gsl_cdf_lognormal_P(x,zeta,sigma);
- printf("prob(x<%f)=%f\n",x,P);
- Q=gsl_cdf_lognormal_Q(x,zeta,sigma);
- printf("prob(x>%f)=%f\n",x,Q);
- x=gsl_cdf_lognormal_Pinv(P,zeta,sigma);
- printf("Pinv(%f)=%f\n",P,x);
- x=gsl_cdf_lognormal_Qinv(Q,zeta,sigma);
- printf("Qinv(%f)=%f\n",Q,x);
- }
- Monte Carlo Method three parts
- 1.exponential
- #include<stdlib.h>
- #include<stdio.h>
- #include<math.h>
- #include<gsl/gsl_rng.h>
- #include<gsl/gsl_randist.h>
- int main(int argc, char *argv[]){
- int i,n;
- float x,alpha;
- gsl_rng *r = gsl_rng_alloc(gsl_rng_mt19937); /*intilaze Gsl Rng*/
- n = atoi(argv[1]);
- alpha = atof(argv[2]);
- x = 0;
- for(i = 0; i < n; i++){
- x =alpha* x + gsl_ran_exponential(r,1);
- printf("%2.4f\n",x);
- }
- return (0);
- }
- run = $ ./7_exponential.out 10 0.4
- 2.Uniform
- //generating uniform random number range(0.0,1.0) using uniform distribution
- #include<stdio.h>
- #include<gsl/gsl_rng.h>
- #include<gsl/gsl_randist.h>
- int main(void)
- {
- const gsl_rng_type *T;
- gsl_rng *r;
- int i, n = 10;
- gsl_rng_env_setup();
- T = gsl_rng_default;
- r = gsl_rng_alloc(T);
- for(i = 0; i < n; i++){
- double U = gsl_rng_uniform(r);
- printf("%.5f\n",U);
- }
- gsl_rng_free(r);
- return 0;
- }
- 3.Binomial
- //binomial distribution
- #include<stdio.h>
- #include<gsl/gsl_rng.h>
- #include<gsl/gsl_randist.h>
- int main(void)
- {
- const gsl_rng_type *T;
- gsl_rng *r;
- int i, n = 10;
- gsl_rng_env_setup();
- T = gsl_rng_default;
- r = gsl_rng_alloc(T);
- float p = 0.3; //print n random variates chossen from the bionomial distribution with mean parameter mu.
- for(i = 0; i < n; i++){
- signed int k = gsl_ran_binomial(r,p,n);
- printf("%u",k);
- printf("\n");
- }
- gsl_rng_free(r);
- return 0;
- }
- Obtain mean, standard error and p value for in_per_capital from table income where sumlevel+0.0=40 from database data-census.db
- #include<apop.h>
- void one_boot(gsl_vector *base_data,gsl_rng *r,gsl_vector *boot_sample);
- void one_boot(gsl_vector *base_data,gsl_rng *r,gsl_vector *boot_sample)
- {
- for (int i=0;i<boot_sample->size;i++)
- gsl_vector_set(boot_sample,i,gsl_vector_get(base_data, gsl_rng_uniform_int
- (r,base_data->size)));
- }
- int main()
- {
- int rep_ct=10000;
- gsl_rng *r =apop_rng_alloc(0);
- apop_db_open("data-census.db");
- gsl_vector *base_data =apop_query_to_vector("select in_per_capita from income where sumlevel+0.0=40");
- double RI = apop_query_to_float("select in_per_capita from income where sumlevel+0.0=40 and geo_id2+0.0=44");
- gsl_vector *boot_sample = gsl_vector_alloc(base_data->size);
- gsl_vector *replications = gsl_vector_alloc(rep_ct);
- for (int i=0; i<rep_ct;i++)
- {
- one_boot (base_data, r,boot_sample);
- gsl_vector_set(replications,i,apop_mean(boot_sample));
- }
- double stderror = sqrt(apop_var(replications));
- double mean= apop_mean(replications);
- printf("mean:%g;standard error:%g;(RI-mean)/stderr:%g; pvalue:%g\n",mean,stderror,2*gsl_cdf_gaussian_Q(fabs(RI-mean),stderror));
- }
- implement weighted linear regression
- on the dataset given as follows .hence
- obtain best fit, covariance matrix &
- Chi square value.
- Year Production
- ( in lakhs)
- X Y W
- 1984 10 0.1
- 1989 15 0.2
- 1995 19 0 .3
- 1998 23 0.4
- table value put inside and balance code is same
- #include <stdio.h>
- #include <gsl/gsl_fit.h>
- int
- main (void)
- {
- int i, n = 4;
- double x[4] = { 1970, 1980, 1990, 2000 };
- double y[4] = { 12, 11, 14, 13 };
- double w[4] = { 0.1, 0.2, 0.3, 0.4 };
- 42
- double c0, c1, cov00, cov01, cov11, chisq;
- gsl_fit_wlinear (x, 1, w, 1, y, 1, n,
- &c0, &c1, &cov00, &cov01, &cov11,
- &chisq);
- printf ("# best fit: Y = %g + %g X\n", c0, c1);
- printf ("# covariance matrix:\n");
- printf ("# [ %g, %g\n# %g, %g]\n",
- cov00, cov01, cov01, cov11);
- printf ("# chisq = %g\n", chisq);
- for (i = 0; i < n; i++)
- printf ("data: %g %g %g\n",
- x[i], y[i], 1/sqrt(w[i]));
- printf ("\n");
- for (i = -30; i < 130; i++)
- {
- double xf = x[0] + (i/100.0) * (x[n-1] - x[0]);
- double yf, yf_err;
- gsl_fit_linear_est (xf,
- c0, c1,
- cov00, cov01, cov11,
- &yf, &yf_err);
- printf ("fit: %g %g\n", xf, yf);
- printf ("hi : %g %g\n", xf, yf + yf_err);
- printf ("lo : %g %g\n", xf, yf - yf_err);
- }
- return 0;
- }
- Implementing goodness of fit Chi Square
- #include <apop.h>
- int main(){
- apop_db_open("data-climate.db");
- apop_data *precip = apop_query_to_data("select PCP from precip");
- apop_model *est = apop_estimate(precip, apop_normal);
- apop_data *precip_binned = apop_data_to_bins(precip/*,
- .bin_count=180*/);
- apop_model *datahist = apop_estimate(precip_binned, apop_pmf);
- apop_model *modelhist = apop_model_to_pmf(.model=est,
- .binspec=apop_data_get_page(precip_binned, "<binspec>"), .draws=1e5);
- double scaling = apop_sum(datahist->data-
- >weights)/apop_sum(modelhist->data->weights);
- gsl_vector_scale(modelhist->data->weights, scaling);
- apop_data_show(apop_histograms_test_goodness_of_fit(datahist,
- modelhist));
- }
- implement non-parametric testing using anova on the columns riders,year &
- line from table riders & lines from data-metro database.
- //imlement non parametric testing
- #include<apop.h>
- int main(){
- apop_db_open("data-metro.db");
- char joinedtab[] = "(Select year, riders,line from riders ,lines Where riders.station = lines.station)";
- or
- char joinedtab[] = "(Select year, riders,line from riders ,lines Where riders.station = lines.station)"
- apop_data_show(apop_anova(joinedtab,"riders","line","year"));
- }
- /*char joinedtab[] = "(Select year, riders,line \
- from riders ,lines \
- Where riders.station = lines.station)";
- char joinedtab[] = "(Select year, riders,line"
- " from riders ,lines"
- " Where riders.station = lines.station)";
- */
- Q)Write a program in c & execute the same
- with gcc compiler via cygwin/ubuntu : to
- building an optimized model –x2sin(x) &
- then solving the same for maximum using
- foolwing methods of obtaining optima
- Apop_simplex_nm
- Apop_cg_fr
- Apop_siman
- Apop_rf_newton
- i)save as sinsq.c extension
- #include <apop.h>
- double sin_square(apop_data *data, apop_model *m){
- double x = apop_data_get(m->parameters, 0, -1);
- return -sin(x)*gsl_pow_2(x);
- }
- apop_model sin_sq_model ={"-sin(x) times x^2",1, .p = sin_square};
- ii)that file call here
- #include "sinsq.c"
- void do_search(int number, char *name, char *trace){
- apop_model *out;
- double p[] = {0};
- double result;
- char *outf;
- asprintf(&outf, "localmax_out/%s.gplot", trace);
- Apop_model_add_group(&sin_sq_model, apop_mle,
- .starting_pt= p,
- .method= number, .tolerance= 1e-4,
- .mu_t= 1.25, .trace_path= outf);
- out = apop_estimate(NULL, sin_sq_model);
- result = gsl_vector_get(out->parameters->vector, 0);
- printf("The %s algorithm found %g.\n", name, result);
- Apop_settings_rm_group(&sin_sq_model, apop_mle);
- }
- int main(){
- system ("mkdir -p localmax_out; rm -f localmax_out/*.gplot");
- apop_opts.verbose ++;
- do_search(APOP_SIMPLEX_NM, "N-M Simplex", "simplex");
- do_search(APOP_CG_FR, "F-R Conjugate gradient", "fr");
- do_search(APOP_SIMAN, "Simulated annealing", "siman");
- do_search(APOP_RF_NEWTON, "Root-finding", "root");
- fflush(NULL);
- system("sed -i \"1iplot '-'\" localmax_out/*.gplot");
- }
- Comparing 2 models using likelihood ratio
- #include <apop.h>
- apop_model * dummies(int slope_dummies){
- apop_data *d = apop_query_to_mixed_data("mmt", "select riders, year-
- 1977, line \
- from riders, lines \
- where riders.station=lines.station");
- apop_data *dummified = apop_data_to_dummies(d, 0, 't', .append='y',
- .remove='y');
- if (slope_dummies){
- Apop_col(d, 1, yeardata);
- for(int i=0; i < dummified->matrix->size2; i ++){
- Apop_col(dummified, i, c);
- gsl_vector_mul(c, yeardata);
- }
- }
- apop_model *out = apop_estimate(dummified, apop_ols);
- apop_model_show(out);
- return out;
- }
- #ifndef TESTING
- int main(){
- apop_db_open("data-metro.db");
- printf("With constant dummies:\n"); dummies(0);
- printf("With slope dummies:\n"); dummies(1);
- }
- #endif
- #define TESTING
- #include "dummies.c"
- void show_normal_test(apop_model *unconstrained, apop_model
- *constrained, int n){
- double statistic = (apop_data_get(unconstrained->info, .rowname="log
- likelihood")
- - apop_data_get(constrained->info, .rowname="log
- likelihood"))/sqrt(n);
- double confidence = gsl_cdf_gaussian_P(fabs(statistic), 1); //one-tailed.
- printf("The Normal statistic is: %g, so reject the null of no difference
- between models "
- "with %g%% confidence.\n", statistic, confidence*100);
- }
- int main(){
- apop_db_open("data-metro.db");
- apop_model *m0 = dummies(0);
- apop_model *m1 = dummies(1);
- show_normal_test(m0, m1, m0->data->matrix->size1);
- }
- Practical III
- 1.Write a program in c & execute the same with gcc compiler via cygwin/ubuntu : to
- plot a vector of columns (year*12+month)/12., temp of temp table from database
- data-climate.(pipe vector)
- Plotting a vector
- #include <apop.h>
- void plot_matrix_now(gsl_matrix *data){
- static FILE *gp = NULL;
- if (!gp)
- gp = popen("gnuplot -persist", "w");
- if (!gp){
- printf("Couldn't open Gnuplot.\n");
- return;
- }
- fprintf(gp,"reset; plot '-' \n");
- apop_matrix_print(data, .output_pipe=gp);
- fflush(gp);
- }
- int main(){
- apop_db_open("data-climate.db");
- plot_matrix_now(apop_query_to_matrix("select (year*12+month)/12., temp from temp"));
- }
- jayesh@jayesh-G31M-S2L:~$ gcc -std=gnu99 pipeplot.c -o pipeplot.out -
- lapophenia -lgsl -lsqlite3
- jayesh@jayesh-G31M-S2L:~$ ./pipeplot.out
- 2.lattice
- Eigen vector
- i)eigenbox.h
- #include<apop.h>
- #include<gsl/gsl_elgen.h>
- void show_projection(gsl_matrix *pc_space, apop_data *data);
- apop_data *query_data();
- ii)header call here
- #include "eigenbox.h"
- apop_data *query_data(){
- apop_db_open("data-census.db");
- return apop_query_to_data(" select postcode as row_names, "
- " m_per_100_f, population/1e6 as population, median_age "
- " from geography, income,demos,postcodes "
- " where income.sumlevel= '040' "
- " and geography.geo_id = demos.geo_id "
- " and income.geo_name = postcodes.state "
- " and geography.geo_id = income.geo_id ");
- }
- void show_projection(gsl_matrix *pc_space, apop_data *data){
- fprintf(stderr,"The eigenvectors:\n");
- apop_matrix_print(pc_space, .output_pipe=stderr);
- apop_data *projected = apop_dot(data, apop_matrix_to_data(pc_space));
- printf("plot '-' using 2:3:1 with labels\n");
- apop_data_show(projected);
- }
- int main(){
- apop_plot_lattice(query_data(), "out");
- }
- jayesh@jayesh-G31M-S2L:~$ gcc -std=gnu99 eigenbox.c -o eigenbox.out -
- lapophenia -lgsl -lsqlite3
- jayesh@jayesh-G31M-S2L:~$ ./eigenbox.out
- jayesh@jayesh-G31M-S2L:~$ gnuplot -persist < out
- 3.error bars
- Query out the month, average, and variance, and plot the data using errorbars.
- Prints to stdout, so pipe the output through Gnuplo
- #include <apop.h>
- int main(){
- apop_db_open("data-climate.db");
- apop_data *d = apop_query_to_data("select \
- (yearmonth/100. - round(yearmonth/100.))*100 as month, \
- avg(tmp), stddev(tmp) \
- from precip group by month");
- printf("set xrange*0:13+; plot ’-’ with errorbars\n");
- apop_matrix_show(d->matrix);
- }
- jayesh@jayesh-G31M-S2L:~$ gcc -std=gnu99 errorbars.c -o errorbars.out -
- lapophenia -lgsl -lsqlite3
- jayesh@jayesh-G31M-S2L:~$ ./errorbars.out | gnuplot –persist
- p2
- i) Multiplication Table
- #include <apop.h>
- int main(){
- gsl_matrix *m = gsl_matrix_alloc(20,15);
- gsl_matrix_set_all(m, 1);
- for (int i=0; i< m->size1; i++){
- Apop_matrix_row(m, i, one_row);
- gsl_vector_scale(one_row, i+1);
- }
- for (int i=0; i< m->size2; i++){
- Apop_matrix_col(m, i, one_col);
- gsl_vector_scale(one_col, i+1);
- }
- apop_matrix_show(m);
- gsl_matrix_free(m);
- }
- jayesh@jayesh-G31M-S2L:~$ gcc -std=gnu99 multiplicationtable.c -o
- multiplicationtable.out -lapophenia -lgsl -lsqlite3
- jayesh@jayesh-G31M-S2L:~$ ./multiplicationtable.out
- ii) the function in will take in a double indicating taxable income and will
- return US income taxes owed, assuming a head of household with two
- dependents taking the standard deduction
- #include <apop.h>
- double calc_taxes(double income){
- double cutoffs[] = {0, 11200, 42650, 110100, 178350, 349700, INFINITY};
- double rates[] = {0, 0.10, .15, .25, .28, .33, .35};
- double tax = 0;
- int bracket = 1;
- income -= 7850; //Head of household standard deduction
- income -= 3400*3; //exemption: self plus two dependents.
- while (income > 0){
- tax += rates[bracket] * GSL_MIN(income, cutoffs[bracket]-cutoffs[bracket-
- 1]);
- income -= cutoffs[bracket];
- bracket ++;
- }
- return tax;
- }
- int main(){
- apop_db_open("data-census.db");
- strncpy(apop_opts.db_name_column, "geo_name", 100);
- apop_data *d = apop_query_to_data("select geo_name,
- Household_median_in as income\
- from income where sumlevel = '040'\
- order by household_median_in desc");
- Apop_col_t(d, "income", income_vector);
- d->vector = apop_vector_map(income_vector, calc_taxes);
- apop_name_add(d->names, "tax owed", 'v');
- apop_data_show(d);
- }
- jayesh@jayesh-G31M-S2L:~$ gcc -std=gnu99 taxes.c -o taxes.out -lapophenia -
- lgsl -lsqlite3
- jayesh@jayesh-G31M-S2L:~$ ./taxes.out
Add Comment
Please, Sign In to add comment