Example #1
0
/** negative log likelihood of negative binomial with mean and tau 
\brief Negative binomial with mean=mu and variance = mu*tau
\author Mollie Brooks
\param x observed counts
\param mu is the predicted mean
\param tau is the overdispersion parameter like in the quasi-poisson. should be >1
\return negative log likelihood \f$ -( \ln(\Gamma(x+k))-\ln(\Gamma(k))-\ln(x!)+k\ln(k)+x\ln(\mu)-(k+x)\ln(k+\mu) )\f$
where \f$ k=\mu/(10^{-120}+\tau-1.0) \f$
\ingroup STATLIB
**/
dvariable dnbinom_tau(const dvector& x, const dvar_vector& mu, const dvar_vector& tau)
{
	//the observed counts are in x
	//mu is the predicted mean
	//tau is the overdispersion parameter
	RETURN_ARRAYS_INCREMENT();
	int i,imin,imax;
	imin=x.indexmin();
	imax=x.indexmax();
	dvariable loglike;
	loglike=0.;

	for(i = imin; i<=imax; i++)
	{
		if (value(tau(i))<1.0)
		{
			cerr<<"tau("<<i<<") is <=1.0 in dnbinom_tau()";
			return(0.0);
		}
		
		loglike += log_negbinomial_density(x(i), mu(i), tau(i));
	}
	RETURN_ARRAYS_DECREMENT();
	return(-loglike);
}
Example #2
0
/**
 * Description not yet available.
 * \param
 */
void lvector::fill_multinomial(const int& seed, const dvector& p)
  // Fils a dvector with random numbers drawn from a multinomial distribution
  {
    double sum=mean(p)*p.size();
    int pmin=p.indexmin();
    int pmax=p.indexmax();
    dvector tmp(pmin,pmax);
    dvector tmp1(pmin,pmax);
    dvector choose(indexmin(),indexmax());
    choose.fill_randu(seed);
    tmp=p/sum;
    tmp1(pmin)=tmp(pmin);
    for (int j=pmin+1;j<=pmax-1;j++)
    {
      tmp1(j)=tmp1(j-1)+tmp(j);
    }
    tmp1(pmax)=1.0;

    for (int i=indexmin();i<=indexmax();i++)
    {
      int j=pmin;
      while (choose(i)>tmp1(j))
      {
        j++;
      }
      (*this)(i)=j;
    }
  }
Example #3
0
/**
 * Description not yet available.
 * \param
 */
dvector solve_trans(const lower_triangular_dmatrix& M,const dvector& y)
{
  int mmin=M.indexmin();
  int mmax=M.indexmax();

  if (y.indexmin() !=mmin || y.indexmax() !=mmax)
  {
    cerr << "incompatible size in solve_trans" << endl;
    ad_exit(1);
  }
  dvector x(mmin,mmax);
  int i,j;

  for (i=mmax;i>=mmin;i--)
  {
    double sum=0.0;
    for (j=i+1;j<=mmax;j++)
    {
      sum+=M(j,i)*x(j);
    }
    x(i)=(y(i)-sum)/M(i,i);
  }

  return x;
}
Example #4
0
/**
Determine if the lower and upper bounds of two evctors match in a specified
function.
\param v1 a data vector
\param v2 a data vector
\param function_nam a pointer to the name of the function in question.
*/
void shape_check(const dvector& v1, const dvector& v2,
  const char *function_name)
{
  if (v1.indexmin() != v2.indexmin() || v1.indexmax() != v2.indexmax())
  {
    cerr << " Vector sizes do no match in" << function_name << "\n";
    ad_exit(1);
  }
}
Example #5
0
/**
Return the total sum of the elements in values.
\param values dvector
*/
double sum(const dvector& values)
{
  double total = 0.0;
  for (int i = values.indexmin(); i <= values.indexmax(); ++i)
  {
    total += values.elem(i);
  }
  return total;
}
Example #6
0
/**
Return dvector results of squaring elements in a values;
constant vector object.

\ingroup misc
\param values of constant object to be squared.
\return vector of the same length as #values containing \f$values_i^2\f$
*/
dvector square(const dvector& values)
{
  dvector results;
  results.allocate(values);
  for (int i = values.indexmin(); i <= values.indexmax(); ++i)
  {
    results(i) = square(values(i));
  }
  return results;
}
Example #7
0
matrix::matrix(const dvector &vec, bool isRow)
	: data(vector<dvector>(isRow? 1: vec.size())), colNum(isRow? vec.size(): 1) {
		if(isRow) {
			data.front() = vec;
		} else {
			for(long i = 0; i < (signed) vec.size(); i++) {
				data.at(i).resize(1);
				data.at(i).front() = vec.at(i);
			}
		}
}
Example #8
0
dvector cubic_spline_function::operator () (const dvector& u)
{
  int mmin=u.indexmin();
  int mmax=u.indexmax();
  dvector z(mmin,mmax);
  for (int i=mmin;i<=mmax;i++)
  {
    z(i)=splint(x,y,y2,u(i));
  }
  return z;
}
Example #9
0
/**
Return ivector filled with flags for 1 positive and -1 negative
for values in dvector v.
*/
ivector sgn(const dvector& v)
{
  int mmin = v.indexmin();
  int mmax = v.indexmax();
  ivector ret(mmin, mmax);
  for (int i = mmin; i <= mmax; ++i)
  {
    ret(i)= v(i) > 0 ? 1 : -1;
  }
  return ret;
}
Example #10
0
dvariable robust_regression(dvector& obs, dvar_vector& pred, 
  const double& cutoff) 
{
  if (obs.indexmin() != pred.indexmin() || obs.indexmax() != pred.indexmax() )
  {
    cerr << "Index limits on observed vector are not equal to the Index\n\
 limits on the predicted vector in robust_reg_likelihood function\n";
  }
  RETURN_ARRAYS_INCREMENT(); //Need this statement because the function
			     //returns a variable type
  int min=obs.indexmin();
  int max=obs.indexmax();
  dvariable sigma_hat;
  dvariable sigma_tilde; 
  int nobs = max-min+1;
  double width=3.0;
  double pcon=0.05;
  double width2=width*width;
  dvariable zpen;
  zpen=0.0;
  double a,a2;
  a=cutoff; 
     // This bounds a between 0.05 and 1.75
  a2=a*a;
  dvariable tmp,tmp2,tmp4,sum_square,v_hat;
  dvar_vector diff_vec = obs-pred;
  tmp = norm(diff_vec);
  sum_square = tmp * tmp;
  v_hat = 1.e-80 + sum_square/nobs;
  sigma_hat=pow(v_hat,.5);
  sigma_tilde=a*sigma_hat;
  double b=2.*pcon/(width*sqrt(PI));
  dvariable log_likelihood;
  dvariable div1;
  dvariable div2,div4;
  div1 = 2*(a2*v_hat);
  div2 = width2*(a2*v_hat);
  div4 = div2*div2;
  log_likelihood = 0;
  for (int i=min; i<=max; i++)
  {
    tmp=diff_vec[i];
    tmp2=tmp*tmp;
    tmp4=tmp2*tmp2;
    log_likelihood -= log((1-pcon)*exp(-tmp2/div1)+b/(1.+tmp4/div4) );
  }
  log_likelihood += nobs*log(a2*v_hat)/2.;
  log_likelihood += zpen;
  RETURN_ARRAYS_DECREMENT(); // Need this to decrement the stack increment
			     // caused by RETURN_ARRAYS_INCREMENT();
  return(log_likelihood);  
}
Example #11
0
/**
 * Description not yet available.
 * \param
 */
dmatrix outer_prod(const dvector& v1, const dvector& v2)
{
    dmatrix tmp(v1.indexmin(),v1.indexmax(), v2.indexmin(), v2.indexmax() );

    for (int i=v1.indexmin(); i<=v1.indexmax(); i++)
    {
        for (int j=v2.indexmin(); j<=v2.indexmax(); j++)
        {
            tmp.elem(i,j)=v1.elem(i)*v2.elem(j);
        }
    }
    return(tmp);
}
Example #12
0
/**
Construct ivector with same dimensions as u.
*/
ivector::ivector(const dvector& u)
{
  allocate(u);
  for (int i=indexmin();i<=indexmax();i++)
  {
#ifdef OPT_LIB
    elem(i) = static_cast<int>(u.elem(i));
#else
    double ui = u.elem(i);
    assert(ui <= INT_MAX);
    elem(i) = static_cast<int>(ui);
#endif
  }
}
  bool brightRGB::getMax(const image& img,dvector& dest) const{

    // image empty?
    if (img.empty()) {
      setStatusString("image empty");
      dest.resize(0);
      return false;
    }

    const rgbPixel transColor = getParameters().transColor;
    ivector maxV(3,-1);
    image::const_iterator it = img.begin();
    if(getParameters().transparent) {
      while(it != img.end()) {
	if(*it != transColor) {
	  if((*it).getRed() > maxV.at(0))
	    maxV.at(0) = (*it).getRed();
	  if((*it).getGreen() > maxV.at(1))
	    maxV.at(1) = (*it).getGreen();
	  if((*it).getBlue() > maxV.at(2))
	    maxV.at(2) = (*it).getBlue();
	}
	it++;
      }
      // only transparent pixels?
      if (maxV.at(0)==-1) {
        setStatusString("only transparent pixels");
        dest.resize(0);
        return false;
      }
    } else { // no transparent color
      while(it != img.end()) {
	if((*it).getRed() > maxV.at(0))
	  maxV.at(0) = (*it).getRed();
	if((*it).getGreen() > maxV.at(1))
	  maxV.at(1) = (*it).getGreen();
	if((*it).getBlue() > maxV.at(2))
	  maxV.at(2) = (*it).getBlue();
	it++;
      }
    }
    if(maxV.at(0) == -1)
      return false;
    dest.castFrom(maxV);
    // normalize to 0..1
    dest.divide(255);
    return true;
  };
Example #14
0
/**
 * Description not yet available.
 * \param
 */
double ghk(const dvector& lower,const dvector& upper,const dmatrix& Sigma,
  const dmatrix& eps,int _i)
{
  int n=lower.indexmax();
  dmatrix ch=choleski_decomp(Sigma);
  dvector l(1,n);
  dvector u(1,n);

  ghk_test(eps,_i); // test for valid i range
  double weight=1.0;
  int k=_i;
  {
    l=lower;
    u=upper;
    for (int j=1;j<=n;j++)
    {
      l(j)/=ch(j,j);
      u(j)/=ch(j,j);
      double Phiu=cumd_norm(u(j));
      double Phil=cumd_norm(l(j));
      weight*=Phiu-Phil;
      double eta=inv_cumd_norm((Phiu-Phil)*eps(k,j)+Phil);
      for (int i=j+1;i<=n;i++)
      {
        double tmp=ch(i,j)*eta;
        l(i)-=tmp;
        u(i)-=tmp;
      }
    }
  }
  return weight;
}
Example #15
0
  bool MLP::calcGradient(const dmatrix& inputs,
                         const ivector& ids,
                         dvector& grad) {

    if (inputs.rows() != ids.size()) {
      setStatusString("Number of vectors not consistent with number of ids");
      return false;
    }

    dvector tmp;
    int i;
    double tmpError;

    totalError = 0;
    calcGradient(inputs.getRow(0),ids.at(0),grad);
    computeActualError(ids.at(0),totalError);

    for (i=1;i<inputs.rows();++i) {
      calcGradient(inputs.getRow(i),ids.at(i),tmp);
      computeActualError(ids.at(i),tmpError);
      grad.add(tmp);
      totalError+=tmpError;
    }

    return true;
  }
Example #16
0
/**
 * Description not yet available.
 * \param
 */
double ghk(const dvector& lower,const dvector& upper,const dmatrix& Sigma,
  const dmatrix& eps)
{
  int m=eps.indexmax();
  int n=lower.indexmax();
  double ssum=0.0;
  dmatrix ch=choleski_decomp(Sigma);
  dvector l(1,n);
  dvector u(1,n);

  for (int k=1;k<=m;k++)
  {
    double weight=1.0;
    l=lower;
    u=upper;
    for (int j=1;j<=n;j++)
    {
      l(j)/=ch(j,j);
      u(j)/=ch(j,j);
      double Phiu=cumd_norm(u(j));
      double Phil=cumd_norm(l(j));
      weight*=Phiu-Phil;
      double eta=inv_cumd_norm((Phiu-Phil)*eps(k,j)+Phil);
      for (int i=j+1;i<=n;i++)
      {
        double tmp=ch(i,j)*eta;
        l(i)-=tmp;
        u(i)-=tmp;
      }
    }
    ssum+=weight;
  }
  return ssum/m;
}
Example #17
0
void param_init_bounded_matrix_vector::set_scalefactor(const dvector& s)
{
  int mmin=indexmin();
  int mmax=indexmax();
  if (s.indexmin()!=mmin || s.indexmax() != mmax)
  {
    cerr << "non matching vector bounds in"
     " init_bounded_matrix_vector::set_scalefactor" << endl;
    ad_exit(1);
  }

  for (int i=mmin;i<=mmax;i++)
  {
    (*this)(i).set_scalefactor(s(i));
  }
}
Example #18
0
    inline void copy_from_host(dvector<bool> &out, const std::vector<bool> &in)
    {
        std::vector<unsigned char> temp;
        temp.assign(in.begin(), in.end());

        out.copy_from_host((const bool *)&temp[0], temp.size());
    }
Particle::Particle(const dvector& pos, const prob_t fitness)
{
    mPos = pos;
    mVel.resize(pos.size());
    mPBestPos = pos;
    mFitness  = fitness;
    mPBestFitness = fitness;
}
Example #20
0
/**
 * Description not yet available.
 * \param
 */
 dvar_vector::dvar_vector(const dvector& t)
 {
   if (!t)
   {
     allocate();
   }
   else
   {
     va=NULL;
     allocate(t.indexmin(),t.indexmax());
     initialize();
     for (int i = indexmin(); i <= indexmax(); i++)
     {
       va[i].x=(t.v)[i];
     }
   }
 }
Example #21
0
   /*
    * compute the error using the last propagated input and the given
    * pattern
    */
  bool MLP::computePatternError(const int id,
                                const dvector& outUnits,
                                double& error) const {

    const int lastIdx = outUnits.size();
    int j;

    double tmp;
    error = 0.0;
    for (j=0;j<lastIdx;++j) {
      tmp = (outUnits.at(j)-((j==id)?on:off));
      error += tmp*tmp;
    }

    error *= 0.5;

    return true;
  }
Example #22
0
    inline void copy_to_host(std::vector<bool> &out, const dvector<bool> &in)
    {
        bool *temp = NULL;
        try
        {
            temp = new bool[in.size()];

            in.copy_to_host(temp, in.size());

            out.assign(temp, temp + in.size());
            delete[] temp;
        }
        catch(...)
        {
            delete[] temp;
            throw;
        }
    }
Example #23
0
 void pvm_number::assign(const dvector& u)
 {
   if(ad_comm::pvm_manager)
   {
     int nsp=ad_comm::pvm_manager->num_slave_processes;
     if (u.indexmin() !=0 || u.indexmax() != nsp)
     {
       cerr << "Error in pvm_number::assign  valid index bounds must be 0 "
            << ad_comm::pvm_manager->num_slave_processes << endl;
       ad_exit(1);
     }
     if (allocated(v))
       v.deallocate();
     v.allocate(0,nsp);
     v=u;
     d=u(0);
   }
 }
Example #24
0
matrix::matrix(long rows, long cols, const dvector &value)
	: data(vector<dvector>(rows)), colNum(cols) {
		for(long i=0; i<rows; i++){
		   data.at(i).resize(cols);
		   for(long j=0; j<cols; j++){
		      data.at(i).at(j) = value.at(i * cols + j);
		   
		   }
		}
}
  bool brightRGB::getAverage(const image& img,dvector& dest) const{

    const rgbPixel transColor = getParameters().transColor;
    dvector avg(3,0.0);
    image::const_iterator it = img.begin();
    // check for empty image
    if (img.columns()==0 || img.rows()==0) {
      setStatusString("image empty");
      dest.resize(0);
      return false;
    }
    if(getParameters().transparent) {
      int counter = 0;
      while(it != img.end()) {
	if(*it != transColor) {
	  avg.at(0) += (*it).getRed();
	  avg.at(1) += (*it).getGreen();
	  avg.at(2) += (*it).getBlue();
	  ++counter;
	}
	it++;
      }
      // check for completely transparent image
      if (counter==0) {
        setStatusString("only transparent pixels");
        dest.resize(0);
        return false;
      }
      avg.divide(counter);
    } else { // no transparent color
      while(it != img.end()) {
	avg.at(0) += (*it).getRed();
	avg.at(1) += (*it).getGreen();
	avg.at(2) += (*it).getBlue();
	it++;
      }
      avg.divide(img.columns()*img.rows());
    }
    // values between 0 and 1
    dest.divide(avg, 255.);
    return true;
  };
Example #26
0
/**
 * Description not yet available.
 * \param
 */
void nograd_assign_row(const dvar_matrix& m, const dvector& v, const int& ii)
{
  // cout << "Entering nograd assign"<<endl;
  //kkludge_object kg;
  if (ii<m.rowmin()||ii>m.rowmax()  ||
   (v.indexmin()!=m(ii).indexmin()) ||
   (v.indexmax()!=m(ii).indexmax()) )
  {
    cerr << "Error -- Index out of bounds in\n"
    "void nograd_assign(const dvar_matrix& m, const dvector& v, const int& ii)"
    << endl;
    ad_exit(1);
  }
  int min=v.indexmin();
  int max=v.indexmax();
  for (int j=min;j<=max;j++)
  {
    value(m(ii,j))=v(j);
  }
    // out(i)=nograd_assign(m(i));
  }
Example #27
0
inline void MACD(int short_window, int long_window, int smooth_window, dvector &input, dvector &macd, dvector &macd_signal, dvector &macd_hist)
{
	std::vector<double> short_emas;
	std::vector<double> long_emas;
	short_emas.reserve(input.size());
	long_emas.reserve(input.size());
	macd.reserve(input.size());
	macd_signal.reserve(input.size());
	macd_hist.reserve(input.size());
	EMA(short_window,input,short_emas);
	EMA(long_window,input,long_emas);
	SUBTRACT(short_emas,long_emas,macd);
	EMA(smooth_window,macd,macd_signal);
	SUBTRACT(macd,macd_signal,macd_hist);
}
Example #28
0
/**
 * @brief Return molt increment matrix based on empirical data
 * @details Fit's a cubic spline to the empirical data.
 * Note that the spline function strictly requires increasing
 * values for each of the knots.
 * 
 * @param data [description]
 * @return dmatrix of molt increments by sex for each size bin
 */
dmatrix get_empirical_molt_increment(const dvector& bin, const dmatrix& data)
{
	cout<<"In get_empirical_molt_increment"<<endl;
	int n = bin.size();
	ivector sex = ivector(column(data,2));
	int nsex = count_factor(sex);

	dmatrix mi(1,nsex,1,n);
	ivector nh(1,nsex); nh.initialize();
	
	// Count number of observations in each sex.
	for (int i = 1; i <= data.rowmax(); ++i)
	{
		int h = sex(i);
		nh(h) ++;
	}
	
	// get male and famale arrays
	dmatrix x(1,nsex,1,nh);
	dmatrix y(1,nsex,1,nh);
	int bb=1; 
	int gg=1;
	for (int i = 1; i <= data.rowmax(); ++i)
	{
		int h = sex(i);
		int j = h==1 ? bb++ : gg++ ;
		
		x(h,j) = data(i,1);
		y(h,j) = data(i,3);	
	}



	// rescale size to 0-1 over bin width
	for (int h = 1; h <= nsex; ++h)
	{
		dvector knts = (x(h) - min(x(h))) / (max(x(h)) - min(x(h)));
		dvector pnts = (bin  - min(bin)) / (max(bin) - min(bin));
		COUT(knts);
		COUT(y(h));
		cubic_spline_function cSmooth(knts,y(h));
		dvector test = cSmooth(pnts);
		COUT(cSmooth(0.5));
		COUT(test);
	}
	
	cout<<"leaving get_empirical_molt_increment"<<endl;
	return mi;

}
Example #29
0
  /**
  \ingroup matop
  Element-wise division of two vectors; constant objects.
  Exits with error if bounds of the two arguments differ.
  \param t1 A vector, \f$u\f$ with valid subscripts in \f$[i_1,i_n]\f$
  \param t2 A vector, \f$v\f$ with valid subscripts in \f$[i_1,i_n]\f$
  \return A vector containing \f$z_i = u_i\div v_i; [i_1,i_n]\f$.
  */
dvector elem_div(const dvector& t1, const dvector& t2)
  {
     if (t1.indexmin() != t2.indexmin() ||  t1.indexmax() != t2.indexmax())
     {
       cerr << "Index bounds do not match in "
       "dvector elem_div(const dvector&, const dvector&)\n";
       ad_exit(1);
     }
     dvector tmp(t1.indexmin(),t1.indexmax());

#ifndef USE_ASSEMBLER
     for (int i=t1.indexmin(); i<=t1.indexmax(); i++)
     {
       tmp[i]=t1[i]/t2[i];
     }
#else
     int min=t1.indexmin();
     int n=t1.indexmax()-min+1;
     dp_vector_elem_prod(&(tmp(min)),&(t1(min)),&(t2(min)),n);
#endif

     return(tmp);
  }
Example #30
0
void stop()
{
    if(f)
    {
        if(demorecording) gzputi(-1);
        gzclose(f);
    };
    f = NULL;
    demorecording = false;
    demoplayback = false;
    demoloading = false;
    loopv(playerhistory) zapdynent(playerhistory[i]);
    playerhistory.setsize(0);
};