You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
69 lines
1.8 KiB
69 lines
1.8 KiB
7 months ago
|
using System.Collections;
|
||
|
using System.Collections.Generic;
|
||
|
using UnityEditor;
|
||
|
using UnityEngine;
|
||
|
|
||
|
public class ProbablilityTest : MonoBehaviour
|
||
|
{
|
||
|
public MathExtras.Keyframe[] quantileKeyframes;
|
||
|
public AnimationCurve quantileCurve;
|
||
|
|
||
|
public int numSamples = 100 * 1000;
|
||
|
public int numBuckets = 100;
|
||
|
|
||
|
public Vector3 labelPos;
|
||
|
|
||
|
private int[] _buckets;
|
||
|
private int _maxBucketCount;
|
||
|
|
||
|
// LIFECYCLE
|
||
|
#if UNITY_EDITOR
|
||
|
private void OnDrawGizmos()
|
||
|
{
|
||
|
// Guard
|
||
|
if (_buckets == null)
|
||
|
return;
|
||
|
|
||
|
// Display histogram
|
||
|
for (int i = 1; i < this._buckets.Length; i++)
|
||
|
{
|
||
|
Vector3 a = new Vector3((i - 1) / (float)this.numBuckets, _buckets[i - 1] / (float)_maxBucketCount, 0f);
|
||
|
Vector3 b = new Vector3(i / (float)this.numBuckets, _buckets[i] / (float)_maxBucketCount, 0f);
|
||
|
|
||
|
Debug.DrawLine(a, b, Color.red);
|
||
|
}
|
||
|
|
||
|
// Display probability
|
||
|
Handles.Label(this.labelPos, $"{(_maxBucketCount / (float)this.numSamples):P2}");
|
||
|
}
|
||
|
#endif
|
||
|
|
||
|
// BEHAVIORS
|
||
|
public void CurveFromKeyframes()
|
||
|
{
|
||
|
this.quantileCurve = MathExtras.CurveFromKeyframes(this.quantileKeyframes);
|
||
|
}
|
||
|
|
||
|
public void VisualizeDistribution()
|
||
|
{
|
||
|
_buckets = new int[this.numBuckets];
|
||
|
_maxBucketCount = 0;
|
||
|
|
||
|
for (int i = 0; i < numSamples; i++)
|
||
|
{
|
||
|
// Take sample from distribution
|
||
|
float rand = MathExtras.SampleDistribution(this.quantileCurve);
|
||
|
|
||
|
// Update histogram
|
||
|
int bucket = Mathf.FloorToInt(rand * this.numBuckets);
|
||
|
|
||
|
if (bucket < 0 || bucket >= this._buckets.Length)
|
||
|
continue;
|
||
|
|
||
|
_buckets[bucket]++;
|
||
|
if (_buckets[bucket] > _maxBucketCount)
|
||
|
_maxBucketCount = _buckets[bucket];
|
||
|
}
|
||
|
}
|
||
|
}
|