AjuuKaru

dat1

Jan 29th, 2019
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 32.76 KB | None | 0 0
  1. Practical No.1
  2. Mont carlo- GSL_RNG_TYPE="taus" GSL_RNG_SEED=124 ./pn.out
  3.  
  4.  
  5.  
  6. .schema
  7. .databases
  8. cd ..
  9. .header on
  10. .mode column
  11. Select * from tn;
  12.  
  13. Create database
  14. sqlite3 test.db
  15.  
  16. Create table
  17. Create table emp(
  18. Id Int pk nn,
  19. Name text nn,
  20. Salary real);
  21.  
  22. Insert records:
  23. Insert into tn(is,name,salary) values (6,'hii',2346);
  24.  
  25. Alter column
  26. Alter table tn add cn datatype;
  27.  
  28. Update column
  29. Update tn set salary=234 where cconditio cn= 466;
  30.  
  31.  
  32.  
  33.  
  34.  
  35.  
  36. 1. List all the records where Age>=25 and salary>=65000
  37. :select * from company where age>=25 and salary>=6500;
  38.  
  39. 2. List all the records where Age>=25 or salary>=65000
  40. :select * from company where age>=25 or salary>=65000;
  41.  
  42. 3. List all the records where Age is not null.
  43. :select * from company where age is not null;
  44.  
  45. 4. List all the records where name starts with ‘R’
  46. :select * from from company where name like 'r%';
  47.  
  48. 5. List all the records where age is either 25 or 27
  49. :select * from company where age in (25,27);
  50.  
  51. 6. List all the records where age is neither 25 or 27
  52. :select * from company where age not in (25,27);
  53.  
  54. 7. List all the records where age is in between 25 and 27
  55. :select * from company where age between 25 and 27;
  56.  
  57. 8. Find the total amount of salary for each customer
  58. :select name,sum(salary) from company group by name;
  59.  
  60. 9. Find total amount of salary on each customer order by name.
  61. :select id,name,sum(salary) from company group by name order by id;
  62.  
  63. 10.Find total amount of salary on each customer order by id(desc).
  64. :select id,name,sum(salary) from company group by name order by id desc;
  65.  
  66. 11.List down all the records where names occur greater than 2 times.
  67. :select * from company group by name having count(name)>2;
  68.  
  69. 12.List down the salaries in ascending order.
  70. : select * from company order by salary asc;
  71.  
  72. 13. List down first 5 records from the table
  73. :select * from company limit 5;
  74.  
  75. 14.List down 4 records except the first 2 records.
  76. :select * from company limit 4 offset 2;
  77.  
  78. 15. Cross join company and department table.
  79. :select emp_id,name,dept from company cross join department;
  80.  
  81. 16. Inner join company and department table where company id=department id
  82. :select emp_id,name,dept from company inner join department on company.id=department.id;
  83.  
  84. 17. Left outer join company and department table where company id=department id
  85. :select emp_id,name,dept from company left outer join department on company.id=department.id;
  86.  
  87.  
  88. 18.
  89. Select * from tn where purchasing date >'30-oct,-2018';
  90.  
  91. 19.select name ,product,purchasing date from tn where product='32Gbusb' order by des
  92.  
  93.  
  94.  
  95.  
  96. HOW TO RUN :- gcc -std=gnu99 *program_name.c* -o *program_name.out* -lapophenia -lgsl -lsqlite3;
  97.  
  98.  
  99.  
  100.  
  101. Practical No.2
  102.  
  103. a. Illustration of gsl Matrix multiplication
  104. #include <apop.h>
  105. int main()
  106. { gsl_matrix *m = gsl_matrix_alloc(20,15);
  107. gsl_matrix_set_all(m, 1);
  108. for (int i=0; i< m->size1; i++){ Apop_matrix_row(m, i, one_row);
  109. gsl_vector_scale(one_row, i+1);
  110. }
  111. for (int i=0; i< m->size2; i++)
  112. {
  113. Apop_matrix_col(m, i, one_col);
  114. gsl_vector_scale(one_col, i+1);
  115. }
  116. apop_matrix_show(m); gsl_matrix_free(m);
  117. }
  118.  
  119. Practical No.3
  120.  
  121. a. Gnu plot for plotting vectors 1: Plotting a vector
  122. #include <apop.h>
  123. void plot_matrix_now(gsl_matrix *data)
  124. { static FILE *gp = NULL;
  125. if (!gp)
  126. gp = popen("gnuplot -persist", "w");
  127. if (!gp)
  128. {
  129. printf("Couldn't open Gnuplot.\n");
  130. return;
  131. }
  132. fprintf(gp,"reset; plot '-' \n");
  133. apop_matrix_print(data, .output_pipe=gp);
  134. fflush(gp);
  135. }
  136. int main()
  137. {
  138. apop_db_open("data-climate.db");
  139. plot_matrix_now(apop_query_to_matrix("select (year*12+month)/12., temp from temp"));
  140. }
  141.  
  142. c. Gnu plot for plotting vectors 3: Error bars
  143.  
  144. #include <apop.h>
  145. int main()
  146. {
  147. apop_db_open("data-climate.db");
  148. 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");
  149. printf("set xrange*0:13+; plot ’-’ with errorbars\n");
  150. apop_matrix_show(d->matrix);
  151. }
  152.  
  153. Practical No.4
  154.  
  155. Discrete Distributions:
  156.  
  157. a . bernoulli.c
  158. #include<stdio.h>
  159. #include<gsl/gsl_randist.h>
  160. int main(void)
  161. {
  162. int i;
  163. double p = 0.6;
  164. float sum = 0;
  165. /* print probablity distribution */ printf("Random variable|||Probability|||cumulative
  166. probability\n");
  167. printf("---------------------------------------------------------\n");
  168. for(i=0;i<=1;i++)
  169. {
  170. float k = gsl_ran_bernoulli_pdf(i,p);
  171. sum = sum+k;
  172. printf("%d \t\t %f \t\t %f\n",i,k,sum);
  173. }
  174. printf("\n"); return 0;
  175. }
  176.  
  177. b. binomial.c
  178. #include <stdio.h>
  179. #include <gsl/gsl_randist.h>
  180. int main(void)
  181. {
  182. int i, n=5;
  183. double p=0.6;
  184. float sum= 0.0;
  185. /*prints probability distribution table*/
  186. printf("random variable ||| probability ||| cumulative probability \n");
  187. printf("---------------------------------------------------------------------------------- \n");
  188. for (i=0 ; i<=n ; i++)
  189. {
  190. float k = gsl_ran_binomial_pdf(i,p,n);
  191. sum= sum +k;
  192. printf("%d\t\t%f\t\t%f\n",i,k,sum);
  193. }
  194. printf("\n");
  195. return 0;
  196. }
  197.  
  198. c. poisson.c
  199. #include <stdio.h>
  200. #include <gsl/gsl_randist.h>
  201. int main(void)
  202. {
  203. int i, n=10;
  204. double mu=3.0;
  205. float sum= 0.0;
  206. /*prints probability distribution table*/
  207. printf("random variable ||| probability ||| cumulative probability \n");
  208. printf("---------------------------------------------------------------------------------- \n");
  209. for (i=0 ; i<=n ; i++)
  210. {
  211. float k = gsl_ran_poisson_pdf(i,mu);
  212. sum= sum +k;
  213. printf("%d\t\t%f\t\t%f\n",i,k,sum);
  214. }
  215. printf("\n");
  216. return 0;
  217. }
  218.  
  219.  
  220. d. hypergeometric.c
  221.  
  222. #include <stdio.h>
  223. #include <gsl/gsl_randist.h>
  224.  
  225. int main(void)
  226. {
  227. int x , s, f ,n;
  228. n=6;
  229. x=2;
  230. //random variable
  231. s= 13;
  232. //success
  233. f= 39;
  234. //failure
  235. /*prints probability */
  236. printf("random variable ||| probability \n"); printf("--------------------------------------------\n");
  237. double pmf = gsl_ran_hypergeometric_pdf(x,s,f,n);
  238. printf("%d\t%3.6f\n",x,pmf);
  239. return 0;
  240. }
  241.  
  242. e. multinomial.c
  243.  
  244. #include <stdio.h>
  245. #include
  246. <gsl/gsl_randist.h>
  247.  
  248. int main(void)
  249. {
  250. int k=3;
  251. const double p[]={0.2,0.4,0.4};
  252. const unsigned int n[]={2,3,4};
  253. /*prints probability distribution table*/
  254. printf("random variable ||| probability \n");
  255. double pmf = gsl_ran_multinomial_pdf(k,p,n);
  256. printf("%3.9f\n",pmf);
  257. return 0;
  258. }
  259.  
  260. f. uniform.c
  261.  
  262. #include <stdio.h>
  263. #include <gsl/gsl_randist.h>
  264.  
  265. Int main (void)
  266. {
  267. double x;
  268. int a,b ;
  269. printf("enter vaue for x ,a,b \n");
  270. scanf("%f",&x);
  271. scanf("%d",&a);
  272. scanf("%d",&b);
  273. float sum=0;
  274. /* prints probability distibution table*/
  275. printf("random variable|||probability \n");
  276. printf("------------------------------------------------------- \n");
  277. float k = (float)gsl_ran_flat_pdf (x,a,b);
  278. printf("%f\t\t%f\n",x,k);
  279. return 0;
  280. }
  281.  
  282.  
  283. Continuous Distributions:
  284. #include <stdio.h>
  285. #include <math.h>
  286. #include <gsl/gsl_rng.h>
  287. #include <gsl/gsl_randist.h>
  288. #include <gsl/gsl_cdf.h>
  289.  
  290. void normal();
  291. void beta();
  292. void gamma1();
  293. void exponential();
  294. void lognormal();
  295.  
  296. int main()
  297. {
  298. int choice;
  299.  
  300. printf (" 1.Normal Distribution \n 2.Beta Distribution \n 3.Gamma Distribution \n 4.Exponential Distribution \n 5.Lognormal Distribution\n");
  301. printf ("Enter Your Choice\n");
  302. scanf("%d", &choice);
  303. switch (choice)
  304. {
  305. case 1:normal();
  306. break;
  307. case 2:beta();
  308. break;
  309. case 3:gamma1();
  310. break;
  311. case 4:exponential();
  312. break;
  313. case 5:lognormal();
  314. break;
  315. default : printf("Wrong Choice \n");
  316. }
  317. return 0;
  318. }
  319. void normal()
  320. { double p , q;
  321. double x= 10;
  322. double sigma=5;
  323. double pdf;
  324. printf("Normal Distribution: x= %f sigma=%f\n", x , sigma);
  325.  
  326. pdf = gsl_ran_gaussian_pdf(x,sigma);
  327. printf("prob(x=%f)=%f\n",x,pdf);
  328.  
  329. p=gsl_cdf_gaussian_P(x, sigma);
  330. printf("prob(x<%f)= %f\n",x,p);
  331.  
  332. q=gsl_cdf_gaussian_Q(x,sigma);
  333. printf("prob(x>%f)= %f\n",x,q);
  334.  
  335. x=gsl_cdf_gaussian_Pinv(p, sigma);
  336. printf("pinv(%f)= %f\n",p,x);
  337.  
  338. x=gsl_cdf_gaussian_Qinv(q, sigma);
  339. printf("qinv(%f)= %f\n",q,x);
  340. }
  341. void gamma1()
  342. {
  343. double p , q;
  344. double x= 1.5;
  345. double a=1, b=2;
  346. double pdf;
  347. printf("Gamma Distribution: x= %f a=%f b=%f \n", x , a,b);
  348.  
  349. pdf = gsl_ran_gamma_pdf(x,a,b);
  350. printf("prob(x=%f)=%f\n",x,pdf);
  351.  
  352. p=gsl_cdf_gamma_P(x, a,b);
  353. printf("prob(x<%f)= %f\n",x,p);
  354.  
  355. q=gsl_cdf_gamma_Q(x,a,b);
  356. printf("prob(x>%f)= %f\n",x,q);
  357.  
  358. x=gsl_cdf_gamma_Pinv(p,a,b);
  359. printf("pinv(%f)= %f\n",p,x);
  360. x=gsl_cdf_gamma_Qinv(q,a,b);
  361. printf("qinv(%f)= %f\n",q,x);
  362. }
  363. void exponential()
  364. {
  365. double p , q;
  366. double x= 0.05;
  367. double lamda=2;
  368. double pdf;
  369. printf("Exponential Distribution: x= %f lamda=%f \n", x ,lamda);
  370.  
  371. pdf = gsl_ran_exponential_pdf(x,lamda);
  372. printf("prob(x=%f)=%f\n",x,pdf);
  373.  
  374. p=gsl_cdf_exponential_P(x,lamda);
  375. printf("prob(x<%f)= %f\n",x,p);
  376.  
  377. q=gsl_cdf_exponential_Q(x,lamda);
  378. printf("prob(x>%f)= %f\n",x,q);
  379.  
  380. x=gsl_cdf_exponential_Pinv(p,lamda);
  381. printf("pinv(%f)= %f\n",p,x);
  382.  
  383. x=gsl_cdf_exponential_Qinv(q,lamda);
  384. printf("qinv(%f)= %f\n",q,x);
  385. }
  386. void beta()
  387. {
  388. double p , q;
  389. double x= 0.8;
  390. double a=0.5,
  391. b=0.5;
  392. double pdf;
  393. printf("Beta Distribution: x= %f a=%f b=%f \n", x , a,b);
  394.  
  395. pdf = gsl_ran_beta_pdf(x,a,b);
  396. printf("prob(x=%f)=%f\n",x,pdf);
  397.  
  398. p=gsl_cdf_beta_P(x, a,b);
  399. printf("prob(x<%f)= %f\n",x,p);
  400. q=gsl_cdf_beta_Q(x,a,b);
  401. printf("prob(x>%f)= %f\n",x,q);
  402. x=gsl_cdf_beta_Pinv(p,a,b);
  403. printf("pinv(%f)= %f\n",p,x);
  404.  
  405. x=gsl_cdf_beta_Qinv(q,a,b);
  406. printf("qinv(%f)= %f\n",q,x);
  407. }
  408. void lognormal()
  409. {
  410. double p , q;
  411. double x=4;
  412. double zeta=2 ,sigma=5;
  413. double pdf;
  414. printf("Lognormal Distribution: x= %f zeta = %f sigma=%f\n", x , sigma);
  415.  
  416. pdf = gsl_ran_lognormal_pdf(x,zeta,sigma);
  417. printf("prob(x=%f)=%f\n",x,pdf);
  418.  
  419. p=gsl_cdf_lognormal_P(x,zeta, sigma);
  420. printf("prob(x<%f)= %f\n",x,p);
  421.  
  422. q=gsl_cdf_lognormal_Q(x,zeta,sigma);
  423. printf("prob(x>%f)= %f\n",x,q);
  424.  
  425. x=gsl_cdf_lognormal_Pinv(p,zeta,sigma);
  426. printf("pinv(%f)= %f\n",p,x);
  427.  
  428. x=gsl_cdf_lognormal_Qinv(q,zeta, sigma);
  429. printf("qinv(%f)= %f\n",q,x);
  430. }
  431.  
  432.  
  433. Practical No.5
  434.  
  435. a. Implementing OLS regression
  436.  
  437. #include <stdio.h>
  438. #include <gsl/gsl_fit.h>
  439. int main(void)
  440. {
  441.  
  442. int i,n=4;
  443. double x[4]={1970, 1980, 1990,2000};
  444. double y[4]={12,11,14,13};
  445. double w[4]={0.1,0.2,0.3,0.4};
  446. double c0,c1,cov00,cov01,cov11,chisq;
  447.  
  448. gsl_fit_wlinear(x,1,w,1,y,1,n,&c0,&c1,&cov00,&cov01,&cov11,&chisq);
  449. printf("#bestfit:Y=%g+%gX\n",c0,c1);
  450. printf("#covariance matrix : \n");
  451. printf("#[%g,%g\n#%g%g]\n",cov00,cov01,cov01,cov11);
  452. printf("#chisq=%g\n",chisq);
  453. for (i=0;i<n;i++)
  454. printf("data:%g%g%g\n",x[i],y[i],1/sqrt(w[i]));
  455. printf("\n");
  456. for (i=-30;i<130;i++)
  457. {
  458. double xf = x[0] + (i/100.0)*(x[n-1]-x[0]);
  459. double yf , yf_err;
  460. gsl_fit_linear_est(xf,c0 , c1, cov00, cov01,cov11,&yf,&yf_err);
  461. printf("fit : %g%g\n",xf,yf);
  462. printf("hi : %g%g\n",xf,yf+yf_err);
  463. printf("lo : %g%g\n",xf,yf-yf_err);
  464. }
  465. return 0;
  466. }
  467.  
  468.  
  469. b. Implementing goodness of fit –chi square
  470.  
  471. #include <apop.h>
  472. int main()
  473. {
  474. apop_db_open("data-climate.db");
  475.  
  476. apop_data *precip = apop_query_to_data("select PCP from precip");
  477. apop_model *est = apop_estimate(precip,apop_normal);
  478. apop_data *precip_binned = apop_data_to_bins(precip/*,.bin_count=180*/);
  479. apop_model *datahist=apop_estimate(precip_binned,apop_pmf);
  480. apop_model*modelhist=apop_model_to_pmf(.model=est,.binspec=apop_data_get_page(precip_binned,"<binspec>"),.draws=1e5);
  481. double scaling=apop_sum(datahist->data->weights)/apop_sum(modelhist->data->weights);
  482. gsl_vector_scale(modelhist->data->weights,scaling);
  483. apop_data_show(apop_histograms_test_goodness_of_fit(datahist,modelhist));
  484. }
  485.  
  486.  
  487. Practical No.6
  488.  
  489. Generating random numbers with Monte Carlo method
  490.  
  491. #include <gsl/gsl_rng.h>
  492. gsl_rng *r;
  493. int main (void)
  494. {
  495. const gsl_rng_type *T;
  496. gsl_rng_env_setup();
  497. T =gsl_rng_default;
  498. r= gsl_rng_alloc(T);
  499.  
  500. printf("Generator type: %s\n", gsl_rng_name(r));
  501. printf("seed=%lu \n",gsl_rng_default_seed);
  502. printf("First value = %lu\n",gsl_rng_get(r));
  503. gsl_rng_free(r);
  504. return 0;
  505. }
  506.  
  507. a. Exponential distribution
  508.  
  509. #include <stdio.h>
  510. #include <math.h>
  511. #include <stdlib.h>
  512. #include <gsl/gsl_rng.h>
  513. #include <gsl/gsl_randist.h>
  514. int main(int argc , char *argv[])
  515. {
  516. int i ,n; float x , alpha;
  517. gsl_rng *r=gsl_rng_alloc (gsl_rng_mt19937);
  518. /*gsl initialises GSL Rng*/ n= atoi(argv[1]);
  519. alpha=atof(argv[2]);
  520. x=0;
  521. for (i=0; i<n;i++)
  522. {
  523. x= alpha *x + gsl_ran_exponential(r,1);
  524. printf("%2.4f\n",x);
  525. }
  526. return 0;
  527. }
  528.  
  529. b. Uniform distribution
  530.  
  531. #include <stdio.h>
  532. #include <gsl/gsl_rng.h>
  533. int main (void)
  534. {
  535. const gsl_rng_type *T;
  536. gsl_rng *r;
  537. int i , n=10;
  538. gsl_rng_env_setup();
  539.  
  540. T =gsl_rng_default;
  541. r= gsl_rng_alloc(T);
  542.  
  543. for (i=0;i<n;i++)
  544. {
  545. double u=gsl_rng_uniform(r);
  546. printf("%.5f\n",u);
  547. }
  548. gsl_rng_free(r);
  549. return 0;
  550. }
  551.  
  552.  
  553. c. Binomial distribution
  554.  
  555. #include <stdio.h>
  556. #include <gsl/gsl_rng.h>
  557. #include<gsl/gsl_randist.h>
  558.  
  559. int main (void)
  560. {
  561. const gsl_rng_type *T;
  562. gsl_rng *r;
  563. int i , n=10;
  564.  
  565. /*create a generator chosen by the environment variable gsl_rng_type*/
  566. gsl_rng_env_setup();
  567. T =gsl_rng_default;
  568. r= gsl_rng_alloc(T);
  569. float p= 0.3;
  570. /*print n random variates chosen from the binomial distribution with mean parameter mu*/
  571. for (i=0;i<n;i++)
  572. {
  573. unsigned int k = gsl_ran_binomial(r,p,n);
  574. printf("%u",k);
  575. }
  576. printf("\n");
  577. gsl_rng_free(r);
  578. return 0;
  579. }
  580.  
  581.  
  582. Practical No.8
  583.  
  584.  
  585. Implementing non-parametric testing – ANOVA
  586.  
  587.  
  588. #include <apop.h>
  589. int main()
  590. {
  591. apop_db_open("data-metro.db");
  592. char joinedtab[ ]="(select year , riders , line \ from riders , lines \
  593. where riders.station=lines.station)";
  594. apop_data_show(apop_anova(joinedtab,"riders","line","year"));
  595. }
  596.  
  597.  
  598. NEW sTART HERE
  599.  
  600. Discrete Distribution
  601. 1.Binomial Distribution
  602. #include<stdio.h>
  603. #include<gsl/gsl_randist.h>
  604. int main (void)
  605. {
  606. int i,n=5;
  607. double p=0.6;
  608. float sum=0;
  609. printf("random variable ||| probability ||| cumulative prob\n");
  610. printf("---------------------------------------------------\n");
  611. for (i=0;i<=n;i++)
  612. {
  613. float k= gsl_ran_binomial_pdf(i,p,n);
  614. sum = sum+k;
  615. printf("%d\t\t%f\t\t\t%f\n",i,k,sum);
  616. }
  617. printf("\n");
  618. return 0;
  619. }
  620.  
  621. 2.Hypergeometric Distributuon
  622. #include<stdio.h>
  623. #include<gsl/gsl_randist.h>
  624. int main (void)
  625. {
  626. int x,s,f,n;
  627. n=6;
  628. x=2; //random vaiable
  629. s=13; //sucess
  630. f=39; //failure
  631.  
  632. printf("random variable ||| probability\n");
  633. printf("---------------------------------------------------\n");
  634. double pmf=gsl_ran_hypergeometric_pdf(x,s,f,n);
  635. printf("%d\t%3.6f\n",x,pmf);
  636. return 0;
  637. }
  638.  
  639. 3.Multinomial Distribution
  640. #include<stdio.h>
  641. #include<gsl/gsl_randist.h>
  642. int main (void)
  643. {
  644. int k=3;
  645. const double p[] ={0.2,0.4,0.4};
  646. const unsigned int n[]= {2,3,4};
  647. printf("random variable ||| probability\n");
  648. printf("---------------------------------------------------\n");
  649. double pmf=gsl_ran_multinomial_pdf(k,p,n);
  650. printf("%3.9f\n",pmf);
  651. return 0;
  652. }
  653.  
  654. 4.Poisson Distribution
  655. #include<stdio.h>
  656. #include<gsl/gsl_randist.h>
  657. int main (void)
  658. {
  659. int i,n=10;
  660. double mu=3.0;
  661. float sum=0;
  662. printf("random variable ||| probability ||| cumulative prob\n");
  663. printf("---------------------------------------------------\n");
  664. for (i=0;i<=n;i++)
  665. {
  666. float k= gsl_ran_poisson_pdf(i,mu);
  667. sum = sum+k;
  668. printf("%d\t\t%f\t\t\t%f\n",i,k,sum);
  669. }
  670. printf("\n");
  671. return 0;
  672. }
  673.  
  674. 5.Bernoulli Distribution
  675. #include<stdio.h>
  676. #include<gsl/gsl_randist.h>
  677. int main (void)
  678. {
  679. int i;
  680. double p=0.6;
  681. float sum=0;
  682. printf("random variable ||| probability ||| cumulative prob\n");
  683. printf("---------------------------------------------------\n");
  684. for (i=0;i<=1;i++)
  685. {
  686. float k= gsl_ran_bernoulli_pdf(i,p);
  687. sum = sum+k;
  688. printf("%d\t\t%f\t\t\t%f\n",i,k,sum);
  689. }
  690. printf("\n");
  691. return 0;
  692. }
  693.  
  694.  
  695. Continuous Distribution
  696. normal ,Gamma,Exponential,Lognormal,Beta all using in case & also separate we do each
  697.  
  698. #include<stdio.h>
  699. #include<gsl/gsl_randist.h>
  700. #include<math.h>
  701. #include<gsl/gsl_rng.h> //rng sstand range
  702. #include<gsl/gsl_cdf.h> //cdf cumulative distribution function
  703.  
  704. void normal();
  705. void beta();
  706. void gamma1();
  707. void exponential();
  708. void lognormal();
  709.  
  710. int main(void)
  711. {
  712. int choice;
  713. printf("continuous distribution\n");
  714. printf("--------------------------\n");
  715. printf("1.Normal Distribution\n");
  716. printf("2.Beta Distribution\n");
  717. printf("3.Gamma Distribution\n");
  718. printf("4.Exponential Distribution\n");
  719. printf("5.LogNormal Distribution\n");
  720. printf("Enter Your Choice\n");
  721. scanf("%d",&choice);
  722.  
  723. switch(choice){
  724. case 1: normal();
  725. break;
  726. case 2: beta();
  727. break;
  728. case 3: gamma1();
  729. break;
  730. case 4: exponential();
  731. break;
  732. case 5: lognormal();
  733. break;
  734. default: printf("wrong choice\n");
  735. }
  736. return 0;
  737. }
  738. void normal() {
  739. double P,Q;
  740. double x=10;
  741. double sigma=5;
  742. double pdf;
  743. printf("normal distribution:x=%f sigma=%f\n",x,sigma);
  744. pdf=gsl_ran_gaussian_pdf(x,sigma);
  745. printf("probe(x=%f)=%f\n",x,pdf);
  746. P=gsl_cdf_gaussian_P(x,sigma);
  747. printf("prob(x<%f)=%f\n",x,P);
  748. Q=gsl_cdf_gaussian_Q(x,sigma);
  749. printf("prob(x>%f)=%f\n",x,Q);
  750. x=gsl_cdf_gaussian_Pinv(P,sigma);
  751. printf("Pinv(%f)=%f\n",P,x);
  752. x=gsl_cdf_gaussian_Qinv(Q,sigma);
  753. printf("Qinv(%f)=%f\n",Q,x);
  754. }
  755. void gamma1() {
  756. double P,Q;
  757. double x=1.5;
  758. double a=1;
  759. double b=2;
  760. double pdf;
  761. printf("gamma distribution:x=%f a=%f,b=%f\n",x,a,b);
  762. pdf=gsl_ran_gamma_pdf(x,a,b);
  763. printf("probe(x=%f)=%f\n",x,pdf);
  764. P=gsl_cdf_gamma_P(x,a,b);
  765. printf("prob(x<%f)=%f\n",x,P);
  766. Q=gsl_cdf_gamma_Q(x,a,b);
  767. printf("prob(x>%f)=%f\n",x,Q);
  768. x=gsl_cdf_gamma_Pinv(P,a,b);
  769. printf("Pinv(%f)=%f\n",P,x);
  770. x=gsl_cdf_gamma_Qinv(Q,a,b);
  771. printf("Qinv(%f)=%f\n",Q,x);
  772. }
  773.  
  774. void exponential() {
  775. double P,Q;
  776. double x=0.05;
  777. double lambda=2;
  778. double pdf;
  779. printf("exponential distribution:x=%f lambda=%f,b=%f\n",x,lambda);
  780. pdf=gsl_ran_exponential_pdf(x,lambda);
  781. printf("probe(x=%f)=%f\n",x,pdf);
  782. P=gsl_cdf_exponential_P(x,lambda);
  783. printf("prob(x<%f)=%f\n",x,P);
  784. Q=gsl_cdf_exponential_Q(x,lambda);
  785. printf("prob(x>%f)=%f\n",x,Q);
  786. x=gsl_cdf_exponential_Pinv(P,lambda);
  787. printf("Pinv(%f)=%f\n",P,x);
  788. x=gsl_cdf_exponential_Qinv(Q,lambda);
  789. printf("Qinv(%f)=%f\n",Q,x);
  790. }
  791.  
  792. void beta() {
  793. double P,Q;
  794. double x=0.8;
  795. double a=0.5;
  796. double b=0.5;
  797. double pdf;
  798. printf("beta distribution:x=%f a=%f,b=%f\n",x,a,b);
  799. pdf=gsl_ran_beta_pdf(x,a,b);
  800. printf("probe(x=%f)=%f\n",x,pdf);
  801. P=gsl_cdf_beta_P(x,a,b);
  802. printf("prob(x<%f)=%f\n",x,P);
  803. Q=gsl_cdf_beta_Q(x,a,b);
  804. printf("prob(x>%f)=%f\n",x,Q);
  805. x=gsl_cdf_beta_Pinv(P,a,b);
  806. printf("Pinv(%f)=%f\n",P,x);
  807. x=gsl_cdf_beta_Qinv(Q,a,b);
  808. printf("Qinv(%f)=%f\n",Q,x);
  809. }
  810.  
  811. void lognormal() {
  812. double P,Q;
  813. double x=4;
  814. double zeta=2;
  815. double sigma=1.5;
  816. double pdf;
  817. printf("lognormal distribution:x=%f zeta=%f,sigma=%f\n",x,zeta,sigma);
  818. pdf=gsl_ran_lognormal_pdf(x,zeta,sigma);
  819. printf("probe(x=%f)=%f\n",x,pdf);
  820. P=gsl_cdf_lognormal_P(x,zeta,sigma);
  821. printf("prob(x<%f)=%f\n",x,P);
  822. Q=gsl_cdf_lognormal_Q(x,zeta,sigma);
  823. printf("prob(x>%f)=%f\n",x,Q);
  824. x=gsl_cdf_lognormal_Pinv(P,zeta,sigma);
  825. printf("Pinv(%f)=%f\n",P,x);
  826. x=gsl_cdf_lognormal_Qinv(Q,zeta,sigma);
  827. printf("Qinv(%f)=%f\n",Q,x);
  828. }
  829.  
  830.  
  831. Monte Carlo Method three parts
  832. 1.exponential
  833. #include<stdlib.h>
  834. #include<stdio.h>
  835. #include<math.h>
  836. #include<gsl/gsl_rng.h>
  837. #include<gsl/gsl_randist.h>
  838.  
  839. int main(int argc, char *argv[]){
  840.  
  841. int i,n;
  842. float x,alpha;
  843. gsl_rng *r = gsl_rng_alloc(gsl_rng_mt19937); /*intilaze Gsl Rng*/
  844. n = atoi(argv[1]);
  845.  
  846. alpha = atof(argv[2]);
  847. x = 0;
  848. for(i = 0; i < n; i++){
  849. x =alpha* x + gsl_ran_exponential(r,1);
  850. printf("%2.4f\n",x);
  851. }
  852.  
  853. return (0);
  854.  
  855. }
  856. run = $ ./7_exponential.out 10 0.4
  857.  
  858. 2.Uniform
  859. //generating uniform random number range(0.0,1.0) using uniform distribution
  860. #include<stdio.h>
  861. #include<gsl/gsl_rng.h>
  862. #include<gsl/gsl_randist.h>
  863. int main(void)
  864. {
  865. const gsl_rng_type *T;
  866. gsl_rng *r;
  867. int i, n = 10;
  868. gsl_rng_env_setup();
  869. T = gsl_rng_default;
  870. r = gsl_rng_alloc(T);
  871.  
  872. for(i = 0; i < n; i++){
  873. double U = gsl_rng_uniform(r);
  874. printf("%.5f\n",U);
  875. }
  876.  
  877. gsl_rng_free(r);
  878. return 0;
  879. }
  880.  
  881. 3.Binomial
  882. //binomial distribution
  883. #include<stdio.h>
  884. #include<gsl/gsl_rng.h>
  885. #include<gsl/gsl_randist.h>
  886. int main(void)
  887. {
  888. const gsl_rng_type *T;
  889. gsl_rng *r;
  890. int i, n = 10;
  891. gsl_rng_env_setup();
  892. T = gsl_rng_default;
  893. r = gsl_rng_alloc(T);
  894. float p = 0.3; //print n random variates chossen from the bionomial distribution with mean parameter mu.
  895.  
  896. for(i = 0; i < n; i++){
  897. signed int k = gsl_ran_binomial(r,p,n);
  898. printf("%u",k);
  899. printf("\n");
  900. }
  901. gsl_rng_free(r);
  902. return 0;
  903. }
  904.  
  905. Obtain mean, standard error and p value for in_per_capital from table income where sumlevel+0.0=40 from database data-census.db
  906.  
  907. #include<apop.h>
  908.  
  909. void one_boot(gsl_vector *base_data,gsl_rng *r,gsl_vector *boot_sample);
  910.  
  911. void one_boot(gsl_vector *base_data,gsl_rng *r,gsl_vector *boot_sample)
  912. {
  913. for (int i=0;i<boot_sample->size;i++)
  914. gsl_vector_set(boot_sample,i,gsl_vector_get(base_data, gsl_rng_uniform_int
  915. (r,base_data->size)));
  916. }
  917.  
  918. int main()
  919. {
  920. int rep_ct=10000;
  921. gsl_rng *r =apop_rng_alloc(0);
  922. apop_db_open("data-census.db");
  923. gsl_vector *base_data =apop_query_to_vector("select in_per_capita from income where sumlevel+0.0=40");
  924. double RI = apop_query_to_float("select in_per_capita from income where sumlevel+0.0=40 and geo_id2+0.0=44");
  925. gsl_vector *boot_sample = gsl_vector_alloc(base_data->size);
  926. gsl_vector *replications = gsl_vector_alloc(rep_ct);
  927. for (int i=0; i<rep_ct;i++)
  928. {
  929. one_boot (base_data, r,boot_sample);
  930. gsl_vector_set(replications,i,apop_mean(boot_sample));
  931. }
  932. double stderror = sqrt(apop_var(replications));
  933. double mean= apop_mean(replications);
  934. printf("mean:%g;standard error:%g;(RI-mean)/stderr:%g; pvalue:%g\n",mean,stderror,2*gsl_cdf_gaussian_Q(fabs(RI-mean),stderror));
  935.  
  936. }
  937.  
  938. implement weighted linear regression
  939. on the dataset given as follows .hence
  940. obtain best fit, covariance matrix &
  941. Chi square value.
  942. Year Production
  943. ( in lakhs)
  944. X Y W
  945. 1984 10 0.1
  946. 1989 15 0.2
  947. 1995 19 0 .3
  948. 1998 23 0.4
  949. table value put inside and balance code is same
  950.  
  951.  
  952.  
  953. #include <stdio.h>
  954. #include <gsl/gsl_fit.h>
  955. int
  956. main (void)
  957. {
  958. int i, n = 4;
  959. double x[4] = { 1970, 1980, 1990, 2000 };
  960. double y[4] = { 12, 11, 14, 13 };
  961. double w[4] = { 0.1, 0.2, 0.3, 0.4 };
  962. 42
  963. double c0, c1, cov00, cov01, cov11, chisq;
  964. gsl_fit_wlinear (x, 1, w, 1, y, 1, n,
  965. &c0, &c1, &cov00, &cov01, &cov11,
  966. &chisq);
  967. printf ("# best fit: Y = %g + %g X\n", c0, c1);
  968. printf ("# covariance matrix:\n");
  969. printf ("# [ %g, %g\n# %g, %g]\n",
  970. cov00, cov01, cov01, cov11);
  971. printf ("# chisq = %g\n", chisq);
  972. for (i = 0; i < n; i++)
  973. printf ("data: %g %g %g\n",
  974. x[i], y[i], 1/sqrt(w[i]));
  975. printf ("\n");
  976. for (i = -30; i < 130; i++)
  977. {
  978. double xf = x[0] + (i/100.0) * (x[n-1] - x[0]);
  979. double yf, yf_err;
  980. gsl_fit_linear_est (xf,
  981. c0, c1,
  982. cov00, cov01, cov11,
  983. &yf, &yf_err);
  984. printf ("fit: %g %g\n", xf, yf);
  985. printf ("hi : %g %g\n", xf, yf + yf_err);
  986. printf ("lo : %g %g\n", xf, yf - yf_err);
  987. }
  988. return 0;
  989. }
  990.  
  991. Implementing goodness of fit Chi Square
  992.  
  993. #include <apop.h>
  994. int main(){
  995. apop_db_open("data-climate.db");
  996. apop_data *precip = apop_query_to_data("select PCP from precip");
  997. apop_model *est = apop_estimate(precip, apop_normal);
  998. apop_data *precip_binned = apop_data_to_bins(precip/*,
  999. .bin_count=180*/);
  1000. apop_model *datahist = apop_estimate(precip_binned, apop_pmf);
  1001. apop_model *modelhist = apop_model_to_pmf(.model=est,
  1002. .binspec=apop_data_get_page(precip_binned, "<binspec>"), .draws=1e5);
  1003. double scaling = apop_sum(datahist->data-
  1004. >weights)/apop_sum(modelhist->data->weights);
  1005. gsl_vector_scale(modelhist->data->weights, scaling);
  1006. apop_data_show(apop_histograms_test_goodness_of_fit(datahist,
  1007. modelhist));
  1008. }
  1009.  
  1010.  
  1011. implement non-parametric testing using anova on the columns riders,year &
  1012. line from table riders & lines from data-metro database.
  1013.  
  1014. //imlement non parametric testing
  1015.  
  1016. #include<apop.h>
  1017.  
  1018. int main(){
  1019. apop_db_open("data-metro.db");
  1020. char joinedtab[] = "(Select year, riders,line from riders ,lines Where riders.station = lines.station)";
  1021. or
  1022. char joinedtab[] = "(Select year, riders,line from riders ,lines Where riders.station = lines.station)"
  1023.  
  1024. apop_data_show(apop_anova(joinedtab,"riders","line","year"));
  1025. }
  1026.  
  1027.  
  1028. /*char joinedtab[] = "(Select year, riders,line \
  1029. from riders ,lines \
  1030. Where riders.station = lines.station)";
  1031.  
  1032. char joinedtab[] = "(Select year, riders,line"
  1033. " from riders ,lines"
  1034. " Where riders.station = lines.station)";
  1035. */
  1036.  
  1037. Q)Write a program in c & execute the same
  1038. with gcc compiler via cygwin/ubuntu : to
  1039. building an optimized model –x2sin(x) &
  1040. then solving the same for maximum using
  1041. foolwing methods of obtaining optima
  1042. Apop_simplex_nm
  1043. Apop_cg_fr
  1044. Apop_siman
  1045. Apop_rf_newton
  1046.  
  1047. i)save as sinsq.c extension
  1048. #include <apop.h>
  1049. double sin_square(apop_data *data, apop_model *m){
  1050. double x = apop_data_get(m->parameters, 0, -1);
  1051. return -sin(x)*gsl_pow_2(x);
  1052. }
  1053. apop_model sin_sq_model ={"-sin(x) times x^2",1, .p = sin_square};
  1054.  
  1055.  
  1056. ii)that file call here
  1057. #include "sinsq.c"
  1058. void do_search(int number, char *name, char *trace){
  1059. apop_model *out;
  1060. double p[] = {0};
  1061. double result;
  1062. char *outf;
  1063. asprintf(&outf, "localmax_out/%s.gplot", trace);
  1064. Apop_model_add_group(&sin_sq_model, apop_mle,
  1065. .starting_pt= p,
  1066. .method= number, .tolerance= 1e-4,
  1067. .mu_t= 1.25, .trace_path= outf);
  1068. out = apop_estimate(NULL, sin_sq_model);
  1069. result = gsl_vector_get(out->parameters->vector, 0);
  1070. printf("The %s algorithm found %g.\n", name, result);
  1071. Apop_settings_rm_group(&sin_sq_model, apop_mle);
  1072. }
  1073. int main(){
  1074.  
  1075. system ("mkdir -p localmax_out; rm -f localmax_out/*.gplot");
  1076. apop_opts.verbose ++;
  1077. do_search(APOP_SIMPLEX_NM, "N-M Simplex", "simplex");
  1078. do_search(APOP_CG_FR, "F-R Conjugate gradient", "fr");
  1079. do_search(APOP_SIMAN, "Simulated annealing", "siman");
  1080. do_search(APOP_RF_NEWTON, "Root-finding", "root");
  1081. fflush(NULL);
  1082. system("sed -i \"1iplot '-'\" localmax_out/*.gplot");
  1083. }
  1084. Comparing 2 models using likelihood ratio
  1085.  
  1086. #include <apop.h>
  1087. apop_model * dummies(int slope_dummies){
  1088. apop_data *d = apop_query_to_mixed_data("mmt", "select riders, year-
  1089. 1977, line \
  1090. from riders, lines \
  1091. where riders.station=lines.station");
  1092. apop_data *dummified = apop_data_to_dummies(d, 0, 't', .append='y',
  1093. .remove='y');
  1094. if (slope_dummies){
  1095. Apop_col(d, 1, yeardata);
  1096. for(int i=0; i < dummified->matrix->size2; i ++){
  1097. Apop_col(dummified, i, c);
  1098. gsl_vector_mul(c, yeardata);
  1099. }
  1100. }
  1101. apop_model *out = apop_estimate(dummified, apop_ols);
  1102.  
  1103. apop_model_show(out);
  1104. return out;
  1105. }
  1106. #ifndef TESTING
  1107. int main(){
  1108. apop_db_open("data-metro.db");
  1109. printf("With constant dummies:\n"); dummies(0);
  1110. printf("With slope dummies:\n"); dummies(1);
  1111. }
  1112. #endif
  1113.  
  1114.  
  1115.  
  1116. #define TESTING
  1117. #include "dummies.c"
  1118. void show_normal_test(apop_model *unconstrained, apop_model
  1119. *constrained, int n){
  1120. double statistic = (apop_data_get(unconstrained->info, .rowname="log
  1121. likelihood")
  1122. - apop_data_get(constrained->info, .rowname="log
  1123. likelihood"))/sqrt(n);
  1124. double confidence = gsl_cdf_gaussian_P(fabs(statistic), 1); //one-tailed.
  1125. printf("The Normal statistic is: %g, so reject the null of no difference
  1126. between models "
  1127. "with %g%% confidence.\n", statistic, confidence*100);
  1128. }
  1129. int main(){
  1130. apop_db_open("data-metro.db");
  1131. apop_model *m0 = dummies(0);
  1132. apop_model *m1 = dummies(1);
  1133. show_normal_test(m0, m1, m0->data->matrix->size1);
  1134. }
  1135.  
  1136.  
  1137. Practical III
  1138.  
  1139.  
  1140. 1.Write a program in c & execute the same with gcc compiler via cygwin/ubuntu : to
  1141. plot a vector of columns (year*12+month)/12., temp of temp table from database
  1142. data-climate.(pipe vector)
  1143.  
  1144. Plotting a vector
  1145. #include <apop.h>
  1146. void plot_matrix_now(gsl_matrix *data){
  1147. static FILE *gp = NULL;
  1148. if (!gp)
  1149. gp = popen("gnuplot -persist", "w");
  1150. if (!gp){
  1151. printf("Couldn't open Gnuplot.\n");
  1152. return;
  1153. }
  1154. fprintf(gp,"reset; plot '-' \n");
  1155. apop_matrix_print(data, .output_pipe=gp);
  1156. fflush(gp);
  1157. }
  1158. int main(){
  1159. apop_db_open("data-climate.db");
  1160. plot_matrix_now(apop_query_to_matrix("select (year*12+month)/12., temp from temp"));
  1161. }
  1162.  
  1163.  
  1164.  
  1165.  
  1166.  
  1167.  
  1168. jayesh@jayesh-G31M-S2L:~$ gcc -std=gnu99 pipeplot.c -o pipeplot.out -
  1169. lapophenia -lgsl -lsqlite3
  1170. jayesh@jayesh-G31M-S2L:~$ ./pipeplot.out
  1171.  
  1172.  
  1173. 2.lattice
  1174. Eigen vector
  1175.  
  1176. i)eigenbox.h
  1177.  
  1178. #include<apop.h>
  1179. #include<gsl/gsl_elgen.h>
  1180.  
  1181. void show_projection(gsl_matrix *pc_space, apop_data *data);
  1182. apop_data *query_data();
  1183.  
  1184.  
  1185.  
  1186. ii)header call here
  1187. #include "eigenbox.h"
  1188. apop_data *query_data(){
  1189. apop_db_open("data-census.db");
  1190. return apop_query_to_data(" select postcode as row_names, "
  1191. " m_per_100_f, population/1e6 as population, median_age "
  1192. " from geography, income,demos,postcodes "
  1193. " where income.sumlevel= '040' "
  1194. " and geography.geo_id = demos.geo_id "
  1195. " and income.geo_name = postcodes.state "
  1196. " and geography.geo_id = income.geo_id ");
  1197. }
  1198. void show_projection(gsl_matrix *pc_space, apop_data *data){
  1199. fprintf(stderr,"The eigenvectors:\n");
  1200. apop_matrix_print(pc_space, .output_pipe=stderr);
  1201. apop_data *projected = apop_dot(data, apop_matrix_to_data(pc_space));
  1202. printf("plot '-' using 2:3:1 with labels\n");
  1203. apop_data_show(projected);
  1204. }
  1205.  
  1206. int main(){
  1207. apop_plot_lattice(query_data(), "out");
  1208. }
  1209. jayesh@jayesh-G31M-S2L:~$ gcc -std=gnu99 eigenbox.c -o eigenbox.out -
  1210. lapophenia -lgsl -lsqlite3
  1211. jayesh@jayesh-G31M-S2L:~$ ./eigenbox.out
  1212. jayesh@jayesh-G31M-S2L:~$ gnuplot -persist < out
  1213.  
  1214.  
  1215. 3.error bars
  1216. Query out the month, average, and variance, and plot the data using errorbars.
  1217. Prints to stdout, so pipe the output through Gnuplo
  1218. #include <apop.h>
  1219. int main(){
  1220. apop_db_open("data-climate.db");
  1221. apop_data *d = apop_query_to_data("select \
  1222. (yearmonth/100. - round(yearmonth/100.))*100 as month, \
  1223. avg(tmp), stddev(tmp) \
  1224.  
  1225. from precip group by month");
  1226. printf("set xrange*0:13+; plot ’-’ with errorbars\n");
  1227. apop_matrix_show(d->matrix);
  1228. }
  1229. jayesh@jayesh-G31M-S2L:~$ gcc -std=gnu99 errorbars.c -o errorbars.out -
  1230. lapophenia -lgsl -lsqlite3
  1231. jayesh@jayesh-G31M-S2L:~$ ./errorbars.out | gnuplot –persist
  1232.  
  1233. p2
  1234.  
  1235. i) Multiplication Table
  1236. #include <apop.h>
  1237. int main(){
  1238. gsl_matrix *m = gsl_matrix_alloc(20,15);
  1239. gsl_matrix_set_all(m, 1);
  1240. for (int i=0; i< m->size1; i++){
  1241. Apop_matrix_row(m, i, one_row);
  1242. gsl_vector_scale(one_row, i+1);
  1243. }
  1244. for (int i=0; i< m->size2; i++){
  1245. Apop_matrix_col(m, i, one_col);
  1246. gsl_vector_scale(one_col, i+1);
  1247. }
  1248. apop_matrix_show(m);
  1249. gsl_matrix_free(m);
  1250. }
  1251. jayesh@jayesh-G31M-S2L:~$ gcc -std=gnu99 multiplicationtable.c -o
  1252. multiplicationtable.out -lapophenia -lgsl -lsqlite3
  1253. jayesh@jayesh-G31M-S2L:~$ ./multiplicationtable.out
  1254.  
  1255. ii) the function in will take in a double indicating taxable income and will
  1256. return US income taxes owed, assuming a head of household with two
  1257. dependents taking the standard deduction
  1258. #include <apop.h>
  1259. double calc_taxes(double income){
  1260. double cutoffs[] = {0, 11200, 42650, 110100, 178350, 349700, INFINITY};
  1261. double rates[] = {0, 0.10, .15, .25, .28, .33, .35};
  1262. double tax = 0;
  1263. int bracket = 1;
  1264. income -= 7850; //Head of household standard deduction
  1265. income -= 3400*3; //exemption: self plus two dependents.
  1266. while (income > 0){
  1267. tax += rates[bracket] * GSL_MIN(income, cutoffs[bracket]-cutoffs[bracket-
  1268. 1]);
  1269. income -= cutoffs[bracket];
  1270. bracket ++;
  1271. }
  1272. return tax;
  1273. }
  1274. int main(){
  1275. apop_db_open("data-census.db");
  1276. strncpy(apop_opts.db_name_column, "geo_name", 100);
  1277. apop_data *d = apop_query_to_data("select geo_name,
  1278. Household_median_in as income\
  1279.  
  1280. from income where sumlevel = '040'\
  1281. order by household_median_in desc");
  1282. Apop_col_t(d, "income", income_vector);
  1283. d->vector = apop_vector_map(income_vector, calc_taxes);
  1284. apop_name_add(d->names, "tax owed", 'v');
  1285. apop_data_show(d);
  1286. }
  1287. jayesh@jayesh-G31M-S2L:~$ gcc -std=gnu99 taxes.c -o taxes.out -lapophenia -
  1288. lgsl -lsqlite3
  1289. jayesh@jayesh-G31M-S2L:~$ ./taxes.out
Add Comment
Please, Sign In to add comment