Problem nr39 Share Price Volatility

Back to General discussions forum

Vladimir_V     2015-06-12 05:21:26

This problem looks so simple at first sight, actually it is simple but still i am not able to get done with it. I followed strictly the algorithm. Also a check manually couple of numbers and Standard deviation is far to be 4 times greater than broker commision but the stock appears in final result. Mister Admin is Everything ok with checking at this problem ?

Vladimir_V     2015-06-12 06:13:21

I mean with Standard deviationa less than 4 , thes stock is in list of goods stocks

Guy Gervais     2015-06-12 17:40:03
User avatar

Well 132 persons have solved that problem, so the checker is probably ok.

Can you post an example of data and your results? With a bit more info, we might be able to help you.

(You can also usually refresh the page to get new test data, in case the checker really has a rare problem...)

Vladimir_V     2015-06-12 19:31:11

24 DALG 76 76 76 75 75 76 77 76 77 76 77 77 77 76 SAKK 25 25 24 23 24 23 23 23 25 23 25 27 24 21 FANT 169 168 168 169 168 167 168 167 167 167 167 167 168 167 INSX 23 25 27 25 24 24 23 21 19 19 17 14 12 15 SLVR 52 54 57 59 62 62 59 56 52 56 55 53 50 52 VDKL 133 133 134 134 134 133 134 135 134 133 134 134 134 133 CKCL 20 19 19 18 17 17 16 16 17 16 16 17 16 17 MARU 72 71 70 69 70 70 70 70 71 72 73 72 71 72 GOLD 22 22 25 27 24 26 23 23 23 22 21 19 16 14 ASDF 22 19 21 24 26 26 29 29 31 28 27 30 29 30 JABA 154 155 155 154 154 155 156 157 156 157 156 156 157 157 SUGR 21 22 23 24 23 23 24 25 25 24 24 25 24 25 PNSN 22 23 21 19 19 17 19 19 19 20 18 18 19 17 OBAM 76 78 77 77 77 78 80 82 83 81 83 85 83 81 GEEK 89 88 88 87 89 91 89 91 91 93 91 89 89 91 JOOG 22 23 22 23 24 25 26 26 26 25 26 27 28 27 FOTA 24 21 21 22 19 16 17 14 12 15 12 15 13 12 ZEOD 23 24 23 22 22 23 22 21 21 20 19 20 20 21 WOWY 33 33 32 33 33 33 34 34 34 33 32 32 31 31 YUKA 170 168 167 170 169 166 163 163 166 166 164 163 166 165 FLNT 64 62 61 59 60 61 59 60 59 58 56 54 52 54 IMIX 21 22 20 22 19 16 14 13 13 11 12 9 10 10 MYTH 182 181 181 181 179 181 182 180 182 184 186 187 185 183 BLEP 115 116 116 116 115 114 114 115 114 113 112 112 113 114

INSX IMIX ------------------- Only two have > 4 according to my calculus

Vladimir_V     2015-06-12 21:15:16

import java.util.Scanner; public class SharePriceVolotile {

public static void main(String[] args) {
    Scanner sc=new Scanner(System.in);
    int num=sc.nextInt();
    String evit=sc.nextLine();
    for(int i=0;i<num;i++){
        String Numbers=sc.nextLine();
        String []array=Numbers.split(" ");
        double [] numbers=new double[14];
        double sum=0;

        for(int j=0;j<14;j++){
            numbers[j]=Double.parseDouble(array[j+1]);
        }

        for(int j=0;j<14;j++){
            sum+=numbers[j];
        }

        //System.out.print(sum+" ");

        double med=sum/14.0;

        //System.out.print(med+" ");

        double t=0;
        for(int j=0;j<14;j++){
            t+=Math.pow(Math.abs(med-numbers[j]), 2);   
        }
        //System.out.print(t+" ");
        double dev=t/14.0;
        //System.out.print(dev+" ");

        double rez=Math.sqrt(dev);
        //System.out.print(rez+" ");

        if(dev>=4)
        System.out.print(array[0]+" ");
    }


}

}

OldProgrammer     2015-06-13 00:52:19

You're almost there.

Read again what the problem specification says about the standard deviation.

Guy Gervais     2015-06-13 02:30:54
User avatar

Out of curiosity, and if you don't mind me asking, just how old is the OldProgrammer?

OldProgrammer     2015-06-14 01:42:04

Guy - the OldProgrammer's age is a bit too close to 50 for comfort!

I chose the name because I think it's likely that I'm quite a bit older than the average user here and I've been programming for a long time. It's surprising how many methods one doesn't encounter over a career.

Guy Gervais     2015-06-14 03:54:41
User avatar

Considering that I'm also closer to 50 than I am to 40... I'd say you're not that old :)

Quandray     2015-06-14 06:36:53
User avatar

You youngsters are really making me feel old :-) I'm 61

Marchel Sugi     2015-07-29 17:27:00
User avatar

Hi, I want to ask something regarding this exercise..
I've already passed it, but that is because I've got a 'perfect set of questions' (after retrying many times). :P
Usually there is one or two mistakes before because of the double value precision in my calculation of 'standard deviation'.

For example:

JABA 58 57 59 56 56 56 57 55 52 53 54 51 54 56  

My code calculations:

Standard Deviation: 2.2038926600773587  
Commission Times 4: 2.2  
SD >= CTF so PRINT "JABA"  

... and its WRONG.. :D

I finally got "CORRECT" after changing this:

(SD >= CTF)  to  
if SD - CTF >= 0.09

I suspect my code was wrong in calculating the deviations,

Thank You

Marchel Sugi     2015-07-29 18:14:07
User avatar

Hmm...

Which 'Commission' comparator to use?

(Average of sums in 2 weeks / 100) * 4 = (0.55) * 4 = 2.2 

or this?

(BuyTime + SellTime / 100) * 4 = (0.58 + 0.56) * 4 = 4.56 

or ...

((BuyTime + SellTime) / 2) / 100 * 4 = (0.57) * 4 = 2.28 

(which make JABA is not good enough)...

stefan.schmiedl     2016-01-26 21:21:17
User avatar

Marchel Sugi put the finger on the problem: There are two conflicting definitions of commission in the second and fifth paragraph:

P2: "takes comission of 1% for each deal. So regarding example above Paul loses 40 dounds when buying shares and 46 more when selling them back."

Here commission is 0.01 * price_1 + 0.01 * price_14.

P5: "for share with mean price 50 comission is 0.5 and standard deviation should be equal or greater than 2."

Now it is 0.01 * mean{prices}, not taking into account that there are two transactions for which the broker requires payment.

Let's look at a simple example over three days, and calculate the cutoff values for the standard deviation:

ABCD 50 50 50, comm(P2): 0.50+0.50 = 1.00  =>  stddev > 4.00
               comm(P5):             0.50  =>  stddev > 2.00

EFGH 48 54 48, comm(P2): 0.48+0.48 = 0.96  =>  stddev > 3.86
               comm(P5):             0.50  =>  stddev > 2.00
stefan.schmiedl     2016-01-26 21:37:26
User avatar

It looks like the checker is based on the second definition (which makes less financial sense).

If I define the commission as 1% of the mean of the prices, several attempts are evaluated as correct.

andydscott     2016-09-27 11:40:45

I am strugglng with this one in the same way as vladimir last year. I always get a few where my calcs say the standard deviation is less then 2 but the checker says they are good (ie > 2) I may well have made a mistake in my code, but even when I check the data in Excel I still get SD less than 2. data etc to follow

andydscott     2016-09-27 11:41:16

Well here is the test data:

21
FANT 67 66 64 62 65 62 65 62 63 63 66 66 69 69
WOWY 98 97 97 98 98 100 97 99 95 97 99 103 99 99
VDKL 62 61 64 65 64 64 61 59 63 60 62 61 65 61
INSX 24 25 24 23 23 22 23 23 22 23 24 25 25 25
OBAM 107 106 107 106 106 107 107 106 105 104 103 103 103 104
MARU 75 71 68 71 70 69 72 70 66 64 67 66 70 68
SUGR 20 20 23 20 20 17 16 17 19 16 15 14 14 12
FOTA 47 48 49 48 47 48 47 48 47 46 45 46 46 45
GEEK 57 56 54 57 54 57 60 60 59 58 60 62 64 62
CKCL 236 237 238 238 239 240 240 241 241 240 239 238 239 239
DALG 25 23 24 24 26 23 26 26 28 30 31 33 33 34
YUKA 23 21 20 22 20 22 24 23 21 22 23 24 26 26
FLNT 21 25 23 25 24 21 21 17 21 25 27 30 28 25
BLEP 23 23 23 24 25 25 26 26 25 25 24 24 25 24
JABA 128 127 127 127 126 126 126 127 127 127 126 126 127 127
GOLD 81 79 79 77 78 79 80 81 80 80 82 83 82 81
JOOG 75 75 75 75 75 76 75 75 74 73 72 73 73 72
JEOP 62 61 61 59 61 62 62 63 62 64 66 68 66 68
MYTH 83 82 79 81 84 83 86 83 80 80 78 76 73 76
PNSN 23 21 19 16 18 18 21 23 23 20 21 21 24 27
ASDF 27 24 22 26 24 23 23 24 26 27 27 25 29 33
andydscott     2016-09-27 11:45:18

My solution:

SUGR GEEK DALG FLNT JEOP MYTH PNSN ASDF

Expected solution:

INSX SUGR GEEK DALG YUKA FLNT BLEP JEOP MYTH PNSN ASDF

So I am missing INSX, YUKA and BLEP

my calculations were:

INSX mean= 23.6428571429 sd= 1.04246567995 comm= 0.236428571429
YUKA mean= 22.6428571429 sd= 1.83642287603 comm= 0.226428571429
BLEP mean= 24.4285714286 sd= 0.979379228629 comm= 0.244285714286

Have I gone wrong with my calc of SD?
Have I misunderstod the problem or is the checker wrong?

Here is my code as well

n =input()

def find_mean(prices):
    N=14  
    #N=len(prices)  
    mean = float(sum(prices)/N)
    sd =  ((   sum( (mean-x)**2 for x in prices)  )    /N )**.5
    return mean, sd




for x in range (n):
    shares=raw_input().split()
    share_name=shares[0]
    prices=map(float,shares[1:])
    [mean,sd]=find_mean(prices)


    commision= mean* .01

    if sd >=  4*commision and sd >=2 :
            print share_name,
andydscott     2016-09-27 11:46:25

Apologies for formatting......I'll try again

andydscott     2016-09-27 11:53:26

rmoved junk badly formatted....

andydscott     2016-09-27 12:04:02

Here are the satndard deviations as calculated by Excel

insx    24  25  24  23  23  22  23  23  22  23  24  25  25  25  1.04246568
yuka    23  21  20  22  20  22  24  23  21  22  23  24  26  26  1.836422876
blep    23  23  23  24  25  25  26  26  25  25  24  24  25  24  0.979379229
Quandray     2016-09-27 18:28:10
User avatar

Hi Andy,

Try removing "and sd >=2"

Where it says "for share with mean price 50 comission is 0.5 and standard deviation should be equal or greater than 2." I think the 2 is just for that example, i.e. 4 times commission of 0.5 is 2.

andydscott     2016-09-27 18:41:23

Thanks quandry. you are quite right. that sorted it.

I read those instructions over and over and every time I understood I needed sd >=2. Now I'm cooking on gas again.

Bryan Mulhern     2016-10-02 05:08:51

For this case: INSX 47 47 47 44 44 46 45 45 46 44 46 45 48 51 broker cost is 0.4607142857142857 stdev is 1.8999710813299528

stdev is > 4*broker, however, I failed the problem the first time because it wasnt on the list for correct answers.

Quandray     2016-10-02 07:51:32
User avatar

Hello Bryan,

I make the stddev for that case 1.83.

Make sure you are calculating the stddev the way specified in the link from the problem description.

If you look at http://www.calculator.net/standard-deviation-calculator.html it shows two ways of calculating stdev. One of them gives 1.83 and the other gives 1.89997

Richard_Roe     2016-11-27 13:53:43

How to solve this problem? What need to calculate? My code: public class HelloWorld {

public static void main(String[] args) {

    Scanner sc = new Scanner(in);

    int loop = sc.nextInt();
    float standDev = 0;
    for (int i = 0; i < loop; i++) {
        String tmp = sc.next();
        ArrayList<Integer> stocks = new ArrayList<>();
        String nums = sc.nextLine();
        String[] stStocks = nums.split(" ");
        for (String s : stStocks) {
            if (!s.isEmpty())
                stocks.add(Integer.parseInt(s));
        }
        int sum = 0;
        for (int st:stocks) {
            sum += st;
        }
        float aver = sum / stocks.size();
        float broker = 4*aver/100;
        sum = 0;
        for (int st:stocks) {
            sum += Math.pow(st - aver, 2);
        }
        aver = (int) Math.sqrt(sum / stocks.size());
        if (aver >= broker){
            //standDev = aver;
            System.out.print(tmp + " ");
        }

    }
    sc.close();
}

}

Sometimes I have too much answers, sometimes not enought. What is wrong, I begin misunderstand this problem.

Quandray     2016-11-27 16:55:46
User avatar

Hi Richard,

I use C/C++, so don't know much about Java, but in

aver = (int) Math.sqrt(sum / stocks.size());

why the "(int)"?

I'd expect Math.sqrt to return a double, which you force to an integer before storing it in aver. In the next line you compare aver with broker (which is a float).

Try having aver & broker as doubles and remove the (int)

Please login and solve 5 problems to be able to post at forum