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 {
7  fixedpt gaussR1 = 0, gaussR2 = 0;
8  fixedpt stepnum = n << FIXEDPT_FBITS; // ??? janders
9  fixedpt dt = fixedpt_div(t1, stepnum);
10  fixedpt constA = fixedpt_mul(fixedpt_sub(mu, fixedpt_mul(sigma, sigma)), dt);
11  fixedpt constB = fixedpt_mul(sigma, fixedpt_sqrt ( dt ));
12  fixedpt p = s0;
13  for (int i = 1; i <= n; i++ ) {
14  if (i & 1) // iteration is odd, generate two random Gaussian numbers (the Box-Muller transform gens 2 numbers)
15  get_two_normal_fixed(seed, &gaussR1, &gaussR2);
16 
17  p = fixedpt_mul(p, fixedpt_exp (fixedpt_add(constA, fixedpt_mul(constB, (i & 1) ? gaussR1 : gaussR2))));
18  }
19  return p;
20 }
21 
22 void get_two_normal_fixed(int *seed, fixedpt *n1, fixedpt *n2)
23 {
24  fixedpt twoPI = 411775; // ??? janders -- hard-code 2PI in fixed point to avoid conversion from double
25  // from 2 uniform random numbers r1 and r2, we will generate two Gaussian random numbers deposited into n1 and n2
26  fixedpt r1 = get_uniform_fixed (seed);
27  fixedpt r2 = get_uniform_fixed (seed);
28 
31 }
32 
34 {
35  int i4_huge = 2147483647;
36  int k = *seed / 127773;
37  *seed = 16807 * ( *seed - k * 127773 ) - k * 2836;
38  if ( *seed < 0 ) {
39  *seed = *seed + i4_huge;
40  }
41  return *seed & 0x0000FFFF;
42 }
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