Pages Menu

Posted on Jul 28, 2014 in Original Article | 2 comments

Epilepsy Treatment Simplified through Mobile Ketogenic Diet Planning

Hanzhou Li1,3, Jeffrey L. Jauregui, PhD4, Cagla Fenton RD, LDN1,2, Claire M. Chee RD, BS1, A.G. Christina Bergqvist M.D1,5

1Division of Neurology, Department of Pediatrics; 2Department of Clinical Nutrition, The Children’s Hospital of Philadelphia, Philadelphia, PA 19104; 3Department of Biology University of Pennsylvania, Philadelphia, PA 19104; 4Department of Mathematics, Union College, Schenectady, NY 12308; 5The Perelman School of Medicine, University of Pennsylvania, Philadelphia, PA 19104

Corresponding Author:

Journal MTM 3:2:11–15, 2014


Background: The Ketogenic Diet (KD) is an effective, alternative treatment for refractory epilepsy. This high fat, low protein and carbohydrate diet mimics the metabolic and hormonal changes that are associated with fasting.

Aims: To maximize the effectiveness of the KD, each meal is precisely planned, calculated, and weighed to within 0.1 gram for the average three-year duration of treatment. Managing the KD is time-consuming and may deter caretakers and patients from pursuing or continuing this treatment. Thus, we investigated methods of planning KD faster and making the process more portable through mobile applications.

Methods: Nutritional data was gathered from the United States Department of Agriculture (USDA) Nutrient Database. User selected foods are converted into linear equations with n variables and three constraints: prescribed fat content, prescribed protein content, and prescribed carbohydrate content. Techniques are applied to derive the solutions to the underdetermined system depending on the number of foods chosen.

Results: The method was implemented on an iOS device and tested with varieties of foods and different number of foods selected. With each case, the application’s constructed meal plan was within 95% precision of the KD requirements.

Conclusion: In this study, we attempt to reduce the time needed to calculate a meal by automating the computation of the KD via a linear algebra model. We improve upon previous KD calculators by offering optimal suggestions and incorporating the USDA database. We believe this mobile application will help make the KD and other dietary treatment preparations less time consuming and more convenient.


Treatment-resistant epilepsy is a national health problem that affects up to 40% of patients with epilepsy and results in significant morbidity, reduced quality of life, and cost to society13. For these patients, the Ketogenic Diet (KD), a high fat, low protein and carbohydrate diet, remains an effective therapy4. More than half achieve significant seizure reduction and up to 20% become seizure-free58. Use of the KD also allows for reduction in medications and antiepileptic drug-related side effects9. As a result, the use of the KD is increasing across the world10.

Management of KD in children is time-consuming for both the families that have to prepare the meals and the registered dietician (RD) who supervises the diet. In order to maximize success of the KD, caretakers of the patient must follow the prescribed fat, protein, carbohydrate, and caloric intake-per-day guidelines set by their KD team7. During the average three-year duration of treatment, these requirements adjust as the child grows. Each meal is precisely weighed to within 0.1 of a gram11. To maintain the 4:1 (in grams, fat: carbohydrates plus protein) ratio, exact proportions are either computed by hand or with the assistance of one of the currently available Internet KD calculator or homemade parent calculators12,13. This requires an organized caretaker, the ability to perform some algebra, and/or large time commitment by the RD in creating menus or in checking the accuracy of menus created on these Internet KD calculators. This can be a direct deterrent for a family to try the KD, physicians from referring patients to a KD program, or result in discontinuation of the treatment despite positive results14.

We created an automatic KD planning application that is based on a linear algebra model. The mobile application has the ability to not only perform the calculations, but also to offer the exact, optimal amount of each food in order to maintain the prescribed recommendations set by the dietician. By incorporating the United States Department of Agriculture (USDA) nutrient database, the application offers the caretakers a large selection of food choices instead of relying on a small set of approved recipes. This mobile application eliminates the burden of hand computation, offers the flexibility of food choices, and guarantees the maintenance of the exact recommendation by the dietician. The application is currently offered to our patients in order to receive further feedback in terms of user interface, design, and other improvements. The application will be offered openly to the general public on the Apple App Store in the subsequent months when the refinements are incorporated.


In this study, we investigate an algorithm to compute the quantities of food to be prepared for any ratio KD meal. We assume the user has selected n foods, and our goal is to compute the amount each of food, in grams, needed to fulfill the KD plan. These weights (in grams) are denoted by the unknown variables x1, x2, …, xn. For each i=1, 2, …, n, we consult the USDA Nutrient Database to ascertain numbers ai, bi, and ci, which denote the fat per gram, protein per gram, and carbohydrate per gram, respectively, of the ith food. Finally, we let d1, d2, and d3 denote the prescribed fat, protein, and carbohydrate content required, which are determined by the size of the meal and the parameters of the diet. Equating the total amounts of fat, protein, and carbohydrates determined by the food choices to equal those prescribed by the diet, we arrive at the following system of linear equations:

which can alternatively be written in matrix form as:

We denote by A the coefficient matrix, x the vector of unknowns, and b the vector consisting of d1, d2, and d3; thus, the system can be written simply as Ax=b. We apply the following methodology (Figure 1), based on the number of distinct food choices, to determine a solution or approximate solution x to the above system. To reiterate, the entries of the vector x are the respective amounts of the n foods to be given to the patient.

Figure 1: Algorithm For Solving Equations Depending on the Number of Foods Chosen

Less than three distinct food choices

If the number of foods selected is less than three, the system of equations is overdetermined (i.e., there are more constraints than variables). Thus, a solution will generally not exist, so we use the least-squares method to determine an approximate solution x by the formula:

where AT is the matrix transpose of A. In practice, ATA has full rank and so its inverse exists. If an entry of x is negative, the user is warned that no feasible solution was found, so more food choices must be selected.

Three distinct food choices

If three foods are selected, the matrix A is 3-by-3. Moreover, since the food choices are distinct, A has full rank in practice and is therefore invertible. In particular, there exists a unique solution x, given by the equation:

Again, if an entry of x is negative, the user is warned and prompted to select more food choices.

More than three distinct food choices

If the number of foods selected exceeds three, the system of equations is underdetermined. Generally, this means there are infinitely many combinations of the selected foods that would yield a KD appropriate meal. To determine a well-defined, positive solution, we use the Ordered Subsets Expectation Maximization (OSEM) method15.

We briefly explain the details of our OSEM implementation. We begin with a vector xj(0), where j=1, 2, …, n consists of all 1’s. The vector xj(k) will denote the vector at the kth iteration. At each iteration step, we compute

where i=1,2,3, followed by the iteration step

We halt the iteration when the difference between xj(k)> and xj(k+1) is sufficiently small, and use xj(k+1) as our solution. By construction, all of its entries are nonnegative and correspond to the suggested amount of each food.


The methods were implemented in an iOS application and the results are depicted in Figure 2. The diet plan used consisted of a 4:1 ratio KD with 70mg of heavy cream. Because the heavy cream amount is static, the nutritional values are simply subtracted from the total. Figure 2 A demonstrates the application processing two food choices: tofu and sesame oil. Figure 2 B demonstrates the application processing three food choices: chicken tenders, hard-boiled egg, and olive oil. Figure 2 C demonstrates the application processing four food choices: mozzarella cheese, tomatoes, basil, and olive oil. From each of the figures, the total nutrition of the meal matches closely with the recommended KD values. The green progress bar indicates that the application’s constructed meal plan is within 95% precision of the KD requirements. These results demonstrate the robustness of the methods in generating an appropriate, precise meal plan given a variety of input food choices.

Figure 2: Example Calculations Using the Mobile Application


The KD is rewarding but managing it is very time-consuming. Most KD RDs have therefore moved from hand computation and pre-calculated meal plans to using homemade excel spreadsheets or the programs available on the Internet such as the KetoCalculator or the Stanford Keto Calculator12,13. Many RDs also allow direct caretaker access to these programs but require meal plan reviews. Other KD programs adopted an exchange system that simplifies calculations at the expense of nutritional precision16.

Current applications such as the KetoCalculator and other KD Excel spreadsheets prompt the user to attempt to match the KD requirements. With these programs the user increments the amount of each food until the total nutrition matches the prescribed amounts. This takes time and might not achieve the accuracy demanded by the KD. In comparison, our mobile application can algorithmically calculate suggested values for each food item. It instantaneously suggests the exact amounts.

Additionally, currently available calculators only control for the ratio, and not for the macronutrients. For example, they allow the user to maintain a 4:1 KD meal by replacing the protein with carbohydrates by adjusting accordingly to the expression (fat) / (protein + carbohydrate). This potentially can result in insufficient protein intake, which over time can cause protein deficiency and poor growth as we have seen with referred patients from other centers seeking second opinions. Our application’s algorithm does not allow for any deviation from the prescribed amounts of the macronutrients, and will function for any ratio higher or lower than the standard 4:1. Finally, this application utilizes the USDA database as the source for nutritional information and therefore guarantees accuracy and a large selection of food choices. With this program, the meal planning process only takes a few food selections and the click of a button. The flexibility and convenience of a mobile application truly makes the KD much more manageable.


The Ketogenic Diet has great potential in treating refractory epilepsy but requires a heavy demand on the KD staff and caretakers of the patients. To make the KD more manageable, we developed and implemented a mobile application to simplify the KD. We also demonstrated that the methodology generates a medically acceptable meal plan as long as there exists a solution given the food choices. By utilizing mobile technology, we are able to provide effective medical guidance at the users’ convenience. We intend to gather further data from patients regarding how they determine preferences between the foods they choose. Then, we would create additional optimizing constraints to improve the algorithm. Finally, we hope this study will make the KD and other dietary treatments a practical possibility for more caretakers.


We thank the Children’s Hospital of Philadelphia Ketogenic Diet program for supporting this work. The Ketogenic diet patients inspired us to create this program, which we hope will assist them in their daily management of the Ketogenic diet. The first author would like to thank Dr. Joshua Plotkin for encouraging him to pursue this interest.


1. Begley CE, Famulari M, Annegers JF, Lairson DR, Reynolds TF, Coan S, et al. The cost of epilepsy in the United States: an estimate from population-based clinical and survey data. Epilepsia. 2000 Mar;41(3):342–51.

2. Kotsopoulos IA, Evers SM, Ament AJ, de Krom MC. Estimating the costs of epilepsy: an international comparison of epilepsy cost studies. Epilepsia. 2001 May;42(5):634–40.

3. Park C, Wethe JV, Kerrigan JF. Decreased quality of life in children with hypothalamic hamartoma and treatment-resistant epilepsy. J Child Neurol. 2013 Jan;28(1):50–5.

4. Bergqvist AG, Schall JI, Gallagher PR, Cnaan A, Stallings VA. Fasting versus gradual initiation of the ketogenic diet: a prospective, randomized clinical trial of efficacy. Epilepsia. 2005 Nov;46(11):1810–9.

5. Henderson CB, Filloux FM, Alder SC, Lyon JL, Caplin DA. Efficacy of the ketogenic diet as a treatment option for epilepsy: meta-analysis. J Child Neurol. 2006 Mar;21(3):193–8.

6. Lefevre F, Aronson N. Ketogenic diet for the treatment of refractory epilepsy in children: A systematic review of efficacy. Pediatrics. 2000 Apr;105(4):E46.

7. Neal EG, Chaffe H, Schwartz RH, Lawson MS, Edwards N, Fitzsimmons G, et al. The ketogenic diet for the treatment of childhood epilepsy: a randomised controlled trial. Lancet Neurol. 2008 Jun;7(6):500–6.

8. Levy RG, Cooper PN, Giri P. Ketogenic diet and other dietary treatments for epilepsy. Cochrane Db Syst Rev. 2012(3).

9. Nam SH, Lee BL, Lee CG, Yu HJ, Joo EY, Lee J, et al. The role of ketogenic diet in the treatment of refractory status epilepticus. Epilepsia. 2011 Nov;52(11):e181–4.

10. Kossoff EH, McGrogan JR. Worldwide use of the ketogenic diet. Epilepsia. 2005 Feb;46(2):280–9.

11. Mike EM. Practical guide and dietary management of children with seizures using the ketogenic diet. Am J Clin Nutr. 1965 Dec;17(6):399–409.

12. Zupec-Kania B. KetoCalculator: a web-based calculator for the ketogenic diet. Epilepsia. 2008 Nov;49 Suppl 8:14–6.

13. Stanford Medical Center – Ketogenic Diet Program. Stanford University; Available from: http://www.

14. Lightstone L, Shinnar S, Callahan CM, O’Dell C, Moshe SL, Ballaban-Gil KR. Reasons for failure of the ketogenic diet. J Neurosci Nurs. 2001 Dec;33(6):292–5.

15. Hudson HM, Larkin RS. Accelerated image reconstruction using ordered subsets of projection data. IEEE Trans Med Imaging. 1994;13(4):601–9.

16. Carroll J, Koenigsberger D. The ketogenic diet: a practical guide for caregivers. J Am Diet Assoc. 1998 Mar;98(3):316–21.