Example #1
0
        public void Study(string filename, bool plotErrorBars, bool plotMarketNeutral, bool plotEvents, string marketSymbol)
        {
            Console.WriteLine("Starting Event Study");

            // compute 0 centered daily returns
            foreach (var symbol in symbols)
            {
                var symList = close.ColumnList(symbol);
                dailyReturns[symbol] = symList.Returnize();
            }

            var marketList = close.ColumnList(marketSymbol);
            IList<float> marketDaily = marketList.Returnize();

            // make it market neutral
            if (plotMarketNeutral)
            {
                foreach (var symbol in symbols)
                {
                    marketNeutralDm[symbol] = new List<float>();
                    for (int i = 0; i < marketDays; i++)
                        marketNeutralDm[symbol].Add(dailyReturns[symbol][i] - marketDaily[i]);
                }

                // remove the market column from consideration
                marketNeutralDm.Remove(marketSymbol);
                IList<int> removedEventList;
                eventMatrix.TryRemove(marketSymbol, out removedEventList);
                symbols.Remove(marketSymbol);
            }
            else
            {
                marketNeutralDm = dailyReturns;
            }

            // Wipe out events which are on the boundary.
            Parallel.ForEach(symbols,
                             symbol =>
                             {
                                 foreach (int i in Enumerable.Range(0, lookbackDays))
                                     eventMatrix[symbol][i] = 0;

                                 foreach (int i in Enumerable.Range(marketDays - lookforwardDays, lookforwardDays))
                                     eventMatrix[symbol][i] = 0;
                             });

            // prepare to build impact matrix
            var returns = marketNeutralDm;
            int numCols = eventMatrix.Keys.Count;

            int numEvents = eventMatrix.Values.SelectMany(i => i).Sum();

            // create a blank impact matrix
            var impact = new double[totalGraphDays, numEvents];

            int currcol = 0;
            // step through each event in event matrix
            foreach (int column in Enumerable.Range(0, numCols))
            {
                string currSym = symbols[column];
                // search event days
                int eventDays = eventMatrix[currSym].Count;
                foreach (int eventIndex in Enumerable.Range(0, eventDays))
                {
                    int eventState = eventMatrix[symbols[column]][eventIndex];

                    // when we find an event
                    if (eventState == 1)
                    {
                        // copy the daily returns in to the impact matrix
                        int start = eventIndex - lookbackDays;
                        start = (start < 0) ? 0 : start;
                        int end = eventIndex + lookforwardDays + 1;

                        for (int i = start, j = 0; i < end && j < eventDays; i++, j++)
                        {
                            impact[j, currcol] = returns[currSym][i];
                        }

                        currcol = currcol + 1;
                    }
                }
            }

            // now compute cumulative daily returns
            Parallel.ForEach(Enumerable.Range(0, impact.GetLength(1)),
                             j =>
                             {
                                 foreach (int i in Enumerable.Range(0, impact.GetLength(0)))
                                     impact[i, j] = impact[i, j] + 1.0d;
                             });

            impact = impact.CumulativeProduct();

            Parallel.ForEach(ParallelEnumerable.Range(0, impact.GetLength(1)),
                             j =>
                             {
                                 var divisor = impact[0, j];
                                 foreach (int i in Enumerable.Range(0, impact.GetLength(0)))
                                 {
                                     impact[i, j] = impact[i, j] / divisor;
                                 }
                             });

            // normalize everything to the time of the event
            Parallel.ForEach(ParallelEnumerable.Range(0, impact.GetLength(1)),
                             j =>
                             {
                                 var divisor = impact[lookbackDays, j];
                                 foreach (int i in Enumerable.Range(0, impact.GetLength(0)))
                                     impact[i, j] = impact[i, j] / divisor;
                             });

            // prepare data for plot
            studyStat = new List<float>(impact.GetLength(0));
            studyStd = new List<float>(impact.GetLength(0));
            foreach (int i in Enumerable.Range(0, impact.GetLength(0)))
            {
                var row = new List<float>();
                for (int j = 0; j < impact.GetLength(1); j++)
                {
                    row.Add((float)impact[i, j]);
                }
                studyStat.Add(row.Average());
                studyStd.Add(row.StdDev());
            }

            studyRange = Enumerable.Range(-lookbackDays, (lookbackDays + lookforwardDays + 1));

            Plot(filename, impact, numEvents, plotMarketNeutral, plotEvents, plotErrorBars);
        }
        private ResultsGroup AverageResultsGroup(List<ResultsGroup> combiningRows)
        {
            List<Int32> NumChargeStates = new List<int>();
            List<Double> ScanDensity= new List<Double>() ;
            List<Double> NumModiStates= new List<Double>() ;
            List<Double> TotalVolume= new List<Double>() ;
            List<Double> ExpectedA= new List<Double>() ;
            List<Double> Score= new List<Double>() ;
            List<Double> CentroidScan= new List<Double>() ;
            List<Double> NumScan= new List<Double>() ;
            List<Double> AvgSigNoise= new List<Double>() ;
            //These are for calculating the features
            List<Int32> MaxScanNum= new List<int>() ;
            List<Int32> MinScanNum= new List<int>() ;
            List<Int32> ScanNums= new List<int>() ;
            List<Int32> ChargeStateList = new List<int>();
            List<Double> AvgSigNoiseList= new List<Double>() ;
            List<Double> CentroidScanLR= new List<Double>() ;
            List<Double> AvgAA2List= new List<Double>() ;
            List<Double> RelativeTotalVolume = new List<double>();

            List<Int32> scan_num = new List<Int32>();
            List<Int32> charge = new List<Int32>();
            List<Int32> abundance = new List<Int32>();
            List<Double> mz = new List<Double>();
            List<Double> fit = new List<Double>();
            List<Double> average_mw = new List<Double>();
            List<Double> monoisotopic_mw = new List<Double>();
            List<Double> mostabundant_mw = new List<Double>();
            List<Double> fwhm = new List<Double>();
            List<Double> signal_noise = new List<Double>();
            List<Int32> mono_abundance = new List<Int32>();
            List<Int32> mono_plus2_abundance = new List<Int32>();
            List<Int32> flag = new List<Int32>();
            List<Double> interference_sore = new List<Double>();

            ResultsGroup FinalAns = new ResultsGroup();
            FinalAns.ListOfOriginalTotalVolumes = combiningRows[0].ListOfOriginalTotalVolumes;

            for (int i = 0; i < combiningRows.Count(); i++)
            {
                NumChargeStates.Add(combiningRows[i].NumChargeStates);
                ScanDensity.Add(combiningRows[i].ScanDensity);
                NumModiStates.Add(combiningRows[i].NumModiStates);
                TotalVolume.Add(combiningRows[i].TotalVolume);
                ExpectedA.Add(combiningRows[i].ExpectedA);
                Score.Add(combiningRows[i].Score);
                CentroidScan.Add(combiningRows[i].CentroidScan);
                NumScan.Add(combiningRows[i].NumOfScan);
                AvgSigNoise.Add(combiningRows[i].AvgSigNoise);
                //These are for calculating the features
                MaxScanNum.Add(combiningRows[i].MaxScanNum);
                MinScanNum.Add(combiningRows[i].MinScanNum);
                CentroidScanLR.Add(combiningRows[i].CentroidScanLR);
                AvgAA2List.AddRange(combiningRows[i].AvgAA2List);
                RelativeTotalVolume.Add(combiningRows[i].RelativeTotalVolume);

                charge.Add(combiningRows[i].DeconRow.charge);
                scan_num.Add(combiningRows[i].DeconRow.ScanNum);
                abundance.Add(combiningRows[i].DeconRow.abundance);
                mz.Add(combiningRows[i].DeconRow.mz);
                fit.Add(combiningRows[i].DeconRow.fit);
                average_mw.Add(combiningRows[i].DeconRow.average_mw);
                monoisotopic_mw.Add(combiningRows[i].DeconRow.MonoisotopicMassWeight);
                mostabundant_mw.Add(combiningRows[i].DeconRow.mostabundant_mw);
                fwhm.Add(combiningRows[i].DeconRow.fwhm);
                signal_noise.Add(combiningRows[i].DeconRow.SignalNoiseRatio);
                mono_abundance.Add(combiningRows[i].DeconRow.MonoisotopicAbundance);
                mono_plus2_abundance.Add(combiningRows[i].DeconRow.MonoisotopicPlus2Abundance);
                flag.Add(combiningRows[i].DeconRow.flag);
                interference_sore.Add(combiningRows[i].DeconRow.interference_sore);
                for (int h = 0; h < combiningRows[i].ListOfOriginalTotalVolumes.Count(); h++)
                {
                    if (combiningRows[i].ListOfOriginalTotalVolumes[h] > FinalAns.ListOfOriginalTotalVolumes[h])
                    {
                        FinalAns.ListOfOriginalTotalVolumes[h] = combiningRows[i].ListOfOriginalTotalVolumes[h];
                    }
                }

            }

            FinalAns.DeconRow = new DeconRow();
            FinalAns.PredictedComposition = combiningRows[0].PredictedComposition;
            FinalAns.NumChargeStates = Convert.ToInt32(NumChargeStates.Average());
            FinalAns.ScanDensity = ScanDensity.Average();
            FinalAns.NumModiStates = NumModiStates.Average();
            FinalAns.TotalVolume = TotalVolume.Average();
            FinalAns.ExpectedA = ExpectedA.Average();
            FinalAns.Score = Score.Average();
            FinalAns.CentroidScan = CentroidScan.Average();
            FinalAns.NumOfScan = NumScan.Average();
            FinalAns.AvgSigNoise = AvgSigNoise.Average();
            FinalAns.MaxScanNum = Convert.ToInt32(MaxScanNum.Average());
            FinalAns.MinScanNum = Convert.ToInt32(MinScanNum.Average());
            FinalAns.CentroidScanLR = CentroidScanLR.Average();
            FinalAns.TotalVolumeSD = TotalVolume.StdDev();
            FinalAns.RelativeTotalVolumeSD = RelativeTotalVolume.StdDev();
            FinalAns.AvgAA2List = new List<double>();
            FinalAns.AvgAA2List.Add(AvgAA2List.Average());
            FinalAns.RelativeTotalVolume = RelativeTotalVolume.Average();

            FinalAns.DeconRow.ScanNum = Convert.ToInt32(scan_num.Average());
            FinalAns.DeconRow.abundance = Convert.ToInt32(abundance.Average());
            FinalAns.DeconRow.mz = mz.Average();
            FinalAns.DeconRow.fit = fit.Average();
            FinalAns.DeconRow.average_mw = average_mw.Average();
            FinalAns.DeconRow.MonoisotopicMassWeight = monoisotopic_mw.Average();
            FinalAns.DeconRow.mostabundant_mw = mostabundant_mw.Average();
            FinalAns.DeconRow.fwhm = fwhm.Average();
            FinalAns.DeconRow.SignalNoiseRatio = signal_noise.Average();
            FinalAns.DeconRow.MonoisotopicAbundance = Convert.ToInt32(mono_abundance.Average());
            FinalAns.DeconRow.MonoisotopicPlus2Abundance = Convert.ToInt32(mono_plus2_abundance.Average());
            FinalAns.DeconRow.flag = Convert.ToInt32(flag.Average());
            FinalAns.DeconRow.interference_sore = interference_sore.Average();

            return FinalAns;
        }