Changeset a804212 in tol


Ignore:
Timestamp:
Dec 2, 2009, 5:23:46 PM (17 years ago)
Author:
Víctor de Buen <vdebuen@…>
Branches:
1941-docker-image-to-test, devel, master
Children:
510e703
Parents:
4c3eee6
Message:

Using classes

git-svn-id: ​https://www.tol-project.org/svn/tolp/trunk@1757 be4846ae-f051-483a-831f-b64e7d08527f

File:
1 edited

Legend:

Unmodified
Added
Removed
  • tol_pkg/MulVarFunAprox/test/test_0001/test.tol

    r4c3eee6 ra804212  
    1818WriteLn("Current Random Seed = "<<rndSeed);
    1919
    20 //#Require MulVarFunAprox;
    21 //#Embed "../../../_embed_all.tol";
    22 #Embed "../../MulVarFunAprox.tol";
     20#Require MulVarFunAprox;
     21//#Embed "../../MulVarFunAprox.tol";
    2322
    24 Real n = 2;
    25 
    26 Real maxDeg = 4;
    27 
    28 FunRn2R::@BaseC2.Monomial base =
    29   FunRn2R::@BaseC2.Monomial::New(n,maxDeg);
    30 
    31 Real K = base::_.K;
    32 Real J = K*20;
    3323
    3424//////////////////////////////////////////////////////////////////////////////
    … …  
    3828  Group("ConcatColumns",For(1,n,VMatrix(Real i)
    3929  {
    40     Real min = Rand(0,0);
     30    Real min = Rand(2,4);
    4131    Real len = Rand(5,10)*(i^(1/n));
    4232    Rand(J,1,min,min+len)
    … …  
    4434};
    4535
    46 //////////////////////////////////////////////////////////////////////////////
    47   NameBlock base.random.fun =
    48 //////////////////////////////////////////////////////////////////////////////
    49 [[
    50   Matrix coef = Rand(base::_.K, 1, -10, 10);
    51   VMatrix eval(VMatrix X)
    52   {
    53     SetSum(For(1,base::_.K, VMatrix(Real k)
    54     {
    55       base::function.v(k,X)*MatDat(coef,k,1)
    56     }))
    57   }
    58 ]];
     36MulVarFunAprox::@DesignC2 design =
     37  MulVarFunAprox::@DesignC2.Mesh.Spheric.Monomial::New(
     38    generate.nodal.points(15000,2));
     39
     40Real design::force.reference.near.nodal := True;
     41Real design::radius.margin := 0.05;
     42Real design::max.deg := 6;
     43
     44Real t0 = Copy(Time);
     45WriteLn("Building model design for "<<design::_.J+" points "<<
     46  "of dimension "<<design::_.n+" ...");
     47Real deg = design::build.model.design(0);
     48Real t.building = Copy(Time)-t0;
     49WriteLn("Built model design in "<<t.building+" seconds");
     50
     51Real t0 := Copy(Time);
     52WriteLn("Building design decomposition for design matrix ("<<
     53  design::_.M+"x"<<design::_.N+") ...");
     54Real design::build.decomposition(0);
     55Real t.decomposition = Copy(Time)-t0;
     56WriteLn("Input decomposed in "<<t.decomposition+" seconds");
     57
     58FunRn2R::@BaseC2 b = design::get.base(0);
     59
     60FunRn2R::@FunC2 base.random.fun=FunRn2R::@FunC2.InBaseC2::Random(b, 1);
    5961
    6062//////////////////////////////////////////////////////////////////////////////
    … …  
    6264//////////////////////////////////////////////////////////////////////////////
    6365{
    64   Set x = For(1,n,VMatrix(Real i) { SubCol(X,[[i]]) });
    65   Sin(x[1])+Cos(x[1])
     66   BinGroup("$*",For(1,design::_.n,VMatrix(Real i)
     67   {
     68     VMatrix Xi = SubCol(X,[[i]]);
     69     If(i%2==1,Sin(Xi),Cos(Xi))
     70   }))
     71   $* RPow(SubCol(X,[[1]]),2)
    6672};
    6773
    68 Code fun.eval = fun.eval.extra.base;
    69 //Code fun.eval = base.random.fun::eval;
     74//Code fun.eval = fun.eval.extra.base;
     75Code fun.eval = base.random.fun::eval.function.v;
     76Real b::upgrade.cache(design::_.X,0);
    7077
    7178
    72 VMatrix X = generate.nodal.points(J,n);
    73 VMatrix Fx = fun.eval(X);
     79Real t0 := Copy(Time);
     80WriteLn("Running regression model for design matrix "<<design::_.Q);
     81MulVarFunAprox::@ModelC2 model = MulVarFunAprox::@ModelC2::New(
     82   design,
     83   fun.eval(design::_.X));
     84Real t.estimating = Copy(Time)-t0;
     85WriteLn("Estimated model in "<<t.estimating+" seconds");
    7486
    75 Real lengthRatio = 0.7*(J/(K*n))^(1/n);
     87VMatrix X2 = { design::generate.internal.points(
     88  Real m = design::_.J/2,
     89  Real design::force.reference.near.nodal)};
    7690
    77 Set X.col.info = For(1,n,Set(Real i)
    78 {[[
    79   VMatrix col = SubCol(X,[[i]]);
    80   Real min = VMatMin(col);
    81   Real max = VMatMax(col);
    82   Real length = max-min;
    83   Real grid.size = 1*length/lengthRatio
    84 ]]});
    85 
    86 Real grid.size = SetAvr(Traspose(Extract(X.col.info,5))[1]);
    87 Real radius = grid.size / Sqrt(2);
    88 
    89 Set range = For(1,n,Set(Real i)
    90 {
    91   Set info = X.col.info[i];
    92   Real points = 1+Ceil(info::length/grid.size);
    93   Real exceed = (points-1)*grid.size-info::length;
    94   Matrix rnk = Tra(DifEq(1/(1-B),Rand(points,1,1,1))-Rand(points,1,1,1));   
    95   Set MatSet((rnk*grid.size)+info::min-exceed/2)[1]
    96 });
    97 
    98 VMatrix Y = Mat2VMat(SetMat(Group("CartProd",range)));
    99 Real H = VRows(Y);
    100 Real N = H*K;
    101 
    102 VMatrix Y.min = Group("ConcatColumns", For(1,n,VMatrix(Real i)
    103 {
    104   Real aux = range[i][1];
    105   Rand(H,1,aux,aux)
    106 }));
    107 
    108 VMatrix Y.max = Group("ConcatColumns", For(1,n,VMatrix(Real i)
    109 {
    110   Real aux = range[i][Card(range[i])];
    111   Rand(H,1,aux,aux)
    112 }));
    113 
    114 
    115 VMatrix Y.index = Mat2VMat(SetMat(Group("CartProd",For(1,n,Set(Real i)
    116 {
    117   Range(1,Card(range[i]),1)
    118 }))));
    119 
    120 VMatrix show.X.Y =
    121   ((X*0) <<  Y   ) |
    122   ( X    << (Y*0));
    123 
    124 //////////////////////////////////////////////////////////////////////////////
    125 VMatrix closest.Y.index(VMatrix A)
    126 //////////////////////////////////////////////////////////////////////////////
    127 {
    128   Real m = VRows(A);
    129   VMatrix cumulated = Rand(m,1,1,1);
    130   VMatrix A.position = Rand(m,1,1,1)+SetSum(For(1,n,VMatrix(Real i_)
    131   {
    132     Real i = n-i_+1;
    133     Real cRi = Card(range[i]);
    134     Set info = X.col.info[i];
    135     VMatrix Ai = SubCol(A,[[i]]);
    136     VMatrix Yi.min = Rand(m,1,range[i][1],range[i][1]);
    137     VMatrix Ri = Round((Ai-Yi.min)/grid.size);
    138     VMatrix minRi = Rand(m,1,0,0);
    139     VMatrix maxRi = Rand(m,1,cRi-1,cRi-1);
    140     VMatrix aux = Min(maxRi,Max(minRi,Ri));
    141     VMatrix pos = aux $* cumulated;
    142     VMatrix cumulated := cumulated*cRi;
    143     pos
    144   }))
    145 };
    146 
    147 VMatrix Y.closest.Y.index = closest.Y.index(Y);
    148 VMatrix Y.closest.Y = SubRow(Y,VCol2Set(Y.closest.Y.index));
    149 Real ok.1 = VMatMax(Abs(Y-Y.closest.Y))==0;
    150 
    151 VMatrix Y.neighbour.pos = Min(Y.max, Y + Rand(H,n,grid.size,grid.size));
    152 VMatrix Y.neighbour.neg = Max(Y.min, Y - Rand(H,n,grid.size,grid.size));
    153 
    154 VMatrix Y.neighbourhood = DifEq(1/(1-B),Rand(H,1,1,1)) |
    155 {
    156   Set aux.1 = BinGroup("<<", For(1,n,Set(Real i)
    157   {
    158     VMatrix pre  = SubCol(Y,Range(1,  i-1,1));
    159     VMatrix post = SubCol(Y,Range(i+1,n,  1));
    160     SetOfVMatrix(pre | SubCol(Y.neighbour.neg,[[i]]) | post,
    161                  pre | SubCol(Y.neighbour.pos,[[i]]) | post)
    162   }));
    163   Group("ConcatColumns",EvalSet(aux.1,closest.Y.index))
    164 };
    165 
    166 Set Y.neighbourhood.extrictly = For(1,H,Set(Real h)
    167 {
    168   VCol2Set(SubRow(Y.neighbourhood,[[h]]))  - [[h]]
    169 });
    170 
    171 Real X.maxNeighbour = 1+2*n;
    172 
    173 //////////////////////////////////////////////////////////////////////////////
    174 VMatrix builds.obs.input(VMatrix X)
    175 //////////////////////////////////////////////////////////////////////////////
    176 {
    177   Real J = VRows(X);
    178  
    179   VMatrix X.closest.Y.index = closest.Y.index(X);
    180   VMatrix X.closest.Y = SubRow(Y,VCol2Set(X.closest.Y.index));
    181  
    182   VMatrix X.j=DifEq(1/(1-B),Rand(J,1,1,1));
    183  
    184   VMatrix X.h=SubRow(Y.neighbourhood, VCol2Set(X.closest.Y.index));
    185  
    186   Set X.dif = For(1,X.maxNeighbour,VMatrix(Real i)
    187   {
    188     X - SubRow(Y,VCol2Set(SubCol(X.h,[[i]])))
    189   });
    190  
    191   Set X.tao = For(1,X.maxNeighbour,VMatrix(Real i)
    192   {
    193     VMatrix X.norm2 = RPow(X.dif[i],2)*Rand(n,1,1,1);
    194     RPow(X.norm2,0.5)/radius
    195   });
    196  
    197   Set X.tao2 = For(1,X.maxNeighbour,VMatrix(Real i)
    198   {
    199     RPow(X.tao[i],2)
    200   });
    201  
    202   Set X.match = For(1,X.maxNeighbour,VMatrix(Real i)
    203   {
    204     LE(X.tao[i],Rand(J,1,1,1))
    205   });
    206  
    207   Set X.lambda = For(1,X.maxNeighbour,VMatrix(Real i)
    208   {
    209     RPow(X.match[i] $* (Rand(J,1,1,1)-X.tao2[i]),4)
    210   });
    211  
    212   VMatrix X.lambda.sum = SetSum(X.lambda);
    213  
    214   Set X.weight = For(1,X.maxNeighbour,VMatrix(Real i)
    215   {
    216     X.lambda[i] $/ X.lambda.sum
    217   });
    218  
    219   Matrix QX.triplet = Group("ConcatRows", For(1,X.maxNeighbour,Matrix(Real i)
    220   {
    221     VMatrix w.i = X.weight[i];
    222     Set sel.rows = MatQuery::SelectMatch(NE(Drop(Abs(w.i),1.E-15),w.i*0));
    223     VMatrix wi = SubRow(w.i,sel.rows);
    224     VMatrix X.hiK = SubRow((SubCol(X.h,[[i]])-1)*K,sel.rows);
    225     VMatrix Xdif.i = SubRow(X.dif[i],sel.rows);
    226     VMatrix X.j.i = SubRow(X.j,sel.rows);
    227     Group("ConcatRows", For(1,K,Matrix(Real k)
    228     {
    229       VMatrix pk = base::function.v(k,Xdif.i);
    230       VMatrix qk = wi$*pk;
    231       VMat2Mat(X.j.i | (X.hiK+k) | qk)
    232     }))
    233   }));
    234   Pack(Convert(Triplet(QX.triplet, J, N),"Cholmod.R.Sparse"))
    235 };
    236 
    237 VMatrix QX = builds.obs.input(X);
    238 
    239 
    240 VMatrix displ.Z.1 =
    241   Rand(H,1,-0.1,0.1)*grid.size/2 |
    242   Rand(H,1, 0.9,1.1)*grid.size/2;
    243 VMatrix displ.Z.2 =
    244   Rand(H,1, 0.9,1.1)*grid.size/2 |
    245   Rand(H,1,-0.1,0.1)*grid.size/2;
    246 VMatrix displ.Z.3 =
    247   Rand(H,1,-0.9,-1.1)*grid.size/2 |
    248   Rand(H,1,-0.9,-1.1)*grid.size/2;
    249 
    250 VMatrix Z.all = (Y+displ.Z.1) << (Y+displ.Z.2) << (Y+displ.Z.3);
    251 VMatrix Z.Y.1.all = Y << Y << Y;
    252 VMatrix Z.Y.2.all = (Y+displ.Z.1*2) << (Y+displ.Z.2*2) << (Y+displ.Z.3*2);
    253 
    254 VMatrix Z = Z.all;
    255 VMatrix Z.Y.1 = Z.Y.1.all;
    256 VMatrix Z.Y.2 = Z.Y.2.all;
    257 VMatrix Z.h.1 = closest.Y.index(Z.Y.1);
    258 VMatrix Z.h.2 = closest.Y.index(Z.Y.2);
    259 
    260 
    261 VMatrix show.X.Y.Z =
    262   ( X    << (Y*0) << (Z*0)) |
    263   ((X*0) <<  Y    << (Z*0)) |
    264   ((X*0) << (Y*0) <<  Z   );
    265 
    266 VMatrix show.Y.Z =
    267   ( Y    << (Z*0)) |
    268   ((Y*0) <<  Z   );
    269 
    270 
    271 Real T = VRows(Z);
    272 Real prior.Z.order = 2;
    273 Real Mz = Case(
    274   prior.Z.order< 0, 0,
    275   prior.Z.order==0, T,
    276   prior.Z.order==1, T*(1+n*K),
    277   prior.Z.order==2, T*(1+(n^2)*K));
    278 
    279 Real Z.maxNeighbour = If(Mz,2,0);
    280 VMatrix Z.t=DifEq(1/(1-B),Rand(T,1,1,1));
    281 
    282 VMatrix Z.h=Z.h.1 | Z.h.2;
    283 
    284 Set Z.dif = For(1,Z.maxNeighbour,VMatrix(Real i)
    285 {
    286   Z - SubRow(Y,VCol2Set(SubCol(Z.h,[[i]])))
    287 });
    288 
    289 Set Z.tao = For(1,Z.maxNeighbour,VMatrix(Real i)
    290 {
    291   VMatrix Z.norm2 = RPow(Z.dif[i],2)*Rand(n,1,1,1);
    292   RPow(Z.norm2,0.5)/radius
    293 });
    294 
    295 Set Z.match = For(1,Z.maxNeighbour,VMatrix(Real i)
    296 {
    297   LE(Z.tao[i],Rand(T,1,1,1))
    298 });
    299 
    300 Matrix PZ.triplet = Group("ConcatRows", For(1,Z.maxNeighbour,Matrix(Real i)
    301 {
    302   VMatrix Z.hiK = (SubCol(Z.h,[[i]])-1)*K;
    303   VMatrix Zdif.i = Z.dif[i];
    304   VMatrix Z.t.i = Z.t;
    305   Group("ConcatRows", For(1,K,Matrix(Real k)
    306   {
    307     Real cumRows = 0;
    308     VMatrix pk = base::function.v(k,Zdif.i)*If(i==1,+1,-1);
    309     VMatrix col.idx = (Z.hiK+k);
    310     {
    311       VMatrix row.idx = Z.t.i+cumRows;
    312       Real cumRows := cumRows + T;
    313       VMat2Mat(row.idx | col.idx | pk)
    314     }  << If(prior.Z.order<1,Rand(0,3,0,0),
    315     Group("ConcatRows",For(1,n,Matrix(Real d)
    316     {
    317       Real sign = If(i==1,+1/2,-1/2);
    318       VMatrix pkd = base::partial.derivative.1.v(k,Zdif.i,d)*sign;
    319       VMatrix row.idx = Z.t.i+cumRows;
    320       Real cumRows := cumRows + T;
    321       VMat2Mat(row.idx | col.idx | pkd)
    322     })))<< If(prior.Z.order<2,Rand(0,3,0,0),
    323     Group("ConcatRows",For(1,n,Matrix(Real d1)
    324     {
    325       Group("ConcatRows",For(d1,n,Matrix(Real d2)
    326       {
    327         Real sign = If(i==1,+1/4,-1/4);
    328         VMatrix pkd2 = base::partial.derivative.2.v(k,Zdif.i,d1,d2)*sign;
    329         VMatrix row.idx = Z.t.i+cumRows;
    330         Real cumRows := cumRows + T;
    331         VMat2Mat(row.idx | col.idx | pkd2)
    332       }))
    333     })))
    334   }))
    335 }));
    336 
    337 
    338 Real M = J+Mz;
    339 
    340 VMatrix PZ = Convert(Triplet(PZ.triplet, Mz, N),"Cholmod.R.Sparse");
    341 
    342 VMatrix Fz = Rand(Mz,1,0,0);
    343 VMatrix F = Fx<<Fz;
    344 VMatrix Q.all = QX << PZ;
    345 Set Q.active = MatQuery::SelectMatch(
    346   NE(Tra(Rand(1,M,1,1)*Abs(Q.all)),Rand(N,1,0,0)));
    347 
    348 VMatrix Q = SubCol(Q.all,Q.active);
    349 
    350 VMatrix LQ = CholeskiFactor(Q,"XtX");
    351 
    352 VMatrix alpha = CholeskiSolve(LQ,Tra(Tra(F)*Q),"PtLLtP");
    353 VMatrix F.frc = SubRow(Q*alpha,Range(1,J,1));
    354 VMatrix F.cmp = Fx | F.frc;
    355 VMatrix E = Fx - F.frc;
    356 Real E.avr = VMatAvr(E);
    357 Real E.ste = VMatStDs(E);
    358 
    359 
    360 VMatrix X2 = generate.nodal.points(J,n);
     91Real b::upgrade.cache(X2,0);
    36192VMatrix Fx2 = fun.eval(X2);
    362 VMatrix QX2 = builds.obs.input(X2);
    363 VMatrix F2.frc = QX2*alpha;
     93Real t0 := Copy(Time);
     94WriteLn("Evaluating forecast for "<<VRows(X2)+" points...");
     95VMatrix F2.frc = model::evaluate(X2);
     96Real t.forecasting = Copy(Time)-t0;
     97WriteLn("Evaluated forecast in "<<t.forecasting+" seconds");
     98WriteLn("Forecast time by point "<<(t.forecasting/VRows(X2))+" seconds");
     99VMatrix F2.cmp = Fx2 | F2.frc;
    364100VMatrix E2 = Fx2 - F2.frc;
    365101Real E2.avr = VMatAvr(E2);
    366102Real E2.ste = VMatStDs(E2);
    367103
     104Real q.1 = 1-model::_.E.ste;
     105Real q.2 = 1-E2.ste;
    368106Set View([[
    369   Real n, K, J, H,
    370   X.maxNeighbour, Z.maxNeighbour,
    371   T, N, Mz,M,
    372   E.avr, E.ste,
     107  Real design::_.n, design::_.K, design::_.J, design::_.H,
     108  design::_.X.maxNeighbour, design::_.Z.maxNeighbour,
     109  design::_.T, design::_.N, design::_.Mz, design::_.M,
     110  model::_.E.avr, model::_.E.ste,
     111  "\n"]],"");
     112
     113Set View([[
    373114  E2.avr, E2.ste,
    374115  "\n"]],"");
    375116
    376 /*
    377 Matrix Q_ = VMat2Mat(Q);
     117VMatrix show.X.Y =
     118  ((design::_.X*0) <<  design::_.Y   ) |
     119  ( design::_.X    << (design::_.Y*0));
    378120
    379 Set svd = SVD(Q_);
    380 Matrix svd.D = Tra(SubDiag(svd[2],0));
     121VMatrix show.X.Y.Z =
     122  ( design::_.X    << (design::_.Y*0) << (design::_.Z*0)) |
     123  ((design::_.X*0) <<  design::_.Y    << (design::_.Z*0)) |
     124  ((design::_.X*0) << (design::_.Y*0) <<  design::_.Z   );
    381125
    382 /*
     126VMatrix show.Y.Z =
     127  ( design::_.Y    << (design::_.Z*0)) |
     128  ((design::_.Y*0) <<  design::_.Z   );
     129
     130
     131
    383132Real numErr1 = Copy(NError);
    384133Real numWar1 = Copy(NWarning);
    385134
    386135Set partialResults_ = [[numErr0, numErr1,
    387                         ok.1, ok.2]];
     136                        q.1, q.2]];
    388137
    389138//This is a messure of the success of the test
    390139Real quality_ = And(numErr1 == numErr0,
    391                     numWar1 == numWar0,
    392                     ok.1, ok.2);
     140                    numWar1 == numWar0)*Min(q.1,q.2);
    393141
    394142//Return the results
Note: See TracChangeset for help on using the changeset viewer.