Ejemplo n.º 1
0
        public GPDApproximation(IList <double> data, FittingMethod method = FittingMethod.Pickands_SupNorm, Random rand = null)
        {
            if (data.Count < 30)
            {
                throw new ArgumentException("Insufficient data count for Pickands Balkema De Haan theorem.");
            }
            sortedData = new List <double>(data);
            sortedData.Sort();
            int m; // Transition index

            switch (method)
            {
            case FittingMethod.BFGS_MSE:
                ApproximateExcessDistributionParametersBFGS(sortedData, out a, out c, out transitionAbscissa);
                // Compute the index of the closest element that is at or before u in the data
                int transitionIndex = sortedData.BinarySearch(transitionAbscissa);
                if (transitionIndex < 0)
                {
                    transitionIndex = ~transitionIndex;
                }
                transitionProportion = transitionIndex * 1.0 / sortedData.Count;     // Shouldn't this be + 1 here?
                break;

            case FittingMethod.Moments_MSE:
                ApproximateExcessDistributionParametersMoments(sortedData, out a, out c, out m);
                transitionProportion = (sortedData.Count - m + 1.0) / sortedData.Count;
                transitionAbscissa   = sortedData[sortedData.Count - m];   // Convert from m to the actual transitionIndex; m is guaranteed to be > 0
                break;

            case FittingMethod.V4:
                ApproximateExcessDistributionParametersV4(sortedData, out a, out c, out transitionAbscissa);
                int transitionIdx = sortedData.BinarySearch(transitionAbscissa);
                if (transitionIdx < 0)
                {
                    transitionIdx = ~transitionIdx;     // Now the index of the next-largest element
                    if (transitionIdx == 0)
                    {
                        transitionProportion = 0; break;
                    }
                    transitionProportion = Interpolation.Lerp(
                        sortedData[transitionIdx - 1], transitionIdx * 1.0 / sortedData.Count,
                        sortedData[transitionIdx], (transitionIdx + 1) * 1.0 / sortedData.Count,
                        transitionAbscissa);
                    break;
                }
                transitionProportion = (transitionIdx + 1) * 1.0 / sortedData.Count;
                break;

            default:
                ApproximateExcessDistributionParametersPickands(sortedData, out a, out c, out m); // Write m to transitionIndex
                transitionProportion = (sortedData.Count - 4 * m + 1.0) / sortedData.Count;
                transitionAbscissa   = sortedData[sortedData.Count - 4 * m];                      // Convert from m to the actual transitionIndex; m is guaranteed to be > 0
                break;
            }

            this.rand = rand ?? Program.rand;
        }
        public FittingMethod fitResults()
        {
            FittingMethod ret = new FittingMethod(NQuantLibcPINVOKE.FittedBondDiscountCurve_fitResults(swigCPtr), false);

            if (NQuantLibcPINVOKE.SWIGPendingException.Pending)
            {
                throw NQuantLibcPINVOKE.SWIGPendingException.Retrieve();
            }
            return(ret);
        }
Ejemplo n.º 3
0
        //! curve reference date fixed for life of curve
        public FittedBondDiscountCurve(Date referenceDate,
                                       List <BondHelper> bondHelpers,
                                       DayCounter dayCounter,
                                       FittingMethod fittingMethod,
                                       double accuracy                  = 1.0e-10,
                                       int maxEvaluations               = 10000,
                                       Vector guess                     = null,
                                       double simplexLambda             = 1.0,
                                       int maxStationaryStateIterations = 100)
            : base(referenceDate, new Calendar(), dayCounter)
        {
            accuracy_       = accuracy;
            maxEvaluations_ = maxEvaluations;
            simplexLambda_  = simplexLambda;
            maxStationaryStateIterations_ = maxStationaryStateIterations;
            guessSolution_ = guess;
            bondHelpers_   = bondHelpers;
            fittingMethod_ = fittingMethod;

            fittingMethod_.curve_ = this;
            setup();
        }
Ejemplo n.º 4
0
 public FittingMethod fitResults() {
   FittingMethod ret = new FittingMethod(NQuantLibcPINVOKE.FittedBondDiscountCurve_fitResults(swigCPtr), false);
   if (NQuantLibcPINVOKE.SWIGPendingException.Pending) throw NQuantLibcPINVOKE.SWIGPendingException.Retrieve();
   return ret;
 }
Ejemplo n.º 5
0
 public FittedBondDiscountCurve(Date referenceDate, RateHelperVector helpers, DayCounter dayCounter, FittingMethod fittingMethod) : this(NQuantLibcPINVOKE.new_FittedBondDiscountCurve__SWIG_9(Date.getCPtr(referenceDate), RateHelperVector.getCPtr(helpers), DayCounter.getCPtr(dayCounter), FittingMethod.getCPtr(fittingMethod)), true) {
   if (NQuantLibcPINVOKE.SWIGPendingException.Pending) throw NQuantLibcPINVOKE.SWIGPendingException.Retrieve();
 }
Ejemplo n.º 6
0
 public FittedBondDiscountCurve(Date referenceDate, RateHelperVector helpers, DayCounter dayCounter, FittingMethod fittingMethod, double accuracy, uint maxEvaluations, QlArray guess) : this(NQuantLibcPINVOKE.new_FittedBondDiscountCurve__SWIG_6(Date.getCPtr(referenceDate), RateHelperVector.getCPtr(helpers), DayCounter.getCPtr(dayCounter), FittingMethod.getCPtr(fittingMethod), accuracy, maxEvaluations, QlArray.getCPtr(guess)), true) {
   if (NQuantLibcPINVOKE.SWIGPendingException.Pending) throw NQuantLibcPINVOKE.SWIGPendingException.Retrieve();
 }
Ejemplo n.º 7
0
 public FittedBondDiscountCurve(uint settlementDays, Calendar calendar, RateHelperVector helpers, DayCounter dayCounter, FittingMethod fittingMethod) : this(NQuantLibcPINVOKE.new_FittedBondDiscountCurve__SWIG_4(settlementDays, Calendar.getCPtr(calendar), RateHelperVector.getCPtr(helpers), DayCounter.getCPtr(dayCounter), FittingMethod.getCPtr(fittingMethod)), true) {
   if (NQuantLibcPINVOKE.SWIGPendingException.Pending) throw NQuantLibcPINVOKE.SWIGPendingException.Retrieve();
 }
Ejemplo n.º 8
0
 public FittedBondDiscountCurve(uint settlementDays, Calendar calendar, RateHelperVector helpers, DayCounter dayCounter, FittingMethod fittingMethod, double accuracy, uint maxEvaluations, QlArray guess, double simplexLambda) : this(NQuantLibcPINVOKE.new_FittedBondDiscountCurve__SWIG_0(settlementDays, Calendar.getCPtr(calendar), RateHelperVector.getCPtr(helpers), DayCounter.getCPtr(dayCounter), FittingMethod.getCPtr(fittingMethod), accuracy, maxEvaluations, QlArray.getCPtr(guess), simplexLambda), true) {
   if (NQuantLibcPINVOKE.SWIGPendingException.Pending) throw NQuantLibcPINVOKE.SWIGPendingException.Retrieve();
 }
 public FittedBondDiscountCurve(Date referenceDate, RateHelperVector helpers, DayCounter dayCounter, FittingMethod fittingMethod, double accuracy, uint maxEvaluations, QlArray guess) : this(NQuantLibcPINVOKE.new_FittedBondDiscountCurve__SWIG_6(Date.getCPtr(referenceDate), RateHelperVector.getCPtr(helpers), DayCounter.getCPtr(dayCounter), FittingMethod.getCPtr(fittingMethod), accuracy, maxEvaluations, QlArray.getCPtr(guess)), true)
 {
     if (NQuantLibcPINVOKE.SWIGPendingException.Pending)
     {
         throw NQuantLibcPINVOKE.SWIGPendingException.Retrieve();
     }
 }
Ejemplo n.º 10
0
 public FittedBondDiscountCurve(uint settlementDays, Calendar calendar, RateHelperVector helpers, DayCounter dayCounter, FittingMethod fittingMethod) : this(NQuantLibcPINVOKE.new_FittedBondDiscountCurve__SWIG_4(settlementDays, Calendar.getCPtr(calendar), RateHelperVector.getCPtr(helpers), DayCounter.getCPtr(dayCounter), FittingMethod.getCPtr(fittingMethod)), true)
 {
     if (NQuantLibcPINVOKE.SWIGPendingException.Pending)
     {
         throw NQuantLibcPINVOKE.SWIGPendingException.Retrieve();
     }
 }
Ejemplo n.º 11
0
 public FittedBondDiscountCurve(uint settlementDays, Calendar calendar, RateHelperVector helpers, DayCounter dayCounter, FittingMethod fittingMethod, double accuracy, uint maxEvaluations, QlArray guess, double simplexLambda) : this(NQuantLibcPINVOKE.new_FittedBondDiscountCurve__SWIG_0(settlementDays, Calendar.getCPtr(calendar), RateHelperVector.getCPtr(helpers), DayCounter.getCPtr(dayCounter), FittingMethod.getCPtr(fittingMethod), accuracy, maxEvaluations, QlArray.getCPtr(guess), simplexLambda), true)
 {
     if (NQuantLibcPINVOKE.SWIGPendingException.Pending)
     {
         throw NQuantLibcPINVOKE.SWIGPendingException.Retrieve();
     }
 }
Ejemplo n.º 12
0
 public FittedBondDiscountCurve(Date referenceDate, RateHelperVector helpers, DayCounter dayCounter, FittingMethod fittingMethod) : this(NQuantLibcPINVOKE.new_FittedBondDiscountCurve__SWIG_9(Date.getCPtr(referenceDate), RateHelperVector.getCPtr(helpers), DayCounter.getCPtr(dayCounter), FittingMethod.getCPtr(fittingMethod)), true)
 {
     if (NQuantLibcPINVOKE.SWIGPendingException.Pending)
     {
         throw NQuantLibcPINVOKE.SWIGPendingException.Retrieve();
     }
 }
Ejemplo n.º 13
0
 internal static global::System.Runtime.InteropServices.HandleRef getCPtr(FittingMethod obj)
 {
     return((obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.swigCPtr);
 }
Ejemplo n.º 14
0
 internal static global::System.Runtime.InteropServices.HandleRef getCPtr(FittingMethod obj) {
   return (obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.swigCPtr;
 }
Ejemplo n.º 15
0
        private static Tuple <Solution, double> Optimize(PackingShape bin, IList <PackingItem> items, SortingMethod sorting, FittingMethod fitting, double delta, bool stackingConstraints, IEvaluator evaluator, CancellationToken token)
        {
            Permutation sorted = null;

            switch (sorting)
            {
            case SortingMethod.Given:
                sorted = new Permutation(PermutationTypes.Absolute, Enumerable.Range(0, items.Count).ToArray());
                break;

            case SortingMethod.VolumeHeight:
                sorted = new Permutation(PermutationTypes.Absolute,
                                         items.Select((v, i) => new { Index = i, Item = v })
                                         .OrderByDescending(x => x.Item.Depth * x.Item.Width * x.Item.Height)
                                         .ThenByDescending(x => x.Item.Height)
                                         .Select(x => x.Index).ToArray());
                break;

            case SortingMethod.HeightVolume:
                sorted = new Permutation(PermutationTypes.Absolute,
                                         items.Select((v, i) => new { Index = i, Item = v })
                                         .OrderByDescending(x => x.Item.Height)
                                         .ThenByDescending(x => x.Item.Depth * x.Item.Width * x.Item.Height)
                                         .Select(x => x.Index).ToArray());
                break;

            case SortingMethod.AreaHeight:
                sorted = new Permutation(PermutationTypes.Absolute,
                                         items.Select((v, i) => new { Index = i, Item = v })
                                         .OrderByDescending(x => x.Item.Depth * x.Item.Width)
                                         .ThenByDescending(x => x.Item.Height)
                                         .Select(x => x.Index).ToArray());
                break;

            case SortingMethod.HeightArea:
                sorted = new Permutation(PermutationTypes.Absolute,
                                         items.Select((v, i) => new { Index = i, Item = v })
                                         .OrderByDescending(x => x.Item.Height)
                                         .ThenByDescending(x => x.Item.Depth * x.Item.Width)
                                         .Select(x => x.Index).ToArray());
                break;

            case SortingMethod.ClusteredAreaHeight:
                double clusterRange = bin.Width * bin.Depth * delta;
                sorted = new Permutation(PermutationTypes.Absolute,
                                         items.Select((v, i) => new { Index = i, Item = v, ClusterId = (int)(Math.Ceiling(v.Width * v.Depth / clusterRange)) })
                                         .GroupBy(x => x.ClusterId)
                                         .Select(x => new { Cluster = x.Key, Items = x.OrderByDescending(y => y.Item.Height).ToList() })
                                         .OrderByDescending(x => x.Cluster)
                                         .SelectMany(x => x.Items)
                                         .Select(x => x.Index).ToArray());
                break;

            case SortingMethod.ClusteredHeightArea:
                double clusterRange2 = bin.Height * delta;
                sorted = new Permutation(PermutationTypes.Absolute,
                                         items.Select((v, i) => new { Index = i, Item = v, ClusterId = (int)(Math.Ceiling(v.Height / clusterRange2)) })
                                         .GroupBy(x => x.ClusterId)
                                         .Select(x => new { Cluster = x.Key, Items = x.OrderByDescending(y => y.Item.Depth * y.Item.Width).ToList() })
                                         .OrderByDescending(x => x.Cluster)
                                         .SelectMany(x => x.Items)
                                         .Select(x => x.Index).ToArray());
                break;

            default: throw new ArgumentException("Unknown sorting method: " + sorting);
            }

            ExtremePointPermutationDecoderBase decoder = null;

            switch (fitting)
            {
            case FittingMethod.FirstFit:
                decoder = new ExtremePointPermutationDecoder();
                break;

            case FittingMethod.FreeVolumeBestFit:
                decoder = new FreeVolumeBestFitExtremePointPermutationDecoder();
                break;

            case FittingMethod.ResidualSpaceBestFit:
                decoder = new ResidualSpaceBestFitExtremePointPermutationDecoder();
                break;

            default: throw new ArgumentException("Unknown fitting method: " + fitting);
            }

            var sol = decoder.Decode(sorted, bin, items, stackingConstraints);
            var fit = evaluator.Evaluate(sol);

            return(Tuple.Create(sol, fit));
        }