Advertisement
Guest User

Untitled

a guest
Jun 24th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.30 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4. #include <pthread.h>
  5. #include <time.h>
  6.  
  7. #include <gmp.h>
  8.  
  9. // Metodo de execução p/ imprimir no arquivo saida.txt e ler de entrada.txt  ./blackscholes<entrada.txt>saida.txt
  10. // Metodo de compillação   gcc blackscholes.c -o blackscholes -lm -lpthread
  11.  
  12.  
  13.  
  14. #define thread_num 4
  15.  
  16. //global variables
  17.  
  18. double S, E, r, sigma, T, M;
  19.  
  20. //declared with the biggest value possible to avoid dinamyc vector
  21. double trials[100000];
  22.  
  23. int calculated[4];
  24.  
  25. double max(double, double);
  26.  
  27. int correct_current(int n){//this function resolve the problem since the code is not sequencial
  28.   if(n ==4){
  29.     n = 0;
  30.   }
  31.   if(calculated[n] == 1){
  32.     n++;
  33.     n = correct_current(n);
  34.   }
  35.   calculated[n] = 1;
  36.   return n;
  37. }
  38.  
  39. void* black_scholes(void* arg){
  40.   int i;
  41.   int current_iteration = *((int*)arg);
  42.   current_iteration = correct_current(current_iteration);
  43.  
  44.   srand(time(NULL));
  45.  
  46.   //declaring aux variables to calculate trials
  47.   double aux1,aux2, aux3, aux4, aux5, aux6, aux7, t;
  48.  
  49.   for(i = M*current_iteration/4; i < M*(current_iteration + 1)/4; i++){
  50.  
  51.     //now, we calcuate trials with the given parameters
  52.  
  53.     t = S * pow(M_E, (r - 0.5*pow(sigma, 2)) * T + sigma*sqrt(T)* (rand()/(double)RAND_MAX));
  54.  
  55.     trials[i] =  pow(M_E,-r*T) * max(t-E, 0);
  56.  
  57.   }
  58. }
  59.  
  60. double max(double num1, double num2){
  61.     if(num1 > num2) return num1;
  62.     else return num2;
  63. }
  64.  
  65. double calculate_mean(){
  66.     double sum = 0.00;
  67.  
  68.     for(int i = 0; i < M; i++){
  69.         sum += trials[i];
  70.     }
  71.  
  72.     sum = sum/((double) M);
  73.  
  74.     return sum;
  75. }
  76.  
  77. double calculate_stddev(double mean){
  78.     double sum = 0.00;
  79.  
  80.     for(int i = 0; i < M; i++){
  81.         sum += pow(trials[i] - mean, 2);
  82.     }
  83.  
  84.     sum = sum/((double) M);
  85.     sum = sqrt(sum);
  86.  
  87.     return sum;
  88. }
  89.  
  90. double calculate_confwidth(double deviation){
  91.     return (1.96 * deviation / sqrt(M));
  92. }
  93.  
  94. double calculate_confmin(double mean, double confwidth){
  95.     return mean - confwidth;
  96. }
  97.  
  98. double calculate_confmax(double mean, double confwidth){
  99.     return mean + confwidth;
  100. }
  101.  
  102.  
  103. int main(){
  104.   double mean, stddev, confwidth, confmin, confmax;
  105.   int thread_number, rstatus;
  106.   scanf("%lf %lf %lf %lf %lf %lf", &S, &E, &r, &sigma, &T, &M);
  107.  
  108.   // declaring threads that will be used
  109.   pthread_t all_threads[thread_num];
  110.  
  111.   for(thread_number =  0; thread_number < thread_num; thread_number++){
  112.  
  113.     pthread_create(&all_threads[thread_number], NULL, black_scholes, &thread_number);
  114.   }
  115.   // implemented with 'for'=> if the number of threads change, the code adapts itself
  116.  
  117.   for(thread_number =  0; thread_number < thread_num; thread_number++){
  118.     rstatus = pthread_join(all_threads[thread_number], NULL);
  119.  
  120.     if(rstatus != 0){
  121.       printf("Error ocurred on thread %d\n", thread_number);
  122.     }
  123.   }
  124.  
  125.   //now, we have all trials calculated, lets now find mean, stddev, confwidth tc;
  126.  
  127.   mean = calculate_mean();
  128.   stddev = calculate_stddev(mean);
  129.   confwidth = calculate_confwidth(stddev);
  130.   confmin = calculate_confmin(mean, confwidth);
  131.   confmax = calculate_confmax(mean, confwidth);
  132.  
  133.   printf("S         %.0lf\nE         %.0lf\nr         %.0lf\nsigma     %.0lf\nT         %.0lf\nM         %.0lf\nConfidence interval (%.4lf, %.4lf)",S, E, r, sigma, T, M, confmin, confmax);
  134.  
  135.  
  136.  
  137.  
  138.  
  139.   return 0;
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement