Closed matthew-blackman closed 1 year ago
I went ahead and wrote out an idea for this. It is called VarianceNumberProperty
and it could be good to have something tangible to be able to continue our discussion about this in Projectile Motion.
@matthew-blackman, if you have a change, please review the implementation and the few tests I wrote for it to see what you think!
This is working great on my end. Only part I’m unsure about is this part of VarianceNumberPropertyTests:
const computeFunction = ( v: number ) => v + 1;
const v = new VarianceNumberProperty( 0, computeFunction );
assert.ok( v.get() === 1, 'initial value' );
assert.ok( v.value === 1, 'value getter' );
Is it a safety check or just more testing?
I implemented it just as a proof of concept. When writing it, I was reminded that we already have something called a "MappedProperty", which takes another Property for its value, and a mapping function. It is pretty close to what we currently have, but it didn't seem like a good fit to have to have two separate properties to store the input and output. That said. It may be an experiement we want to have. I'm not quite sure yet. Mostly I wanted to have a prototype that we could try out in Projectile motion to see if this is the best strategy, and also because I think examples are the best way to explain an idea.
It would be nice to be able to pass mutable values for mean and standard deviation into VarianceNumberProperty, and have all of the VarianceComputer logic self-contained. There are only a handful of statistical distributions to draw from, so an overridable VarianceComputer parameter makes sense to me.
Here is the usage I'm picturing with ProjectileMotionModel as an example:
this.launchSpeedRandomizedProperty = new VarianceNumberProperty(this.initialSpeedProperty, this.initialSpeedStandardDeviationProperty, Statistics.NORMAL);
launchSpeedRandomizedProperty would draw from the latest values of initialSpeedProperty and initialSpeedStandardDeviationProperty whenever it is accessed, and put them through the VarianceComputer (in this case set to Statistics.NORMAL). There could be several enumerated functions for common statistical distributions, or the option for a custom VarianceComputer.
It would be nice to be able to pass mutable values for mean and standard deviation into VarianceNumberProperty, and have all of the VarianceComputer logic self-contained. There are only a handful of statistical distributions to draw from, so an overridable VarianceComputer parameter makes sense to me.
How about something like this:
class MyVarianceNumberProperty extends VarianceNumberProperty{
constructor(value) {
const standardDeviationProperty= new NumberProperty(2);
const computerFunction = ()=>{
. . . .
return x * standardDeviationProperty.value;
}
super( value, computerFunction);
}
}
VarianceNumberProperty is working well for our purposes in Projectile Motion. The initial speed and launch angle of the projectiles are now VarianceNumberProperties.
Will need to have launch speed randomized based on mean and standard deviation for data fluency initiative. Investigating how to best do this. #279