Ejemplo n.º 1
0
void QP
( const DistSparseMatrix<Real>& A, 
  const DistMultiVec<Real>& B, 
        DistMultiVec<Real>& X, 
  const qp::direct::Ctrl<Real>& ctrl )
{
    DEBUG_CSE

    const Int m = A.Height();
    const Int n = A.Width();
    const Int k = B.Width();
    mpi::Comm comm = A.Comm();
    DistSparseMatrix<Real> Q(comm), AHat(comm);
    DistMultiVec<Real> bHat(comm), c(comm);

    Herk( LOWER, ADJOINT, Real(1), A, Q );
    MakeHermitian( LOWER, Q );
    Zeros( AHat, 0, n );
    Zeros( bHat, 0, 1 );
    Zeros( X,    n, k );

    DistMultiVec<Real> q(comm), y(comm), z(comm);
    auto& qLoc = q.Matrix();
    auto& XLoc = X.Matrix();
    auto& BLoc = B.LockedMatrix();
    for( Int j=0; j<k; ++j )
    {
        auto xLoc = XLoc( ALL, IR(j) );
        auto bLoc = BLoc( ALL, IR(j) );

        Zeros( c, n, 1 );
        Zeros( q, m, 1 );
        qLoc = bLoc;
        Multiply( ADJOINT, Real(-1), A, q, Real(0), c );

        Zeros( q, n, 1 );
        qLoc = xLoc;
        El::QP( Q, AHat, bHat, c, q, y, z, ctrl );
        xLoc = qLoc;
    }
}
Ejemplo n.º 2
0
void QP
( const ElementalMatrix<Real>& APre, 
  const ElementalMatrix<Real>& BPre, 
        ElementalMatrix<Real>& XPre,
  const qp::direct::Ctrl<Real>& ctrl )
{
    DEBUG_CSE

    DistMatrixReadProxy<Real,Real,MC,MR>
      AProx( APre ),
      BProx( BPre );
    DistMatrixWriteProxy<Real,Real,MC,MR>
      XProx( XPre );
    auto& A = AProx.GetLocked();
    auto& B = BProx.GetLocked();
    auto& X = XProx.Get();

    const Int n = A.Width();
    const Int k = B.Width();
    const Grid& g = A.Grid();
    DistMatrix<Real> Q(g), AHat(g), bHat(g), c(g);

    Herk( LOWER, ADJOINT, Real(1), A, Q );
    Zeros( AHat, 0, n );
    Zeros( bHat, 0, 1 );
    Zeros( X,    n, k );

    DistMatrix<Real> y(g), z(g);
    for( Int j=0; j<k; ++j )
    {
        auto x = X( ALL, IR(j) );
        auto b = B( ALL, IR(j) );

        Zeros( c, n, 1 );
        Gemv( ADJOINT, Real(-1), A, b, Real(0), c );

        El::QP( Q, AHat, bHat, c, x, y, z, ctrl );
    }
}
Ejemplo n.º 3
0
double AnalyticIntegral(
	SpkModel                    &model           ,
	const std::valarray<int>    &N               , 
	const std::valarray<double> &y               ,
	const std::valarray<double> &alpha           ,
	const std::valarray<double> &L               ,
	const std::valarray<double> &U               ,
	size_t                       individual      )
{
	double Pi   = 4. * atan(1.);

	// set the fixed effects
	model.setPopPar(alpha);

	// set the individual
	model.selectIndividual(individual);

	// index in y where measurements for this individual starts
	size_t start = 0;
	size_t i;
	for(i = 0; i < individual; i++)
	{	assert( N[i] >= 0 );
		start += N[i];
	}

	// number of measurements for this individual
	assert( N[individual] >= 0 );
	size_t Ni = N[individual];

	// data for this individual
	std::valarray<double> yi = y[ std::slice( start, Ni, 1 ) ];

	// set the random effects = 0
	std::valarray<double> b(1);
	b[0] = 0.;
	model.setIndPar(b);

	std::valarray<double> Fi(Ni);
	model.dataMean(Fi);

	// model for the variance of random effects
	std::valarray<double> D(1);
	model.indParVariance(D);

	// model for variance of the measurements given random effects
	std::valarray<double> Ri( Ni * Ni );
	model.dataVariance(Ri);

	// determine bHat
	double residual = 0;
	size_t j;
	for(j = 0; j < Ni; j++)
	{	assert( Ri[j * Ni + j] == Ri[0] );
		residual += (yi[j] - Fi[j]);
	}
	std::valarray<double> bHat(1);
	bHat[0] = residual * D[0] / (Ni * D[0] + Ri[0]);

	// now evaluate the Map Bayesian objective at its optimal value
	model.setIndPar(bHat);
	model.dataMean(Fi);
	double sum = bHat[0] * bHat[0] / D[0] + log( 2. * Pi * D[0] );
	for(j = 0; j < Ni; j++)
	{	sum += (yi[j] - Fi[j]) * (yi[j] - Fi[j]) / Ri[0];
		sum += log( 2. * Pi * Ri[0] );
	}
	double GHat = .5 * sum;

	// square root of Hessian of the Map Bayesian objective
	double rootHessian = sqrt( 1. / D[0] + Ni / Ri[0]);

	// determine the values of beta that correspond to the limits
	double Gamma_L = (L[0] - bHat[0]) * rootHessian;
	double Gamma_U = (U[0] - bHat[0]) * rootHessian;

	// compute the upper tails corresponding to the standard normal
	double Q_L    = gsl_sf_erf_Q(Gamma_L);
	double Q_U    = gsl_sf_erf_Q(Gamma_U);

	// analytic value of the integral
	double factor   = exp(-GHat) * sqrt(2. * Pi) / rootHessian;
	return  factor * (Q_L - Q_U);
}
Ejemplo n.º 4
0
void RLS
( const DistSparseMatrix<Real>& A, 
  const DistMultiVec<Real>& b, 
        Real rho,
        DistMultiVec<Real>& x, 
  const socp::affine::Ctrl<Real>& ctrl )
{
    DEBUG_CSE
    const Int m = A.Height();
    const Int n = A.Width();
    mpi::Comm comm = A.Comm();

    DistMultiVec<Int> orders(comm), firstInds(comm);
    Zeros( orders, m+n+3, 1 );
    Zeros( firstInds, m+n+3, 1 );
    {
        const Int localHeight = orders.LocalHeight();
        for( Int iLoc=0; iLoc<localHeight; ++iLoc )
        {
            const Int i = orders.GlobalRow(iLoc);
            if( i < m+1 )
            {
                orders.SetLocal( iLoc, 0, m+1 );
                firstInds.SetLocal( iLoc, 0, 0 );
            }
            else
            {
                orders.SetLocal( iLoc, 0, n+2 ); 
                firstInds.SetLocal( iLoc, 0, m+1 );
            }
        }
    }

    // G := | -1  0  0 |
    //      |  0  0  A |
    //      |  0 -1  0 |
    //      |  0  0 -I |
    //      |  0  0  0 |
    DistSparseMatrix<Real> G(comm);
    {
        Zeros( G, m+n+3, n+2 );

        // Count the number of entries of G to reserve
        // -------------------------------------------
        Int numLocalUpdates = 0;
        const Int localHeight = G.LocalHeight();
        for( Int iLoc=0; iLoc<localHeight; ++iLoc )
        {
            const Int i = G.GlobalRow(iLoc);
            if( i == 0 || i == m+1 || (i>m+1 && i<m+n+2) )
                ++numLocalUpdates;
        }
        const Int numEntriesA = A.NumLocalEntries();
        G.Reserve( numLocalUpdates+numEntriesA, numEntriesA );

        // Queue the local updates
        // ----------------------- 
        for( Int iLoc=0; iLoc<localHeight; ++iLoc )
        {
            const Int i = G.GlobalRow(iLoc);
            if( i == 0 )
                G.QueueLocalUpdate( iLoc, 0, -1 );
            else if( i == m+1 )
                G.QueueLocalUpdate( iLoc, 1, -1 );
            else if( i > m+1 && i < m+n+2 )
                G.QueueLocalUpdate( iLoc, i-m, -1 );
        }

        // Queue the remote updates
        // ------------------------
        for( Int e=0; e<numEntriesA; ++e )
            G.QueueUpdate( A.Row(e)+1, A.Col(e)+2, A.Value(e) );

        G.ProcessQueues();
    }

    // h := | 0 |
    //      | b |
    //      | 0 |
    //      | 0 |
    //      | 1 |
    DistMultiVec<Real> h(comm);
    Zeros( h, m+n+3, 1 ); 
    auto& bLoc = b.LockedMatrix();
    {
        const Int bLocalHeight = b.LocalHeight();
        h.Reserve( bLocalHeight );
        for( Int iLoc=0; iLoc<bLocalHeight; ++iLoc )
            h.QueueUpdate( b.GlobalRow(iLoc)+1, 0, bLoc(iLoc) );
        h.ProcessQueues();
    }
    h.Set( END, 0, 1 );

    // c := [1; rho; 0]
    DistMultiVec<Real> c(comm);
    Zeros( c, n+2, 1 );
    c.Set( 0, 0, 1 );
    c.Set( 1, 0, rho );

    DistSparseMatrix<Real> AHat(comm);
    Zeros( AHat, 0, n+2 );
    DistMultiVec<Real> bHat(comm);
    Zeros( bHat, 0, 1 );

    DistMultiVec<Real> xHat(comm), y(comm), z(comm), s(comm);
    SOCP( AHat, G, bHat, c, h, orders, firstInds, xHat, y, z, s, ctrl );
    x = xHat( IR(2,END), ALL );
}
Ejemplo n.º 5
0
void RLS
( const ElementalMatrix<Real>& APre, 
  const ElementalMatrix<Real>& bPre, 
        Real rho,
        ElementalMatrix<Real>& xPre,
  const socp::affine::Ctrl<Real>& ctrl )
{
    DEBUG_CSE

    DistMatrixReadProxy<Real,Real,MC,MR>
      AProx( APre ),
      bProx( bPre );
    DistMatrixWriteProxy<Real,Real,MC,MR>
      xProx( xPre );
    auto& A = AProx.GetLocked();
    auto& b = bProx.GetLocked();
    auto& x = xProx.Get();

    const Int m = A.Height();
    const Int n = A.Width();
    const Grid& g = A.Grid();

    DistMatrix<Int,VC,STAR> orders(g), firstInds(g);
    Zeros( orders, m+n+3, 1 );
    Zeros( firstInds, m+n+3, 1 );
    {
        const Int localHeight = orders.LocalHeight();
        for( Int iLoc=0; iLoc<localHeight; ++iLoc )
        {
            const Int i = orders.GlobalRow(iLoc);
            if( i < m+1 )
            {
                orders.SetLocal( iLoc, 0, m+1 );
                firstInds.SetLocal( iLoc, 0, 0 );
            }
            else
            {
                orders.SetLocal( iLoc, 0, n+2 );
                firstInds.SetLocal( iLoc, 0, m+1 );
            }
        }
    }

    // G := | -1  0  0 |
    //      |  0  0  A |
    //      |  0 -1  0 |
    //      |  0  0 -I |
    //      |  0  0  0 |
    DistMatrix<Real> G(g);
    {
        Zeros( G, m+n+3, n+2 );
        G.Set( 0, 0, -1 );
        auto GA = G( IR(1,m+1), IR(2,n+2) );
        GA = A;
        G.Set( m+1, 1, -1 );
        auto GI = G( IR(m+2,m+n+2), IR(2,n+2) );
        Identity( GI, n, n );
        GI *= -1;
    }

    // h := | 0 |
    //      | b |
    //      | 0 |
    //      | 0 |
    //      | 1 |
    DistMatrix<Real> h(g);
    Zeros( h, m+n+3, 1 );
    auto hb = h( IR(1,m+1), ALL );
    hb = b;
    h.Set( END, 0, 1 );

    // c := [1; rho; 0]
    DistMatrix<Real> c(g);
    Zeros( c, n+2, 1 );
    c.Set( 0, 0, 1 );
    c.Set( 1, 0, rho );

    DistMatrix<Real> AHat(g), bHat(g);
    Zeros( AHat, 0, n+2 );
    Zeros( bHat, 0, 1 );

    DistMatrix<Real> xHat(g), y(g), z(g), s(g);
    SOCP( AHat, G, bHat, c, h, orders, firstInds, xHat, y, z, s, ctrl );
    x = xHat( IR(2,END), ALL );
}