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 fixedpt asset_path_fixed_simplified ( fixedpt s0, fixedpt mu, fixedpt sigma, fixedpt t1, int n, int *seed ){
6  int i;
7  fixedpt dt, stepnum, p;
8  fixedpt gaussR1, gaussR2;
9 
10  // stepnum = fixedpt_rconst(n);
11  stepnum = n << FIXEDPT_FBITS; // ??? janders
12  dt = fixedpt_div(t1, stepnum);
13 
14  fixedpt constA = fixedpt_mul(fixedpt_sub(mu, fixedpt_mul(sigma, sigma)), dt);
15  fixedpt constB = fixedpt_mul(sigma, fixedpt_sqrt ( dt ));
16 
17  p = s0;
18  for ( i = 1; i <= n; i++ )
19  {
20  if (i & 1) // iteration is odd, generate two random Gaussian numbers (the Box-Muller transform gens 2 numbers)
21  get_two_normal_fixed(seed, &gaussR1, &gaussR2);
22 
23  p = fixedpt_mul(p, fixedpt_exp (fixedpt_add(constA,
24  fixedpt_mul(constB, i & 1 ? gaussR1 : gaussR2))));
25 
26 
27  // fixedpt_print(p);
28  }
29  return p;
30 }
31 
32 void get_two_normal_fixed(int *seed, fixedpt *n1, fixedpt *n2)
33 {
34  fixedpt r1, r2;
35 
36  fixedpt twoPI = 411775; // ??? janders -- hard-code 2PI in fixed point to avoid conversion from double
37  // from 2 uniform random numbers r1 and r2, we will generate two Gaussian random numbers deposited into n1 and n2
38  r1 = get_uniform_fixed (seed);
39  r2 = get_uniform_fixed (seed);
40 
43 }
44 
46 
47 {
48  int i4_huge = 2147483647;
49  int k;
50  fixedpt r;
51 
52  k = *seed / 127773;
53  // printf("k = %i \n", k);
54 
55  *seed = 16807 * ( *seed - k * 127773 ) - k * 2836;
56  //printf("*seed = %i \n", *seed);
57 
58  if ( *seed < 0 )
59  {
60  *seed = *seed + i4_huge;
61  }
62 
63  r = *seed & 0x0000FFFF;
64 
65  return r;
66 }
67 /******************************************************************************/
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
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