<commit_before>#ifndef CDIST_H
#define CDIST_H
#include <cmath>
#include <vector>
#include "CMatrix.h"
#include "CTransform.h"
#include "ndlutil.h"


const string DISTVERSION="0.1";
class CDist : public CTransformable {
  
 public:
  CDist(){}
  ~CDist(){}
  const int getNumParams() const
    {
      return nParams;
    }
  void setNumParams(const int num)
    {
      nParams = num;
    }
  virtual double getParam(const int paramNo) const=0;
  virtual void setParam(const double val, const int paramNo)=0;
  virtual void getGradParams(CMatrix& g) const
    {
      // This is a dummy function
      cerr << "getGradParams should not be used in CDist" << endl;
      exit(1);
    }
  virtual void writeParamsToStream(ostream& out) const;
  virtual void readParamsFromStream(istream& in);
  //CDist(CDist& dist);
  virtual double getGradInput(double x) const=0;
  void setInitParam();

  virtual double logProb(double val) const=0;
  void setParamName(const string name, const int index)
    {
      assert(index>=0);
      assert(index<nParams);
      if(paramNames.size() == index)
	paramNames.push_back(name);
      else 
	{
	  if(paramNames.size()<index)
	    paramNames.resize(index+1, "no name");
	  paramNames[index] = name;
	}
    }
  virtual string getParamName(const int index) const
    {
      assert(index>=0);
      assert(index<paramNames.size());
      return paramNames[index];
    }

  void setType(string name)
    {
      type = name;
    }
  string getType() const
    {
      return type;
    }
  void setName(string name)
    {
      distName = name;
    }
  string getName() const
    {
      return distName;
    }
  
 private:
  int nParams;
  string type;
  string distName;
  vector<string> paramNames;
};

void writeDistToStream(const CDist& dist, ostream& out);
CDist* readDistFromStream(istream& in);

class CGaussianDist : public CDist {

 public:
  CGaussianDist();
  CGaussianDist(const CGaussianDist&);
  ~CGaussianDist();
  CGaussianDist* clone() const
    {
      return new CGaussianDist(*this);
    }
  double getParam(const int paramNo) const;
  void setParam(const double val, const int paramNo);
  double getGradInput(double x) const;
  void setInitParam();
  double logProb(double val) const;

 private:
  double precision;
};


class CGammaDist : public CDist {

 public:
  CGammaDist();
  CGammaDist(const CGammaDist&);
  ~CGammaDist();
  CGammaDist* clone() const
    {
      return new CGammaDist(*this);
    }
  double getParam(const int paramNo) const;
  void setParam(const double val, const int paramNo);
  double getGradInput(double x) const;
  void setInitParam();
  double logProb(double val) const;

 private:
  double a;
  double b;
};

class CParamPriors : CMatinterface {
  
 public:
#ifdef _NDLMATLAB
  mxArray* toMxArray() const;
  void fromMxArray(const mxArray* distArray);
#endif
  void addDist(CDist* dist, const int index)
    {
      assert(index>=0);
      distIndex.push_back(index);
      dists.push_back(dist);
    }

  void clearDists()
    {
      distIndex.clear();
      dists.clear();
    }
  inline string getDistType(const int ind) const
    {
      assert(ind>=0);
      assert(ind<getNumDists());
      return dists[ind]->getType();
    }
  inline int getDistIndex(const int ind) const
    {
      assert(ind>=0);
      assert(ind<getNumDists());
      return distIndex[ind];
    }
  inline const int getNumDists() const
    {
      return dists.size();
    }
  vector<CDist*> dists;
  vector<int> distIndex;

};


class CRegularisable {

 public:

  // these are the pure virtual functions.
  virtual const int getNumParams() const=0;
  virtual double getParam(const int paramNo) const=0;
  virtual void setParam(const double val, const int paramNo)=0;
  virtual void getGradParams(CMatrix& g) const=0;

  // these are default implementations.
  virtual void getParams(CMatrix& params) const
    {<commit_msg>General commit.<commit_after>#ifndef CDIST_H
#define CDIST_H
#include <cmath>
#include <vector>
#include "CMatrix.h"
#include "CTransform.h"
#include "ndlutil.h"
              

const string DISTVERSION="0.1";

// Base distribution class.
class CDist : public CTransformable {
  
 public:
  CDist(){}
  ~CDist(){}
  const int getNumParams() const
    {
      return nParams;
    }
  void setNumParams(const int num)
    {
      nParams = num;
    }
  virtual double getParam(const int paramNo) const=0;
  virtual void setParam(const double val, const int paramNo)=0;
  virtual void getGradParams(CMatrix& g) const
    {
      // This is a dummy function
      cerr << "getGradParams should not be used in CDist" << endl;
      exit(1);
    }
  virtual void writeParamsToStream(ostream& out) const;
  virtual void readParamsFromStream(istream& in);
  //CDist(CDist& dist);
  // get the gradient with respect to an input.
  virtual double getGradInput(double x) const=0;
  void setInitParam();
  // Get log probability at a particualar value
  virtual double logProb(double val) const=0;
  void setParamName(const string name, const int index)
    {
      assert(index>=0);
      assert(index<nParams);
      if(paramNames.size() == index)
	paramNames.push_back(name);
      else 
	{
	  if(paramNames.size()<index)
	    paramNames.resize(index+1, "no name");
	  paramNames[index] = name;
	}
    }
  virtual string getParamName(const int index) const
    {
      assert(index>=0);
      assert(index<paramNames.size());
      return paramNames[index];
    }

  void setType(string name)
    {
      type = name;
    }
  string getType() const
    {
      return type;
    }
  void setName(string name)
    {
      distName = name;
    }
  string getName() const
    {
      return distName;
    }
  
 private:
  int nParams;
  string type;
  string distName;
  vector<string> paramNames;
};

// Read and write dists to streams
void writeDistToStream(const CDist& dist, ostream& out);
CDist* readDistFromStream(istream& in);

// The Gaussian distribution.
class CGaussianDist : public CDist {

 public:
  CGaussianDist();
  CGaussianDist(const CGaussianDist&);
  ~CGaussianDist();
  CGaussianDist* clone() const
    {
      return new CGaussianDist(*this);
    }
  double getParam(const int paramNo) const;
  void setParam(const double val, const int paramNo);
  double getGradInput(double x) const;
  void setInitParam();
  double logProb(double val) const;

 private:
  double precision;
};

// The gamma distribution
class CGammaDist : public CDist {

 public:
  CGammaDist();
  CGammaDist(const CGammaDist&);
  ~CGammaDist();
  CGammaDist* clone() const
    {
      return new CGammaDist(*this);
    }
  double getParam(const int paramNo) const;
  void setParam(const double val, const int paramNo);
  double getGradInput(double x) const;
  void setInitParam();
  double logProb(double val) const;

 private:
  double a;
  double b;
};
// A class which stores distributions in a container for priors over parameters.
class CParamPriors : CMatinterface {
  
 public:
#ifdef _NDLMATLAB
  mxArray* toMxArray() const;
  void fromMxArray(const mxArray* distArray);
#endif
  void addDist(CDist* dist, const int index)
    {
      assert(index>=0);
      distIndex.push_back(index);
      dists.push_back(dist);
    }

  void clearDists()
    {
      distIndex.clear();
      dists.clear();
    }
  inline string getDistType(const int ind) const
    {
      assert(ind>=0);
      assert(ind<getNumDists());
      return dists[ind]->getType();
    }
  inline int getDistIndex(const int ind) const
    {
      assert(ind>=0);
      assert(ind<getNumDists());
      return distIndex[ind];
    }
  inline const int getNumDists() const
    {
      return dists.size();
    }
  vector<CDist*> dists;
  vector<int> distIndex;

};

// A virtual base class which makes its descendents regluarisable.
class CRegularisable {

 public:

  // these are the pure virtual functions.
  virtual const int getNumParams() const=0;
  virtual double getParam(const int paramNo) const=0;
  virtual void setParam(const double val, const int paramNo)=0;
  virtual void getGradParams(CMatrix& g) const=0;

  // these are default implementations.
  virtual void getParams(CMatrix& params) const
    {
{'fraction_non_alphanumeric': 0.10713410149546458, 'fraction_numerical': 0.004167688158862467, 'mean_word_length': 3.262800417972832, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1139574', 'n_tokens_mistral': 2673, 'n_tokens_neox': 2613, 'n_words': 736}