Esempio n. 1
0
        private void btProcessPCA_Click(object sender, EventArgs e)
        {
            #region link up PCA stuff...
            PCA.PCAinitialCalcs InitialCalcs = new PCA.PCAinitialCalcs(ds.Tables[0]);

            //update grid view for principal components
            BsPCA1.DataSource = InitialCalcs.dtEigenV;
            dgvEigenVectorAnalysis.DataSource = BsPCA1;

            //update grid view for vector matrix
            BsPCA2.DataSource          = InitialCalcs.dtEigenM;
            dgvEigenVectors.DataSource = BsPCA2;

            //upate the pca gridview for the pCA datatable
            BsPCA3.DataSource = InitialCalcs.PCAcomp;
            dgvPrincipalComponents.DataSource         = BsPCA3;
            dgvPrincipalComponents.AllowUserToAddRows = false;//prevent any new rows being added

            #endregion

            #region Monte Carlo Simulation

            //initialise new MonteCarlo..
            MC.MonteCarlo MCclac = new MC.MonteCarlo();
            //convert the pcatable into an array[]
            MCclac.PCAvariables = dataconversion.arrayconvert(InitialCalcs.PCAcomp);                           //convert the pca array into a usuable format..
            MCclac.timedeltas   = new double[] { 1, 10, 20, 30 };                                              //(double)1, (double)(1 + (con.Rollingdays / 252)), (1 + ((con.Rollingdays * 2) / 252)), (1 + ((con.Rollingdays * 3) / 252)) };
            //select the initial curve...
            MCclac.forwardcurvesimulations = InitialCalcs.initdata.Rows[InitialCalcs.initdata.Rows.Count - 1]; //select last row...used a initial curve....
            MCclac.ForwardSimulations      = InitialCalcs.initdata.Clone();

            //set of the process
            MCclac.Process();
            s1.Datatab = MCclac.simlist[0];
            s2.Datatab = MCclac.simlist[1];
            s3.Datatab = MCclac.simlist[2];
            s4.Datatab = MCclac.simlist[3];
            simulations.Data.Add(s1);
            simulations.Data.Add(s2);
            simulations.Data.Add(s3);
            simulations.Data.Add(s4);

            BsSim4.DataSource                  = simulations.Data;
            dgvsimulations.DataSource          = BsSim4;
            dgvsimulations.AllowUserToAddRows  = false;
            dgvsimulations.AutoGenerateColumns = true;


            BsSimData5.DataSource     = BsSim4;
            BsSimData5.DataMember     = "Datatab";
            dgvForCurveSim.DataSource = BsSimData5;
            //dgvForCurveSim.DataMember = "Datatab";
            dgvForCurveSim.AllowUserToAddRows  = false;
            dgvForCurveSim.AutoGenerateColumns = true;

            //chart bind below for the moment leave commented out...
            //chartdatatable(MCclac.ForwardSimulations, chtSimForwCurve);

            #endregion

            #region optimisation

            #region optimisation code
            //initialise new optimisation....
            OPT.Optimisation Ops = new OPT.Optimisation(con);

            //this is for th intrinsic evaluation....
            //set up the inital curve, pass through to the find the optimisation...
            //you are starting with april ...ending with march....
            var stringArray = InitialCalcs.initdata.Rows[InitialCalcs.initdata.Rows.Count - 1].ItemArray.Cast <double>().ToArray();
            //take april to january 2016-2017 period...
            double[,] curve = new double[12, 2];
            for (int r = 0; r < 12; r++)
            {
                curve[r, 0] = r;
                curve[r, 1] = (double)stringArray[r + 3];
            }

            //compute the optimisation...
            Ops.OptFunc(curve, InjectionVol, WithdrawalVol);//simulation 1 intrinsic...
            simoutputlist.Add(Ops);

            //you need to pass on the open positions so they can be taken into consideration for the next sale,
            //to do this we need to pass through current injections and widthdrawls...
            for (int i = 0; i < 12; i++)
            {
                InjectionVol[i, 0] = i;
                InjectionVol[i, 1] = (double)Ops.Imptable.Rows[0][i];
            }


            for (int i = 0; i < 12; i++)
            {
                WithdrawalVol[i, 0] = i;
                WithdrawalVol[i, 1] = (double)Ops.Withtable.Rows[0][i];
            }

            //collapse the intrinsic model...
            Ops = null;

            //find the four forward curves your going to use...0
            //you have to effective create 1 datatables, populate with the simulations from the four runs....
            // Ops.SimulationResults;//simulation1 instrinsic....
            //we need to initial a new sim results for every run...
            //initialise new optimisation....
            double len = (double)s1.Datatab.Rows.Count;


            for (int si = 0; si < len; si++)
            {
                OPT.Optimisation tempOps = new OPT.Optimisation(con);//create a new instance of the optimisation..
                //run each of the simulations, if one of the revaluations does not add value, exclude it!
                //first find the curve to use...
                int iloop = 0;
                for (int a = 0; a < 4; a++)
                {
                    var tempcurve = MCclac.simlist[a].Rows[si].ItemArray.Cast <double>().ToArray();
                    //take april to january 2016-2017 period...
                    double[,] tempforwardcurve = new double[12, 2];
                    for (int r = 0; r < 12; r++)
                    {
                        tempforwardcurve[r, 0] = r;
                        tempforwardcurve[r, 1] = (double)tempcurve[r + 3];
                    }

                    //
                    tempOps.OptFunc(tempforwardcurve, InjectionVol, WithdrawalVol);//carry out the optimisation....


                    //check to see if the new simulation adds value exclude it if it doesn't

                    double resutl = Convert.ToDouble(tempOps.Proftable.Rows[a - iloop]["Total"]);
                    if (a > 0 && resutl < Convert.ToDouble(tempOps.Proftable.Rows[(a - iloop) - 1]["Total"]))
                    {//if the new optimisation is worse than the previous delete the optimisation...
                        tempOps.Proftable.Rows[a - iloop].Delete();
                        tempOps.ForCurvetable.Rows[a - iloop].Delete();
                        tempOps.Withtable.Rows[a - iloop].Delete();
                        tempOps.Imptable.Rows[a - iloop].Delete();
                        tempOps.Positiontable.Rows[a - iloop].Delete();
                        iloop++;
                    }
                    else
                    {
                        // {};
                        //  simoutputlist.Add(tempOps);
                        //to do this we need to pass through current injections and widthdrawls...
                        for (int i = 0; i < 12; i++)
                        {
                            InjectionVol[i, 0] = i;
                            InjectionVol[i, 1] = (double)tempOps.Imptable.Rows[a - iloop][i];
                        }
                        for (int i = 0; i < 12; i++)
                        {
                            WithdrawalVol[i, 0] = i;
                            WithdrawalVol[i, 1] = (double)tempOps.Withtable.Rows[a - iloop][i];
                        }
                    }
                }
                simoutputlist.Add(tempOps);
            }

            //need to create the optimisation output....
            //Review the intrinsic...
            double highestval  = 0;
            int    datatableno = 0;
            double rownumb     = 0;
            double rownumcalc  = 0;
            for (int ti = 0; ti < simoutputlist.Count; ti++)
            {
                rownumcalc = 0;
                foreach (DataRow dr in simoutputlist[ti].Proftable.Rows)
                {
                    double currentval = Convert.ToDouble(dr["Total"]);
                    if (highestval < currentval)
                    {
                        datatableno = ti;
                        rownumb     = rownumcalc;
                        highestval  = currentval;
                    }
                    rownumcalc++;
                }
            }


            intrinsicoutput = simoutputlist[0].Positiontable.Clone();
            //you need to create a datatable to hold injections withdrawals and profit for intrinsic,
            //also create three tables to hold injections , withdrawals and prositions for rolling...with reference to each revaluation period...


            #endregion

            #region binding sources optimisations..
            //update grid view for  intrinsic injections grid view...
            BsOptimData6.DataSource             = intrinsicoutput; //simoutputlist[0].Imptable;
            dgvIntInjections.DataSource         = BsOptimData6;
            dgvIntInjections.AllowUserToAddRows = false;           //prevent any new rows being added

            //update grid view for  intrinsic Withdrawals grid view...
            BsOptimData7.DataSource                    = simoutputlist[0].Withtable;
            dgvIntrinsicWithdrawals.DataSource         = BsOptimData7;
            dgvIntrinsicWithdrawals.AllowUserToAddRows = false;//prevent any new rows being added

            //upate the  gridview intrinsic profit/loss
            BsOptimData8.DataSource               = simoutputlist[0].Proftable;
            dgvIntrinsicProfit.DataSource         = BsOptimData8;
            dgvIntrinsicProfit.AllowUserToAddRows = false;//prevent any new rows being added

            //update grid view for rolling intrinsic injections grid view...
            BsOptimData9.DataSource                    = simoutputlist[datatableno].Imptable;
            dgvRolntrinsicInjection.DataSource         = BsOptimData9;
            dgvRolntrinsicInjection.AllowUserToAddRows = false;//prevent any new rows being added

            //update grid view for rolling intrinsic Withdrawals grid view...
            BsOptimData10.DataSource                    = simoutputlist[datatableno].Withtable;
            dgvRoIntrinsicWithdrawal.DataSource         = BsOptimData10;
            dgvRoIntrinsicWithdrawal.AllowUserToAddRows = false;//prevent any new rows being added

            //upate the gridview rolling intrinsic profit/loss
            BsOptimData11.DataSource                = simoutputlist[datatableno].Proftable;
            dgvRolntrinsicProfit.DataSource         = BsOptimData11;
            dgvRolntrinsicProfit.AllowUserToAddRows = false;//prevent any new rows being added
            #endregion
            #endregion
        }
        private void btProcessPCA_Click(object sender, EventArgs e)
        {
            #region link up PCA stuff...
            PCA.PCAinitialCalcs InitialCalcs = new PCA.PCAinitialCalcs(ds.Tables[0]);

            //update grid view for principal components
            BsPCA1.DataSource = InitialCalcs.dtEigenV;
            dgvEigenVectorAnalysis.DataSource = BsPCA1;

            //update grid view for vector matrix
            BsPCA2.DataSource = InitialCalcs.dtEigenM;
            dgvEigenVectors.DataSource = BsPCA2;

            //upate the pca gridview for the pCA datatable
            BsPCA3.DataSource = InitialCalcs.PCAcomp;
            dgvPrincipalComponents.DataSource = BsPCA3;
            dgvPrincipalComponents.AllowUserToAddRows = false;//prevent any new rows being added

            #endregion

            #region Monte Carlo Simulation

            //initialise new MonteCarlo..
            MC.MonteCarlo MCclac = new MC.MonteCarlo();
            //convert the pcatable into an array[]
            MCclac.PCAvariables = dataconversion.arrayconvert(InitialCalcs.PCAcomp);//convert the pca array into a usuable format..
            MCclac.timedeltas = new double[] { 1, 10, 20, 30 };//(double)1, (double)(1 + (con.Rollingdays / 252)), (1 + ((con.Rollingdays * 2) / 252)), (1 + ((con.Rollingdays * 3) / 252)) };
            //select the initial curve...
            MCclac.forwardcurvesimulations = InitialCalcs.initdata.Rows[InitialCalcs.initdata.Rows.Count - 1];//select last row...used a initial curve....
            MCclac.ForwardSimulations = InitialCalcs.initdata.Clone();

            //set of the process
            MCclac.Process();
            s1.Datatab = MCclac.simlist[0];
            s2.Datatab = MCclac.simlist[1];
            s3.Datatab = MCclac.simlist[2];
            s4.Datatab = MCclac.simlist[3];
            simulations.Data.Add(s1);
            simulations.Data.Add(s2);
            simulations.Data.Add(s3);
            simulations.Data.Add(s4);

            BsSim4.DataSource = simulations.Data;
            dgvsimulations.DataSource = BsSim4;
            dgvsimulations.AllowUserToAddRows = false;
            dgvsimulations.AutoGenerateColumns = true;

            BsSimData5.DataSource = BsSim4;
            BsSimData5.DataMember = "Datatab";
            dgvForCurveSim.DataSource = BsSimData5;
            //dgvForCurveSim.DataMember = "Datatab";
            dgvForCurveSim.AllowUserToAddRows = false;
            dgvForCurveSim.AutoGenerateColumns = true;

            //chart bind below for the moment leave commented out...
            //chartdatatable(MCclac.ForwardSimulations, chtSimForwCurve);

            #endregion

            #region optimisation

            #region optimisation code
            //initialise new optimisation....
            OPT.Optimisation Ops = new OPT.Optimisation(con);

            //this is for th intrinsic evaluation....
            //set up the inital curve, pass through to the find the optimisation...
            //you are starting with april ...ending with march....
            var stringArray = InitialCalcs.initdata.Rows[InitialCalcs.initdata.Rows.Count - 1].ItemArray.Cast<double>().ToArray();
            //take april to january 2016-2017 period...
            double[,] curve = new double[12, 2];
            for (int r = 0; r < 12; r++)
            {
                curve[r, 0] = r;
                curve[r, 1] = (double)stringArray[r + 3];
            }

            //compute the optimisation...
            Ops.OptFunc(curve, InjectionVol, WithdrawalVol);//simulation 1 intrinsic...
            simoutputlist.Add(Ops);

            //you need to pass on the open positions so they can be taken into consideration for the next sale,
            //to do this we need to pass through current injections and widthdrawls...
            for (int i = 0; i < 12; i++)
            {
                InjectionVol[i, 0] = i;
                InjectionVol[i, 1] = (double)Ops.Imptable.Rows[0][i];

            }

            for (int i = 0; i < 12; i++)
            {
                WithdrawalVol[i, 0] = i;
                WithdrawalVol[i, 1] = (double)Ops.Withtable.Rows[0][i];

            }

            //collapse the intrinsic model...
            Ops = null;

            //find the four forward curves your going to use...0
            //you have to effective create 1 datatables, populate with the simulations from the four runs....
            // Ops.SimulationResults;//simulation1 instrinsic....
            //we need to initial a new sim results for every run...
            //initialise new optimisation....
            double len = (double)s1.Datatab.Rows.Count;

            for (int si = 0; si < len; si++)
            {
                OPT.Optimisation tempOps = new OPT.Optimisation(con);//create a new instance of the optimisation..
                //run each of the simulations, if one of the revaluations does not add value, exclude it!
                //first find the curve to use...
                int iloop = 0;
                for (int a = 0; a < 4; a++)
                {
                    var tempcurve = MCclac.simlist[a].Rows[si].ItemArray.Cast<double>().ToArray();
                    //take april to january 2016-2017 period...
                    double[,] tempforwardcurve = new double[12, 2];
                    for (int r = 0; r < 12; r++)
                    {
                        tempforwardcurve[r, 0] = r;
                        tempforwardcurve[r, 1] = (double)tempcurve[r + 3];
                    }

                    //
                    tempOps.OptFunc(tempforwardcurve, InjectionVol, WithdrawalVol);//carry out the optimisation....

                    //check to see if the new simulation adds value exclude it if it doesn't

                    double resutl = Convert.ToDouble(tempOps.Proftable.Rows[a - iloop]["Total"]);
                    if (a > 0 && resutl < Convert.ToDouble(tempOps.Proftable.Rows[(a - iloop) - 1]["Total"]))
                    {//if the new optimisation is worse than the previous delete the optimisation...
                        tempOps.Proftable.Rows[a - iloop].Delete();
                        tempOps.ForCurvetable.Rows[a - iloop].Delete();
                        tempOps.Withtable.Rows[a - iloop].Delete();
                        tempOps.Imptable.Rows[a - iloop].Delete();
                        tempOps.Positiontable.Rows[a - iloop].Delete();
                        iloop++;
                    }
                    else
                    {
                        // {};
                        //  simoutputlist.Add(tempOps);
                        //to do this we need to pass through current injections and widthdrawls...
                        for (int i = 0; i < 12; i++)
                        {
                            InjectionVol[i, 0] = i;
                            InjectionVol[i, 1] = (double)tempOps.Imptable.Rows[a - iloop][i];

                        }
                        for (int i = 0; i < 12; i++)
                        {
                            WithdrawalVol[i, 0] = i;
                            WithdrawalVol[i, 1] = (double)tempOps.Withtable.Rows[a - iloop][i];

                        }
                    }
                }
                simoutputlist.Add(tempOps);
            }

            //need to create the optimisation output....
            //Review the intrinsic...
            double highestval = 0;
            int datatableno = 0;
            double rownumb = 0;
            double rownumcalc = 0;
            for (int ti = 0; ti < simoutputlist.Count; ti++)
            {
                rownumcalc = 0;
                foreach (DataRow dr in simoutputlist[ti].Proftable.Rows)
                {

                    double currentval = Convert.ToDouble(dr["Total"]);
                    if (highestval < currentval)
                    {
                        datatableno = ti;
                        rownumb = rownumcalc;
                        highestval = currentval;
                    }
                    rownumcalc++;
                }
            }

            intrinsicoutput = simoutputlist[0].Positiontable.Clone();
            //you need to create a datatable to hold injections withdrawals and profit for intrinsic,
            //also create three tables to hold injections , withdrawals and prositions for rolling...with reference to each revaluation period...

            #endregion

            #region binding sources optimisations..
            //update grid view for  intrinsic injections grid view...
            BsOptimData6.DataSource = intrinsicoutput;//simoutputlist[0].Imptable;
            dgvIntInjections.DataSource = BsOptimData6;
            dgvIntInjections.AllowUserToAddRows = false;//prevent any new rows being added

            //update grid view for  intrinsic Withdrawals grid view...
            BsOptimData7.DataSource = simoutputlist[0].Withtable;
            dgvIntrinsicWithdrawals.DataSource = BsOptimData7;
            dgvIntrinsicWithdrawals.AllowUserToAddRows = false;//prevent any new rows being added

            //upate the  gridview intrinsic profit/loss
            BsOptimData8.DataSource = simoutputlist[0].Proftable;
            dgvIntrinsicProfit.DataSource = BsOptimData8;
            dgvIntrinsicProfit.AllowUserToAddRows = false;//prevent any new rows being added

            //update grid view for rolling intrinsic injections grid view...
            BsOptimData9.DataSource = simoutputlist[datatableno].Imptable;
            dgvRolntrinsicInjection.DataSource = BsOptimData9;
            dgvRolntrinsicInjection.AllowUserToAddRows = false;//prevent any new rows being added

            //update grid view for rolling intrinsic Withdrawals grid view...
            BsOptimData10.DataSource = simoutputlist[datatableno].Withtable;
            dgvRoIntrinsicWithdrawal.DataSource = BsOptimData10;
            dgvRoIntrinsicWithdrawal.AllowUserToAddRows = false;//prevent any new rows being added

            //upate the gridview rolling intrinsic profit/loss
            BsOptimData11.DataSource = simoutputlist[datatableno].Proftable;
            dgvRolntrinsicProfit.DataSource = BsOptimData11;
            dgvRolntrinsicProfit.AllowUserToAddRows = false;//prevent any new rows being added
            #endregion
            #endregion
        }