# Simulation of Dealer Commissions with Matlab

As demonstrated in an earlier article dealer or sales commissions can easily be calculated with the .NET Calculation Engine finaquant® calcs.

This article demonstrates how changes in commission amounts can be simulated by varying selected table-valued inputs. In example presented here, the math software Matlab is connnected with the Calculation Engine implemented with finaquant® calcs in order to:

1. Control the variation of selected inputs; Sales and Commission Scales.
2. Reporting: Visualize the resultant total commissions with plot diagrams.

### Simulation Scenario

• The sales amounts in the sales table are increased or decreased in proportion for all products. For example, if the Sales Multiplier is 1.2, all sales amounts for all products are multiplied with 1.2
• The commission scale (lower limit) amounts of the scale table are increased or decreased in proportion for all commission contracts.

### Configuration of the Calculation Network

C#/.NET code below shows how the Calculation Network is configured for the simulation with two variable table-valued inputs: Sales and Commission Scales. Other inputs of the Calculation Nodes are fetched from the external database as before. In Simulation Mode, not input or output table is stored in the generic database.

You may find all the C# code examples presented here in the Visual Studio project file FinaquantCalcsStarter (Demo Functions for Getting Started) beginning from release 1.02 (see related downloads at finaquant® calcs).

Configure Calculation Network for Commission Simulation

### Calculating Total Commissions

C# method for calculating Total Commissions for given pair of Sales and Scale multipliers. The output table Commissions per Dealer is aggregated to obtain total commissions for all dealers.

Calculate Total Commissions
 // Return total commissions for given pair of Sales and Scale multipliers private static void SimNetwork_GetTotalCommissions(Network nw, TableRow CalcInstance, MatrixTable SalesTbl, MatrixTable ComScaleTbl, double SalesMultiplier, double ScaleMultiplier, out double TotalCommissions) { // obtain adjusted input tables MatrixTable NewSalesTbl = SalesTbl * SalesMultiplier;   MatrixTable NewComScaleTbl = MatrixTable.MultiplySelectedKeyFigureByScalar(ComScaleTbl, ScaleMultiplier, "lower_limit", "lower_limit");   // explicit assignment of input tables nw[0].SetInput(0, NewSalesTbl); // 1. input of 1. node nw[1].SetInput(2, NewComScaleTbl); // 3. input of 2. node   // execute calculation network MatrixTable LogTable; nw.ExecuteNetwork(CalcInstance, out LogTable);   // return total commissions MatrixTable ComTbl = nw[1].GetOutput(1); ComTbl = ComTbl.ExcludeColumn("dealer"); ComTbl = MatrixTable.AggregateAllKeyFigures(ComTbl, null); TotalCommissions = (double) ComTbl.GetFieldValue("commission", 0); }

### Simulation Function called by Matlab

Simulation method that receives two equal-sized NxM input matrices, SalesMult (sales multipliers) and ScaleMult (scale multipliers) from Matlab, and returns NxM output matrix TotalCom with total commissions for each multiplier value pair in input matrices.

Get Total Commissions for Every Multiplier Pair

### Calling Simulation Function directly within C#/.NET

The simulation method above that expects matrix inputs can also be called directly within C#/.NET; we need an additional software like Matlab especially for data visualization (plots, graphs etc.) and reporting.

Call Simulation Function in .NET
 // Simulate Calculation Network to obtain Sales Commissions public static void SimNetwork_GetTotalCommissions() { // get total commissions for multiple Sales and Scale multipliers KeyVector SalesMultipliers = KeyVector.CreateSequenceVector(StartValue: 0.8, Interval: 0.1, nLength: 5);   KeyVector ScaleMultipliers = KeyVector.CreateSequenceVector(StartValue: 0.9, Interval: 0.05, nLength: 5);   // Get multiplier matrix with all possible combinations of sales & scales multipliers KeyMatrix SalesMult = KeyMatrix.CreateConstantMatrix(nRows: ScaleMultipliers.nLength, nCols: SalesMultipliers.nLength);   KeyMatrix ScaleMult = KeyMatrix.CreateConstantMatrix(nRows: ScaleMultipliers.nLength, nCols: SalesMultipliers.nLength);   for (int i = 0; i < SalesMult.nRows; i++) { for (int j = 0; j < SalesMult.nCols; j++) { SalesMult[i,j] = SalesMultipliers[j]; ScaleMult[i,j] = ScaleMultipliers[i]; } } KeyMatrix TotalCom = SimNetwork_GetTotalCommissions(SalesMult, ScaleMult);   // TEST Console.WriteLine("Sales Multiplier Matrix: \n" + SalesMult); Console.WriteLine("Commission Scales Multiplier Matrix: \n" + ScaleMult); Console.WriteLine("Total Commission Matrix: \n" + TotalCom); Console.ReadKey(); }

### Calling Simulation Function from Matlab; Displaying Results

Following matlab script illustrates how the simulation function in C#/.NET is called from matlab to initiate simulation and display the results in graphs.

See Calling .NET Methods in Matlab for general information about the integration of Matlab and .NET.

Matlab Script for Simulation
 % Simulation of Sales Commissions % see: http://forum.finaquant.com/viewtopic.php?f=4&t=1224   % dll assembly (replace this path with your own path) assemblypath = 'C:\Users\finaquant\Documents\Visual Studio 2012\Projects\FinaquantCalcs\FinaquantCalcsStarter\bin\Release\FinaquantCalcsStarter.exe';   % load .NET matrix function library NET.addAssembly(assemblypath);   % Generate Sales & Commission Scale Multipliers [SalesMult, ScaleMult] = meshgrid(0.8:0.05:1.2, 0.7:0.05:1.3)   % get total commissions SalesMult_NET = NET.convertArray(SalesMult,'System.Double'); ScaleMult_NET = NET.convertArray(ScaleMult,'System.Double');   TotalCom_NET = FinaquantCalcsStarter.DemoFunctions.SimNetwork_GetTotalCommissions(SalesMult_NET,ScaleMult_NET); TotalCom = TotalCom_NET.double   % visualize simulation results in graphs   % 3-dim plot: Total Commissions vs Sales & Scales multipliers mesh(SalesMult, ScaleMult, TotalCom); figure   % 2-dim plot: Total Commissions vs Scale multiplier % where Sales multiplier = 1.0 (5. column of matrices) plot(ScaleMult(:,5), TotalCom(:,5)); figure   % Contour plot: Total Commissions vs Sales & Scales multipliers [C,h] = contour(SalesMult, ScaleMult, TotalCom); text_handle = clabel(C,h);

### Conclusions

• This demonstration shows generally how the Calculation Engine finaquant® calcs implementing any analytical relationship can be used for simulations.
• Note that this simulation example doesn’t make much sense from the business perspective. However, it can be used as a technical prototype for any kind of simulations.
• The Calculation Engine could represent any analytical relationship, or mechanism that is based on table data, like Dealer or Sales Commissions, Performance Fees, Financial Plans; in fact Estimations and Predictions about anything in business and science.
• Ability to simulate means also ability to optimize. Optimization functions of software like Matlab could be used for this purpose.