Author: Paco Hern�ndez G�mez
Miscellaneous
Uses files from the file systemIt writes information to fileIt reads information from a file
0 Views
0 Downloads
0 Favorites
pnn3
//+------------------------------------------------------------------+
//| A probabilistic neural network (PNN) implementation              |
//|                                                                  |
//|                                                          PNN.mq4 |
//|                                             Paco Hernández Gómez |
//|                                    http://www.hernandezgomez.com |
//+------------------------------------------------------------------+
#property copyright "Paco Hernández Gómez"
#property link      "http://www.hernandezgomez.com"
#property library

#define  TOTAL_CLASS   2
#define  NON_CLASS     -100
#define  CLASS_RATIO   10

double SIGMA = 0.05;



/**
 * Probabilistic neural network data is stored in an array composed by the training vectors and its classified classes.
 *
 * To optimize speed, the possible classes are numbered from 0 to n, and are store in the first position of each
 * trained vector.
 *
 * pnn[x][0] = Class where the training vector is classified (There are two possible classes (0 - Buy, 1 - Sell)).
 * pnn[x][1..n] = Training vector components classified in class pnn[x][0].
 *
 * In this example, training vectors are going to have 60 different components.
 */   
double pnn[0][61];

/**
 * Initialize PNN Vectors with 0 traning vectors.
 */
void PNNInit() {
   ArrayResize(pnn, 0);
}

/**
 * Add a training vector to the trained vectors set and classify it to a class.
 */
void PNNAddVector(int class, double vector[]) {
   // Create a new position in the array to store the new training vector and its associated class.
   int length = ArrayRange(pnn, 0);
   ArrayResize(pnn, length + 1);
   
   // Store the new training vector class
   pnn[length][0] = class;

   // Store the new training vector
   for (int i = 0; i < ArrayRange(vector, 0); i++) {
      pnn[length][i + 1] = vector[i];
   }
}

/**
 * Calculate two vectors's scalar product, needed in order to classify the vector.
 */
double euclideanScalarProduct(double p[], double q[]) {
	double euclideanScalarProduct = 0;
	
   int length = ArrayRange(p, 0);
	
	for (int i = 0; i < length; i++) {
		euclideanScalarProduct += MathPow(p[i] - q[i], 2);
	}

	return (euclideanScalarProduct);
}

/////////////////////////////////////////////////////

/**
 * Classify a vector in one class.
 */
int PNNClassifyVector(double vector[])
   {
   double length = ArrayRange(vector, 0);

   string mystr;
   double eucle , sumeucle=0;

	double result = -99999999999999999999;
	int resultClass = NON_CLASS;

   double fx[TOTAL_CLASS] = {0, 0};
   double classVectorCount[2] = {0, 0};

   for (int i = 0; i < ArrayRange(pnn, 0); i++)
      {	
      int class = pnn[i][0];

      double classVector[60];
      for (int j = 0; j < length; j++)
         {
         classVector[j] = pnn[i][j + 1];
         }

      classVectorCount[class]++;
	   
	   eucle = euclideanScalarProduct(vector, classVector);
	   sumeucle += eucle;
	   
      fx[class] += MathExp((-1) * eucle / (2 * MathPow(SIGMA, 2))) ;
      }

   double aux ;
   double myvar[TOTAL_CLASS] ;
   int    classes[TOTAL_CLASS] ;
   int    myi;
   
   for (i = 0; i < TOTAL_CLASS ; i++) classes[i] = i ;
   
   for (i = 0; i < ArrayRange(fx, 0); i++)
      {
      //fx[i] *= 1 / (MathPow(2 * 3.14159265, length / 2) * MathPow(SIGMA, length)) * (1 / classVectorCount[i]);
      fx[i] *=  ( 1 / classVectorCount[i] ) ; //* ( 1 / (MathPow(2 * 3.14159265, 0.5 ) ) ) * MathPow(SIGMA, length)) * ;
      myvar[i] = fx[i] ;
      }

   for (i = 0; i < TOTAL_CLASS; i++)
      for(j = i+1 ; j < TOTAL_CLASS ; j ++ )
         {
         if (  myvar[i] < myvar[j] )
            {
            aux = myvar[i] ;
            myvar[i] = myvar[j] ;
            myvar[j] = aux ;
               
            myi = classes[i] ;
            classes[i] = classes[j] ;
            classes[j] = myi ;
            }
         }
      
   resultClass = NON_CLASS ;
   if ((myvar[0] > CLASS_RATIO * myvar[1])&& (myvar[0] > result))
      {
      resultClass = classes[0];
      }

   mystr = mystr + "\n   euclex = " + DoubleToStr(eucle,8) + "\n   sumeucle = " + DoubleToStr(sumeucle,8);
   mystr = mystr + "\n ,fx[0] = " + DoubleToStr(fx[0],5) + "   ,fx[1] = " + DoubleToStr(fx[1],5);
   mystr = mystr + "\n  ,classVectorCount[0] = " + DoubleToStr( classVectorCount[0] , 1 ) ;
   mystr = mystr + "\n  ,classVectorCount[1] = " + DoubleToStr( classVectorCount[1] , 1 ) ;
   mystr = mystr + "\n\n  ,result = " + DoubleToStr(result,8) + "\n  Class=" + DoubleToStr(resultClass,1) ;
   
   Comment(mystr);

	return (resultClass);
}

//////////////////////////////////////////////////

/**
 * Store the trained PNN in a file
 */
void PNNSave() {
   int handle;
   
   handle = FileOpen("pnn.dat", FILE_WRITE | FILE_BIN);

   int vectorSize = ArrayRange(pnn, 1);

   for (int i = 0; i < ArraySize(pnn); i++) {
      FileWriteDouble(handle, pnn[i / vectorSize][i % vectorSize]);
   }
   
   FileClose(handle);
}

/**
 * Load a trained PNN from a file
 */
void PNNLoad() {
   int handle;
   
   handle = FileOpen("pnn.dat", FILE_READ | FILE_BIN);

   int fileSize = FileSize(handle);

   ArrayResize(pnn, fileSize / (61 * 8));
   int vectorSize = ArrayRange(pnn, 1);

   for (int i = 0; i < fileSize; i++) {
      pnn[i / vectorSize][i % vectorSize] = FileReadDouble(handle);
   }
   
   FileClose(handle);
}

Comments

Markdown supported. Formatting help

Markdown Formatting Guide

Element Markdown Syntax
Heading # H1
## H2
### H3
Bold **bold text**
Italic *italicized text*
Link [title](https://www.example.com)
Image ![alt text](image.jpg)
Code `code`
Code Block ```
code block
```
Quote > blockquote
Unordered List - Item 1
- Item 2
Ordered List 1. First item
2. Second item
Horizontal Rule ---