Untitled

mail@pastecode.io avatar
unknown
csharp
2 years ago
13 kB
4
Indexable
Never
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PS4
{
    public partial class Form1 : Form
    {
        Random rnd = new Random();
        public Form1()
        {
            InitializeComponent();
        }
        private void label1_Click(object sender, EventArgs e)
        {

        }
        private void textBox3_TextChanged(object sender, EventArgs e)
        {
            chart.Series[0].Points.Clear();
        }
        private void btnExit_Click(object sender, EventArgs e)
        {
            DialogResult result = MessageBox.Show("Are you sure you want to quit?", "Exit Application", MessageBoxButtons.YesNo);
            if (result == System.Windows.Forms.DialogResult.Yes)
            {
                Application.Exit();
            }
        }
        private void rbUniPV_CheckedChanged(object sender, EventArgs e)
        {
            chart.Series[0].Points.Clear();
            txtPvMean.Text = "";
            txtPvStanDev.Text = "";
            lblMeanPv.Text = ("Min");
            lblStanDevPv.Text = ("Max");
            txtPvMin.Visible = true;
            txtPvMean.Visible = false;
            txtPvMax.Visible = true;
            txtPvStanDev.Visible = false;
        }

        private void rbNormN_CheckedChanged(object sender, EventArgs e)
        {
            chart.Series[0].Points.Clear();
            txtNMin.Text = "";
            txtNMax.Text = "";
            lblMinN.Text = ("Mean");
            lblMaxN.Text = ("Standard Dev.");
            txtNMin.Visible = false;
            txtNMean.Visible = true;
            txtNMax.Visible = false;
            txtNStanDev.Visible = true;
        }
        private void rbNormPv_CheckedChanged(object sender, EventArgs e)
        {
            chart.Series[0].Points.Clear();
            txtPvMin.Text = "";
            txtPvMax.Text = "";
            lblMeanPv.Text = ("Mean");
            lblStanDevPv.Text = ("Standard Dev.");
            txtPvMean.Visible = true;
            txtPvMin.Visible = false;
            txtPvStanDev.Visible = true;
            txtPvMax.Visible = false;
        }

        private void rbUniN_CheckedChanged(object sender, EventArgs e)
        {
            chart.Series[0].Points.Clear();
            txtNMean.Text = "";
            txtNStanDev.Text = "";
            lblMinN.Text = ("Min");
            lblMaxN.Text = ("Max");
            txtNMean.Visible = false;
            txtNStanDev.Visible = false;
            txtNMin.Visible = true;
            txtNMax.Visible = true;
        }
        public int GetRandomUnifrom(int min, int max)
        {
            int uniform;
            uniform = rnd.Next(min, max+1);
            return uniform;
        }
        public double GetRandomNormal(double mean, double standev)
        {
            double r, phi, x, z;
            r = rnd.NextDouble();
            phi = rnd.NextDouble();
            z = (Math.Cos(2 * Math.PI * r)) * (Math.Sqrt(-2 * Math.Log(phi)));
            x = z * standev + mean;
            return x;
        }
        public int GetBinIndex(double mini,double maxi, int numbins, double valueTobin)
        {
            int BinIndex;
            BinIndex = (int) Math.Floor((valueTobin - mini) / ((maxi - mini)/ numbins));
            return BinIndex+1;
        }
        private void btnRun_Click(object sender, EventArgs e)
        {
            try
            {
                double numIterations;
                double.TryParse(txtItertions.Text, out numIterations);
                int numBins;
                int.TryParse(txtBins.Text, out numBins);
                int nMinUser;
                int.TryParse(txtNMin.Text, out nMinUser);
                int nMaxUser;
                int.TryParse(txtNMax.Text, out nMaxUser);
                double nMean;
                double.TryParse(txtNMean.Text, out nMean);
                double nStdev;
                double.TryParse(txtNStanDev.Text, out nStdev);
                int PvMinUser;
                int.TryParse(txtPvMin.Text, out PvMinUser);
                int PvMaxUser;
                int.TryParse(txtPvMax.Text, out PvMaxUser);
                double PvMean;
                double.TryParse(txtPvMean.Text, out PvMean);
                double PvStdev;
                double.TryParse(txtPvStanDev.Text, out PvStdev);
                if (rbNormPv.Checked && rbNormN.Checked)
                {
                    double PvMin = PvMean - 3 * PvStdev;
                    double PvMax = PvMean + 3 * PvStdev;
                    double nMin = nMean - 3 * nStdev;
                    double nMax = nMean + 3 * nStdev;
                    double PtMin = nMin * PvMin;
                    double PtMax = nMax * PvMax;
                    double Range = PtMax - PtMin;
                    double BinIncrement = Range / numBins;
                    int[] BinNumber = new int[numBins + 1];
                    double Sum = 0;

                    for (int i = 0; i < numIterations; i++)
                    {
                        double randomN = GetRandomNormal(nMean, nStdev);
                        double randomPv = GetRandomNormal(PvMean, PvStdev);
                        double Pt = randomN * randomPv;
                        int binindex = GetBinIndex(PtMin, PtMax, numBins, Pt);

                        if (binindex < BinNumber.Length)
                        {
                            BinNumber[binindex] = BinNumber[binindex] + 1;
                        }
                        Sum = Sum + Pt;
                    }
                    int arraycount = BinNumber.Sum();
                    double Average = Sum / arraycount;
                    txtAverageProfit.Text = "$" + Average.ToString("N");

                    chart.Series[0].Points.Clear();
                    for (int a = 1; a < BinNumber.Length; a++)
                    {
                        chart.Series[0].Points.AddXY(a, BinNumber[a]);
                        chart.Series[0].Points[a - 1].AxisLabel = (PtMin + BinIncrement * a).ToString("");
                    }
                }
                if (rbNormPv.Checked && rbUniN.Checked)
                {
                    double PvMin = PvMean - 3 * PvStdev;
                    double PvMax = PvMean + 3 * PvStdev;
                    double PtMin = nMinUser * PvMin;
                    double PtMax = nMaxUser * PvMax;
                    double Range = PtMax - PtMin;
                    double BinIncrement = Range / numBins;
                    int[] BinNumber = new int[numBins + 1];
                    double Sum = 0;
                    for (int i = 0; i < numIterations; i++)
                    {
                        int randomN = GetRandomUnifrom(nMinUser, nMaxUser);
                        double randomPv = GetRandomNormal(PvMean, PvStdev);
                        double Pt = randomN * randomPv;
                        int binindex = GetBinIndex(PtMin, PtMax, numBins, Pt);
                        if (binindex < BinNumber.Length)
                        {
                            BinNumber[binindex] = BinNumber[binindex] + 1;
                        }
                        Sum = Sum + Pt;
                        int arraycount = BinNumber.Sum();
                        double Average = Sum / arraycount;
                        txtAverageProfit.Text = "$" + Average.ToString("N");
                        chart.Series[0].Points.Clear();
                        for (int a = 1; a < BinNumber.Length; a++)
                        {
                            chart.Series[0].Points.AddXY(a, BinNumber[a]);
                            chart.Series[0].Points[a - 1].AxisLabel = (PtMin + BinIncrement * a).ToString("");
                        }
                    }
                }
                if (rbUniPV.Checked && rbNormN.Checked)
                {
                    double nMin = nMean - 3 * nStdev;
                    double nMax = nMean + 3 * nStdev;
                    double PtMin = PvMinUser * nMin;
                    double PtMax = PvMaxUser * nMax;
                    double Range = PtMax - PtMin;
                    double BinIncrement = Range / numBins;
                    int[] BinNumber = new int[numBins + 1];
                    double Sum = 0;
                    for (int i = 0; i < numIterations; i++)
                    {
                        double randomN = GetRandomNormal(nMean, nStdev);
                        int randomPv = GetRandomUnifrom(PvMinUser, PvMaxUser);
                        double Pt = randomN * randomPv;
                        int binindex = GetBinIndex(PtMin, PtMax, numBins, Pt);
                        if (binindex < BinNumber.Length)
                        {
                            BinNumber[binindex] = BinNumber[binindex] + 1;
                        }
                        Sum = Sum + Pt;
                    }
                    int arraycount = BinNumber.Sum();
                    double Average = Sum / arraycount;
                    txtAverageProfit.Text = "$" + Average.ToString("N");
                    chart.Series[0].Points.Clear();
                    for (int a = 1; a < BinNumber.Length; a++)
                    {
                        chart.Series[0].Points.AddXY(a, BinNumber[a]);
                        chart.Series[0].Points[a - 1].AxisLabel = (PtMin + BinIncrement * a).ToString("");
                    }
                }
                if (rbUniPV.Checked && rbUniN.Checked)
                {
                    double PtMin = nMinUser * PvMinUser;
                    double PtMax = nMaxUser * PvMaxUser;
                    double Range = PtMax - PtMin;
                    double BinIncrement = Range / numBins;
                    int[] BinNumber = new int[numBins + 1];
                    double Sum = 0;
                    for (int i = 0; i < numIterations; i++)
                    {
                        int randomN = GetRandomUnifrom(nMinUser, nMaxUser);
                        int randomPv = GetRandomUnifrom(PvMinUser, PvMaxUser);
                        double Pt = randomN * randomPv;
                        int binindex = GetBinIndex(PtMin, PtMax, numBins, Pt);
                        if (binindex < BinNumber.Length)
                        {
                            BinNumber[binindex] = BinNumber[binindex] + 1;
                        }
                        Sum = Sum + Pt;
                    }
                    int arraycount = BinNumber.Sum();
                    double Average = Sum / arraycount;
                    txtAverageProfit.Text = "$" + Average.ToString("N");
                    chart.Series[0].Points.Clear();
                    for (int a = 1; a < BinNumber.Length; a++)
                    {
                        chart.Series[0].Points.AddXY(a, BinNumber[a]);
                        chart.Series[0].Points[a - 1].AxisLabel = (PtMin + BinIncrement * a).ToString("");
                    }
                }
            }
            catch
            {
                MessageBox.Show("Error Occured: Check Inputs");
            }
        }
        private void txtPvStanDev_TextChanged(object sender, EventArgs e)
        {
            chart.Series[0].Points.Clear();
        }
        private void txtMin_TextChanged(object sender, EventArgs e)
        {
            chart.Series[0].Points.Clear();
        }
        private void txtMax_TextChanged(object sender, EventArgs e)
        {
            chart.Series[0].Points.Clear();
        }
        private void txtBins_TextChanged(object sender, EventArgs e)
        {
            chart.Series[0].Points.Clear();
        }
        private void txtItertions_TextChanged(object sender, EventArgs e)
        {
            chart.Series[0].Points.Clear();
        }
        private void txtPvMax_TextChanged(object sender, EventArgs e)
        {
            chart.Series[0].Points.Clear();
        }
        private void txtPvMin_TextChanged(object sender, EventArgs e)
        {
            chart.Series[0].Points.Clear();
        }
        private void txtNMean_TextChanged(object sender, EventArgs e)
        {
            chart.Series[0].Points.Clear();
        }
        private void txtNStanDev_TextChanged(object sender, EventArgs e)
        {
            chart.Series[0].Points.Clear();
        }
    }
}