void TStrFeatureSpace::ToStr(const TFltV& FeatureIds, TChA& ChA, int k, char Sep) const { TIntSet TakenIndexes(k); int Len = TMath::Mn(FeatureIds.Len(), k); for (int i = 0; i < Len; i++) { double MxVal = TFlt::Mn; int MxIndex = 0; for (int j = 0; j < FeatureIds.Len(); j++) { const TFlt& FeatureVal = FeatureIds[j]; if (FeatureVal > MxVal) { if (!TakenIndexes.IsKey(j)) { MxVal = FeatureVal; MxIndex = j; } } } TakenIndexes.AddKey(MxIndex); ChA += ISpace.KeyFromOfs(Space[MxIndex]); ChA += ':'; ChA += TFlt::GetStr(MxVal, "%2.6f");; if (i < Len) { ChA += Sep; } } }
void TChiSquare::Update(const TFltV& OutValVX, const TFltV& OutValVY) { Chi2 = 0.0; EAssertR(OutValVX.Len() == OutValVY.Len(), "TChiSquare: histogram dimensions do not match!"); // http://www.itl.nist.gov/div898/software/dataplot/refman1/auxillar/chi2samp.htm double SumR = TLinAlg::SumVec(OutValVX); double SumS = TLinAlg::SumVec(OutValVY); // Do nothing if zero histogram is detected if (SumR <= 0.0 || SumS <= 0.0) { return; } double K1 = TMath::Sqrt(SumS / SumR); double K2 = 1.0 / K1; for (int ValN = 0; ValN < OutValVX.Len(); ValN++) { double Ri = OutValVX[ValN]; double Si = OutValVY[ValN]; double RpS = Ri + Si; if (RpS > 0) { Chi2 += TMath::Sqr(K1 * Ri - K2 * Si) / RpS; } } if (Chi2 == 0.0) { P = TFlt::PInf; } else { P = TSpecFunc::GammaQ(0.5*(DegreesOfFreedom), 0.5*(Chi2)); } }
void TestEigSvd() { PNGraph G = TSnap::GenRndGnm<PNGraph>(100,1000, true); PUNGraph UG = TSnap::ConvertGraph<PUNGraph>(G); TSnap::SaveMatlabSparseMtx(G, "test1.mtx"); TSnap::SaveMatlabSparseMtx(UG, "test2.mtx"); TFltV SngValV; TVec<TFltV> LeftV, RightV; TSnap::GetSngVec(G, 20, SngValV, LeftV, RightV); printf("Singular Values:\n"); for (int i =0; i < SngValV.Len(); i++) { printf("%d\t%f\n", i, SngValV[i]()); } printf("LEFT Singular Vectors:\n"); for (int i=0; i < LeftV[0].Len(); i++) { printf("%d\t%f\t%f\t%f\t%f\t%f\n", i, LeftV[0][i](), LeftV[1][i](), LeftV[2][i](), LeftV[3][i](), LeftV[4][i]()); } printf("RIGHT Singular Vectors:\n"); for (int i=0; i < RightV[0].Len(); i++) { printf("%d\t%f\t%f\t%f\t%f\t%f\n", i, RightV[0][i](), RightV[1][i](), RightV[2][i](), RightV[3][i](), RightV[4][i]()); } TFltV EigValV; TVec<TFltV> EigV; TSnap::GetEigVec(UG, 20, EigValV, EigV); printf("Eigen Values:\n"); for (int i =0; i < EigValV.Len(); i++) { printf("%d\t%f\n", i, EigValV[i]()); } printf("Eigen Vectors %d:\n", EigV.Len()); for (int i =0; i < EigV[0].Len(); i++) { printf("%d\t%f\t%f\t%f\t%f\t%f\n", i, EigV[0][i](), EigV[1][i](), EigV[2][i](), EigV[3][i](), EigV[4][i]()); } }
// to get first few eigenvectors void GetEigVec(const PUNGraph& Graph, const int& EigVecs, TFltV& EigValV, TVec<TFltV>& EigVecV) { const int Nodes = Graph->GetNodes(); // Lanczos TUNGraphMtx GraphMtx(Graph); int CalcVals = int(2*EigVecs); if (CalcVals > Nodes) { CalcVals = Nodes; } TFltVV EigVecVV; //while (EigValV.Len() < EigVecs && CalcVals < 10*EigVecs) { try { TSparseSVD::Lanczos(GraphMtx, EigVecs, 2*EigVecs, ssotFull, EigValV, EigVecVV, false); } catch(...) { printf("\n ***EXCEPTION: TRIED %d GOT %d values** \n", CalcVals, EigValV.Len()); } if (EigValV.Len() < EigVecs) { printf(" ***TRIED %d GOT %d values** \n", CalcVals, EigValV.Len()); } // CalcVals += EigVecs; //} TFltIntPrV EigValIdV; for (int i = 0; i < EigValV.Len(); i++) { EigValIdV.Add(TFltIntPr(EigValV[i], i)); } EigValIdV.Sort(false); EigValV.Sort(false); for (int v = 0; v < EigValIdV.Len(); v++) { // vector components are not sorted!!! EigVecV.Add(); EigVecVV.GetCol(EigValIdV[v].Val2, EigVecV.Last()); } IsAllValVNeg(EigVecV[0], true); }
///////////////////////////////////////////////// // Online Moving Standard M2 void TVar::Update(const double& InVal, const uint64& InTmMSecs, const TFltV& OutValV, const TUInt64V& OutTmMSecsV, const int& N) { pNo = N; int tempN = N - 1 + OutValV.Len(); double delta; // check if all the values are removed if (OutValV.Len() >= tempN) { Ma = 0; M2 = 0; tempN = 0; } else { // remove old values from the mean for (int ValN = 0; ValN < OutValV.Len(); ValN++) { tempN--; delta = OutValV[ValN] - Ma; Ma = Ma - delta / tempN; M2 = M2 - delta * (OutValV[ValN] - Ma); } } //add the new value to the resulting mean delta = InVal - Ma; Ma = Ma + delta/N; M2 = M2 + delta * (InVal - Ma); TmMSecs = InTmMSecs; }
void GetEigVals(const PUNGraph& Graph, const int& EigVals, TFltV& EigValV) { // Lanczos TUNGraphMtx GraphMtx(Graph); //const int Nodes = Graph->GetNodes(); //int CalcVals = int(2*EigVals); //if (CalcVals > Nodes) { CalcVals = Nodes; } //while (EigValV.Len() < EigVals && CalcVals < 3*EigVals) { try { if (EigVals > 4) { TSparseSVD::SimpleLanczos(GraphMtx, 2*EigVals, EigValV, false); } else { TFltVV EigVecVV; // this is much more precise, but also much slower TSparseSVD::Lanczos(GraphMtx, EigVals, 3*EigVals, ssotFull, EigValV, EigVecVV, false); } } catch(...) { printf("\n ***EXCEPTION: TRIED %d GOT %d values** \n", 2*EigVals, EigValV.Len()); } if (EigValV.Len() < EigVals) { printf(" ***TRIED %d GOT %d values** \n", 2*EigVals, EigValV.Len()); } // CalcVals += EigVals; //} EigValV.Sort(false); /*if (EigValV.Len() > EigVals) { EigValV.Del(EigVals, EigValV.Len()-1); } else { while (EigValV.Len() < EigVals) EigValV.Add(1e-6); } IAssert(EigValV.Len() == EigVals);*/ }
double TAGMFit::Likelihood(const TFltV& NewLambdaV, double& LEdges, double& LNoEdges) { IAssert(CIDNSetV.Len() == NewLambdaV.Len()); IAssert(ComEdgesV.Len() == CIDNSetV.Len()); LEdges = 0.0; LNoEdges = 0.0; for (int e = 0; e < EdgeComVH.Len(); e++) { TIntSet& JointCom = EdgeComVH[e]; double LambdaSum = SelectLambdaSum(NewLambdaV, JointCom); double Puv = 1 - exp(- LambdaSum); if (JointCom.Len() == 0) { Puv = PNoCom; } IAssert(! _isnan(log(Puv))); LEdges += log(Puv); } for (int k = 0; k < NewLambdaV.Len(); k++) { int MaxEk = CIDNSetV[k].Len() * (CIDNSetV[k].Len() - 1) / 2; int NotEdgesInCom = MaxEk - ComEdgesV[k]; if(NotEdgesInCom > 0) { if (LNoEdges >= TFlt::Mn + double(NotEdgesInCom) * NewLambdaV[k]) { LNoEdges -= double(NotEdgesInCom) * NewLambdaV[k]; } } } double LReg = 0.0; if (RegCoef > 0.0) { LReg = - RegCoef * TLinAlg::SumVec(NewLambdaV); } return LEdges + LNoEdges + LReg; }
int TGnuPlot::AddPlot(const TFltV& YValV, const TGpSeriesTy& SeriesTy, const TStr& Label, const TStr& Style) { TFltKdV XYValV(YValV.Len(), 0); for (int i = 0; i < YValV.Len(); i++) { XYValV.Add(TFltKd(TFlt(i+1), TFlt(YValV[i]))); } return AddPlot(XYValV, SeriesTy, Label, Style); }
void TEpidemModel::RungeKutta(const TFltV& y, const TFltV& dydx, double x, double h, TFltV& SirOutV) { const int n = y.Len(); IAssert(y.Len() == n && dydx.Len() == n); TFltV dym(n), dyt(n), yt(n); int i; double hh=h*0.5; double h6=h/6.0; double xh=x+hh; for (i=0; i < n; i++) { yt[i]=y[i]+hh*dydx[i]; } GetDerivs(xh, yt, dyt); for (i=0; i<n; i++) { yt[i]=y[i]+hh*dyt[i]; } GetDerivs(xh,yt,dym); for (i=0; i<n; i++) { yt[i]=y[i]+h*dym[i]; dym[i] += dyt[i]; } GetDerivs(x+h,yt,dyt); SirOutV.Clr(false); for (i=0; i<n; i++) { SirOutV.Add(y[i]+h6 * (dydx[i]+dyt[i]+2.0*dym[i])); } }
TEST(TSlottedHistogramTest, Simple2) { try { TQm::TStreamAggrs::PSlottedHistogram obj = TQm::TStreamAggrs::TSlottedHistogram::New(20, 5, 3); obj->Add(1, 0); // slot 0 obj->Add(7, 0); // slot 1 obj->Add(12, 1); // slot 2 obj->Add(18, 0); // slot 3 obj->Add(22, 1); // slot 0 obj->Add(38, 0); // slot 3 TFltV Stats; obj->GetStats(41, 45, Stats); // slots from 0 to 1 inclusive ASSERT_EQ(Stats.Len(), 3); ASSERT_EQ(Stats[0], 2.0); ASSERT_EQ(Stats[1], 1.0); ASSERT_EQ(Stats[2], 0.0); obj->GetStats(45, 47, Stats); // slots from 1 to 1 inclusive ASSERT_EQ(Stats.Len(), 3); ASSERT_EQ(Stats[0], 1.0); ASSERT_EQ(Stats[1], 0.0); ASSERT_EQ(Stats[2], 0.0); } catch (PExcept& Except) { printf("Error: %s", Except->GetStr()); throw Except; } }
TFltV KalmanFilter::Correct(TFltV measurement) { // temp2 = H * P'(k) TLinAlg::Multiply(measurementMatrix, errorCovPre, temp2VV); // temp3 = temp2 * Ht + R TLinAlg::Gemm(1.0, temp2VV, measurementMatrix, 1.0, measurementNoiseCov, temp3VV, TLinAlg::GEMM_B_T); // temp4 = inv(temp3) * temp2 = Kt(k) TLinAlg::Inverse(temp3VV, itemp3VV, TLinAlg::DECOMP_SVD); TLinAlg::Multiply(itemp3VV, temp2VV, temp4VV); // K(k) TLinAlg::Transpose(temp4VV, gain); // temp2V = z(k) - H*x'(k) temp1V.Gen(1, 1); TLinAlg::Multiply(measurementMatrix, statePre, temp1V); temp2V.Gen(measurement.Len(), measurement.Len()); TLinAlg::AddVec(-1.0, temp1V, measurement, temp2V); // x(k) = x'(k) + K(k) * temp2V temp1V.Gen(gain.GetRows(), gain.GetRows()); TLinAlg::Multiply(gain, temp2V, temp1V); TLinAlg::AddVec(1.0, statePre, temp1V, statePost); // P(k) = P'(k) - K(k) * temp2 TLinAlg::Gemm(-1.0, gain, temp2VV, 1.0, errorCovPre, errorCovPost, TLinAlg::GEMM_NO_T); // return statePost return statePost; }
int TGnuPlot::AddErrBar(const TFltV& YValV, const TFltV& DeltaYV, const TStr& Label) { IAssert(YValV.Len() == DeltaYV.Len()); TFltKdV XYFltValV(YValV.Len(), 0); for (int i = 0; i < YValV.Len(); i++) { XYFltValV.Add(TFltKd(TFlt(i+1), YValV[i])); } return AddErrBar(XYFltValV, DeltaYV, Label); }
int TLSHash::EuclideanHash::Hash(TFltV Datum) { double Proj = Line[Datum.Len()]; for (int i=0; i<Datum.Len(); i++) { Proj += Datum[i] * Line[i]; } Proj /= Gap; return static_cast<int>(Proj); }
void TNEANetMP::Dump(FILE *OutF) const { const int NodePlaces = (int) ceil(log10((double) GetNodes())); const int EdgePlaces = (int) ceil(log10((double) GetEdges())); fprintf(OutF, "-------------------------------------------------\nDirected Node-Edge Network: nodes: %d, edges: %d\n", GetNodes(), GetEdges()); for (TNodeI NodeI = BegNI(); NodeI < EndNI(); NodeI++) { fprintf(OutF, " %*d]\n", NodePlaces, NodeI.GetId()); // load node attributes TIntV IntAttrN; IntAttrValueNI(NodeI.GetId(), IntAttrN); fprintf(OutF, " nai[%d]", IntAttrN.Len()); for (int i = 0; i < IntAttrN.Len(); i++) { fprintf(OutF, " %*i", NodePlaces, IntAttrN[i]()); } TStrV StrAttrN; StrAttrValueNI(NodeI.GetId(), StrAttrN); fprintf(OutF, " nas[%d]", StrAttrN.Len()); for (int i = 0; i < StrAttrN.Len(); i++) { fprintf(OutF, " %*s", NodePlaces, StrAttrN[i]()); } TFltV FltAttrN; FltAttrValueNI(NodeI.GetId(), FltAttrN); fprintf(OutF, " naf[%d]", FltAttrN.Len()); for (int i = 0; i < FltAttrN.Len(); i++) { fprintf(OutF, " %*f", NodePlaces, FltAttrN[i]()); } fprintf(OutF, " in[%d]", NodeI.GetInDeg()); for (int edge = 0; edge < NodeI.GetInDeg(); edge++) { fprintf(OutF, " %*d", EdgePlaces, NodeI.GetInEId(edge)); } fprintf(OutF, "\n"); fprintf(OutF, " out[%d]", NodeI.GetOutDeg()); for (int edge = 0; edge < NodeI.GetOutDeg(); edge++) { fprintf(OutF, " %*d", EdgePlaces, NodeI.GetOutEId(edge)); } fprintf(OutF, "\n"); } for (TEdgeI EdgeI = BegEI(); EdgeI < EndEI(); EdgeI++) { fprintf(OutF, " %*d] %*d -> %*d", EdgePlaces, EdgeI.GetId(), NodePlaces, EdgeI.GetSrcNId(), NodePlaces, EdgeI.GetDstNId()); // load edge attributes TIntV IntAttrE; IntAttrValueEI(EdgeI.GetId(), IntAttrE); fprintf(OutF, " eai[%d]", IntAttrE.Len()); for (int i = 0; i < IntAttrE.Len(); i++) { fprintf(OutF, " %*i", EdgePlaces, IntAttrE[i]()); } TStrV StrAttrE; StrAttrValueEI(EdgeI.GetId(), StrAttrE); fprintf(OutF, " eas[%d]", StrAttrE.Len()); for (int i = 0; i < StrAttrE.Len(); i++) { fprintf(OutF, " %*s", EdgePlaces, StrAttrE[i]()); } TFltV FltAttrE; FltAttrValueEI(EdgeI.GetId(), FltAttrE); fprintf(OutF, " eaf[%d]", FltAttrE.Len()); for (int i = 0; i < FltAttrE.Len(); i++) { fprintf(OutF, " %*f", EdgePlaces, FltAttrE[i]()); } fprintf(OutF, "\n"); } fprintf(OutF, "\n"); }
bool IsAllValVNeg(TFltV& ValV, const bool& InvertSign) { bool IsAllNeg=true; for (int i = 0; i < ValV.Len(); i++) { if (ValV[i]>0.0) { IsAllNeg=false; break; } } if (IsAllNeg && InvertSign) { for (int i = 0; i < ValV.Len(); i++) { ValV[i] = -ValV[i]; } } return IsAllNeg; }
// Result = A' * Vec void TNGraphMtx::PMultiplyT(const TFltV& Vec, TFltV& Result) const { const int RowN = GetRows(); Assert(Vec.Len() >= RowN && Result.Len() >= RowN); const THash<TInt, TNGraph::TNode>& NodeH = Graph->NodeH; for (int i = 0; i < RowN; i++) Result[i] = 0.0; for (int j = 0; j < RowN; j++) { const TIntV& RowV = NodeH[j].OutNIdV; for (int i = 0; i < RowV.Len(); i++) { Result[RowV[i]] += Vec[j]; } } }
void TBowMatrix::PMultiply(const TFltV& Vec, TFltV& Result) const { IAssert(Vec.Len() >= PGetCols() && Result.Len() >= PGetRows()); int RowN = PGetRows(), ColN = PGetCols(); int i, j; //TFlt *ResV = Result.BegI(); for (i = 0; i < RowN; i++) Result[i] = 0.0; for (j = 0; j < ColN; j++) { PBowSpV ColV = ColSpVV[j]; int len = ColV->Len(); for (i = 0; i < len; i++) { Result[ColV->GetWId(i)] += ColV->GetWgt(i) * Vec[j]; } } }
double TLogRegPredict::GetCfy(const TFltV& AttrV, const TFltV& NewTheta) { int len = AttrV.Len(); double res = 0; if (len < NewTheta.Len()) { res = NewTheta.Last(); //if feature vector is shorter, add an intercept } for (int i = 0; i < len; i++) { if (i < NewTheta.Len()) { res += AttrV[i] * NewTheta[i]; } } double mu = 1 / (1 + exp(-res)); return mu; }
void GetSngVals(const PNGraph& Graph, const int& SngVals, TFltV& SngValV) { const int Nodes = Graph->GetNodes(); IAssert(SngVals > 0); if (Nodes < 100) { // perform full SVD TFltVV AdjMtx(Nodes+1, Nodes+1); TFltVV LSingV, RSingV; TIntH NodeIdH; // create adjecency matrix for (TNGraph::TNodeI NodeI = Graph->BegNI(); NodeI < Graph->EndNI(); NodeI++) { NodeIdH.AddKey(NodeI.GetId()); } for (TNGraph::TNodeI NodeI = Graph->BegNI(); NodeI < Graph->EndNI(); NodeI++) { const int NodeId = NodeIdH.GetKeyId(NodeI.GetId()) + 1; for (int e = 0; e < NodeI.GetOutDeg(); e++) { const int DstNId = NodeIdH.GetKeyId(NodeI.GetOutNId(e)) + 1; // no self edges if (NodeId != DstNId) AdjMtx.At(NodeId, DstNId) = 1; } } try { // can fail to converge but results seem to be good TSvd::Svd1Based(AdjMtx, LSingV, SngValV, RSingV); } catch(...) { printf("\n***No SVD convergence: G(%d, %d)\n", Nodes, Graph->GetEdges()); } } else { // Lanczos TNGraphMtx GraphMtx(Graph); int CalcVals = int(2*SngVals); //if (CalcVals > Nodes) { CalcVals = int(2*Nodes); } //if (CalcVals > Nodes) { CalcVals = Nodes; } //while (SngValV.Len() < SngVals && CalcVals < 10*SngVals) { try { if (SngVals > 4) { TSparseSVD::SimpleLanczosSVD(GraphMtx, 2*SngVals, SngValV, false); } else { TFltVV LSingV, RSingV; // this is much more precise, but also much slower TSparseSVD::LanczosSVD(GraphMtx, SngVals, 3*SngVals, ssotFull, SngValV, LSingV, RSingV); } } catch(...) { printf("\n ***EXCEPTION: TRIED %d GOT %d values** \n", 2*SngVals, SngValV.Len()); } if (SngValV.Len() < SngVals) { printf(" ***TRIED %d GOT %d values** \n", CalcVals, SngValV.Len()); } // CalcVals += SngVals; //} } SngValV.Sort(false); //if (SngValV.Len() > SngVals) { // SngValV.Del(SngVals, SngValV.Len()-1); } //else { // while (SngValV.Len() < SngVals) SngValV.Add(1e-6); } //IAssert(SngValV.Len() == SngVals); }
void TLogRegFit::GetNewtonStep(TFltVV& HVV, const TFltV& GradV, TFltV& DeltaLV) { bool HSingular = false; for (int i = 0; i < HVV.GetXDim(); i++) { if (HVV(i,i) == 0.0) { HVV(i,i) = 0.001; HSingular = true; } DeltaLV[i] = GradV[i] / HVV(i, i); } if (! HSingular) { if (HVV(0, 0) < 0) { // if Hessian is negative definite, convert it to positive definite for (int r = 0; r < Theta.Len(); r++) { for (int c = 0; c < Theta.Len(); c++) { HVV(r, c) = - HVV(r, c); } } TNumericalStuff::SolveSymetricSystem(HVV, GradV, DeltaLV); } else { TNumericalStuff::SolveSymetricSystem(HVV, GradV, DeltaLV); for (int i = 0; i < DeltaLV.Len(); i++) { DeltaLV[i] = - DeltaLV[i]; } } } }
/// Fills the numpyvec array with TFltV vector values. /// Note that only the first n values are filled. void TFltVToNumpy(TFltV& FltV, float* FltNumpyVecOut, int n) { int limit = min(FltV.Len(), n); for (int i=0; i < limit; i++) { FltNumpyVecOut[i] = static_cast<float>(FltV[i]); } }
void GetSngVec(const PNGraph& Graph, TFltV& LeftSV, TFltV& RightSV) { const int Nodes = Graph->GetNodes(); TFltVV LSingV, RSingV; TFltV SngValV; if (Nodes < 500) { // perform full SVD TFltVV AdjMtx(Nodes+1, Nodes+1); TIntH NodeIdH; // create adjecency matrix for (TNGraph::TNodeI NodeI = Graph->BegNI(); NodeI < Graph->EndNI(); NodeI++) { NodeIdH.AddKey(NodeI.GetId()); } for (TNGraph::TNodeI NodeI = Graph->BegNI(); NodeI < Graph->EndNI(); NodeI++) { const int NodeId = NodeIdH.GetKeyId(NodeI.GetId()) + 1; for (int e = 0; e < NodeI.GetOutDeg(); e++) { const int DstNId = NodeIdH.GetKeyId(NodeI.GetOutNId(e)) + 1; // no self edges if (NodeId != DstNId) AdjMtx.At(NodeId, DstNId) = 1; } } try { // can fail to converge but results seem to be good TSvd::Svd1Based(AdjMtx, LSingV, SngValV, RSingV); } catch(...) { printf("\n***No SVD convergence: G(%d, %d)\n", Nodes, Graph->GetEdges()); } } else { // Lanczos TNGraphMtx GraphMtx(Graph); TSparseSVD::LanczosSVD(GraphMtx, 1, 8, ssotFull, SngValV, LSingV, RSingV); } TFlt MxSngVal = TFlt::Mn; int ValN = 0; for (int i = 0; i < SngValV.Len(); i++) { if (MxSngVal < SngValV[i]) { MxSngVal = SngValV[i]; ValN = i; } } LSingV.GetCol(ValN, LeftSV); RSingV.GetCol(ValN, RightSV); IsAllValVNeg(LeftSV, true); IsAllValVNeg(RightSV, true); }
PJsonVal TJsonVal::NewArr(const TFltV& FltV) { PJsonVal Val = TJsonVal::NewArr(); for (int FltN = 0; FltN < FltV.Len(); FltN++) { Val->AddToArr(TJsonVal::NewNum(FltV[FltN])); } return Val; }
void TNetInfBs::SaveObjInfo(const TStr& OutFNm) { TGnuPlot GnuPlot(OutFNm); TFltV Objective; for (THash<TIntPr, TEdgeInfo>::TIter EI = EdgeInfoH.BegI(); EI < EdgeInfoH.EndI(); EI++) { if (Objective.Len()==0) { Objective.Add(EI.GetDat().MarginalGain); } else { Objective.Add(Objective[Objective.Len()-1]+EI.GetDat().MarginalGain); } } GnuPlot.AddPlot(Objective, gpwLinesPoints); GnuPlot.SavePng(); }
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); } }
void TNNet::FeedFwd(const TFltV& InValV){ // check if number of input values same as number of input neurons EAssertR(InValV.Len() == LayerV[0].GetNeuronN() - 1, "InValV must be of equal length than the first layer!"); // assign input values to input neurons for(int InputN = 0; InputN < InValV.Len(); ++InputN){ LayerV[0].SetOutVal(InputN, InValV[InputN]); } // forward propagation for(int LayerN = 1; LayerN < LayerV.Len(); ++LayerN){ TLayer& PrevLayer = LayerV[LayerN - 1]; for(int NeuronN = 0; NeuronN < LayerV[LayerN].GetNeuronN() - 1; ++NeuronN){ LayerV[LayerN].FeedFwd(NeuronN, PrevLayer); } } }
void TLogReg::IRLS(const TMatrix& Matrix, TFltV& y, TFltV& bb, const double& ChangeEps, const int& MaxStep, const int& Verb) { IAssert(Matrix.GetCols() == y.Len()); int M = Matrix.GetRows(), R = Matrix.GetCols(), i; if (bb.Len() != M+1) { bb.Gen(M+1); bb.PutAll(0.0); } TFltV mu(R), w(R), z(R), delta; // adjust y for (i = 0; i < R; i++) { if (y[i] >= 1.0) y[i] = 0.999; else if (y[i] <= 0.0) y[i] = 0.001; } //const double eps = 0.01; double NewDEV = 0.0, OldDEV = -100.0; forever { Matrix.MultiplyT(bb, z); for (i = 0; i < R; i++) { z[i] += bb[M]; // evaluate current model mu[i] = 1/(1 + exp(-z[i])); // calculate weights w[i] = mu[i] * (1 - mu[i]); // calculate adjusted dependent variables z[i] += (y[i] - mu[i]) / w[i]; } // get new aproximation for bb CG(Matrix, w, z, bb, MaxStep, Verb); // calculate deviance (error measurement) NewDEV = 0.0; for (i = 0; i < R; i++) { double yi = y[i], mui = mu[i]; NewDEV += yi*log(yi / mui) + (1 - yi)*log((1 - yi)/(1 - mui)); } if (Verb == 1) printf(" -> %.5f\n", NewDEV); else if (Verb > 1) printf("NewDEV = %.5f\n", NewDEV); // do we stop? if (fabs(NewDEV - OldDEV) < ChangeEps) break; OldDEV = NewDEV; } }
PJsonVal TGraphCascade::GetPosterior(const TStrV& NodeNmV, const TFltV& QuantileV) const { PJsonVal Result = TJsonVal::NewObj(); TIntV NodeIdV; if (NodeNmV.Empty()) { // go over all zero timestamps for which samples exist TIntV FullNodeIdV; Graph.GetNIdV(FullNodeIdV); int Nodes = Graph.GetNodes(); for (int NodeN = 0; NodeN < Nodes; NodeN++) { int NodeId = FullNodeIdV[NodeN]; if (Timestamps.IsKey(NodeId) && Sample.IsKey(NodeId) && !Sample.GetDat(NodeId).Empty() && Timestamps.GetDat(NodeId) == 0) { NodeIdV.Add(NodeId); } } } else { int Nodes = NodeNmV.Len(); for (int NodeN = 0; NodeN < Nodes; NodeN++) { if (!NodeNmIdH.IsKey(NodeNmV[NodeN])) { continue; } int NodeId = NodeNmIdH.GetDat(NodeNmV[NodeN]); if (Timestamps.IsKey(NodeId) && Sample.IsKey(NodeId) && !Sample.GetDat(NodeId).Empty() && Timestamps.GetDat(NodeId) == 0) { NodeIdV.Add(NodeId); } } } EAssertR(QuantileV.Len() > 0, "TGraphCascade::GetPosterior quantiles should not be empty!"); for (int QuantileN = 0; QuantileN < QuantileV.Len(); QuantileN++) { EAssertR((QuantileV[QuantileN] >= 0.0) && (QuantileV[QuantileN] <= 1.0), "TGraphCascade::GetPosterior quantiles should be between 0.0 and 1.0"); } int Nodes = NodeIdV.Len(); for (int NodeN = 0; NodeN < Nodes; NodeN++) { int NodeId = NodeIdV[NodeN]; TStr NodeNm = NodeIdNmH.GetDat(NodeId); int Quantiles = QuantileV.Len(); TUInt64V SampleV = Sample.GetDat(NodeId); SampleV.Sort(true); int SampleSize = SampleV.Len(); PJsonVal QuantilesArr = TJsonVal::NewArr(); for (int QuantileN = 0; QuantileN < Quantiles; QuantileN++) { int Idx = (int)floor(QuantileV[QuantileN] * SampleSize); Idx = MIN(Idx, SampleSize - 1); uint64 UnixTimestamp = TTm::GetUnixMSecsFromWinMSecs(SampleV[Idx]); QuantilesArr->AddToArr((double)UnixTimestamp); } Result->AddToObj(NodeNm, QuantilesArr); } return Result; }
double TPartialGS::DotProdoctWithCol(const int& ColId, const TFltV& w) { Assert(w.Len() == R.Len()); int l = TInt::GetMn(ColId, R.Len()-1); double Result = 0.0; for (int i = 0; i <= l; i++) Result += R[i][ColId - i] * w[i]; return Result; }
// Inverse participation ratio: normalize EigVec to have L2=1 and then I=sum_k EigVec[i]^4 // see Spectra of "real-world" graphs: Beyond the semicircle law by Farkas, Derenyi, Barabasi and Vicsek double GetInvParticipRat(const TFltV& EigVec) { double Sum2=0.0, Sum4=0.0; for (int i = 0; i < EigVec.Len(); i++) { Sum2 += EigVec[i]*EigVec[i]; Sum4 += pow(EigVec[i].Val, 4.0); } return Sum4/(Sum2*Sum2); }