From 4fbaa9ca5d01ff04ae46f0f3266e5d7a91a6708b Mon Sep 17 00:00:00 2001 From: Michael Pilosov <40366263+mathematicalmichael@users.noreply.github.com> Date: Thu, 6 Aug 2020 07:31:20 -0600 Subject: [PATCH 1/7] Create __init__.py this file is required to identify package modules --- src/cafelytics/__init__.py | 1 + 1 file changed, 1 insertion(+) create mode 100644 src/cafelytics/__init__.py diff --git a/src/cafelytics/__init__.py b/src/cafelytics/__init__.py new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/src/cafelytics/__init__.py @@ -0,0 +1 @@ + From b8840f8ee7df34a18e445087b80c4caf06327f5a Mon Sep 17 00:00:00 2001 From: Michael Pilosov <40366263+mathematicalmichael@users.noreply.github.com> Date: Thu, 6 Aug 2020 07:33:06 -0600 Subject: [PATCH 2/7] Delete ImportSurvey.m --- src/ImportSurvey.m | 12 ------------ 1 file changed, 12 deletions(-) delete mode 100644 src/ImportSurvey.m diff --git a/src/ImportSurvey.m b/src/ImportSurvey.m deleted file mode 100644 index 4d67534..0000000 --- a/src/ImportSurvey.m +++ /dev/null @@ -1,12 +0,0 @@ -function data = ImportSurvey(fileToRead1) -% Import the file -pkg load io -sheetName='Relevant Data'; -[numbers, strings, raw] = xlsread(fileToRead1, sheetName); -if ~isempty(numbers) - newData1.data = numbers; -end - -data = newData1.data; - -end %end function From a2016af8c299e746e90911875be71785dae2ef3d Mon Sep 17 00:00:00 2001 From: Michael Pilosov <40366263+mathematicalmichael@users.noreply.github.com> Date: Thu, 6 Aug 2020 07:33:14 -0600 Subject: [PATCH 3/7] Delete ProdSimulation.m --- src/ProdSimulation.m | 137 ------------------------------------------- 1 file changed, 137 deletions(-) delete mode 100644 src/ProdSimulation.m diff --git a/src/ProdSimulation.m b/src/ProdSimulation.m deleted file mode 100644 index 68f68fc..0000000 --- a/src/ProdSimulation.m +++ /dev/null @@ -1,137 +0,0 @@ -function [Year, Yield] = ProdSimulation(data, - num_years, - PurchaseYear_Borbon, - PurchaseYear_Catuai, - Intercrop_Catuai, - Intercrop_Borbon) -% runs a simulation for r years, stepping through the year n. -% this simulation tests strategies to minimize yield losses - -Borbon=data(1,12); % number of years until full yield -Catuai=data(2,12); -E14=data(3,12); -Catura=data(4,12); - -Age=data(:,2); % age of plots -Species=data(:,3:6); -Prod=data(:,7:9); % production data - -sz=size(data); -numplots=sz(1); % number of plots -Exp=zeros(numplots,1); % expected harvest -Year=zeros(num_years,1); -Yield=zeros(num_years,1); -for j=1:num_years - Year(j)=2011+j; -end - -sz=size(data); numplots=sz(1); % number of plots -for n=1:num_years - % Uncomment the following to simulate early pruning -% if n==18 -% data(1,13)=20; -% data(1,14)=20; -% elseif n==33 -% data(1,13)=25; -% data(1,14)=23; -% end - - if n==PurchaseYear_Borbon % Purchasing land and planting in year n - Prod(89,3)=Borbon; - Age(89)=0; - end - if n==PurchaseYear_Catuai % Purchasing land and planting in year n - Prod(90,3)=Catuai; - Age(90)=0; - end - if n==Intercrop_Catuai % Intercropping Catuai in year n - data(2,12)=2; - end - - for j=1:numplots % stepping through each plot - - if Prod(j,3)==-1 % if tree will start producing in full this year, - Prod(j,3)=NaN; % set years to prod to blank - Prod(j,1)=max(Species(j,:)); % set all trees to 'in production' - Prod(j,2)=0; % set 'out of production' to zero - elseif Prod(j,3)==0 % if it's a partial harvest year - Prod(j,1)=Prod(j,1)+Prod(j,2)*(data(5,15)/100); % set to given percentage (decimal value) - end - - % Borbon - if isnan(Species(j,1))==0 % check for species - if mod(Age(j),Borbon+data(1,13)) >= data(1,14)+Borbon % if land is now in decline - Prod(j,1)=Prod(j,1)*(1-1/(data(1,13)-data(1,14))); - end - if mod(Age(j),Borbon+data(1,13))==0 % if it has been producing for a full cycle, - Prod(j,2)=Species(j,1); % set all to out of production - Prod(j,1)=0; % set in production to zero - Prod(j,3)=Borbon-1; % set yrs to prod to value of years until full yield - end - Exp(j,n)=Prod(j,1)*data(1,15); - - % Catuai - elseif isnan(Species(j,2))==0 - if Age(j)>= Catuai+data(2,14) - Prod(j,1)=Prod(j,1)*(1-1/(data(2,13)-data(2,14))); - end - - % check for replanting condition - if mod(Age(j),Catuai+data(2,13))==0 - Prod(j,2)=Species(j,2); - Prod(j,1)=0; - Prod(j,3)=data(2,12)-1; - Age(j)=0; % tree will be replanted this year, NOT cut. Same for species below - - % some farmers may want to plant more productive species in this field - if n>=Intercrop_Borbon && randi(10)<4 %after year n, plant Borbon - Species(j,1)=Species(j,2); - Species(j,2)=NaN; - Prod(j,3)=Borbon-2; - Age(j)=2; - - end - - end - Exp(j,n)=Prod(j,1)*data(2,15); - - % E14 - elseif isnan(Species(j,3))==0 - if Age(j)>= E14+data(3,14) - Prod(j,1)=Prod(j,1)*(1-1/(data(3,13)-data(3,14))); - end - if mod(Age(j),E14+data(3,13))==0 - Prod(j,2)=Species(j,3); - Prod(j,1)=0; - Prod(j,3)=E14-1; - Age(j)=0; - end - Exp(j,n)=Prod(j,1)*data(3,15); - - % Catura - elseif isnan(Species(j,4))==0 - if Age(j)==Catura+data(4,14) - Prod(j,1)=Prod(j,1)*(1-1/(data(4,13)-data(4,14))); - end - if mod(Age(j),Catura+data(4,13))==0 - Prod(j,2)=Species(j,4); - Prod(j,1)=0; - Prod(j,3)=data(4,12)-1; - Age(j)=0; - end - Exp(j,n)=Prod(j,1)*data(4,15); - end - - - end %end check each plot - -Age=Age+1; -Prod(:,3)=Prod(:,3)-1; -end %end year - -% Create vector -for n=1:num_years - Yield(n,1)=sum(Exp(:,n)); -end - -end %end function From 9a0b07ecccef1291d5297443942585327ab66026 Mon Sep 17 00:00:00 2001 From: Michael Pilosov <40366263+mathematicalmichael@users.noreply.github.com> Date: Thu, 6 Aug 2020 07:33:23 -0600 Subject: [PATCH 4/7] Delete comparativeline.m --- src/comparativeline.m | 54 ------------------------------------------- 1 file changed, 54 deletions(-) delete mode 100644 src/comparativeline.m diff --git a/src/comparativeline.m b/src/comparativeline.m deleted file mode 100644 index 00bd99a..0000000 --- a/src/comparativeline.m +++ /dev/null @@ -1,54 +0,0 @@ -function comparativeline(Year, Yields) - -xtick_labels = []; -numYears = size(Year,1); -for y=1:5:numYears - xtick_labels(end+1) = Year(y); -end - -figure1 = figure('NumberTitle','off','Name',sprintf('%d yr', numYears)); - -% Create axes -axes1 = axes('Parent',figure1,... - 'YMinorTick','on',... - 'YGrid','on',... - 'XGrid', 'on',... - 'gridalpha', 0.5,... - 'XTick',xtick_labels,... - 'XMinorTick','on'); - -% Uncomment the following line to preserve the Y-limits of the axes -ylim(axes1,[0 35000]); -box(axes1,'on'); -hold(axes1,'all'); - -% Create xlabel -xlabel('Year','FontSize',16); - -% Create ylabel -ylabel('Yield (lbs)','FontSize',16); - -% Create title [TODO: Automatic Title based on Strategy] -%title('Purchase 20c. of Borbon in Year 5','FontSize',16); - -colors = ['blue'; 'red'; 'black'; 'cyan']; -linestyles = {"-", "-"}; -linewidths = [5, 3, 2, 1]; -% Create multiple lines using matrix input to plot -plot1 = plot(Year,Yields); -for i=1:size(Yields,2) - displayName = sprintf('getcolumn(Year vs. Yield,%d)',i); - ls = mod(i+1,2); - if i==1 - c = 1; - else - c = 2; - end - set(plot1(i),... - 'LineStyle', linestyles{1+ls},... - 'Color', colors(c),... - 'LineWidth', linewidths(1+ls),... - 'DisplayName', displayName); -end - -end \ No newline at end of file From cae0d59af67b0eb5f0e8056608cbb389f527a10a Mon Sep 17 00:00:00 2001 From: Michael Pilosov <40366263+mathematicalmichael@users.noreply.github.com> Date: Thu, 6 Aug 2020 07:33:29 -0600 Subject: [PATCH 5/7] Delete Worksheet.m --- src/Worksheet.m | 27 --------------------------- 1 file changed, 27 deletions(-) delete mode 100644 src/Worksheet.m diff --git a/src/Worksheet.m b/src/Worksheet.m deleted file mode 100644 index 5138224..0000000 --- a/src/Worksheet.m +++ /dev/null @@ -1,27 +0,0 @@ -% Load the data from our spreadsheet. -data = ImportSurvey('./data/Farmer Survey (Cleaned Up).xlsx'); - -% strategic intervention variables -PurchaseYear_Borbon=NaN; PurchaseYear_Catuai=NaN; -Intercrop_Catuai=NaN; Intercrop_Borbon=NaN; -num_years = 50; - -% create baseline simulation -[Year, Yield_Orig] = ProdSimulation(data, num_years, PurchaseYear_Borbon, PurchaseYear_Catuai, Intercrop_Catuai, Intercrop_Borbon); - -% Uncomment following line to purchase land at specified year -%PurchaseYear_Borbon=5; % 20c. of Borbon -PurchaseYear_Catuai=5; % 20c. of Catuai - -% Uncomment following line to intercrop Catuai in declining -% Catuai fields at specified year -%Intercrop_Catuai=5; - -% Uncomment following line to start intercropping Borbon in -% declining Catuai fields at specified year -%Intercrop_Borbon=10; - - -[Year, Yield] = ProdSimulation(data, num_years, PurchaseYear_Borbon, PurchaseYear_Catuai, Intercrop_Catuai, Intercrop_Borbon); - -comparativeline(Year,[Yield_Orig Yield]); From 0fcf3dca4057dea1ecfe906003f1b33c15798eff Mon Sep 17 00:00:00 2001 From: Michael Pilosov <40366263+mathematicalmichael@users.noreply.github.com> Date: Thu, 6 Aug 2020 07:35:03 -0600 Subject: [PATCH 6/7] Update __init__.py put in content from pyscaffold --- src/cafelytics/__init__.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/cafelytics/__init__.py b/src/cafelytics/__init__.py index 8b13789..c99a72a 100644 --- a/src/cafelytics/__init__.py +++ b/src/cafelytics/__init__.py @@ -1 +1,11 @@ +# -*- coding: utf-8 -*- +from pkg_resources import get_distribution, DistributionNotFound +try: + # Change here if project is renamed and does not equal the package name + dist_name = __name__ + __version__ = get_distribution(dist_name).version +except DistributionNotFound: + __version__ = 'unknown' +finally: + del get_distribution, DistributionNotFound From a55cd979bf1cf13e6db0d5ef1245c37f158ec911 Mon Sep 17 00:00:00 2001 From: Michael Pilosov <40366263+mathematicalmichael@users.noreply.github.com> Date: Thu, 6 Aug 2020 07:45:41 -0600 Subject: [PATCH 7/7] Delete index.ipynb --- index.ipynb | 268 ---------------------------------------------------- 1 file changed, 268 deletions(-) delete mode 100644 index.ipynb diff --git a/index.ipynb b/index.ipynb deleted file mode 100644 index 6387a95..0000000 --- a/index.ipynb +++ /dev/null @@ -1,268 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Cafélytics\n", - "---\n", - "\n", - "## Backstory\n", - "\n", - "\n", - "In 2012 Michael Pilosov traveled to a coffee cooperative in Guatemala as a volunteer. \n", - "For his volunteer project, he decided to apply his mathematical and coding abilities to analyze strategies for the co-op’s agricultural development. \n", - "He discovered the data that had been collected by co-op over the years was sparse, containing only very basic information about land ownership and species planted. \n", - "\n", - "Michael did what he could despite the lack of data--he developed a model that projected a general trend of future yields cooperative-wide, using information gathered from conversations.\n", - "Equipped with a model, he discovered that, despite a bright forecast for growth in the coming years, the co-op was not on track to sustain consistent harvests in the long-term. \n", - "The current operational state of the co-op (assuming no additional acquisitions of land) depicted large dips in harvest yields.\n", - "\n", - "These stemmed from the cyclic nature of the coffee tree itself. \n", - "Since they were planted at the same time, many trees would deteriorate and require replanting simultaneously.\n", - "\n", - "Consequently, the rapid growth of the cooperative would come back to plague them in the form of rapid decline.\n", - "This posed a serious threat on a medium-term timeline, potentially leading to a complete restructuring of existing arrangements for coffee beans. \n", - "\n", - "Michael engaged with the managers to discuss possible strategies to address year-over-year variation in the yields on the horizon. \n", - "It was necessary for the co-op to execute decisions that would level these production trends to ensure the long-term sustainability of the cooperative.\n", - "\n", - "The model could be used to simulate every idea that was proposed (e.g. land investment, tree intercropping, etc.); many scenarios could be played out in search of a feasible strategy to address the looming threats.\n", - "\n", - "Sustainability and longevity were the primary operating goals, so an ideal strategy would result in a relatively “flat” yield projection, with perhaps a gentle positive slope.\n", - "In order to stabilize the cooperative’s future yields, a solution had to be implemented that would mitigate harvest declines without requiring significant capital intervention.\n", - "\n", - "Small amounts of land would need to be purchased to supplement the co-op’s present yield trends. \n", - "Optimal purchase intervals were assessed, but some intervals of expansion exasperated the sustainability problems. \n", - "\n", - "Thus, an optimal purchase interval would need to be elected.\n", - "Simultaneously, it would be necessary for some portion of farmers (or plots) to prematurely upheave healthy trees in the peak of their fruiting maturity.\n", - "\n", - "Before their trees entered the final phase and began to produce lower yields, while still at full-capacity, they would need to be cut down and seedlings planted in their place.\n", - "According to the simulation, about one-third of existing candidate plots would have to be sacrificed for the greater good. \n", - "This action (in accordance with well-timed land purchases), would keep the cooperative’s yields in a range that allowed them to meet contracts consistently for decades to come. \n", - "\n", - "\n", - "---\n", - "\n", - "\n", - "This code was originally written for Matlab but has since been edited to work with Octave.\n", - "The original scripts were used to build up a demonstration notebook (this document) to tell a brief version of the story behind the code and the findings from the study. \n", - "Help is available using the `%help` magic or using `?` with a command. " - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "% add folder with all of our functions to PATH\n", - "addpath(\"src\")" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "scrolled": true - }, - "outputs": [], - "source": [ - "% import data using a little wrapper script around xlsread.m\n", - "data = ImportSurvey('./src/data/farm.xlsx');" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "% strategic intervention variables -- baseline\n", - "PurchaseYear_Borbon=NaN; PurchaseYear_Catuai=NaN; \n", - "Intercrop_Catuai=NaN; Intercrop_Borbon=NaN; \n", - "num_years = 101;" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "% create baseline simulation\n", - "[Year, Yield_Orig] = ProdSimulation(data, num_years, PurchaseYear_Borbon, PurchaseYear_Catuai, Intercrop_Catuai, Intercrop_Borbon);" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Land-Purchasing Interventions" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "% Uncomment following line to purchase land at specified year\n", - "%PurchaseYear_Borbon=5; % 20c. of Borbon\n", - "%PurchaseYear_Catuai=5; % 20c. of Catuai" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Intercropping Interventions\n", - "These were found to have minimal impact on long-term sustainability, but may help ease small declines." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "% Uncomment following line to intercrop Catuai in declining\n", - "% Catuai fields at specified year\n", - "%Intercrop_Catuai=5;\n", - "\n", - "% Uncomment following line to start intercropping Borbon in \n", - "% declining Catuai fields at specified year\n", - "Intercrop_Borbon=10;" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "[Year, Yield] = ProdSimulation(data, num_years, PurchaseYear_Borbon, PurchaseYear_Catuai, Intercrop_Catuai, Intercrop_Borbon);" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Compare Strategic Intervention to Baseline Yield Forecast" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "Yields = [Yield_Orig, Yield]; % append vectors as such, first being baseline\n", - "comparativeline(Year, Yields);" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Sensitivity Analysis\n", - "The simulation only assumes some proportion (about a 30%) of farmers will choose to switch from Catuai to Borbon trees when they perform intercropping, starting 10 years into the process. \n", - "\n", - "This means our simulation will produce different results each time it runs since it incorporates some stochasticity. To get a sense of the behavior, we can look at \"spaghetti plots\" to see the variability in the yield forecasts." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "% choose a number of times to run simulations\n", - "num_repeats = 10;" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Example\n", - "If we wait too long before implementing this, we actually lose much of the benefit and end up making the variability even more pronounced." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "Intercrop_Borbon = 10; % how many years until strategy is implemented?\n", - "Yields = [Yield_Orig];\n", - "for i=1:num_repeats\n", - " [Year, Yield] = ProdSimulation(data, num_years, PurchaseYear_Borbon, PurchaseYear_Catuai, Intercrop_Catuai, Intercrop_Borbon);\n", - " Yields(:,end+1) = Yield;\n", - "end\n", - "comparativeline(Year, Yields);" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "Intercrop_Borbon = 15; % how many years until strategy is implemented?\n", - "Yields = [Yield_Orig];\n", - "for i=1:num_repeats\n", - " [Year, Yield] = ProdSimulation(data, num_years, PurchaseYear_Borbon, PurchaseYear_Catuai, Intercrop_Catuai, Intercrop_Borbon);\n", - " Yields(:,end+1) = Yield;\n", - "end\n", - "comparativeline(Year, Yields);\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - } - ], - "metadata": { - "anaconda-cloud": {}, - "kernelspec": { - "display_name": "Octave", - "language": "octave", - "name": "octave" - }, - "language_info": { - "file_extension": ".m", - "help_links": [ - { - "text": "GNU Octave", - "url": "https://www.gnu.org/software/octave/support.html" - }, - { - "text": "Octave Kernel", - "url": "https://github.com/Calysto/octave_kernel" - }, - { - "text": "MetaKernel Magics", - "url": "https://metakernel.readthedocs.io/en/latest/source/README.html" - } - ], - "mimetype": "text/x-octave", - "name": "octave", - "version": "4.2.2" - } - }, - "nbformat": 4, - "nbformat_minor": 4 -}