Skip to content

Commit

Permalink
Adding horizon notebook, to be adjusted to fit new example format
Browse files Browse the repository at this point in the history
  • Loading branch information
kjjarvis committed Aug 31, 2023
1 parent cdf22b7 commit db5aae4
Showing 1 changed file with 48 additions and 24 deletions.
72 changes: 48 additions & 24 deletions examples/horizon.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"# Welcome to ProgPy's Horizon Example Notebook! "
"# Welcome to ProgPy's Horizon Example Notebook"
]
},
{
Expand All @@ -26,7 +26,7 @@
"\n",
"The results of this prediction will be:\n",
"- Predicted future values (inputs, states, outputs, event_states) with uncertainty from prediction\n",
"- Time event is predicted to occur (with uncertainty)"
"- Time the event is predicted to occur (with uncertainty)"
]
},
{
Expand Down Expand Up @@ -70,7 +70,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"To do prediction, we need a distribution of samples. Here, we'll create a distribution around the initial state using a Multivariate Normal Distribution. We'll use 1000 samples for prediction. "
"To do prediction, we need a distribution of samples. Here, we'll create a distribution around the initial state using a Multivariate Normal Distribution. We'll use 500 samples for prediction. "
]
},
{
Expand All @@ -79,7 +79,7 @@
"metadata": {},
"outputs": [],
"source": [
"NUM_SAMPLES = 1000\n",
"NUM_SAMPLES = 500\n",
"x = MultivariateNormalDist(initial_state.keys(), initial_state.values(), np.diag([x_i*0.01 for x_i in initial_state.values()]))"
]
},
Expand All @@ -96,20 +96,18 @@
"metadata": {},
"outputs": [],
"source": [
"mc = predictors.MonteCarlo(m)"
"mc = MonteCarlo(m)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now, let's perform a prediction. We given the `predict` method the following arguments:\n",
"- Samples ()\n",
"- Number of samples \n",
"Now, let's perform a prediction. We give the `predict` method the following arguments:\n",
"- Distribution of initial samples\n",
"- Number of samples for prediction \n",
"- Step size for the prediction \n",
"- \n",
"\n",
"Finally, the predict function requires a future loading function to be passed. This function is used to generate future inputs to the model. Since our model does not have any inputs, we'll pass a function that returns an empty set."
"- Prediction horizon, i.e. time value to predict to\n"
]
},
{
Expand All @@ -118,22 +116,18 @@
"metadata": {},
"outputs": [],
"source": [
"PREDICTION_HORIZON = 5\n",
"samples = filt.x\n",
"STEP_SIZE = 0.001\n",
"\n",
"def future_loading(t, x = None):\n",
" return {}\n",
"PREDICTION_HORIZON = 7.7\n",
"STEP_SIZE = 0.01\n",
"\n",
"# Making Prediction\n",
"mc_results = mc.predict(samples, future_loading, dt=STEP_SIZE, horizon = PREDICTION_HORIZON)\n"
"# Make Prediction\n",
"mc_results = mc.predict(x, n_samples=NUM_SAMPLES, dt=STEP_SIZE, horizon = PREDICTION_HORIZON)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Let's see the results of the predicted time of event!"
"Let's see the results of the predicted time of event. We'll plot histograms of the distribution of times where `falling` and `impact` occurred. Note that no events occur after 7.7 seconds, since we enforced a prediction horizon at this value. "
]
},
{
Expand All @@ -144,7 +138,6 @@
"source": [
"metrics = mc_results.time_of_event.metrics()\n",
"print(\"\\nPredicted Time of Event:\")\n",
"print(\"\\nSamples where impact occurs before horizon: {:.2f}%\".format(metrics['impact']['number of samples']/100 * 100))\n",
"pprint(metrics) # Note this takes some time\n",
"mc_results.time_of_event.plot_hist(keys = 'impact')\n",
"mc_results.time_of_event.plot_hist(keys = 'falling')"
Expand All @@ -154,20 +147,51 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"As you can see from the results..."
"Now let's calculate the percentage of each event that occurred before the prediction horizon was met. "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(\"\\nSamples where falling occurs before horizon: {:.2f}%\".format(metrics['falling']['number of samples']/NUM_SAMPLES * 100))\n",
"print(\"\\nSamples where impact occurs before horizon: {:.2f}%\".format(metrics['impact']['number of samples']/NUM_SAMPLES * 100))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"All samples reach `falling` before the prediction horizon, but only some of the samples reach `impact`. "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"To conclude..."
"To conclude, in this example, we've shown how to implement a prediction `horizon`. Specifying a prediction horizon defines the time value with which to predict to, and can be used anytime a user is only interested in events that occur before a specific point in time. "
]
}
],
"metadata": {
"kernelspec": {
"display_name": "ProgPyEnvAll",
"language": "python",
"name": "python3"
},
"language_info": {
"name": "python"
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.6"
},
"orig_nbformat": 4
},
Expand Down

0 comments on commit db5aae4

Please sign in to comment.