PandA-2024.02
black_scholes.c
Go to the documentation of this file.
1 # include "black_scholes.h"
2 # include "fixedptc.h"
3 
4 
5 inline fixedpt asset_path_fixed_simplified ( fixedpt s0, fixedpt mu, fixedpt sigma, fixedpt t1, int n, int *seed ) __attribute__((always_inline));
6 fixedpt asset_path_fixed_simplified ( fixedpt s0, fixedpt mu, fixedpt sigma, fixedpt t1, int n, int *seed ){
7  int i;
8  fixedpt dt, stepnum, p;
9  fixedpt gaussR1 = 0, gaussR2 = 0;
10 
11  // stepnum = fixedpt_rconst(n);
12  stepnum = n << FIXEDPT_FBITS; // ??? janders
13  dt = fixedpt_div(t1, stepnum);
14 
15  fixedpt constA = fixedpt_mul(fixedpt_sub(mu, fixedpt_mul(sigma, sigma)), dt);
16  fixedpt constB = fixedpt_mul(sigma, fixedpt_sqrt ( dt ));
17 
18  p = s0;
19  for ( i = 1; i <= n; i++ )
20  {
21  if (i & 1) // iteration is odd, generate two random Gaussian numbers (the Box-Muller transform gens 2 numbers)
22  get_two_normal_fixed(seed, &gaussR1, &gaussR2);
23 
24  p = fixedpt_mul(p, fixedpt_exp (fixedpt_add(constA,
25  fixedpt_mul(constB, i & 1 ? gaussR1 : gaussR2))));
26 
27 
28  // fixedpt_print(p);
29  }
30  return p;
31 }
32 
33 inline void get_two_normal_fixed(int *seed, fixedpt *n1, fixedpt *n2) __attribute__((always_inline));
34 void get_two_normal_fixed(int *seed, fixedpt *n1, fixedpt *n2)
35 {
36  fixedpt r1, r2;
37 
38  fixedpt twoPI = 411775; // ??? janders -- hard-code 2PI in fixed point to avoid conversion from double
39  // from 2 uniform random numbers r1 and r2, we will generate two Gaussian random numbers deposited into n1 and n2
40  r1 = get_uniform_fixed (seed);
41  r2 = get_uniform_fixed (seed);
42 
45 }
46 
48 
49 {
50  int i4_huge = 2147483647;
51  int k;
52  fixedpt r;
53 
54  k = *seed / 127773;
55  // printf("k = %i \n", k);
56 
57  *seed = 16807 * ( *seed - k * 127773 ) - k * 2836;
58  //printf("*seed = %i \n", *seed);
59 
60  if ( *seed < 0 )
61  {
62  *seed = *seed + i4_huge;
63  }
64 
65  r = *seed & 0x0000FFFF;
66 
67  return r;
68 }
69 /******************************************************************************/
fixedpt fixedpt_sqrt(fixedpt A)
Definition: fixedptc.c:96
#define FIXEDPT_FBITS
Definition: fixedptc.h:104
fixedpt get_uniform_fixed(int *seed)
Definition: black_scholes.c:33
fixedpt fixedpt_ln(fixedpt x)
Definition: fixedptc.c:259
#define FIXEDPT_TWO
Definition: fixedptc.h:120
static const uint32_t k[]
Definition: sha-256.c:22
#define fixedpt_add(A, B)
Definition: fixedptc.h:110
fixedpt fixedpt_exp(fixedpt fp)
Definition: fixedptc.c:174
fixedpt fixedpt_mul(fixedpt A, fixedpt B)
Definition: fixedptc.c:11
float float3 __attribute__((vector_size(16)))
fixedpt fixedpt_sin(fixedpt fp)
Definition: fixedptc.c:130
fixedpt fixedpt_div(fixedpt A, fixedpt B)
Definition: fixedptc.c:17
fixedpt asset_path_fixed_simplified(fixedpt s0, fixedpt mu, fixedpt sigma, fixedpt t1, int n, int *seed)
Definition: black_scholes.c:5
fixedpt fixedpt_cos(fixedpt A)
Definition: fixedptc.c:162
void get_two_normal_fixed(int *seed, fixedpt *n1, fixedpt *n2)
Definition: black_scholes.c:22
#define twoPI
Definition: fft.h:6
int32_t fixedpt
Definition: fixedptc.h:79
#define fixedpt_sub(A, B)
Definition: fixedptc.h:111

Generated on Mon Feb 12 2024 13:02:49 for PandA-2024.02 by doxygen 1.8.13