In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the relevant knowledge of "what is the Java quantile calculation method". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
There is a requirement for Java quantile (quantile) calculation
Give the PB (average price-to-book ratio) for a period of time, and then select a day during this period to calculate the quantile value for that day.
A very simple mathematical statistics subject, for low achiever, who has graduated for many years, it is still necessary to record the calculation methods of the following related problems.
For example:
The pb of the last 1. 1-1. 10 is (1, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5).
Then the value returned to me is 0%, and 100% if 1.10 is passed in.
So how to build a mathematical model?
Note: quantile value description:
Pn is the n quantile value. It indicates that n% of the data in the surveyed group is less than this value. The size of n reflects the different levels of the market. P10, P25, P50, P75 and P90 are usually used to represent the different levels of the market.
10 quartile: indicates that 10% of the data is less than this value, reflecting the low-end level of the market.
25 quartile: indicates that 25% of the data is less than this value, reflecting the lower end of the market.
50 quartile: indicates that 50% of the data is less than this value, reflecting the average level of the market.
75 quartile: indicates that 75% of the data is less than this value, reflecting the higher end of the market.
90 quartile: indicates that 90% of the data is less than this value, reflecting the high-end level of the market.
Example: find the 25 quartile, 50 quartile, 75 quartile of a set of data in the following example:
A = [65 23 55 78 98 54 88 90 33 48 91 84]
Solution:
Method 1:
1. First sort the above 12 numbers from small to large
1 23
2 33
3 48
4 54
5 55
6 65
7 78
8 84
9 88
10 90
11 91
12 98
2. There are 11 intervals for the 12 numbers, and there are 11 for each quartile and 2.75 for each quartile
3 、
① calculates the 25th quartile:
The first quartile is the first + 2.75-3.75 of the above 12 numbers.
It refers to the value 48 corresponding to the third number and the 0.75 position between the third number and the fourth number, that is, 48 + (0.75) * (54-48) = 52.5 (52.5 is the 25th quantile).
② calculates 50 quantiles:
The second quartile is the first + 2.752-6.5 of the above 12 numbers.
It refers to the value 65 corresponding to the sixth number and the 0.5 position between the sixth number and the seventh number, that is, 65 + (0.5) (78-65) = 71.5 (71.5 is the 50th quantile).
[the median can also be calculated in a very simple way, arranged from small to large:
If the number of numbers in the array is odd, then the value corresponding to the middle number is the median.
If the number of numbers in the array is even, then the average value of the middle two values is the median, as above 78-65) / 2-71.5]
③ calculates the 75th quartile:
The third quartile is the first + 2.753-9 of the above 12 numbers.
It refers to the value 88 corresponding to the 9th number and the 0.25 position between the 9th number and the 10th number, that is, 88 + (0.25) (90-88) = 88.5 (88.5 is the 75 quartile).
[if you divide 1 to 100 into 10 equals, there are 10 10 quantiles. The values of 10 quartiles and 90 quantiles can be calculated by using the above method. (P10 examples 34.5 minutes 90.9 of the above examples)]
The following is implemented in Java code
Related methods
Private static double getPercentile (List dataList, double p) {int n = dataList.size (); dataList.sort (new Comparator () {/ / sort from smallest to largest @ Override public int compare (Double o1, Double O2) {if (o1 = = null | | O2 = null) {return 0 } return o1.compareTo (O2);}}); double px = p* (NMur1); int I = (int) java.lang.Math.floor (px); double g = px-I; if (gadolinium 0) {return dataList.get (I) } else {return (1murg) * dataList.get (I) + g*dataList.get (iTun1);}}
Test interface
@ RequestMapping ("CalcPercentile") @ ResponseBody public R CalcPercentile (@ Param ("per") double per) {double [] arrs = {2jing4ping5); List doubleList = new ArrayList (); for (double currArr: arrs) {doubleList.add (currArr);} double percentile = getPercentile (doubleList, per); return R.ok (). Message ("query service data") } normal output
Input 1 is 100%, normal output 5.0, input 0.5, output 3.0
Fulfill the requirements
The above analysis is to know the percentile and find the corresponding value of the percentile? And our demand is to use a certain value in the sample time to find the corresponding percentile? After consulting a lot of data did not find the relevant formula, of course, there are bosses know can prompt. After all, the problem still needs to be solved. The solution used here is to divide the sample data into 100 points, find the corresponding value for each percentile, and then compare it with the target data in the sample to find the nearest number. The percentile value of the target data is indirectly obtained.
Core code
Java asked for the percentile to explain.
Percentile, statistical term, if a group of data is sorted from small to large and the corresponding cumulative percentile is calculated, the value of the data corresponding to a 100 percentile is called the percentile. Can be expressed as: a group of n observations arranged by numerical size. For example, the value in the p% position is called the p percentile.
Java code public static double percentile (double [] data,double p) {int n = data.length; Arrays.sort (data); double px = p * (nMuth1); int I = (int) java.lang.Math.floor (px); double g = px-I; if (glossary 0) {return data [I];} else {return (1Mul g) * Data [I] + g*data [I] This is the end of the content of "what is the Java quantile calculation method". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.