# Neural Network

### Designing A Custom Neural Network In MATLAB

The MATLAB Neural Network toolbox ships with numerous predefined and canonical neural nets, however sometimes you may need to create a custom net with just the right connections, biases and hidden layers to suite your particular problem domain. To achieve this goal we can use the matlab *network* object. The * network *object allows granular design of neural networks by exposing all properties of the net that we are designing. The preceding code demonstrates how to build a simple neural to learn the truth table for Logical AND.

First lets look at the Logical AND truth table:

p |
q |
p ∧ q |
---|---|---|

T | T | T |

T | F | F |

F | T | F |

F | F | F |

Open a new edit window in MATLAB and enter the following code:

- This creates an empty network object and assigns it to the
*net*variable, sets up the number of inputs and uses cell array syntax to index into its properties.%% Design Custom Neural Network net = network; % create network net.numInputs = 2; % set number of inputs net.inputs{1}.size = 1; % assign 2 to input size net.inputs{2}.size = 1; net.numLayers = 1; % add 1 layer to network net.layers{1}.size = 1; % assign number of neurons in layer net.inputConnect(1) = 1; % connet input to layer 1 net.inputConnect(2) = 1; net.biasConnect(1) = 1; % connect bias to layer 1 net.biases{1}.learnFcn = 'learnp'; % set bias learning function net.biases{1}.initFcn = 'initzero'; % set bias init function net.outputConnect(1) = 1; net.layers{1}.transferFcn = 'hardlim'; % set layer transfer function [hard limit] net.inputWeights{1}.initFcn = 'initzero'; % set input wieghts init function net.inputWeights{1}.learnFcn = 'learnp'; % set input weight learning function net.inputWeights{2}.learnFcn = 'learnp'; net.inputWeights{2}.initFcn = 'initzero'; net.initFcn = 'initlay'; % set network init function net.trainFcn = 'trainc'; % set network training function net.performFcn = 'mae'; % set network perf evaluation function view(net) net = train(net,[0 0 1 1;0 1 0 1],[0 0 0 1]) ; % train network

- Custom Network Diagram:
- Test Network

In the command window typenet([1;1])

This should output a 1 to the command window indicating 1 AND 1 = 1

### Predicting The Lottery With MATLAB® Neural Network

DISCLAMER: This post does not in any way prove or disprove the validity of using neural networks to predict the lottery. It is purely for the purpose of demonstrating certain capabilities available in MATLAB ® . The results and conclusions are my opinion and may or may not constitute applicable techniques of predicting the popular Jamaican Lottery Cash Pot game.

**Background:**

Supreme Ventures Jamaica Limited has a lottery game called Cash Pot (CP) . The game is based on 36 balls being loaded into a chamber and one ball been selected at random from the grouping. The game is ran four (4) times each day seven (7) days per week.

**Anecdotal Heuristics:**

While doing a little tongue and cheek research at my favorite barbershop, I stumbled upon some heuristics that are employed by most patrons who play the (CP) game. One involved writing down the day, time, and winning number for each day’s lottery. After building up a sufficient dataset, they could then query a particular day and time; and with some simple arithmetic tally the most likely number to be played on that day and time. I was informed that this proved to be a very efficient way of telling which number was to be played next. Another popular heuristic involved pre-assigned symbols; these symbols were associated with each of the thirty six (36) numbers. Then based on dreams aka “rakes” numbers would be chosen that matched the symbols seen in the “rake”. These two methods were the favorite amongst the players of Cash Pot.

**Procedure for predicting Cash Pot with MATLAB ANN:**

- Get the dataset from Supreme Ventures Jamaica website. [contains all winning numbers with date and time]
- We will need to do some twiddling with the file in order to get it into a format that MATLAB can use. To do that we need to remove all headings/sub-headings and labels.
- Next remove the DRAW# and MARK columns since we will not be using those in our analysis.
- In column D use the =WEEKDAY() formula to get the day number from the corresponding date: repeat for all rows.
- Use find and replace to replace MORNING with 1, MIDDAY with 2, DRIVETIME with 3 and EVENING with 4. [Save the file]
- Using MATLAB folder explorer, navigate to the file then double click on it to run the import tool.
- Select columns B and D then hit the import button; this should import only columns B and D, rename the imported matrix to cpInputs .
- Select column C and hit the import button; this should import column C only, rename the imported matrix to cpTargets.
- Because MATLAB sees Neural Network(NN) features as rows, transpose the two matrices using
- In the MATLAB command window type nntool.
- Import cpInputs and cpTargets into the NN data manager.
- Hit the new button on the Neural Network Data Manager and change the default name to cpNN.
- Set Input data to cpInputs, Target data to cpTargets.
- Hit the create button to create the NN.
- Let’s go ahead and train this network. On the train tab of the Network: cpNN dialog, select cpInputs for Inputs and cpTargets for Targets; then press the Train Network button to start the network training.
- Results of training.
- After training the network to the desired tolerance’s go back to the Neural Network/Data Manager dialog box and hit the export button, select cpNN from the list then hit the export button.
- Go back to the MATLAB command window and type
- The resulting value will be the NN’s best guess of what will be the winning entry for Cash Pot on a Tuesday at DRIVETIME.

cpInputs = cpInputs’; cpTargets = cpTargets’;

** **

**Note:**

The newly created NN has two inputs, the first been the day of the week on which the [CP] is scheduled to be played and the second input the time of day that the [CP] is scheduled to played. It also has a hidden layer with 10 neurons with associated bias, and an output layer with 1 neuron and its associated bias. The output is a scalar double which represents the predicted winning number.

CpNN([2;3]) % [day;time]

**Conclusions:**

My initial analysis of the results of the NN was not conclusive, maybe the parameters of the NN could be adjusted and the results compared to actual winning numbers. However, even after doing so one may find that the outputs are still random and contain no discernible patterns, which should be the case for a supposedly random game of chance.