Category Archives: math

Pondering via the web

This morning got up early and sat at my desk while having a coffee. I got multiple windows up on my multi 24″ monitor setup and my adequate quadcore Windows personal computer. Perused the latest news and tech stories.

Then I opened my Feedly account and looked at my Science feeds. “The Reference Frame” has a new post. I like that blog. Most of the time I don’t know what he is writing about, and the anti-climate change diatribes sound like Fox News, but in all it has some tidbits for us science aficionados. Like today, he is discussing something about intelligence. Which leads me to recall that there was another attempt to present an “equation” of intelligence by another sciencecy type, Bart Kosko. Bart was discussing creativity. Creativity is the measure of the number of results or responses to the same number of data or stimulus, or something like that.

So, I searched for the Kosko reference. Couldn’t find it. I did find this quote, which is very good: ““Whenever the invisible hand isn’t operating the iron fist is”” — Bart Kosko. Then I sidetracked to the Subsethood theorem. Then to “The Sample Mean“.

Which of course, I now had to browse the web to make sure I really knew what the sample mean is. This led to views of various sites and of course Wikipedia (which led to all sorts of side tracks into other things).

Anyway, back to the sample mean. While reading up on it, I thought well (not listing the paths that led here) if the standard deviation is so weak when the data has huge outliers, why not do the same computations using the Median measure? I ran Excel and did a few. Not bad. If data is pretty ‘regular’ it is close to the STD. Hmmm. Maybe this should be part of statistics? Well, more browsing and of course it turns out that it is. It is called the Median Absolute Deviation (MAD). MAD can even be added to Excel.

Well, that was an hour. Not wasted. Certainly better than watching TV. Now back to the task at hand, working on something that may make a good Kickstarter project.

More links

Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License.

Fuzzy Cognitive Maps in C++

Here is some code I wrote to compute FCM. I used this while learning about Fuzzy Systems. Just posted here for storage.

FCM is one of the technologies in field of Soft Computing.

// *************************************************************************
// File:  FCM2.H  ver. 0.03   Date:  September 25, 1993
// By:    Josef Betancourt    System:  Borland C++ ver. 2.0.
// *************************************************************************
/*
Purpose:  Illustrate the Fuzzy Cognitive Map computations.

This is a very very simple implementation of the FCM example found in:
"Neural Networks and Fuzzy Systems". Kosko, B. page 154.
"Fuzzy Thinking, the new science of fuzzy logic". Kosko, B. page 222.
"Fuzzy Logic". McNeill D., Freiberger, P. page 237.

I used a very nice matrix class called Beginner's Understandable Matrix Package, 
BUMP.ZIP, by Clopper Almon. It is found in the Borland C++ forum on CIS. 

This version of my FCM implementation allows the setting of more than one
policy variable in the policy vector.  This is accomplished by loading
a state setting i by 2 matrix.  If column one is zero than cell i in the
policy vector is not reset by the state vector, else during the reasoning
process that cell is reset to the value in column two.

 -------------------------------------------------------------------------
 Of course, a general purpose interactive graphical system where the FCM
 can be entered graphically by multiple experts and the inference process
 is visually presented would be nice.  It should be a FCM CAD system!
 But are FCMs really useful?  How are they used in control systems?

--------------------------------------------------------------------------
USE:  This must be compiled and linked with BUMP.CPP. Large memory model.

        Example       FCM file       Policy        State
    ------------------------------------------------------------------
      Africa         AFRICA.TXT     INVEST.TXT    INVEST2.TXT
      Africa         AFRICA.TXT     DIVEST.TXT    DIVEST2.TXT
      Cocaine        COCAINE.TXT    INTERDIC.TXT  INTERDI2.TXT

    To test the africa example,
    Use the syntax:  FCM africa.txt invest.txt invest2.txt 9 .5
    or               FCM africa.txt divest.txt divest2.txt 9 .5

    To test the cocaine example in the book Fuzzy Thinking,
    Use the syntax: FCM cocaine.txt interdic.txt interdi2.txt 11 .5
    ( this example does not result in cocaine supply falling as stated in
    the book.)
*/
#ifndef FCM2_H
#define FCM2_H

// -----------------------
// dependencies
#include <iostream.h>   // for cout
#include <ctype.h>      // for isdigit()
#include <stdio.h>      // for fgets()
#include <alloc.h>      // for coreleft()
#include <stdlib.h>     // for atof()
#include <conio.h>      // for cprintf()
#include "bump.h"       // for Matrix class
// -----------------------

#define TRUE (1==1)
#define FALSE (!TRUE)

class FCM;  // forward reference.

// Prototypes
void Reason( FCM &fcm);
void Again(void);
void ThresholdMF( Matrix & mat, float thresh = .5);
void ConvertMat( Matrix & matDest, Matrix &matSrc);
void ResetPolicy( Matrix & matDest, Matrix &matSrc);
void ShowCondenseMP( Matrix &Mat);
void DrawBorder(void);


class FCM{  // Fuzzy Cognitive Map
    protected:
        Matrix *pmatFCM;    // directed graph.
        Matrix *pmatPolicy; // initial policy.
        Matrix *pmatState;  // policies to reset after epoch.
        char *Names[];      // the labels of each node.
        int   nPolicies;     // number of nodes.
        float fThreshold;
    public:
        FCM( int n);  // constructor.
        // FCM( FCM other); // copy constructor.
        // ~FCM(); // destructor.
        void  SetSizeI( int size){ nPolicies = size;}
        void  SetThresh( float fthr){ fThreshold = fthr; }
        int   IGetSize( void){ return nPolicies;}
        void  SetFCM( Matrix *fcm){ pmatFCM = fcm;}
        void  SetPolicy( Matrix *Policy){ pmatPolicy = Policy;}
        void  SetState( Matrix *State){ pmatState = State; }
        void  ReadFCM( char *psz);
        float &Cell( Matrix *pmat, int i, int j);
        float GetCell( Matrix *pmat, int i, int j);
        void  ReasonStep( void);
        void  ThresholdMF(void);
        void  ResetPolicy( void );
        void  DisplayPolicy( char *msg);
        Matrix &WhatIsPolicy( void){  return *pmatPolicy ; }
        void  DumpPolicy(void);
};

// end of file FCM2.H
#endif

// *************************************************************************
// File:  FCM2.CPP  ver. 0.03               Date:  September 25, 1993
// By:  Josef Betancourt  tcmits1@cs.com    System:  Borland C++ 4.0
// *************************************************************************
/*
Purpose:  Illustrate the Fuzzy Cognitive Map computations.

*/
// dependencies....

#include "fcm2.h"

// -----------------------------------------
//  constructor.
FCM::FCM( int size){
    SetSizeI(size );
    fThreshold = .5;
}
// -----------------------------------------
// get address of Cell.
float &FCM::Cell( Matrix *pmat, int i, int j){
    return pmat->operator[](i)[j];
}
// -----------------------------------------
// get cell contents.
float FCM::GetCell( Matrix *pmat, int i, int j){
    return pmat->operator[](i)[j];
}
// -----------------------------------------
//
void FCM::ReasonStep( void){
   // Matrix multiply.  Nice syntax!
    *pmatPolicy = (*pmatPolicy)*(*pmatFCM);
   //DisplayPolicy( "policy after multiply: ");

    ThresholdMF();
   //DisplayPolicy( "policy after threshold: ");

    ResetPolicy();
   //DisplayPolicy( "policy after reset: ");
}
// -----------------------------------------
// threshold policy vector.
void FCM::ThresholdMF( void ){
    // apply thresh to policy matrix, default thresh is 0.5.
    for( int i = 1; i < nPolicies + 1; i++){
        if( GetCell( pmatPolicy, 1, i) >= fThreshold ){
            Cell(pmatPolicy, 1, i) = 1.;
        }else{
            Cell( pmatPolicy, 1, i) = 0;
        }
    }
}
// -----------------------------------------
// reset policy with state vector.
void FCM::ResetPolicy( void ){
    for( int i = 1; i < nPolicies+1; i++){
        if( GetCell( pmatState, i,1) == 1){
            Cell( pmatPolicy, 1, i) = GetCell(pmatState, i, 2) ;
        }
    }
}
// -----------------------------------------
// show policy using matrix library display routine.
void FCM::DisplayPolicy( char *message){
    pmatPolicy->Display(message);
}
// -----------------------------------------
// dump policy as simple number dump.
void FCM::DumpPolicy(void){
    for( int i=1; i < nPolicies+1; i++){
        cout << GetCell( pmatPolicy, 1, i);
    }
}
// -----------------------
int main(int argc, char * argv[]){
    cout << "\n*** Fuzzy Cognitive Map example.  By Josef Betancourt ***\n";
    if( argc != 6 ){
     cerr << "\nUsage: fcm <FCM file> <policy file> <state file>";
     cerr << "<matrix size> <thresh>\n";
     cerr << "         Where cell and value refer to policy vector.\n";
     exit (-1);
    }
    // convert strings to integers...........
    int iSize = atoi(argv[4]); // matrix order.
    float fThresh = atof( argv[5]); // threshold value.

    FCM theFCM( iSize);

   // create the helper matrices.
    Matrix matFCM(iSize,iSize), matPolicy(1, iSize),
            matVPolicy(1,iSize), matState( iSize, 2);

   // link them into the FCM
    theFCM.SetFCM( &matFCM);
    theFCM.SetPolicy( &matPolicy);
    theFCM.SetState( &matState);
    theFCM.SetThresh( fThresh);

    // populate using files
    matFCM.ReadA( argv[1] );
    matPolicy.ReadA( argv[2] );
    matState.ReadA( argv[3] );

   // and show matrices ..........
    matFCM.Display("This is the FCM matrix:");
    matPolicy.Display("This is the policy matrix: ");
   matState.Display("This is the state matrix: ");

    cout << "test of dump\n" ;
    theFCM.DumpPolicy();

    Again();    // wait for key tap or escape.

   // perform the FCM process using the object.
    Reason(theFCM );

    return FALSE;
}  // end of main.
// -----------------------
void Reason( FCM &fcm ){
    // apply simple FCM computation using matrices.
    int i = 1;  // epoch counter.
    while( 1){
        cout << "Epoch " << i << '\n';
        fcm.ReasonStep();
        fcm.DisplayPolicy("New policy vector: ");

      // for ease in seeing limit cycles.
        ShowCondenseMP( fcm.WhatIsPolicy() );
        DrawBorder();

        Again();   // wait for key tap or escape.
        i++;
    }
} // reason end.
// -----------------------
void ShowCondenseMP( Matrix &Mat){
    // print policy vector in a more visual pattern.
    cout << "\nPattern: ";
    char temp ;
    for( int i=1; i< Mat.columns()+1; i++){
        if( Mat[1][i] > 0 ){
            temp = 0x2;
        }else{
            if( Mat[1][i] < 0){
                temp = 0x1F;
            }else{
                temp = 0x1;
            }
        }
        cout.put( temp);
    }
    cout << '\n' ;
}
// -----------------------
void DrawBorder(void){
    cout << '\n';
    for( int i=0; i<75; i++){
        cout.put( char(0xDF) );
    }
    cout << '\n';
}
// -----------------------
void Again(void){
    // query user for continuation or end of run.
    cerr << "\n\t\t\t< Tap a key to continue.  ESC to exit. >\n";
    if(getch() == 27){
        exit(0);
    }
}
// ----------------------------------------------------------------------
/*  computing Eigenvectors is similar to computing a FCM without the
     thresholding.  Here is such an algorithm.

void EigenTest(int size, char *pszName){
    // algorithim from Computational Linear Algebra with Models.
    // Williams, Gareth.  Allyn and Bacon, Massachusetts. 1978. page 449.
     float e, s, t;
     int i, j, k, M;
     Matrix A(size,size), X(size,1),Y(size,1),Z(size,1);
     A.ReadA(pszName);
     A.Display("Here is the B matrix:");
     AllOnes( X);
     while(1){
        cout << "Iteration " << i << "\n";
        Y = A * X;
        Y.Display("Here is the Y matrix:");
        k=1;
        for( j=2; j < Y.rows()+1;j++){
            if( fabs( Y[k][1]) >= fabs( Y[j][1]) ){
                continue;
            }
            k= j;
        }
        Y = ( 1/ fabs( Y[k][1])) * Y;
        X = Y;
        X.Display("the adjusted vector is:");
        Z = A*X;
        s=0;
        t=0;
        for( M=1; M<size + 1; M++){
            s += X[M][1] * Z[M][1];
            t += X[M][1] * X[M][1];
        }
        e = s/t;
        cout << "Approx eigenvalue is " << e << "\n" ;
        Again();
    }
}
*/
// -----------------------
// End of file FCM.CPP *********************************

Off topic
There are a lot of examples of connectionist presentations. I wonder if these could be coupled with the FCM technique to create computable structures. For example, look at the work of Mark Lombardi.

Links

Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License.

The multiplicative inverse of zero

This is silly post, but was inspired while reading something which mentioned that zero has no multiplicative inverse. Since grade school I’ve always been puzzled by how math has weird corner cases like this, another: 1 divided by 3, never ends! Anyway, what if zero did have an inverse? Is there some Abstract Algebra thingy that has such a property?

So, lets call the inverse “j”, not ∞, it already has some meanings. Then we have: j0=1. Or, 0=1/j. This means that 0/0 = 1. I.e., (1/j)/(1/j) = 1*j/j*1 = 1.

Is there a physical meaning? What could 1/0 mean? Since multiplication can be interpreted as a scaling, then what happens when you scale something to zero? Well, using the analogy of a map or engineering drawing, a scaling doesn’t destroy a physical measurement, it allows a smaller or large value represent the other value. In a floor plan we may see something like 1/4 inch = 1 foot. In a full size scale, the multiplier is 1. As the multiplier becomes smaller we can represent larger measures. At zero scale, we represent infinity. Thus, scaling infinity to zero, still scales the whole thing, which is 1.

Yeah, doesn’t really work, unless your a mystic. Oh well, like I said silly. I won’t quit my day job.

Links

Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License.