Assembla home | Assembla project page
 

Changeset 147

Show
Ignore:
Timestamp:
05/05/08 11:53:24 (2 months ago)
Author:
ToyinA
Message:

More base optimization classes and enhancements

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/QLNet/QLNet/Math/Optimization/Constraint.cs

    r117 r147  
    11/* 
    22 Copyright (C) 2008 Siarhei Novik (snovik@gmail.com) 
    3    
     3 Copyright (C) 2008 Toyin Akin (toyin_akin@hotmail.com) 
     4  
    45 This file is part of QLNet Project http://www.qlnet.org 
    56 
     
    6667    }; 
    6768 
     69        //! %Constraint imposing positivity to all arguments 
     70        public class PositiveConstraint : Constraint 
     71        { 
     72        public PositiveConstraint() 
     73            : base(new PositiveConstraint.Impl()) 
     74        { 
     75        } 
     76 
     77        private class Impl : IConstraint 
     78                { 
     79                        public bool test(Vector v) 
     80                        { 
     81                                for (int i =0; i<v.Count; ++i) 
     82                                { 
     83                                        if (v[i] <= 0.0) 
     84                                                return false; 
     85                                } 
     86                                return true; 
     87                        } 
     88                } 
     89        } 
     90 
     91        //! %Constraint imposing all arguments to be in [low,high] 
     92        public class BoundaryConstraint : Constraint 
     93        { 
     94        public BoundaryConstraint(double low, double high) 
     95            : base(new BoundaryConstraint.Impl(low, high)) 
     96        { 
     97        } 
     98 
     99        private class Impl : IConstraint 
     100                { 
     101            private double low_; 
     102            private double high_; 
     103 
     104            public Impl(double low, double high) 
     105                        { 
     106                                low_ = low; 
     107                                high_ = high; 
     108                        } 
     109                        public bool test(Vector v) 
     110                        { 
     111                for (int i = 0; i < v.Count; i++) 
     112                                { 
     113                    if ((v[i] < low_) || (v[i] > high_)) 
     114                                                return false; 
     115                                } 
     116                                return true; 
     117                        } 
     118                } 
     119        } 
     120 
    68121    //! %Constraint enforcing both given sub-constraints 
    69122    public class CompositeConstraint : Constraint { 
  • trunk/QLNet/QLNet/Models/Parameter.cs

    r117 r147  
    11/* 
    22 Copyright (C) 2008 Siarhei Novik (snovik@gmail.com) 
    3    
     3 Copyright (C) 2008 Toyin Akin (toyin_akin@hotmail.com) 
     4 *  
    45 This file is part of QLNet Project http://www.qlnet.org 
    56 
     
    2223using System.Text; 
    2324 
    24 namespace QLNet { 
     25namespace QLNet 
     26
    2527    //! Base class for model arguments 
    26     public class Parameter { 
     28    public class Parameter 
     29    { 
    2730        protected Impl impl_; 
    2831        public Impl implementation() { return impl_; } 
    29      
     32 
    3033        protected Vector params_; 
    3134        public Vector parameters() { return params_; } 
     
    3336        protected Constraint constraint_; 
    3437 
    35         public Parameter() { 
     38        public Parameter() 
     39        { 
    3640            constraint_ = new NoConstraint(); 
    3741        } 
    3842 
    39         protected Parameter(int size, Impl impl, Constraint constraint) { 
     43        protected Parameter(int size, Impl impl, Constraint constraint) 
     44        { 
    4045            impl_ = impl; 
    4146            params_ = new Vector(size); 
     
    5055 
    5156        //! Base class for model parameter implementation 
    52         public abstract class Impl { 
     57        public abstract class Impl 
     58        { 
    5359            public abstract double value(Vector p, double t); 
    5460        } 
    5561    } 
     62 
     63    //! Standard constant parameter \f$ a(t) = a \f$ 
     64    public class ConstantParameter : Parameter 
     65    { 
     66        new private class Impl : Parameter.Impl 
     67        { 
     68            public override double value(Vector parameters, double UnnamedParameter1) 
     69            { 
     70                return parameters[0]; 
     71            } 
     72        } 
     73        public ConstantParameter(Constraint constraint) 
     74            : base(1, new ConstantParameter.Impl(), constraint) 
     75        { 
     76        } 
     77 
     78        public ConstantParameter(double value, Constraint constraint) 
     79            : base(1, new ConstantParameter.Impl(), constraint) 
     80        { 
     81            params_[0] = value; 
     82 
     83            if (!(testParams(params_))) 
     84                throw new ApplicationException(": invalid value"); 
     85        } 
     86 
     87    } 
     88 
     89    //! %Parameter which is always zero \f$ a(t) = 0 \f$ 
     90    public class NullParameter : Parameter 
     91    { 
     92        new private class Impl : Parameter.Impl 
     93        { 
     94            public override double value(Vector UnnamedParameter1, double UnnamedParameter2) 
     95            { 
     96                return 0.0; 
     97            } 
     98        } 
     99        public NullParameter() 
     100            : base(0, new NullParameter.Impl(), new NoConstraint()) 
     101        { 
     102        } 
     103    } 
     104 
     105    //! Piecewise-constant parameter 
     106    //    ! \f$ a(t) = a_i if t_{i-1} \geq t < t_i \f$. 
     107    //        This kind of parameter is usually used to enhance the fitting of a 
     108    //        model 
     109    //     
     110    public class PiecewiseConstantParameter : Parameter 
     111    { 
     112        new private class Impl : Parameter.Impl 
     113        { 
     114            public Impl(List<double> times) 
     115            { 
     116                times_ = times; 
     117            } 
     118 
     119            public override double value(Vector parameters, double t) 
     120            { 
     121                int size = times_.Count; 
     122                for (int i = 0; i < size; i++) 
     123                { 
     124                    if (t < times_[i]) 
     125                        return parameters[i]; 
     126                } 
     127                return parameters[size]; 
     128            } 
     129            private List<double> times_; 
     130        } 
     131        public PiecewiseConstantParameter(List<double> times) 
     132            : base(times.Count + 1, new PiecewiseConstantParameter.Impl(times), new NoConstraint()) 
     133        { 
     134        } 
     135    } 
     136 
     137    ////! Deterministic time-dependent parameter used for yield-curve fitting 
     138    //public class TermStructureFittingParameter : Parameter 
     139    //{ 
     140    //    private class NumericalImpl : Parameter.Impl 
     141    //    { 
     142    //        public NumericalImpl(Handle<YieldTermStructure> termStructure) 
     143    //        { 
     144    //            times_ = null; 
     145    //            values_ = null; 
     146    //            termStructure_ = termStructure; 
     147    //        } 
     148 
     149    //        public void setvalue(double t, double x) 
     150    //        { 
     151    //            times_.Add(t); 
     152    //            values_.Add(x); 
     153    //        } 
     154    //        public void change(double x) 
     155    //        { 
     156    //            values_[values_.Count-1] = x; 
     157    //        } 
     158    //        public void reset() 
     159    //        { 
     160    //            times_.Clear(); 
     161    //            values_.Clear(); 
     162    //        } 
     163    //        public override double value(Vector UnnamedParameter1, double t) 
     164    //        { 
     165    //           //std::vector<Time>::const_iterator result = 
     166    //           //     std::find(times_.begin(), times_.end(), t); 
     167    //           // QL_REQUIRE(result!=times_.end(), 
     168    //           //            "fitting parameter not set!"); 
     169    //           // return values_[result - times_.begin()]; 
     170 
     171    //            throw new NotImplementedException("Need to implement the FindIndex method()"); 
     172 
     173    //            //int nIndex = times_.FindIndex( delegate(double val) { return val == locVal; }); 
     174    //            //if (nIndex == -1) 
     175    //            //    throw new ApplicationException("fitting parameter not set!"); 
     176 
     177    //            //return values_[nIndex]; 
     178    //        } 
     179 
     180    //        public Handle<YieldTermStructure> termStructure() 
     181    //        { 
     182    //            return termStructure_; 
     183    //        } 
     184    //        private List<double> times_; 
     185    //        private List<double> values_; 
     186    //        private Handle<YieldTermStructure> termStructure_; 
     187    //    } 
     188 
     189    //    public TermStructureFittingParameter(Parameter.Impl impl) 
     190    //        : base(0, impl, new NoConstraint()) 
     191    //    { 
     192    //    } 
     193 
     194    //    public TermStructureFittingParameter(Handle<YieldTermStructure> term) 
     195    //        : base(0, new NumericalImpl(term), new NoConstraint()) 
     196    //    { 
     197    //    } 
     198    //} 
    56199}