void TMultinomial::AddFtr(const TStrV& StrV, const TFltV& FltV, TIntFltKdV& SpV) const { // make sure we either do not have explicit values, or their dimension matches with string keys EAssertR(FltV.Empty() || (StrV.Len() == FltV.Len()), "TMultinomial::AddFtr:: String and double values not aligned"); // generate internal feature vector SpV.Gen(StrV.Len(), 0); for (int StrN = 0; StrN < StrV.Len(); StrN++) { const int FtrId = FtrGen.GetFtr(StrV[StrN]); // only use features we've seen during updates if (FtrId != -1) { const double Val = FltV.Empty() ? 1.0 : FltV[StrN].Val; if (Val > 1e-16) { SpV.Add(TIntFltKd(FtrId, Val)); } } } SpV.Sort(); // merge elements with the same id int GoodSpN = 0; for (int SpN = 1; SpN < SpV.Len(); SpN++) { if (SpV[GoodSpN].Key == SpV[SpN].Key) { // repetition of previous id, sum counts SpV[GoodSpN].Dat += SpV[SpN].Dat; } else { // increase the pointer to the next good position GoodSpN++; // and move the new value down to the good position SpV[GoodSpN] = SpV[SpN]; } } // truncate the vector SpV.Trunc(GoodSpN + 1); // replace values with 1 if needed if (IsBinary()) { for (TIntFltKd& Sp : SpV) { Sp.Dat = 1.0; } } // final normalization, if needed if (IsNormalize()) { TLinAlg::Normalize(SpV); } }
double TEpidemModel::GetErr(const TFltV& TrueV, const TFltV& SimV, const int& SimT0) { if (SimV.Empty() || TrueV.Empty()) { return -1.0; } double Err = 0.0; double S=0; for (int t=0; t < TrueV.Len(); t++) { if (t-SimT0 >= 0) { S = SimV[t-SimT0]; } else { S = 0; } Err += TMath::Sqr(TrueV[t]-S); } if (Err <= 0) { Err = TFlt::Mx; } return Err; }
static void ConjugGrad(const TMatrix& Matrix, const TFltV& b, TFltV& x, const int& CGMxIter, const double& RelErr, const TFltV& x0) { // prepare start vector x.Gen(Matrix.GetCols()); if (x0.Empty()) { x.PutAll(0.0); } else { x = x0; } // do the magic }
TFltV KalmanFilter::Predict(TFltV control) { // update the state: x'(k) = A * x(k) TLinAlg::Multiply(transitionMatrix, statePost, statePre); // x'(k) = x'(k) + B * u(k) if (!control.Empty()) { TLinAlg::Multiply(controlMatrix, control, temp1V); TLinAlg::AddVec(1.0, statePre, temp1V, temp2V); } // update error covariance matrices: temp1 = A * P(k) TLinAlg::Multiply(transitionMatrix, errorCovPost, temp1VV); // P'(k) = temp1 * At + Q TLinAlg::Gemm(1.0, temp1VV, transitionMatrix, 1.0, processNoiseCov, errorCovPre, TLinAlg::GEMM_B_T); // return statePre return statePre; }
void GetInvParticipRat(const PUNGraph& Graph, int MaxEigVecs, int TimeLimit, TFltPrV& EigValIprV) { TUNGraphMtx GraphMtx(Graph); TFltVV EigVecVV; TFltV EigValV; TExeTm ExeTm; if (MaxEigVecs<=1) { MaxEigVecs=1000; } int EigVecs = TMath::Mn(Graph->GetNodes(), MaxEigVecs); printf("start %d vecs...", EigVecs); try { TSparseSVD::Lanczos2(GraphMtx, EigVecs, TimeLimit, ssotFull, EigValV, EigVecVV, false); } catch(...) { printf("\n ***EXCEPTION: TRIED %d GOT %d values** \n", EigVecs, EigValV.Len()); } printf(" ***TRIED %d GOT %d values in %s\n", EigVecs, EigValV.Len(), ExeTm.GetStr()); TFltV EigVec; EigValIprV.Clr(); if (EigValV.Empty()) { return; } for (int v = 0; v < EigVecVV.GetCols(); v++) { EigVecVV.GetCol(v, EigVec); EigValIprV.Add(TFltPr(EigValV[v], GetInvParticipRat(EigVec))); } EigValIprV.Sort(); }
void PlotSngValDistr(const PNGraph& Graph, const int& SngVals, const TStr& FNmPref, TStr DescStr) { const int NBuckets = 50; TFltV SngValV; for (int f = 1; SngValV.Empty() && f < 4; f++) { TSnap::GetSngVals(Graph, f*SngVals, SngValV); } SngValV.Sort(true); THash<TFlt, TFlt> BucketCntH; double Step = (SngValV.Last()-SngValV[0]) / double(NBuckets-1); for (int i = 0; i < NBuckets; i++) { BucketCntH.AddDat(SngValV[0]+Step*(i+0.5), 0); } for (int i = 0; i < SngValV.Len(); i++) { const int Bucket = (int) floor((SngValV[i]-SngValV[0]) / Step); BucketCntH[Bucket] += 1; } TFltPrV EigCntV; BucketCntH.GetKeyDatPrV(EigCntV); if (DescStr.Empty()) { DescStr = FNmPref; } TGnuPlot::PlotValV(EigCntV, "sngDistr."+FNmPref, TStr::Fmt("%s. G(%d, %d). Largest eig val = %f", DescStr.CStr(), Graph->GetNodes(), Graph->GetEdges(), SngValV.Last().Val), "Singular value", "Count", gpsAuto, false, gpwLinesPoints); }