-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathedma.c
60 lines (43 loc) · 1.26 KB
/
edma.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#define S_SIZE 3
double rho;
double s[S_SIZE] = {0.0};
double t = 0, tmax = 0;
//rho: Compute decay factor from half-life in seconds
//tau is the desired time at which an element loses half its weight (eg 15 seconds)
double determine_rho(double tau)
{
return pow(2, -1/tau);
}
void element_arrives()
{
double new_value = 20 + 10.0 * drand48(); //random value between 20 and 30, say instrument price
#define e fmax(0, t - tmax) //recompute
int i;
for(i = 0; i < S_SIZE; i++)
{
s[i] = pow(rho,e) * s[i] + pow(new_value, i); //many optimizations remain
}
tmax = fmax(t,tmax);
double count = pow(rho,e) * s[0];
double mean = s[1]/s[0];
double var = (s[2] - pow(s[1],2)/s[0]) / (s[0] - 1 / pow(rho,e));
printf("t: %6.1f\tprice: %-5.2f\ts0: %-7.2f s1: %-7.2f s2: %-7.2f\t", t, new_value, s[0], s[1], s[2]);
printf("count: %-7.2f\tmean: %-7.2f\tvar: %-7.2f", count, mean, var);
printf("\n");
}
int main()
{
int i;
srand48(time(0));
rho = determine_rho(15.0); //use half life of 15 seconds
for(i = 0; i < 40; i++)
{
element_arrives();
t = t + 8.0 * drand48(); //update time to random time between "now" and 8 seconds from now
}
return 0;
}