Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Intoduction to Java Assigment - Help needed

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Intoduction to Java Assigment - Help needed

    Hi folks,
    I'm a total newbie, but please if you have the time and patience, your guys' help would be very much appreciated

    I have the following assignment:
    See program BlackScholes.java (BlackScholes.java) that takes s, x, r, sigma, and t from the command line and prints the Black-Scholes value.

    Typically the volatility is the unknown value in the Black-Scholes formula. Write a program that reads s, x, r, t, and the current price of the option from the command line and uses binary search (see Exercise 2.1.29) to compute σ.

    public class BlackScholesBinary {
     
     
     
    // Black-Scholes formula (calculates the BS option value, using the class Gaussian. This method will be used in the binary search below to find the volatility)
     
        public static double callPrice(double s, double x, double r, double sigma, double t) {
            double a = (Math.log(s/x) + (r + sigma * sigma/2) * t) / (sigma * Math.sqrt(t));
            double b = a - sigma * Math.sqrt(t);
            return s * Gaussian.Phi(a) - x * Math.exp(-r * t) * Gaussian.Phi(b);
        }
     
           public static double getSigma(double s, double x, double r, double t, double price, double lo, double hi, double delta) { 
     
           lo = 0.0;
           hi = 100000.0;
           delta = 0.01;  
           double mid = lo + (hi - lo) / 2;
           if (price < callPrice(s, x, r, mid, t))                                         return getSigma(s, x, r, t, price, lo, mid, delta);     // should look for a lower volatility
           else if  (price > callPrice(s, x, r, mid, t))                                   return getSigma(s, x, r, t, price, mid, hi, delta);    // should look for a higher volatility
           else if (price == callPrice(s, x, r, mid, t) && (hi - lo < delta))              return mid;    //should return the found volatility as a return of the function
            }
     
     public static void main(String[] args) {
            double s     = Double.parseDouble(args[0]);
            double x     = Double.parseDouble(args[1]);
            double r     = Double.parseDouble(args[2]);
            double t     = Double.parseDouble(args[3]);
            double price = Double.parseDouble(args[4]);
            double vol = getSigma(s, x, r, t, price);
     
            System.out.println("The volatility is" + vol + " %");
        }
    }
    Error message received when trying to compile the program: public static double getSigma() - This method must return a result of type double.

    Could someone please tell me whats wrong with my program??
    Last edited by jps; November 20th, 2012 at 08:53 PM. Reason: added code tags


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Intoduction to Java Assigment - Help needed

    Could someone please tell me whats wrong with my program??
    The error message did:
    method must return a result of type double
    public static double getSigma(.....) does not return a value of type double through every possible path of execution.
    If the first second and last if are all false, execution falls out the bottom of the method without a return of type double.

  3. #3
    Junior Member
    Join Date
    Nov 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Intoduction to Java Assigment - Help needed

    I modified the code in the following manner:

    public class BlackScholesBinary {



    // Black-Scholes formula (calculates the BS option value, to which the binary search below will be used to find the volatility)
    public static double callPrice(double s, double x, double r, double sigma, double t) {
    double a = (Math.log(s/x) + (r + sigma * sigma/2) * t) / (sigma * Math.sqrt(t));
    double b = a - sigma * Math.sqrt(t);
    return s * Gaussian.Phi(a) - x * Math.exp(-r * t) * Gaussian.Phi(b);
    }

    public static double getSigma(double s, double x, double r, double t, double price, double lo, double hi, double delta) { //delta = tolerance
    // lo = 0.0;
    // hi = 100000.0;
    // delta = 0.01;
    double mid = lo + (hi - lo) / 2;
    if (price < callPrice(s, x, r, mid, t)) return getSigma(s, x, r, t, price, lo, mid, delta);
    else if (price > callPrice(s, x, r, mid, t)) return getSigma(s, x, r, t, price, mid, hi, delta);
    else if (price == callPrice(s, x, r, mid, t) && (hi - lo < delta)) return mid;
    else return Double.NaN;
    }


    public static void main(String[] args) {
    double s = Double.parseDouble(args[0]);
    double x = Double.parseDouble(args[1]);
    double r = Double.parseDouble(args[2]);
    double t = Double.parseDouble(args[3]);
    double price = Double.parseDouble(args[4]);
    double vol = getSigma(s, x, r, t, price, 0.0, 100000.0, 0.01);
    if (vol == Double.NaN) {
    System.out.println("Invalid parameters inserted - results in nonreal volatility");
    }
    else {
    System.out.println("The volatility is " + 100*vol + " %");
    }
    }
    }

    This works for some (apparently small values of volatility), as for e.g. when given an input
    > run BlackScholesBinary 30.14 15.0 0.01 0.25 15.178

    The program returns "The volatility is 42.30907986831588 %"

    But when I give an input
    > run BlackScholesBinary 30.14 15.0 0.01 0.25 15.177, the program crashes.

    Whats wrong with the code???

Similar Threads

  1. intoduction
    By sidharth bathina in forum Member Introductions
    Replies: 1
    Last Post: October 11th, 2012, 09:01 AM
  2. new to java, and need help with assigment
    By knotski in forum What's Wrong With My Code?
    Replies: 22
    Last Post: March 30th, 2012, 01:48 PM
  3. Need help with java assigment
    By Lutzor in forum What's Wrong With My Code?
    Replies: 0
    Last Post: October 3rd, 2011, 05:29 PM
  4. No Time To Deal With The Assigment
    By Vins25 in forum Java Theory & Questions
    Replies: 14
    Last Post: May 3rd, 2011, 07:39 AM
  5. help needed with java...
    By amzz in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: April 12th, 2011, 03:33 AM

Tags for this Thread