adagsl_335d13f0/examples/movstat/src/movstat.adb

  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
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
with System ;
with Ada.Text_IO;            use Ada.Text_IO;
with Ada.Text_IO.C_Streams;
with Ada.Long_Float_Text_IO; use Ada.Long_Float_Text_IO;
with Ada.Integer_Text_IO;    use Ada.Integer_Text_IO;
with Interfaces.C;           use Interfaces.C;
with Interfaces.C.Strings;   use Interfaces.C.Strings;
with Ada.Strings.Unbounded;  use Ada.Strings.Unbounded;
with Ada.Command_Line;       use Ada.Command_Line;
with Ada.Numerics.Elementary_Functions ; use Ada.Numerics.Elementary_Functions;

with GNAT.Source_Info; use GNAT.Source_Info;

with gsl;
with gsl.movstat ;
with gsl.rng ;
with gsl.randist ;
with gsl.vector_double ;
with math ;

with windowing ;

procedure Movstat is

   N : Integer := 500 ;        -- Length of time series
   K : Integer := 11 ;         -- Window Size

   NN : Integer := 1000 ;
   KK : Integer := 41 ;

   Status : Int ;
   function sign(x : double) return double is
   begin
      if x >= 0.0
      then
         return 1.0 ;
      else
         return -1.0 ;
      end if ;
   end sign ;

   procedure Test1 is

      ws : access gsl.movstat.gsl_movstat_workspace :=
                  gsl.movstat.alloc( size_t(K) );
      x : access gsl.vector_double.gsl_vector :=
                  gsl.vector_double.alloc ( size_t(N) ) ;
      xmean : access gsl.vector_double.gsl_vector :=
                  gsl.vector_double.alloc (size_t(N)) ;
      xmin : access gsl.vector_double.gsl_vector :=
                  gsl.vector_double.alloc ( size_t(N) ) ;
      xmax : access gsl.vector_double.gsl_vector :=
                  gsl.vector_double.alloc (size_t(N)) ;
      rng : access gsl.rng.gsl_rng := gsl.rng.alloc(gsl.rng.default) ;
      ei : double ;
      xi : double ;
      csvfilename : String := gnat.source_info.enclosing_entity & ".csv" ;
      csvfile : File_Type ;
   begin
      Put_Line(gnat.source_info.enclosing_entity);
      Create(csvfile,Out_File,csvfilename);
      Set_Output(csvfile);
      for i in 1..N
      loop
         xi := double(cos(4.0 * math.M_PI * Float(i) / Float(N))) ;
         ei := gsl.randist.gaussian(rng,0.1);
         gsl.vector_double.set(x , size_t(i-1) , xi + ei );
         --Put(Long_Float(xi)); Put(" "); Put(Long_Float(ei)); Put(" "); New_Line;
      end loop ;
      Status := gsl.movstat.mean(gsl.movstat.GSL_MOVSTAT_END_PADVALUE,x , xmean, ws);
      Status := gsl.movstat.minmax(gsl.movstat.GSL_MOVSTAT_END_PADVALUE,x , xmin, xmax, ws);
      for i in 1..N
      loop
         Put(i) ; Put(" ; ");
         Put(Long_Float(gsl.vector_double.get(x,size_t(i-1)))); Put("  ; ");
         Put(Long_Float(gsl.vector_double.get(xmean,size_t(i-1)))); Put(" ; ");
         Put(Long_Float(gsl.vector_double.get(xmin,size_t(i-1)))); Put(" ; ");
         Put(Long_Float(gsl.vector_double.get(xmax,size_t(i-1)))); Put(" ");
         New_Line;
      end loop ;
      Set_Output(Standard_Output);
      Close(csvfile);
      gsl.vector_double.free(xmax);
      gsl.vector_double.free(xmin);
      gsl.vector_double.free(xmean);
      gsl.vector_double.free(x);
   end Test1 ;
   procedure Test2 is

      rng : access gsl.rng.gsl_rng := gsl.rng.alloc(gsl.rng.default) ;
      ws : access gsl.movstat.gsl_movstat_workspace :=
                  gsl.movstat.alloc( size_t(KK) );

      x : access gsl.vector_double.gsl_vector :=
                  gsl.vector_double.alloc ( size_t(NN) ) ;
      xmedian : access gsl.vector_double.gsl_vector :=
                  gsl.vector_double.alloc ( size_t(NN) ) ;
      xmad : access gsl.vector_double.gsl_vector :=
                  gsl.vector_double.alloc ( size_t(NN) ) ;
      xiqr : access gsl.vector_double.gsl_vector :=
                  gsl.vector_double.alloc ( size_t(NN) ) ;
      xSn : access gsl.vector_double.gsl_vector :=
                  gsl.vector_double.alloc ( size_t(NN) ) ;
      xQn : access gsl.vector_double.gsl_vector :=
                  gsl.vector_double.alloc ( size_t(NN) ) ;
      xsd : access gsl.vector_double.gsl_vector :=
                  gsl.vector_double.alloc ( size_t(NN) ) ;
      csvfilename : String := gnat.source_info.enclosing_entity & ".csv" ;
      csvfile : File_Type ;
      sigma : gsl.double_array := (1.0, 5.0, 1.0, 3.0, 5.0) ;
      N_Sigma : array (1..5) of size_t := (200,450,600,850,1000) ;
      gi, u, outlier, xi : double ;
      varidx : size_t := 1 ;     -- variance changes
   begin
      Put_Line(gnat.source_info.enclosing_entity);
      Create(csvfile,Out_File,csvfilename);
      Set_Output(csvfile);
      for i in 1..NN
      loop
         gi := gsl.randist.gaussian(rng,sigma(Integer(varidx)-1)) ;
         u := gsl.rng.uniform(rng);
         if u < 0.01
         then
            outlier := 15.0 * sign(gi);
         else
            outlier := 0.0 ;
         end if ;
         xi := gi + outlier ;
         gsl.vector_double.set(x,size_t(i-1),xi);
         if size_t(i) = N_Sigma(Integer(varidx))
         then
            varidx := varidx + 1;
         end if ;
      end loop ;
      Status := gsl.movstat.mad(gsl.movstat.GSL_MOVSTAT_END_TRUNCATE, x, xmedian, xmad, ws);
      Status := gsl.movstat.qqr(gsl.movstat.GSL_MOVSTAT_END_TRUNCATE, x, 0.25, xiqr, ws);
      Status := gsl.movstat.Sn(gsl.movstat.GSL_MOVSTAT_END_TRUNCATE, x, xSn, ws);
      Status := gsl.movstat.Qn(gsl.movstat.GSL_MOVSTAT_END_TRUNCATE, x, xQn, ws);
      Status := gsl.movstat.sd(gsl.movstat.GSL_MOVSTAT_END_TRUNCATE, x, xsd, ws);
      Status := gsl.vector_double.scale(xiqr, 0.7413);

      varidx := 1;
      for i in 1..NN
      loop
         Put(i); Put(" ; ");
         Put(Long_Float(gsl.vector_double.get(x,size_t(i-1)))); Put(" ; ");
         Put(Long_Float(sigma(Integer(varidx)-1))); Put(" ; ");
         Put(Long_Float(gsl.vector_double.get(xmad,size_t(i-1)))); Put(" ; ");
         Put(Long_Float(gsl.vector_double.get(xiqr,size_t(i-1)))); Put(" ; ");
         Put(Long_Float(gsl.vector_double.get(xSn,size_t(i-1)))); Put(" ; ");
         Put(Long_Float(gsl.vector_double.get(xQn,size_t(i-1)))); Put(" ; ");
         Put(Long_Float(gsl.vector_double.get(xsd,size_t(i-1)))); Put(" ; ");
         New_Line;
         if size_t(i) = N_Sigma(Integer(varidx))
         then
            varidx := varidx + 1;
         end if ;
      end loop ;

      Set_Output(Standard_Output);
      Close(csvfile);
      gsl.vector_double.free(x);
      gsl.vector_double.free(xmedian);
      gsl.vector_double.free(xmad);
      gsl.vector_double.free(xiqr);
      gsl.vector_double.free(xSn);
      gsl.vector_double.free(xQn);
      gsl.vector_double.free(xsd);
      gsl.rng.free(rng);
      gsl.movstat.free(ws);
   end Test2 ;

   fn : aliased gsl.movstat.gsl_movstat_function ;

   procedure Test3 is
      proc : String := gnat.source_info.enclosing_entity ;
      N3 : constant := 1000 ;
      K3 : constant := 11 ;
      alpha : double := 0.1 ;
      x : access gsl.vector_double.gsl_vector := gsl.vector_double.alloc(N3) ;
      y : access gsl.vector_double.gsl_vector := gsl.vector_double.alloc(N3) ;
      rng : access gsl.rng.gsl_rng := gsl.rng.alloc(gsl.rng.default);
      ws : access gsl.movstat.gsl_movstat_workspace :=
                  gsl.movstat.alloc( size_t(K3) );
      ui, outlier : double ;
      sum : double := 0.0 ;
      temp : double ;
      csvfile : File_Type ;
   begin
      Put_line(proc);
      for i in 1..N3
      loop
         ui := gsl.randist.gaussian(rng,1.0);
         temp := gsl.rng.uniform(rng);
         if temp < 0.01
         then
            outlier := sign(ui) * 10.0;
         else
            outlier := 0.0 ;
         end if;
         sum := sum + ui ;
         gsl.vector_double.set(x,size_t(i-1),sum+outlier);
      end loop;
      Create(csvfile,Out_File,proc & ".csv") ;
      Set_Output(csvfile);
      fn.c_function := windowing.windowingfn'access ;
      fn.params := alpha'Address ;
      Status := gsl.movstat.apply(gsl.movstat.GSL_MOVSTAT_END_PADVALUE , fn'access , x , y , ws );
      for i in 1..N3
      loop
         Put(i) ; Put(" ; ");
         Put(Long_Float(gsl.vector_double.get(x,size_t(i-1)))); Put(" ; ");
         Put(Long_Float(gsl.vector_double.get(y,size_t(i-1)))); Put(" ; ");
         New_Line;
      end loop ;
      Set_Output(Standard_Output);
      Close(csvfile);
      gsl.vector_double.free(x);
      gsl.vector_double.free(y);
      gsl.movstat.free(ws);
      gsl.rng.free(rng);
   end Test3 ;
begin
   if Argument_Count > 0
   then
      N := Integer'Value( Argument(1) );
      if Argument_Count > 1
      then
         K := Integer'Value( Argument(2) );
      end if ;
   end if ;
   Ada.Long_Float_Text_IO.Default_aft := 4 ;
   Ada.Long_Float_Text_IO.Default_exp := 0 ;
   Test1 ;
   Test2 ;
   Test3 ;
end Movstat;