Revision 1098
branches/threaded2/examples/threaded/multisimple/msimplenothreads.tpl (revision 1098)  

1 
//$Id: $ 

2 
DATA_SECTION 

3 
!! ad_comm::change_datafile_name("msimple.dat"); 

4 
init_int nobs 

5 
init_int nchunk 

6 
vector Y(1,nobs) 

7 
vector x(1,nobs) 

8 
number A 

9 
number B 

10 
number S 

11  
12 
int chunk_size 

13  
14 
LOCAL_CALCS 

15 
chunk_size = nobs/nchunk + 1; 

16 
A = 2.0; 

17 
B = 4.0; 

18 
S = 7.0; 

19 
random_number_generator rng(101); 

20 
dvector err(1,nobs); 

21 
x.fill_randu(rng); 

22 
x *= 100.0; 

23 
Y = A*x + B; 

24  
25  
26 
err.fill_randn(rng); 

27 
Y += S*err; 

28 
// for lognormal error 

29 
//Y = elem_prod(Y,exp(S*err)); 

30  
31 
chunk_size = nobs/nchunk; 

32 
/* 

33 
cout << "chunk_size = " << chunk_size << endl; 

34 
int end_pos = 0; 

35 
for (int kk=1;kk<=nchunk;kk++) 

36 
{ 

37 
int start_pos = end_pos + 1; 

38 
end_pos = start_pos+chunk_size1; 

39 
if (kk == nchunk) 

40 
end_pos = nobs; 

41 
cout << " * * * chunk " << kk << " from " << start_pos << " to " << end_pos << endl; 

42 
dvector t = x(start_pos,end_pos); 

43 
cout << " t runs from " << t.indexmin() << " to " << t.indexmax() << endl; 

44 
} 

45 
if (1) exit(1); 

46 
*/ 

47  
48  
49 
PARAMETER_SECTION 

50 
init_number a 

51 
init_number b 

52 
number s; 

53 
vector ff(1,nchunk) 

54 
objective_function_value f 

55 
vector pred_Y(1,nobs); 

56  
57 
PRELIMINARY_CALCS_SECTION 

58 
a = 1.0; 

59 
b = 2.0; 

60 
crit = 1e3; 

61  
62 
PROCEDURE_SECTION 

63 
#define __CHUNKY__ 

64 
#ifdef __CHUNKY__ 

65 
int end_pos = 0; 

66 
for (int kk=1;kk<=nchunk;kk++) 

67 
{ 

68 
int start_pos = end_pos + 1; 

69 
end_pos = start_pos+chunk_size1; 

70 
if (kk == nchunk) 

71 
end_pos = nobs; 

72 


73 
pred_Y(start_pos,end_pos) = a*x(start_pos,end_pos) + b; 

74  
75 
ff(kk) = norm2(pred_YY(start_pos,end_pos)); 

76 
} 

77 
// sum the results to compute the objective function 

78 
s = sum(ff); 

79 
f = nobs/2.*log(s); // make it a likelihood function so that 

80 
// covariance matrix is correct 

81 
#else 

82  
83 
pred_Y=a*x+b; 

84 
s=(norm2(pred_YY)); 

85 
f=nobs/2.*log(s); // make it a likelihood function so that 

86 
// covariance matrix is correct 

87 
#endif 

88  
89  
90 
REPORT_SECTION 

91 
s = sqrt(s/nobs); 

92 
report << "A = " << A << "; B = " << B << "; S = " << S <<endl; 

93 
report << "a = " << a << "; b = " << b << "; s = " << s << endl; 

94 
report << "f = " << f <<endl; 

95 
report << "nobs = " << nobs << endl; 

96 
#ifdef __CHUNKY__ 
Also available in: Unified diff