# Vital Signs 2: Forcing a Run

Source: https://obsvra.com/learn/modeling-101/modeling-lesson-vitals-2

### Problem statement

The problem statement in Lesson 1 called for about 1 in 1,000 readings to be anomalous. But that couldn't reliably produce what an alert test needs: several abnormal readings _in a row_.

So our new problem statement is to create a model that will still generate data at the expected rates, but also ensure that we get recognizable runs or sequences of abnormal results instead of "blips".

### Attempt 1: turn up the rate (still Free-tier)

The obvious first thing to try: since 0.1% almost never strings together, let's just raise the rate in order to tilt probability to our side.

```
<Mix name="Vitals" synchronous="false">
  <input name="NormalReading" weight="0.75"/>
  <input name="AnomalousReading" weight="0.25"/>
</Mix>
```

Now, a full quarter of readings come from the tachycardic population; that's not very realistic, of course, but it should get us the streaks we need. Running it at 1,000 rows (with Lesson 1's seed of 42), we observe our longest run of consecutive anomalies is 5, and it happens once. Raising that to 5,000 rows, a run of 5 happens 4 times. Out of 929 separate anomalous episodes total, 688 of them (74%) are still a single isolated reading, not a run at all.

This may be good enough for some purposes, but not others, and it doesn't match the requirements of the problem statement. We now have the streaks we're looking for, but no longer the desired rate, and the end result is closer to noise than anything realistic.

### Attempt 2: force the run to occur (Pro-tier)

`Perturb` doesn't mess with the odds. It runs the baseline for a scheduled stretch, then switches to the disturbance population for a defined number of readings, then switches back — on a timer, not a coin flip.

```
<?xml version="1.0" encoding="UTF-8"?>
<dataset columns="heartRate, systolicBp, diastolicBp">
  <root node="Vitals"/>
  <nodes>
    <Perturb name="Vitals" repeat="true">
      <input name="NormalReading" role="normal"/>
      <input name="AnomalousReading" role="disturbance"/>
      <input name="WaitGen" role="wait"/>
      <input name="DurGen" role="duration"/>
    </Perturb>
    <MultivariateNormal name="NormalReading" means="74,120,80" covariance="64,0,0; 0,144,67.2; 0,67.2,64"/>
    <MultivariateNormal name="AnomalousReading" means="195,120,80" covariance="64,0,0; 0,144,67.2; 0,67.2,64"/>
    <Constant name="WaitGen" value="4995" type="double"/>
    <Constant name="DurGen" value="5" type="double"/>
  </nodes>
</dataset>
```

We used the same two distributions as in Lesson 1. `WaitGen`/`DurGen` set the schedule: 4995 normal readings, then a run of exactly 5, repeating, keeping the 0.1% average rate of anomalies.

Examining the output, we see the transition into the first run, right where we expect it (looking at rows 4993–5000, with a seed of 42):

```
59.17, 120.67, 78.99
68.89, 119.05, 88.20
77.44, 133.94, 89.03
184.96, 122.24, 86.36
205.38, 105.11, 68.41
206.78, 107.42, 67.55
198.23, 109.20, 71.91
209.10, 108.99, 76.09
```

The run starts exactly when it was told to and then holds for exactly 5 rows. From this model, you will consistently get a run of five anomalies for every 5000 rows you generate. The baseline stays the normal population from Lesson 1. The run you need to test against is no longer a matter of luck.

**What this gives you:** a scheduled disturbance instead of a random one. This is the difference between a fixture that _might_ contain the case you're testing and one that _does_, on a timer you control and in the shape you need.

Note that we used Constant to produce fixed values for the wait and duration parameters; to inject randomness, you could use a statistical distribution for each input, as long as they're guaranteed to produce positive numbers. You can also create more complex models if you need to include streaks with different frequency or duration shapes.
