zaiff
zaiff

Reputation: 1009

How to draw a gaussian curve in android?

I have to draw a simple gaussian distribution curve in android. A sample code will be a great help.

Upvotes: 0

Views: 1219

Answers (3)

Parthi
Parthi

Reputation: 658

It may be a very late answer. I recently tried to plot a Gaussian curve from given data. Maybe, it will be very helpful to someone.

val listOfDriverSpeeds = fetchListOfSpeedRecords()

Step 1: Calculate mean value of the records.

val iterator = listOfDriverSpeeds.iterator()
        iterator.forEach {
            Log.d("driverSpeed", "${it.driverName} driving in ${it.velocity} kmph")
            total += it.velocity            
        }
val mean: Double = total / (listOfDriverSpeeds.size)
Log.d("mean", "$mean kmph")

Step 2: Calculate the standard deviation of the records.

var sDeviation = 0.0

for (i in 0 until listOfDriverSpeeds.size) {
    sDeviation += (listOfDriverSpeeds[i].velocity - mean).toDouble().pow(2.0)
}
val standardDeviation = sqrt(sDeviation / listOfDriverSpeeds.size);
Log.d("StandardDeviation", "$standardDeviation")

Step 3: Calculate the 7 sigma (3 +sigma, mean, 3 -sigma)

val sigma = mean
val sigma1 = mean + standardDeviation
val sigma2 = mean + (2 * standardDeviation)
val sigma3 = mean + (3 * standardDeviation)

val sigma1n = mean - standardDeviation
val sigma2n = mean - (2 * standardDeviation)
val sigma3n = mean - (3 * standardDeviation)

Step 4: Use the least sigma as the initial value and the maximum sigma value will be the final value. I m adding one more value to calculate the deviation with accuracy

val initialProjection = sigma3n.toInt() - 1
val finalProjection = sigma3.toInt() + 1

Step 5: Now the range is ready. We need to calculate the normal distribution for each range of records.

val fakeProjectedData: MutableList<DriverSpeed> = ArrayList()

var k = 0;
while((initialProjection + k) < finalProjection){
    val fakeProjection = DriverSpeed()
    val projection = (initialProjection + k)
    fakeProjection.projectedVelocity = projection
    val normDistribution = calcNormalDistribution()
    fakeProjection.normalDistribution = normDistribution.toFloat()
    fakeProjectedData.add(fakeProjection)
    k++
}

val values = ArrayList<Entry>()

for (i in 0 until fakeProjectedList.size) {
    Log.d("Pojection", "item: $i =  ${fakeProjectedList[i].toString()}")    
    values.add(Entry(fakeProjectedList[i].projectedVelocity.toFloat(), 
    fakeProjectedList[i].normalDistribution, 0))
}
private fun calcNormalDistribution(): Double {
    return (exp(((projection-mean)/standardDeviation).pow(2.0)* 
           (-1)/2))/(standardDeviation * sqrt(2*3.14))
}

Step 6: Draw a Gaussian curve from the list of data using any line chart library. I would suggest Android MP chart. It provides a lot of customization.

drawGaussionLineChart(values)

enter image description here

The actual normal distribution formula derived from here

enter image description here

You can calculate the values with any math library. If anything that improves my answer, please share with me.

Upvotes: 0

Nasif Noorudeen
Nasif Noorudeen

Reputation: 142

You can draw gaussian curve by using log scale in x axiz . Take common log of x axis value and can plot y values. Can use Android chart engine libs as a reference

Upvotes: 1

Jave
Jave

Reputation: 31846

Here is some information on drawing things in android:
http://developer.android.com/guide/topics/graphics/2d-graphics.html
And for drawing a curve you could probably use the Path-class to draw on a canvas as described in the above link.
General info on Gaussian distribution: http://en.wikipedia.org/wiki/Gaussian_distribution

Upvotes: 2

Related Questions